	var regionCdSelected="";
	var countryCdSelected="";
	var locationCdSelected="";
	var parentLocationCdSelected="";
	
	var selectedLocationLevel=0;
	var totalLocationLevel=0;
	var tempLocationCd="";
	
	var locationLanguage;
	var searchType="";
	
	var criteria = "hotelSearchingCriteria";
	
	function getAllRegions(){
    	//document.getElementById('totalLocationLevel').value=0;
    	
		//clear the country and location level
		if(document.getElementById('countryCd')!=null){
	  			document.getElementById('countryDiv').removeChild(document.getElementById('countryCd'));
	    	}
	    	if(document.getElementById('locationLevel')!=null){
	  			document.getElementById('locationDiv').removeChild(document.getElementById('locationLevel'));
	    	}
	    	if(document.getElementById('regionCd')!=null){
				var regionDiv=document.getElementById("regionDiv");
				if(regionDiv!=null){
	  				regionDiv.removeChild(document.getElementById('regionCd'));
	  			}
	    	}
	    LocationService.getAllRegions(locationLanguage,false,getAllRegionsCallback);
	}
	
	function getAllActiveRegions(){
    	//document.getElementById('totalLocationLevel').value=0;
    	
		//clear the country and location level
		if(document.getElementById('countryCd')!=null){
	  			document.getElementById('countryDiv').removeChild(document.getElementById('countryCd'));
	    	}
	    	if(document.getElementById('locationLevel')!=null){
	  			document.getElementById('locationDiv').removeChild(document.getElementById('locationLevel'));
	    	}
	    	if(document.getElementById('regionCd')!=null){
				var regionDiv=document.getElementById("regionDiv");
				if(regionDiv!=null){
	  				regionDiv.removeChild(document.getElementById('regionCd'));
	  			}
	    	}
	    LocationService.getAllActiveRegions(locationLanguage,"P",getAllRegionsCallback);
	}
	
	function getAllRegionsCallback(data)
    {
    	try
  		{
			if ( data ){
				if(data.length>0){
					var regionDiv=document.getElementById("regionDiv");
					if(document.getElementById('regionCd')!=null && regionDiv!=null){
	  					regionDiv.removeChild(document.getElementById('regionCd'));
	    			}
					var s = document.createElement('select'); 

					if(searchType=="p"){
						criteria = "fitSearchingCriteria";
					}else if(searchType=="h"){
						criteria = "hotelSearchingCriteria";
					}

					//'hotelSearchingCriteria.regionCd'
			        s.setAttribute('name',criteria.concat('.regionCd')); 
			        s.setAttribute('id','regionCd'); 
			        //s.setAttribute('class','td_content');
			        //add onchange action
					s.onchange=function(){ 
						getCountries(s.value);
					} 

					s.onkeyup=function(){
							getCountries(s.value);
					}

					if(regionDiv!=null){
			    		regionDiv.appendChild(s);
			    	}

			    	if(locationLanguage=="ENG"){
			    		s.options[s.options.length] = new Option("--All Regions--", "");
			    	}else{
			    		s.options[s.options.length] = new Option("--請選擇區域--", "");
			    	}

					for (var i=0; i<data.length ; i++){
						s.options[s.options.length] = new Option(data[i].name, data[i].code);
						
						if(regionCdSelected!=null && regionCdSelected!=""){
							if(regionCdSelected==data[i].code){
								s.selectedIndex=i+1;
								getCountries(data[i].code);
							}
						}
					}
				}
			}
		}catch(err)
		{
			alert(err);
			regionCdSelected="";
    		countryCdSelected="";
    		locationCdSelected="";
    		parentLocationCdSelected="";
    		getAllRegions();
    		
		}
    }

    function getCountries(regionCode){

    	try{
    		if(regionCdSelected!=null && regionCdSelected!=""){

    			if(regionCode!=regionCdSelected){
		    		regionCdSelected="";
		    		countryCdSelected="";
		    		locationCdSelected="";
    				parentLocationCdSelected="";
		    		document.getElementById('totalLocationLevel').value=0;
	    		}
	    	}

			if(searchType=="p"){
		    	if(regionCode=="MO"){
		    		if(document.getElementById("fromDate")!=null){
			    		var today = new Date();
						today.setDate(today.getDate()+3); 
			    		document.getElementById("fromDate").value=dt2dtstr(today);
		    		}
		    	}else{
					if (document.getElementById("fromDate")!=null){
			    		var fromDateValue=document.getElementById("fromDate").value;
		
			    		if(fromDateValue!=""){
			    			fromDateValue=str2dt(fromDateValue);
			    			var today = new Date();
							today.setDate(today.getDate()+5); 
			    			if(today>fromDateValue){
			    				document.getElementById("fromDate").value=dt2dtstr(today);
			    			}
			    		}
		    		}
		    	}
		    }

	    	if(document.getElementById('countryCd')!=null){
	  			document.getElementById('countryDiv').removeChild(document.getElementById('countryCd'));
	    	}
	    	
	    	if(document.getElementById('locationLevel')!=null){
	  			document.getElementById('locationDiv').removeChild(document.getElementById('locationLevel'));
	    	}
	    	
	    	if(regionCode=="EU" || regionCode=="USCA"){
	    		LocationService.getActiveCountries(locationLanguage,"P",regionCode,getCountriesCallback);
	    	}else{
		    		var s = document.createElement('input'); 
		    		
					if(searchType=="p"){
						criteria = "fitSearchingCriteria";
					}else if(searchType=="h"){
						criteria = "hotelSearchingCriteria";
					}
		    		
		    		s.setAttribute('name',criteria.concat('.countryCd')); 
			        s.setAttribute('id','countryCd');
			        s.setAttribute('type','hidden');
			        s.setAttribute('value',regionCode);
			    	document.getElementById('countryDiv').appendChild(s);
			    	getLocationsByCountry(regionCode);
	    	}
	    }catch(err)
		{
			regionCdSelected="";
    		countryCdSelected="";
    		locationCdSelected="";
    		parentLocationCdSelected="";
    		getAllRegions();
		}
    }
    
    function getCountriesCallback( data ){
    	try{
			if ( data ){
				if(data.length>0){
					if(document.getElementById('countryCd')!=null){
	  					document.getElementById('countryDiv').removeChild(document.getElementById('countryCd'));
	    			}
					var s = document.createElement('select'); 
					
					if(searchType=="p"){
						criteria = "fitSearchingCriteria";
					}else if(searchType=="h"){
						criteria = "hotelSearchingCriteria";
					}
					
					//s.setAttribute('class','td_content');
			        s.setAttribute('name',criteria.concat('.countryCd')); 
			        s.setAttribute('id','countryCd'); 
			        
		         //add onchange action
					s.onchange=function(){ 
						getLocationsByCountry(s.value);
					} 
					s.onkeyup=function(){
						getLocationsByCountry(s.value);
					}
					
			    	document.getElementById('countryDiv').appendChild(s);
			    	if(data!=null && data.length>1){
			    		//if(locationLanguage=="ENG"){
			    		//	s.options[s.options.length] = new Option("", "");
			    		//}else{
			    			s.options[s.options.length]=new Option("","");
			    		//}
			    	}
			    	
			    	for (var i=0; i<data.length ; i++){
						s.options[s.options.length] = new Option(data[i].name, data[i].code);
						if(countryCdSelected!=null && countryCdSelected!=""){
							if(countryCdSelected==data[i].code){
								//if(data.length>1){
									//s.selectedIndex=i+1;
								//}else{
									s.selectedIndex=i+1;
								//}
								getLocationsByCountry(data[i].code);
							}
						}
					}
					
					//if(countryCdSelected==null || countryCdSelected==""){
					//	getLocationsByCountry(data[0].code);
					//}
				}
			}    
		}catch(err)
		{
			regionCdSelected="";
    		countryCdSelected="";
    		locationCdSelected="";
    		parentLocationCdSelected="";
    		getAllRegions();
		}		
    }
 
    function getLocationsByCountry(countryCode){
    	try{
    		if(countryCdSelected!=null && countryCdSelected!=""){
	    		if(countryCode!=countryCdSelected){
		    		countryCdSelected="";
		    		locationCdSelected="";
    				parentLocationCdSelected="";
		    		document.getElementById('totalLocationLevel').value=0;
		    	}
	    	}
	    	
	    	if(document.getElementById('locationLevel')!=null){
	  			document.getElementById('locationDiv').removeChild(document.getElementById('locationLevel'));
	    	}
	    	
	    	if(countryCode!=""){
	    		LocationService.getLocationsByCountry(locationLanguage,countryCode,false,searchType,getLocationsByCountryCallback);
	    	}
    	}catch(err)
		{
			regionCdSelected="";
    		countryCdSelected="";
    		locationCdSelected="";
    		parentLocationCdSelected="";
    		getAllRegions();
		}
    }
    
    function getLocationsByCountryCallback( data ){
    	try{
	 		if ( data ){
	 			if(data.length>0){
				    if(document.getElementById('locationLevel')!=null){
			  			document.getElementById('locationDiv').removeChild(document.getElementById('locationLevel'));
			    	}
			    	
		 			var locationLayer = document.createElement('locationLevel'); 
				    locationLayer.setAttribute('id','locationLevel'); 
				    document.getElementById('locationDiv').appendChild(locationLayer);
		 			var divLocation1;
			    	divLocation1 = document.createElement('div'); 
		 			divLocation1.setAttribute('name','div_location_0');  
				    divLocation1.setAttribute('id','div_location_0'); 
				    document.getElementById('locationLevel').appendChild(divLocation1); 
				    
				     			
					var s = document.createElement("select"); 
			        s.setAttribute('name','locationCd_0'); 
			        s.setAttribute('id','locationCd_0');
			         
			        //add onchange action
			        if(searchType=="h"){
						s.style.width="150px";
						s.onchange=function(){ 
							getLocationsByParent(s.value);
						} 
						s.onkeyup=function(){
							getLocationsByParent(s.value);
						}
					}
			    	divLocation1.appendChild(s);
			    	
					for (var i=0; i<data.length ; i++){
						s.options[s.options.length] = new Option(data[i].name, data[i].code);
						
						if(parentLocationCdSelected!=null && parentLocationCdSelected!=""){
							if(parentLocationCdSelected==data[i].code){
								if(searchType=="h"){
									//s.selectedIndex=i+1;
									s.selectedIndex=i;
									getLocationsByParent(data[i].code);
								}
							}
						}else{
							if(locationCdSelected==data[i].code){
									//s.selectedIndex=i+1;
									s.selectedIndex=i;
							}
						}
					}
					document.getElementById('totalLocationLevel').value=1;
					//if level have only one data, check is it have the child locationLevel
					if(searchType=="h"){
							getLocationsByParent(data[s.selectedIndex].code);
					}
				}
			}   
		}catch(err)
		{
			regionCdSelected="";
    		countryCdSelected="";
    		locationCdSelected="";
    		parentLocationCdSelected="";
    		getAllRegions();
		} 	
    }   
    
    function getLocationsByParent(locationCode){
    	try{
	    	if(locationCode!=""){	    		
		    	if(document.getElementById('div_location_1')!=null){
					  document.getElementById('locationLevel').removeChild(document.getElementById('div_location_1'));
				}
		    	LocationService.getLocationsByParent(locationLanguage,locationCode,false,searchType,getLocationsByParentCallback);
		    }else{
				if(document.getElementById('div_location_1')!=null){
					  	document.getElementById('locationLevel').removeChild(document.getElementById('div_location_1'));
				}
		    	document.getElementById('totalLocationLevel').value=1;
		    }
		  }catch(err)
		{
			regionCdSelected="";
    		countryCdSelected="";
    		locationCdSelected="";
    		parentLocationCdSelected="";
    		getAllRegions();
		}
    }
    
    function getLocationsByParentCallback( data ){
    	try{
			if ( data ){
				if(data.length>0){
					if(document.getElementById('div_location_1')!=null){
	  					document.getElementById('locationLevel').removeChild(document.getElementById('div_location_1'));
	    			}
			    	divLocationSubLevel = document.createElement('div'); 
			    	divLocationSubLevel.setAttribute('name','div_location_1');
					divLocationSubLevel.setAttribute('id','div_location_1'); 
					document.getElementById('locationLevel').appendChild(divLocationSubLevel); 
					
					var s = document.createElement('select'); 
			        s.setAttribute('name','locationCd_1'); 
			        s.setAttribute('id','locationCd_1'); 
			       // s.setAttribute('class','td_content');
			        
			    	divLocationSubLevel.appendChild(s);
			    	
			    	if(data.length>1){
			    		if(locationLanguage=="ENG"){
			    			s.options[s.options.length] = new Option("All Locations", "");
			    		}else{
			    			s.options[s.options.length] = new Option("所有地區", "");
			    		}
			    	}
			    	
					for (var i=0; i<data.length ; i++){
						s.options[s.options.length] = new Option(data[i].name, data[i].code);
						if(locationCdSelected!=null && locationCdSelected!=""){
							if(s.options[i].value==locationCdSelected){
								s.options[i].selected=true;
							}
						}
					}
					
	    			document.getElementById('totalLocationLevel').value=2;
				}else{
					document.getElementById('totalLocationLevel').value=1;
				}
			} else{
				//no data call back
				document.getElementById('totalLocationLevel').value=1;	
			} 
		}catch(err)
		{
			regionCdSelected="";
    		countryCdSelected="";
    		locationCdSelected="";
    		parentLocationCdSelected="";
    		getAllRegions();
		}
    }
