var currentCitiesID;
var currentCitiesSelectID;
var currentCitiesDivID;
var currentZipCodeID;
var currentZipCodeDivID;
var currentZipCodeFieldID;

function onRegisterClick(){
    return confirm(txtRegisterAgain);
}

function onSearchClick(){   // nötig? ich glaube nicht. steffi 11.09.06
    if (document.getElementById('search_commodity_type').value==""){
        alert(txtSearchInputValid);
        return false;
    } else return true;
}

function onCommoditiesPriceSpecialArrangementClick(radio,price_input){
    if(radio.id == "commodities_price_special_arrangement_1"){
        price_input.value="";
    }
}

function openPopup (Adresse, name, breite) {
  Fenster1 = window.open(Adresse, name, "width="+breite+",height=600,scrollbars=1,resizable=yes");
  Fenster1.focus();
}

function openPopupWundH (Adresse, name, breite, hoehe) {
  Fenster1 = window.open(Adresse, name, "width="+breite+",height="+hoehe+",scrollbars=1,resizable=yes");
  Fenster1.focus();
}

function openPriceCalculator() {
    w = window.open("/popup/preisrechner.php","FahrpreisRechner","width=340,height=430,scrollbars=1");
    targetField = document.getElementById("commodities_price");
    radioTargetField0 = document.getElementById("commodities_price_special_arrangement_0");
    radioTargetField0.checked = true;
    w.targetField = targetField;
    w.focus();
    return false;
}

function openBahnhofSelector(nr, smallSearch) {
    var targetFieldCity = document.getElementById('mobile_locations_city_'+nr+'_');
    var targetFieldZip  = document.getElementById('mobile_locations_zip_code_'+nr+'_');
    smallSearch         = (smallSearch === true);
    var uri             = '/popup/bahnhofs.php?nr='+nr+'&smallSearch='+smallSearch+(targetFieldCity&&targetFieldCity.value ? '&letter='+escape(targetFieldCity.value) : '');
    var w               = window.open(uri, 'BahnhofSelector', 'width=450,height=430,scrollbars=1');
    w.focus();
    return false;
}

//callback function for the return value of the popup
function setTargetFields(nr, city, zip_code, smallSearch) {
    if (smallSearch === true) {
        document.getElementById('mobile_locations_city_'+nr).value = city;
    } else {
        document.getElementById('mobile_locations_city_'+nr+'_').value     = city;
        document.getElementById('mobile_locations_zip_code_'+nr+'_').value = zip_code;
    }
}

// Adressvalidierung, Geocoding
function openGeocoder() {
    var addressStreet  = document.getElementById('space_location_street').value;
    var addressHouseno = document.getElementById('space_location_houseno').value;
    var addressCountry = document.getElementById('space_location_country').value;
    var addressZip     = document.getElementById('space_location_zip_code').value;
    var addressCity    = document.getElementById('space_location_city').value;
    var uri = '/popup/geocode.php?'
             +'&street='+addressStreet
             +'&houseno='+addressHouseno
             +'&country='+addressCountry
             +'&zip='+addressZip
             +'&city='+addressCity;
    var w   = window.open(uri, 'Adressvalidierung', 'width=500,height=800,scrollbars=1');
    w.focus();
    return false;
}

function openGetMarkerll() {
    var addressCountry = document.getElementById('space_location_country').value;
    var addressCity    = document.getElementById('space_location_city').value;
    var addressZip     = document.getElementById('space_location_zip_code').value;
    var uri = '/popup/geo_putmarker.php?'
             +'&country='+addressCountry
             +'&zip='+addressZip
             +'&city='+addressCity;
    var w   = window.open(uri, 'Adressvalidierung', 'width=500,height=400,scrollbars=1');
    w.focus();
    return false;
}


//function setLocationFields(street, houseno, city, zip_code, country, lat, lng, accuracy) {
//alert("bin in funktion setLocationFields");
//street, houseno, city, zip_code, country, lat, lng, accuracy
//        document.getElementById('space_location_lat').value     = lat;
//        document.getElementById('space_location_lng').value     = lng;
//        document.getElementById('space_location_city').value     = city;
//        document.getElementById('mobile_locations_zip_code_'+nr+'_').value = zip_code;
//}

function setLocationField(targetField, val){
  if (document.getElementById(targetField)) {
    document.getElementById(targetField).value = val;
  }
  //window.focus();
}

