var CITIES = 1;
var HOTELS = 2;
var currentType=0;

function createPostString(type){
var poststr = "";
if(type == CITIES){
var  stateid=getElement("stateid").value;	
if( !stateid)  stateid=document.getElementById("searchhotelform").stateid.value;
 poststr = "type=CITIES&stateid=" + stateid + "&ajaxcachebust="+new Date().getTime();
} else if(type == HOTELS){
	
var cityid=getElement("cityId").value;
if(!cityid) cityid= document.getElementById("searchhotelform").cityId.value;
 poststr = "type=HOTELS&cityid=" + cityid + "&ajaxcachebust="+new Date().getTime();
}
return poststr;
}
function refreshCities(basePath){
currentType = CITIES;
var poststr=createPostString(CITIES);
ajaxpack.getAjaxRequest(basePath+"commons/getcities.php", poststr, handleResponse, "txt");
}

function refreshHotels(basePath){
currentType = HOTELS;
var poststr=createPostString(HOTELS);
ajaxpack.getAjaxRequest(basePath+"commons/getcities.php", poststr, handleResponse, "txt");

}

function handleResponse(){
	var myajax=ajaxpack.ajaxobj;

	if (myajax.readyState == 4){ //if request of file completed
		if (myajax.status==200 || window.location.href.indexOf("http")==-1){ //if request was successful or running script locally
			
				 updateMenu( myajax.responseText );		
				
		} else {
		  document.getElementById("status_txt").innerHTML='Error in reading cities';
		}
	}	
}

function updateMenu( responseList ){
	
	var dropDown= null;
    if( currentType == CITIES){
		dropDown =  getElement('cityId');
      if(!dropDown) dropDown = document.getElementById("searchhotelform").cityId ;
	   
	}else if(currentType == HOTELS)	{  
	dropDown =  getElement('hotelId');
      
	  if(!dropDown) dropDown = document.getElementById("searchhotelform").hotelId ;
	}
   
   	if( dropDown != null){
	   dropDown.options.length = 1;
	}
	
   	if( responseList == '' || responseList.length ==0 || responseList =='0' ){
	    document.getElementById("status_txt").innerHTML="<font style='color:#CF0000;font-weight:bold;'>No result found</font>";
   		 return;
     }
   

    var responseArray = responseList.split(",");
	dropDown.options.length = parseInt( (parseInt(responseArray.length)/2) + 1 );
	for(i=0,j=1;  i<responseArray.length; i+=2,j++){
	    dropDown.options[j].value = responseArray[i].substring(1,responseArray[i].length-1);
		dropDown.options[j].text =  responseArray[i+1].substring(1,responseArray[i+1].length-1);
	}	
}
 
function getElement(id ){
    var returnVar=null;
    if (document.getElementById)
        returnVar = document.getElementById(id);
    else if (document.all)
        returnVar = document.all[id];
    else if (document.layers)
        returnVar = document.layers[id];
    return returnVar;
}
