
var htmlBody = '';
var Timer = null;
var Cache = [];
var AcceptMessage=true;
var userKey = "012345678901234567890123456789";
var globalSelectAjaxedClass = 'ajaxed_default';

var selectCoverOpen = '&nbsp;<small>';
var selectCoverClose = '</small>';
var h2DCoverOpen = "<small>";
var h2DCoverClose = "</small>";

var uniFName = "";
var uniFVal = "";

var ajaxLIMG = "";

function setAjaxLIMG(imga) {
 ajaxLIMG = "<img src='"+imga+"'>&nbsp;"; 
}

function setSelectCover(st,ed) {
 selectCoverOpen=st;
 selectCoverClose=ed;
}


function setHTML2DIVCover(st,ed) {
 h2DCoverOpen=st;
 h2DCoverClose=ed;
}


function setUniFltr(name, uvl) {
 uniFName = name;
 uniFVal = uvl;
}

   function eID(id) { return document.getElementById(id); }

   function getGSAC() {

    if (globalSelectAjaxedClass != null) {
     gSAClass = globalSelectAjaxedClass;
    } else {
     gSAClass = "ajaxed_default";
    }

    return gSAClass;

   }



   function TimeRandFunction() {
    var now;
    now=new Date();
    strng=now.getTime()+"_"+(Math.random()*999999);
    return strng;
   }



   function Milliseconds() {
    var now;
    now=new Date();
    return now.getTime();
   }



   function runJS(divID) {

      var scripts = eID(divID).getElementsByTagName("script");

      for(i=0; i<scripts.length; i++) {
       var curScript = scripts[i]; 
       if ( !curScript.executed ) {
        curScript.executed = true;
        var sc = curScript.innerHTML;
        if ( sc ) {
         var a = eval(sc);
        }
       }
      }

   }


   function LoadHTML2Div(divID, url) {

    htmlBody = h2DCoverOpen+"Error loading requested page..."+h2DCoverClose;

    var call2 = domain+'/controller.php?call=loadhtmlfromurl'+
                      '&url='+encodeURIComponent(url.replace(/\//g,"#"))+
                      '&timer='+TimeRandFunction();

    jQuery.getScript(call2, 
     function(){
      if (htmlBody == "") {
       LoadHTML2Div(divID, url);
      } else {
       eID(divID).innerHTML = htmlBody;
      }
     }
    );

   }




   function LoadQuery2Div(divID,query){

    htmlBody = selectCoverOpen+"Error loading requested page..."+selectCoverClose;

    var call2 = domain+'/controller.php?call=loadhtmlfromquery'+
                      '&query='+encodeURIComponent(query.replace(/\//g,"#"))+
                      '&timer='+TimeRandFunction();

    jQuery.getScript(call2, 
     function(){
      eID(divID).innerHTML = htmlBody;
      runJS(divID);
     }
    );

   }


   function searchProps(destEl, sCriteria) {
    LoadQuery2Div(destEl,sCriteria);
   }
 

   var rinfo = {};
   function GetResorts(n,wpo){

      if (n==null) {n='';}

      var t=1;

      if (eID("n_state"+n)== null || eID("n_state"+n)=="undefined") {
       n_state = 0;
      } else {
       n_state = eID("n_state"+n).value;
      }

      if (eID("n_city"+n)== null || eID("n_city"+n)=="undefined") {
       n_city = 0;
      } else {
       n_city = eID("n_city"+n).value;
      }

      gc = 'getresortsbycs';

      if (wpo!=null && wpo==1) {gc= gc + '_wpo';}

      if (eID("c_resorts_list"+n)!=null) {
       eID("c_resorts_list"+n).innerHTML = selectCoverOpen+ajaxLIMG+"Loading resorts list..."+selectCoverClose;
      }
      //rinfo = {};
      var call2 = domain+'/controller.php?call='+gc+ //getresortsbycs'+
                      '&country='+eID("c_country"+n).value+
                      '&state='+n_state+
                      '&city='+n_city+
                      '&f6m=1'+
                      '&SessID='+userKey+
                      '&timer='+TimeRandFunction();

      jQuery.getScript(call2, 
       function(){
        if (eID("c_resorts_list"+n)!=null) {
         eID("c_resorts_list").innerHTML = resortsList;
         //alert(rinfo);
         resort_fix1row_caller();
         gSAClass = getGSAC();
         eID("n_resort_id").className = gSAClass;
        }
       }
      );
}

var resort_fix1row_func = "";

function set_rfix1rfn(fn) {
 resort_fix1row_func = fn;
}

function resort_fix1row_caller() {
 if (resort_fix1row_func != "") {
  return eval(resort_fix1row_func);
 }
}







function SetCity(n) {

      if (n==null) {n='';}

      if (jQuery("#n_city"+n).val()>0) {
       eID("c_city_fld"+n).value = eID("n_city"+n).options[eID("n_city"+n).selectedIndex].label;
       eID("c_city_fld_control"+n).value = eID("n_city"+n).options[eID("n_city"+n).selectedIndex].label;
      } else {
       eID("c_city_fld"+n).value = ""; //eID("n_city"+n).options[eID("n_city"+n).selectedIndex].label;
       eID("c_city_fld_control"+n).value = ""; //eID("n_city"+n).options[eID("n_city"+n).selectedIndex].label;
      }
      //alert('YO!');

}




function checkStateCity(resort_id,n,cReload) {
 if (n==null) {n='';}
 if (eID("n_state"+n).selectedIndex>0 && jQuery('#n_state'+n).val()==rinfo[resort_id].state_id) {
  //state already selected
 } else {
  //select state
  jQuery('#n_state'+n+' option[value='+rinfo[resort_id].state_id+']').attr("selected", "selected");
  //alert('['+rinfo[resort_id].state_id+'] ['+jQuery('#n_state'+n).val()+']');
  //also need to load cities & set apropriate for selected resort
  GetCities(n,1,rinfo[resort_id].city_id)
  
 }
 
 if (/*eID("n_city"+n).selectedIndex*/jQuery('#n_city'+n).val()>0 && jQuery('#n_city'+n).val()==rinfo[resort_id].city_id) {
  //city already selected
 } else {
  //select city
  if (cReload) {
   GetCities(n,1,rinfo[resort_id].city_id)
  } else {
   jQuery('#n_city'+n+' option[value='+rinfo[resort_id].city_id+']').attr("selected", "selected");
   SetCity(n);
  }
 }
}




function SetResort(n) {

      if (n==null) {n='';}
 

      if (eID("n_resort_id"+n)!==null && eID("n_resort_id"+n).selectedIndex>0) {
       eID("c_resort_fld"+n).value = eID("n_resort_id"+n).options[eID("n_resort_id"+n).selectedIndex].label;
       eID("c_resort_fld_control"+n).value = eID("n_resort_id"+n).options[eID("n_resort_id"+n).selectedIndex].label;
       checkStateCity(eID("n_resort_id"+n).value,n);
      } else {
       eID("c_resort_fld"+n).value = ""; 
       eID("c_resort_fld_control"+n).value = ""; 
      }

}






function SetCountry(n) {

      if (n==null) {n='';}

      if (eID("c_country"+n).selectedIndex>0) {
       eID("c_country_fld"+n).value = eID("c_country"+n).options[eID("c_country"+n).selectedIndex].label;
      } else {
       eID("c_country_fld"+n).value = ""; 
      }

}









function GetCities(n,wpo,city_id){

      if (n==null) {n='';}

      if (eID("n_state"+n).selectedIndex>0) {
       eID("c_state_fld"+n).value = eID("n_state"+n).options[eID("n_state"+n).selectedIndex].label;
       eID("c_state_fld_control"+n).value = eID("n_state"+n).options[eID("n_state"+n).selectedIndex].label;
      } else {
       eID("c_state_fld"+n).value = ""; //eID("n_state"+n).options[eID("n_state"+n).selectedIndex].label;
       eID("c_state_fld_control"+n).value = ""; //eID("n_state"+n).options[eID("n_state"+n).selectedIndex].label;
      }

      eID("c_city_fld"+n).value = "";
      eID("c_city_fld_control"+n).value = "";

      gc = 'getcities';

      if (wpo!=null && wpo==1) {gc= gc + '_wpo';}

      eID("c_cities_list"+n).innerHTML = selectCoverOpen+ajaxLIMG+"Loading cities list... "+selectCoverClose;

      var uniFltr = ""
      if (uniFName!="" && uniFVal!="") {
       uniFltr = "&"+uniFName+"="+uniFVal;
      }
      var call2 = domain+'/controller.php?call='+gc+ //getcities'+
                      '&country='+eID("c_country"+n).value+
                      '&state='+eID("n_state"+n).value+
		      uniFltr+
                      '&f6m=1'+
                      '&SessID='+userKey+
                      '&timer='+TimeRandFunction();

      jQuery.getScript(call2, 
       function(){
         if (n==null) {n='';}
     
         if (n!='') {
                     citiesList = citiesList.replace('n_city ','n_city'+n+' ');
                     citiesList = citiesList.replace('n_city ','n_city'+n+' ');
                     citiesList = citiesList.replace('SetCity()','SetCity('+n+')');
                    }
     
         eID("c_cities_list"+n).innerHTML = citiesList;
     
         gSAClass = getGSAC();
     
         if (n!='') {
          eID("n_city"+n).className = gSAClass;
         } else {
          eID("n_city").className = gSAClass;
         }
         if (city_id!=null) {
          jQuery('#n_city'+n+' option[value='+city_id+']').attr("selected", "selected");
          SetCity(n);
         }
       }
      );



}







function GetOnlyStates(n,wpo){

      if (n==null) {n='';}

      gc = 'getonlystates';

      if (wpo!=null && wpo==1) {gc= gc + '_wpo';}

	 if (n!='') {
             statesList = statesList.replace('n_state ','n_state'+n+' ');
             statesList = statesList.replace('n_state ','n_state'+n+' ');
             statesList = statesList.replace('GetCities()','GetCities('+n+',1)');
            }


      var uniFltr = ""
      if (uniFName!="" && uniFVal!="") {
       uniFltr = "&"+uniFName+"="+uniFVal;
      }
      var call2 = domain+'/controller.php?call='+gc+ //getcities'+
                      '&country='+eID("c_country"+n).value+
		      uniFltr+
                      '&f6m=1'+
                      '&SessID='+userKey+
                      '&timer='+TimeRandFunction();

      eID("c_states_list"+n).innerHTML = selectCoverOpen+ajaxLIMG+"Loading states list..."+selectCoverClose;
      eID("c_cities_list"+n).innerHTML = selectCoverOpen+ajaxLIMG+"Loading cities list..."+selectCoverClose;

      jQuery.getScript(call2, 
       function(){
	 eID("c_states_list"+n).innerHTML = statesList;
	
	 gSAClass = getGSAC();
	
	 if (n!='') {
	  eID("n_state"+n).className = gSAClass;
	 } else {
	  eID("n_state").className = gSAClass;
	 }
	
	 eID("c_cities_list"+n).innerHTML = selectCoverOpen+"You have to select state first..."+selectCoverClose;
         if (eID("c_resorts_list"+n)!=null) {
 	  eID("c_resorts_list"+n).innerHTML = selectCoverOpen+"You have to select state first..."+selectCoverClose;
         }
       }
      );

}

















function GetOnlyCountries(n,wpo){

      if (n==null) {n='';}

      gc = 'getonlycountries';

      if (wpo!=null && wpo==1) {gc= gc + '_wpo';}

	 if (n!='') {
             statesList = statesList.replace('n_state ','n_state'+n+' ');
             statesList = statesList.replace('n_state ','n_state'+n+' ');
             statesList = statesList.replace('GetCities()','GetCities('+n+',1)');
            }


      var uniFltr = ""
      if (uniFName!="" && uniFVal!="") {
       uniFltr = "&"+uniFName+"="+uniFVal;
      }
      var call2 = domain+'/controller.php?call='+gc+
		      uniFltr+
                      '&f6m=1'+
                      '&SessID='+userKey+
                      '&timer='+TimeRandFunction();

      eID("c_countries_list"+n).innerHTML = selectCoverOpen+ajaxLIMG+"Loading countries list..."+selectCoverClose;
      eID("c_states_list"+n).innerHTML = selectCoverOpen+ajaxLIMG+"Loading states list..."+selectCoverClose;
      eID("c_cities_list"+n).innerHTML = selectCoverOpen+ajaxLIMG+"Loading cities list..."+selectCoverClose;

      jQuery.getScript(call2, 
       function(){
	 eID("c_countries_list"+n).innerHTML = countriesList;
	
	 gSAClass = getGSAC();
	
	 if (n!='') {
	  eID("c_country"+n).className = gSAClass;
	 } else {
	  eID("c_country").className = gSAClass;
	 }

	 eID("c_states_list"+n).innerHTML = selectCoverOpen+"You have to select country first..."+selectCoverClose;
	 eID("c_cities_list"+n).innerHTML = selectCoverOpen+"You have to select country first..."+selectCoverClose;
         if (eID("c_resorts_list"+n)!=null) {
	  eID("c_resorts_list"+n).innerHTML = selectCoverOpen+"You have to select country first..."+selectCoverClose;
         }
       }
      );

}









function GetStates(n,wpo){

      if (n==null) {n='';}

      eID("c_state_fld"+n).value = "";
      eID("c_city_fld"+n).value = "";
      eID("c_state_fld_control"+n).value = "";
      eID("c_city_fld_control"+n).value = "";


      gc = 'getstates';

      if (wpo!=null && wpo==1) {gc= gc + '_wpo';}


      var uniFltr = ""
      if (uniFName!="" && uniFVal!="") {
       uniFltr = "&"+uniFName+"="+uniFVal;
      }
      var call2 = domain+'/controller.php?call='+gc+ //getcities'+
                      '&country='+eID("c_country"+n).value+
		      uniFltr+
                      '&f6m=1'+
                      '&SessID='+userKey+
                      '&timer='+TimeRandFunction();

      eID("c_states_list"+n).innerHTML = selectCoverOpen+ajaxLIMG+"Loading states list..."+selectCoverClose;
      eID("c_cities_list"+n).innerHTML = selectCoverOpen+ajaxLIMG+"Loading cities list..."+selectCoverClose;

      jQuery.getScript(call2, 
       function(){

	 if (n!='') {
             statesList = statesList.replace('n_state ','n_state'+n+' ');
             statesList = statesList.replace('n_state ','n_state'+n+' ');
             statesList = statesList.replace('GetCities()','GetCities('+n+',1)');
             citiesList = citiesList.replace('n_city ','n_city'+n+' ');
             citiesList = citiesList.replace('n_city ','n_city'+n+' ');
             citiesList = citiesList.replace('SetCity()','SetCity('+n+')');
            }

	 eID("c_states_list"+n).innerHTML = statesList;
	 eID("c_cities_list"+n).innerHTML = citiesList;

	 gSAClass = getGSAC();
	
	 if (n!='') {
	  eID("n_state"+n).className = gSAClass;
	  eID("n_city"+n).className = gSAClass;
	 } else {
	  eID("n_state").className = gSAClass;
	  eID("n_city").className = gSAClass;
	 }
       }
      );


}