function setTargetField(targetField, val){
  if (targetField) {
    targetField.value = val;
  }
  window.focus();
}

function clearSelect(oSelect)
{
    for (var i = oSelect.length; i >= 0 ; i--){
        oSelect.options[i] = null;
    }
}

function absLeft(el) {
    return (el.offsetParent)?
    el.offsetLeft+absLeft(el.offsetParent) : el.offsetLeft;
}

function absTop(el) {
    return (el.offsetParent)?
    el.offsetTop+absTop(el.offsetParent) : el.offsetTop;
}

function changeRowStyle(tr_id, style_class){
    theRow = (typeof(tr_id)=='string') ? document.getElementById(tr_id) : tr_id;
    theRow.className = style_class;
}



String.prototype.startsWith = function(value) {
    return this.substr(0, value.length) == value;
}

Array.prototype.hasValue = function(value) {
    for (var i=0; i<this.length; i++) {
        if (this[i] == value) { return true; }
    }
    return false;
}

Array.prototype.equalsValues = function(other) {
    return this.length == other.length && this.containsValues(other);
}

Array.prototype.containsValues = function(other) {
    for (var i=0; i<this.length; i++) {
        if (!other.hasValue(this[i])) { return false; }
    }
    return true;
}

var track_onload = null;
function track() {
    track_onload = window.onload;
    window.onload = insertTracking;
}
function insertTracking() {
    var head = document.getElementsByTagName('head')[0];
    var script = document.createElement('script');
    script.setAttribute('type', 'text/javascript');
    script.setAttribute('src', '//www.easytracking.de/easytracking.js.php?id=raumobil');
    head.appendChild(script);
    if (typeof(track_onload) == 'function') { track_onload(); }
}

/*
Function: Fuer Bahn Inserat. Die suche in 1.Form nach Startbahnhof , PLZ und
kopiert die Werte in 2. Form und dann schicke.
*/
function inserratAngeben(){
  //get form 2
  var form2=document.getElementById("frmSearch_2");

  //get start bahnhof obj from doc
  var country1=document.getElementById("mobile_locations_country_0_");
  var zip1=document.getElementById("mobile_locations_zip_code_0_");
  var city1=document.getElementById("mobile_locations_city_0_");

  //get start bahhhof obj form form2
  var country2	=document.getElementById("f2startCountry");
  var zip2			=document.getElementById("f2zip");
  var city2			=document.getElementById("f2city");

  //set start bahnhof obj
  country2.value=country1.value;
  zip2.value=zip1.value;
  city2.value=city1.value;

  //get ziel bahnhof value from form 1
  var country2=document.getElementById("mobile_locations_country_99_");
  var zip2=document.getElementById("mobile_locations_zip_code_99_");
  var city2=document.getElementById("mobile_locations_city_99_");

  //get ziel bahnhof obj from form 2
  var countryZiel=document.getElementById("f2zielCountry");
  var zipZiel=document.getElementById("f2zielzip");
  var cityZiel=document.getElementById("f2zielcity");

  //set ziel bahnhof obj
  countryZiel.value=country2.value;
  zipZiel.value=zip2.value;
  cityZiel.value=city2.value;

  //send form 2
  form2.submit();
  return false;
}

/*sucheergebenis aufklappen und details darstellen

*/
function ajax_show_detail(obj_inputTR){

    if(!obj_inputTR){ alert("Error! ajax_show_detailn() can not get object."); return false;}

    var rowIndex=obj_inputTR.rowIndex; //click index
    var iIndex=rowIndex; //insert index
    var isLast=false;
    var r=false; //response

    /*alert("you click auf :"+rowIndex);
    alert("insert iIndex: "+iIndex);*/

    var node_tbody=document.getElementById("mobilView_result_table_body");
    if(!node_tbody){alert("Error! no tbody object was found!"); return false;}
    var trSize=node_tbody.rows.length;
    /*alert("Table tr size:" + trSize);*/

    if(rowIndex==trSize){
      //alert("You click on the last line!");
      isLast=true;
    }

    var count=0;

    if(isLast){
      node_tbody.appendChild(ajax_createNewTR());
      return true;
    }

    for(var i=0; i<node_tbody.childNodes.length; i++)	{
      if(node_tbody.childNodes[i]. nodeName=="TR"){
          if(count==iIndex){
            r=node_tbody.insertBefore(  ajax_createNewTR(), node_tbody.childNodes[i] );
            return true;
          }
        count++;
      }
    }
    return false;
}

