var requestBL = null;
var sSwapBLName = '';
var sFormName = '';
function createRequestBL(){
    try {
        requestBL = new XMLHttpRequest();
    } catch (trymicrosoft) {
        try {
            requestBL = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (othermicrosoft) {
            try {
                requestBL = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (failed) {
                requestBL = null;
            }
        }
    }
}

var arearequest = null;
function createAreaRequest(){
    try {
        arearequest = new XMLHttpRequest();
    } catch (trymicrosoft) {
        try {
            arearequest = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (othermicrosoft) {
            try {
                arearequest = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (failed) {
                arearequest = null;
            }
        }
    }
}

function getBundeslaender(land, sFormularName, sSwapName) {
    createRequestBL();
    sSwapBLName = sSwapName;
    sFormName =sFormularName;

    if (0 != land) {
        var url = "/ajaxGetRegions/" + land;
        requestBL.open("GET", url, true);
        requestBL.onreadystatechange = updateBundeslaender;
        requestBL.send(null);
    }
    else {

      setDefaultRegionSelect(sFormName);
      disableRegionSelectBox(sFormName, true);
      disableThirdSelectBox(sFormName, true);
      disableCitySelectBox(sFormName, true);

    }
}


/* Erweiterte Version von getBundeslaender fuer die Themennavigation */
function getThemeBundeslaender(land, sFormularName, sSwapName, sThema) {

    /////// <Ajax History-Back> /////////////////////////

      var oFormObject = new Object();
      if ( sThema == 'unterkuenfte' | sThema == 'reiseangebote' | sThema == 'reisefuehrer') {
        oFormObject.sSelectBoxName = 'land_hotel';
      }
      else {
        oFormObject.sSelectBoxName = 'land';
      }
      oFormObject.FormNameToChange = sFormularName;
      oFormObject.containerNameToChange = sSwapName;
      oFormObject.sTheme = sThema;
      oFormObject.sSelectBoxValue=land;

      if ( typeof historyStorage == 'object' & isBrowserCompatibleWithRSH() ) {
        historyStorage.put('oFormObject', oFormObject);
      }
    ///////</Ajax History-Back> /////////////////////////

    createRequestBL();
    sSwapBLName = sSwapName;
    sFormName =sFormularName;

    if (0 != land) {
        var url = "/ajaxGetRegionsByTheme/" + land + "/" + sThema;
        requestBL.open("GET", url, true);
        requestBL.onreadystatechange = updateThemeBundeslaender;
        requestBL.send(null);
    }
    else {

      setDefaultRegionSelect(sFormName);
      disableRegionSelectBox(sFormName, true);
      disableThirdSelectBox(sFormName, true);
      disableCitySelectBox(sFormName, true);

    }
}

function updateBundeslaender(){
    if (requestBL.readyState == 4) {
    
        var jsondata = eval('(' + requestBL.responseText + ')');
        blandselect = getSecondSelectHTML();

        for(i=0;i<jsondata.blaender.length; i++){
          selected='';
          blandselect += "<option value=\"" + jsondata.blaender[i][0] + "\"" + selected + ">" + jsondata.blaender[i][1] + "</option>";
        }
        blandselect += "</select>\n";

      sDoThis = "document.getElementById('" + sSwapBLName + "').innerHTML = blandselect";
      eval(sDoThis);

      if ( document.getElementById("area_hotel") != null ) {
            disableRegionSelectBox(sFormName, false);
            disableThirdSelectBox(sFormName, true);
            disableCitySelectBox(sFormName, true);
      }
    }
}



function getBundeslaenderInitPageLoad(land, sFormularName, sSwapName) {
      createRequestBL();
      sSwapBLName = sSwapName;
      sFormName =sFormularName;

      var url = "/ajaxGetRegions/" + land;
      requestBL.open("GET", url, true);
      requestBL.onreadystatechange = updateBundeslaenderInitPageLoad;
      requestBL.send(null);
}


function getBundeslaenderByThemeInitPageLoad(land, sFormularName, sSwapName, sThema) {
      createRequestBL();
      sSwapBLName = sSwapName;
      sFormName =sFormularName;

      var url = "/ajaxGetRegionsByTheme/" + land + "/" + sThema;
      requestBL.open("GET", url, true);
      requestBL.onreadystatechange = updateBundeslaenderByThemeInitPageLoad;
      requestBL.send(null);
}


function updateThemeBundeslaender(){
    if (requestBL.readyState == 4) {
        var jsondata = eval('(' + requestBL.responseText + ')');
        blandselect = getSecondSelectHTML();

        for(i=0;i<jsondata.blaender.length; i++){
          selected='';
          blandselect += "<option value=\"" + jsondata.blaender[i]['schneehoehen.p_sh_navigation.sec_id'] + "\"" + selected + ">" + jsondata.blaender[i]['schneehoehen.p_sh_navigation.sec_name'] + "</option>";
        }
        blandselect += "</select>\n";

      sDoThis = "document.getElementById('" + sSwapBLName + "').innerHTML = blandselect";
      eval(sDoThis);

      if ( document.getElementById("area_hotel") != null ) {
            disableRegionSelectBox(sFormName, false);
            disableThirdSelectBox(sFormName, true);
            disableCitySelectBox(sFormName, true);
      }
    }
}

function updateBundeslaenderByThemeInitPageLoad(){
    if (requestBL.readyState == 4) {
    
        var jsondata = eval('(' + requestBL.responseText + ')');
        blandselect = getSecondSelectHTML();

        myRegion = getValueFromURLSearchParameter("region_hotel");
        for(i=0;i<jsondata.blaender.length; i++){
          //CH:Pruefen, ob Parameter mitgegeben wurden und dann selecten
          if ( jsondata.blaender[i]['schneehoehen.p_sh_navigation.sec_id'] == myRegion & myRegion != '') {
            selected=' selected=\"true\" ';
          }
          else {
            selected='';
          }
          blandselect += "<option value=\"" + jsondata.blaender[i]['schneehoehen.p_sh_navigation.sec_id'] + "\"" + selected + ">" + jsondata.blaender[i]['schneehoehen.p_sh_navigation.sec_name'] + "</option>";
        }
        blandselect += "</select>\n";

      sDoThis = "document.getElementById('" + sSwapBLName + "').innerHTML = blandselect";
      eval(sDoThis);

      if ( document.getElementById("area_hotel") != null ) {
          if (myRegion != 0) {
              getThirdSelectBoxContent(myRegion);
          }
          else {
            disableRegionSelectBox(sFormName, false);
            disableThirdSelectBox(sFormName, true);
            disableCitySelectBox(sFormName, true);
          }
      }
    }
}








function updateBundeslaenderInitPageLoad(){
    if (requestBL.readyState == 4) {
        var jsondata = eval('(' + requestBL.responseText + ')');
        blandselect = getSecondSelectHTML();

        myRegion = getValueFromURLSearchParameter("region_hotel");
        for(i=0;i<jsondata.blaender.length; i++){
          //CH:Pruefen, ob Parameter mitgegeben wurden und dann selecten
          if ( jsondata.blaender[i][0] == myRegion ) {
            selected=' selected=\"true\" ';
          }
          else {
            selected='';
          }
          blandselect += "<option value=\"" + jsondata.blaender[i][0] + "\"" + selected + ">" + jsondata.blaender[i][1] + "</option>";
        }
        blandselect += "</select>\n";

      sDoThis = "document.getElementById('" + sSwapBLName + "').innerHTML = blandselect";
      eval(sDoThis);

      if ( document.getElementById("area_hotel") != null ) {
          if (myRegion != 0) {
              getThirdSelectBoxContent(myRegion);
          }
          else {
            disableRegionSelectBox(sFormName, false);
            disableThirdSelectBox(sFormName, true);
            disableCitySelectBox(sFormName, true);
          }
      }
    }
}

function getThirdSelectBoxContent(region) {
    createAreaRequest();

    if (0 != region) {
        var url = getThirdAjaxMethod(region);
        arearequest.open("GET", url, true);
        arearequest.onreadystatechange = updateThirdSelectBox;
        arearequest.send(null);
    }
}


function updateThirdSelectBox(){
    if (arearequest.readyState == 4) {

      sDoThis = "document.getElementById('swapArea" + sFormName + "')";
      oElement = eval(sDoThis);
      if ( oElement != null ) {

          var jsondata = eval('(' + arearequest.responseText + ')');
          areaselect = getThirdSelectHTML();

          myArea = getValueFromURLSearchParameter("area_hotel");

          for(i=0;i<jsondata.data.length; i++){
            //CH:Pruefen, ob Parameter mitgegeben wurden und dann selecten
            if ( jsondata.data[i][0] == myArea ) {
              selected=' selected=\"true\" ';
            }
            else {
              selected='';
            }
            areaselect += "<option value=\"" + jsondata.data[i][0] + "\"" + selected + ">" + jsondata.data[i][1] + "</option>";
          }
          areaselect += "</select>\n";

          sDoThis = "document.getElementById('swapArea" + sFormName + "').innerHTML = areaselect";
          eval(sDoThis);

          disableCitySelectBox(sFormName, true);

          if ( oElement != null ) {
            if (myArea != 0) {
              getCities(myArea);
            }
        }
       }
    }
}



function getCities(area) {
    createRequestBL();

    if (0 != area) {
        var url = "/ajaxGetCities/" + area;
        requestBL.open("GET", url, true);
        requestBL.onreadystatechange = updateCityBox;
        requestBL.send(null);
    }
}

function updateCityBox(){
    if (requestBL.readyState == 4) {
        var jsondata = eval('(' + requestBL.responseText + ')');
        cityselect = "<p>\n<label for=\"city_hotel\">Ort(e):</label>\n<select name=\"city_hotel\" id=\"city_hotel\" style=\"height:75px\" multiple>\n<option id=\"default_city\" name=\"default_city\" value=\"0\">Bitte w&auml;hlen</option>\n";

       myCities =getMultipleValuesFromURLSearchParameter("city_hotel");

        for(i=0;i<jsondata.cities.length; i++){
          //CH:Pruefen, ob Parameter mitgegeben wurden und dann selecten
          for(iCity=0;iCity<myCities.length; iCity++){
              if ( jsondata.cities[i][0] == myCities[iCity] ) {
                selected=' selected=\"true\" ';
                break;
              }
              else {
                selected='';
              }
           }
          cityselect += "<option value=\"" + jsondata.cities[i][0] + "\"" + selected + ">" + jsondata.cities[i][1] + "</option>";
        }
        cityselect += "</select>\n</p>";

    if ( document.getElementById("swapCity") != null ) {
          document.getElementById("swapCity").innerHTML = cityselect;
      }
    }
}


function setDefaultRegionSelect(sFormname) {
    sDoThis = "document." + sFormname + ".region_hotel";
    var oSelect = eval(sDoThis);
    if ( oSelect != null ) {
      oSelect.selectedIndex = 0;
   }
}

function disableRegionSelectBox(sFormname, bDisable) {
    sDoThis = "document." + sFormname + ".region_hotel";
    var oSelect = eval(sDoThis);
    if ( oSelect != null ) {
      oSelect.disabled=bDisable;
   }
}

function disableThirdSelectBox(sFormname, bDisable) {
    sDoThis = "document." + sFormname + ".area_hotel";
    var oSelect = eval(sDoThis);
    if ( oSelect != null ) {
      oSelect.selectedIndex = 0;
      oSelect.disabled=bDisable;
      }
}

function disableCitySelectBox(sFormname, bDisable) {
    sDoThis = "document." + sFormname + ".city_hotel";
    var oSelect = eval(sDoThis);
    if ( oSelect != null ) {
      oSelect.disabled=bDisable;
      oSelect.selectedIndex = 0;
    }

}

function getValueFromURLSearchParameter(name){
   var i=1;  //Suchposition in der URL
   var suche = name+"=";

   while (i<location.search.length){
      if (location.search.substring(i, i+suche.length)==suche){
         var ende = location.search.indexOf("&", i+suche.length);
         ende = (ende>-1) ? ende : location.search.length;
         var loca = location.search.substring(i+suche.length, ende);
         return unescape(loca);
      }
      i++;
   }
   return "";
}


function getMultipleValuesFromURLSearchParameter(my_key){
  var i,my_ppaare, my_i=0;
  var my_wert =new Array();
  var my_params=window.location.search;

  if (my_params != ''){
    // ? aus Parameterstring entfernen:
    my_params=my_params.split('\?');

    // Parameterstring zerteilen in einzelne Hashes:
    my_params=my_params[1].split('\&');

    // Hashes nach Key durchsuchen, den Wert abfangen:
    for (i=0; i<my_params.length; i++){
      my_ppaare=my_params[i].split('\=');
      if (my_ppaare[0]==my_key) {
        my_wert[my_i]=my_ppaare[1];
        my_i++;
      }
    }
  }
  return my_wert;
}


function isBrowserCompatibleWithRSH(){
    var agent = navigator.userAgent.toLowerCase();

    if ( agent.indexOf('chrome') >-1) {
        return false;
    } else if ( agent.indexOf('safari') >-1) {
        return false;
    } else {
        return true;
    }
}