/*
Function:build and return a TR obj
Parm: none
Return:  TR object
*/

function ajax_createNewTR(){
  var tr=document.createElement("tr");
  var td=document.createElement("td");
  td.colSpan="11";//can auto
  td.style["backgroundColor"] = "#E7EF88";
  td.style["height"] = "100px";
  td.style["textAlign"]="center";
  var content= document.createTextNode(" waiting for response from server.....\n");
  var br=document.createElement("br");

  var img=document.createElement("img");
  img.src="/gfx/warteAnimation_02.gif";



  td.appendChild(img);
  td.appendChild(content);
  td.appendChild(br);
  td.appendChild( ajax_newCloseBtn(tr) );
  td.appendChild(br);
  tr.appendChild(td);
  return tr;
}

function ajax_newCloseBtn(parm){
  var cancelBtn=document.createElement("button");
  var cancelTxt=document.createTextNode("Cancel");
  cancelBtn.appendChild(cancelTxt);
  cancelBtn.onclick=createHeaderClickHandler(this, "closeNewTr", parm);
  return cancelBtn;
}

function closeNewTr(obj){
  //alert(" I am ready to close  " + obj.rowIndex );
  document.getElementById("mobilView_result_table_body").deleteRow(obj.rowIndex-1);
  return false;
}

/****************************************************************************************************
global Function
Function:  create Thead onclick event (for sort)
Parm: object methodename parameter
Returm:  js event handler
***************************************************************************************************/
function createHeaderClickHandler(object, methodName, parameter){
  return function(){
    object[methodName](parameter);
  }
}

function showWait(){
    document.getElementById('wait').style.visibility = 'visible';
}

/***************************
Storyboard Begin
***************************/
  var renderMethod=2; /*1: , 2: overlib, 3: float div*/
  var maxHeight=45;
  var maxWidth =45;
  var nheight=0;
  var nwidth=0;

  function initStoryBoard(){
    var divID="storyBoard";
    var div=document.getElementById(divID);
    if(!div){ alert("Can not get Div # "+divID);}
    var listArray=div.getElementsByTagName("li");
    for(var i=0; i<listArray.length; i++){
      var img= listArray[i].getElementsByTagName("img")[0];
      if(!img){ return false;}
      var originalWidth=img.width;
      var originalHeight=img.height;
      var ratio = originalHeight/originalWidth ;

      listArray[i].onmouseover = createItemHandler(this, "overZoom", img);
      listArray[i].onmouseout = createItemHandler(this, "outZoom", img );
    }
    if(renderMethod!=3){
      var hoverDiv = document.getElementById("infobox");
      var divID="storyBorad";
      var div=document.getElementById(divID);
      hoverDiv.style["display"]="none";
    }
    return true;
  }

/* commen function to create a event handler
*
*/
function createItemHandler(object, methodName, itemNum){
  return function(){
    object[methodName](itemNum);
  }
}

function overZoom( imgObj){
  /* 1. einface zoom */
  if(renderMethod==1){
    imgObj.style["float"] = "none";
    imgObj.style["width"] = "96px";
    return null;
  }

  /* 2. mit overlib */
  if(renderMethod==2){
  var string="<div class='storyHover'>";
    string+="<img src='"+imgObj.src+"'/>";
    string+="<p>"+imgObj.title+"</p></div>";
    return overlib(string);
  }

  /* 3. */
  if(renderMethod==3){
    var ibox=document.getElementById("infobox");
    var previewImg=document.createElement("img");
      previewImg.src=imgObj.src;
      /*previewImg.style['width'] = "96px";*/
      ibox.appendChild(previewImg);
    if(true){
      ibox.style["float"] = "left";
    }
    else{
      ibox.style["float"] = "right";
    }
    ibox.style["border"] = "2px solid #9db31a";
    return null ;
  }

}

function outZoom( imgObj){
  /* 1. */
  if(renderMethod==1){
    imgObj.style["float"] = "right";
    imgObj.style["width"] = "45px";
  }


  /* 2. */
  if(renderMethod==2){
    nd();
  }

  /* 3. */
  if(renderMethod==3){
    var ibox=document.getElementById("infobox");
    var children=ibox.childNodes;
    var i=0;
    while(ibox.childNodes.length>0){
      ibox.removeChild(ibox.firstChild);
    }
    ibox.style["border"] = "0px";
  }
}
/*********************************
StoryBoard end
*********************************/

/*********************************
result table js start
*********************************/
  var c1="white";	// defualt background color
  var c2="#fff2d2";// grade zahl  row background color
  var c3="#efefbb"; //on  mouse over bacgkround color
  var c4="black"; //text color
  var c5="black"; //text over color
  var $DEBUG=true;
/* Create a event handler
*
*/
function initTableMouseOver( idName){
    if(false){alert("initTableMouseOver Effect, idName="+idName);}
    var t=document.getElementById(idName);
    if(t==null){
      if($DEBUG){alert("Can not get table");}
      return false;
    }
    var n=0;
    var tbody=t.tBodies[0];
    var r=tbody.rows.length; /*get tbody line nummer*/
    for(n=0; n<r; n++){
      var tr=tbody.rows[n];
      if(tr.id != "travis_f"){
        tr.onmouseover = createItemHandler(this,"overItemHandler",tbody.rows[n]);
        var a = new Array(2);
        a[0]=tr.bgColor;/*save the original tds bg color propertie*/
        a[1]=tr; /*save the td object*/
        tr.onmouseout = createItemHandler(this,"outItemHandler",a);
        /*
        tbody.rows[n].onclick=createItemHandler(this,"clickItemHandler" ,tbody.rows[n]);
        */
        for(var i=0; i<tr.cells.length-1; i++){
          tr.cells[i].onclick=createItemHandler(this,"clickItemHandler" ,tr);
        }
      }
    }
    return false;
  }
/*
init style for airport list table
*/
function initTableStyle( tabId){
  var t = $(tabId); if(!t){alert("Can not get table "+tabOd); return false;}
  var tbody=t.tBodies[0];
  var r=tbody.rows.length;
  for(n=0; n<r; n++){
    var tr=tbody.rows[n];
    tr.onmouseover = createItemHandler(this,"overItemHandler",tbody.rows[n]);
    var a = new Array(2);
    if(n%2==0){tr.bgColor = c1;}
    a[0]=tr.bgColor;/*save the original tds bg color propertie*/
    a[1]=tr; /*save the td object*/
    tr.onmouseout = createItemHandler(this,"outItemHandler",a);
  }
  return false;
}







/* commen function to create mouse over change background effect
* parm: tr object
*/
function overItemHandler(obj){
  if(obj){
    obj.style["backgroundColor"]=c3;
    obj.style["cursor"]="pointer";
    obj.style["color"]=c5;
  }
}

/* common function to create mouse out effect
*
*
*/
function outItemHandler (obj){
  if(obj[1]){
    obj[1].style['backgroundColor']=obj[0];
    obj[1].style['color'] = c4;
  }
}

/*
*/
function clickItemHandler(trobj){
  var links=trobj.getElementsByTagName('a');
  if(links[links.length-1].getAttribute('rel') != 'external') { 
    location.href=links[links.length-1].href; 
  } 

}
/*********************************
result table js end
*********************************/






/*** globla function to solve the popup window in xhtml for all browser ***/
/** Quelle: http://www.sitepoint.com/article/standards-compliant-world/ **/
function externalLinks() {
 if (!document.getElementsByTagName) return;
 var anchors = document.getElementsByTagName("a");
 for (var i=0; i<anchors.length; i++) {
   var anchor = anchors[i];
   if (anchor.getAttribute("href") &&
       anchor.getAttribute("rel") == "external")
     anchor.target = "_blank";
 }
}

/* For these li can click. follow the link in child a.
*/
function followLink(obj){
  location.href=obj.getElementsByTagName('a')[0].href;
  return false;
}


/* get var "usemap" from  cookies
*
*/
function isUseMap(){
  var result = true; //default TRUE
  var cookieString = document.cookie;
    var patten = /showmap=/ ;
    var result=cookieString.search(patten);
    if(result != -1){
      //found !
      var cookievalue = cookieString.substring(result, result+cookieString.search(';')+1);
      //alert(cookievalue);
      eval(cookievalue);
      if(typeof(showmap) != undefined ){
        //sucess
       // alert(showmap);
        result = showmap;
      }
    }
    return result;
}


function displayAvisForm(){
  if(document.getElementById('avis_formular').style.display=="none"){
      document.getElementById('avis_formular').style.display = 'block';
  } else {
      document.getElementById('avis_formular').style.display = 'none';
  }
}



