// JavaScript Document
// for location change
function doPost(optional){
	var ajax_path = "http://www.thaihoteldiscounts.com/ajax/";
	var country_ID = document.getElementById("country_ID").value;
	var city_ID = document.getElementById("city_ID").value;

	var request = "country_ID="+country_ID+"&city_ID="+city_ID;
	var url = "";
	var myConn = new XHConn();		
	
		if(optional == "city"){
			 url = ajax_path + "city_changer.php";
		}else if(optional == "location"){
			url = ajax_path + "location_changer.php";
		}else if(optional == "location_search"){
			url = ajax_path + "location_search_changer.php";
		}else if(optional == "byname"){
			url = ajax_path + "search_byname.php";
		}else if(optional == "byprice"){
			url = ajax_path + "search_byprice.php";
		}else if(optional == "hotel"){
			url = ajax_path + "hotel.php";
		}else if(optional == "review"){
			url = ajax_path + "review.php";
		}else if(optional == "recommend"){
			url = ajax_path + "recommendation.php";
		}
		
		
		myConn.connect( url, "POST", request, function( XML ) {
				var city;
				var location;
				var location_ID;
				var byname;
				var byprice;
				var hotel;
				if(optional == "city"){
					city = document.getElementById("city");
					city.innerHTML = XML.responseText;
					doPost('location');
				}else if(optional == "location"){
					location = document.getElementById("location");
					location.innerHTML = XML.responseText;
					if(typeof(document.getElementById("location_search")) != 'undefined'){
						doPost('location_search');
					}
					doPost('byname');
					doPost('byprice');
					doPost('hotel');
					doPost('review');
					doPost('recommend');
				}else if(optional == "location_search"){
					if(typeof(document.getElementById("location_search")) != 'undefined'){
						try
						{
							location_ID = document.getElementById("location_search");
							location_ID.innerHTML = XML.responseText;
						}
						catch(err)
					   {						  
						//alert("error")
					   }

					}
				}else if(optional == "byname"){
					byname = document.getElementById("byname");
					byname.innerHTML = XML.responseText;
				}else if(optional == "byprice"){
					byprice = document.getElementById("byprice");
					byprice.innerHTML = XML.responseText;
				}else if(optional == "hotel"){
					hotel = document.getElementById("hotel");
					hotel.innerHTML = XML.responseText;
				}else if(optional == "review"){
					hotel = document.getElementById("review");
					hotel.innerHTML = XML.responseText;
				}else if(optional == "recommend"){
					hotel = document.getElementById("recommend");
					hotel.innerHTML = XML.responseText;
				}
			} 
		);
	
	
}

function doPostLocation(optional){

	var ajax_path = "http://www.thaihoteldiscounts.com/ajax/";
	var request = "";
	var url = "";
	var myConn = new XHConn();			
	
	if(optional == "country"){
		url = ajax_path + "load_country.php?country_ID=" + check_uri('country_ID=') + "&city_ID=" + check_uri('city_ID=');
	}else if(optional == "city"){
		url = ajax_path + "load_city.php?country_ID=" + check_uri('country_ID=') + "&city_ID=" + check_uri('city_ID=');
	}else if(optional == "location"){
		url = ajax_path + "load_location.php?country_ID=" + check_uri('country_ID=') + "&city_ID=" + check_uri('city_ID=');
	}else if(optional == "location_search"){
		url = ajax_path + "load_location_search.php?country_ID=" + check_uri('country_ID=') + "&city_ID=" + check_uri('city_ID=');
	}else if(optional == "byname"){
		url = ajax_path + "load_search_byname.php?city_ID=" + check_uri('city_ID=');
	}else if(optional == "byprice"){
		url = ajax_path + "load_search_byprice.php?city_ID=" + check_uri('city_ID=');
	}else if(optional == "hotel"){
		url = ajax_path + "load_hotel.php?country_ID=" + check_uri('country_ID=') + "&city_ID=" + check_uri('city_ID=');
	}else if(optional == "review"){
		url = ajax_path + "load_review.php?country_ID=" + check_uri('country_ID=') + "&city_ID=" + check_uri('city_ID=');
	}else if(optional == "recommend"){
		url = ajax_path + "load_recommendation.php?country_ID=" + check_uri('country_ID=') + "&city_ID=" + check_uri('city_ID=');
	}
	
	myConn.connect( url, "POST", request, function( XML ) {
			var element;			
			if(optional == "country"){
				element = document.getElementById("country");
				element.innerHTML = XML.responseText;
				doPostLocation('city');
			}else if(optional == "city"){
				element = document.getElementById("city");
				element.innerHTML = XML.responseText;
				doPostLocation('location');
			}else if(optional == "location"){
				element = document.getElementById("location");
				element.innerHTML = XML.responseText;
				doPostLocation('byname');
				doPostLocation('byprice');
				doPostLocation('hotel');
				doPostLocation('review');
				doPostLocation('recommend');
			}else if(optional == "location_search"){
				element = document.getElementById("location_search");
				element.innerHTML = XML.responseText;
			}else if(optional == "byname"){
				element = document.getElementById("byname");
				element.innerHTML = XML.responseText;
			}else if(optional == "byprice"){
				element = document.getElementById("byprice");
				element.innerHTML = XML.responseText;
			}else if(optional == "hotel"){
				element = document.getElementById("hotel");
				element.innerHTML = XML.responseText;
			}else if(optional == "review"){
				element = document.getElementById("review");
				element.innerHTML = XML.responseText;
			}else if(optional == "recommend"){
				element = document.getElementById("recommend");
				element.innerHTML = XML.responseText;
			}
			
		} 
	);
	
}

//script for get value of uri parameter in url
function check_uri(findStr){
	var url = location.href;
	var posQuery = (url.search(/\?/));
	var posStr;
	var foundStr = "";
	var strQuery = "";
	var aryStrQuery;
	

	if(posQuery > 0){
		strQuery = url.substring((posQuery)+1, url.length);
	}
	
	aryStrQuery = strQuery.split("&");
	
	
	for(i=0; i<aryStrQuery.length; i++){
		posStr = (aryStrQuery[i].search(findStr));
		if(posStr != -1){
			foundStr = aryStrQuery[i].substring((posStr+findStr.length), aryStrQuery[i].length);			
		}
	}
	return foundStr;
}


//-----------------------------------random recommendation-------------------------------

function doPostRandomRecomend(cityId, elementId){
	var ajax_path = "http://www.thaihoteldiscounts.com/ajax/";

	var request = "";
	var url = "";
	var myConn = new XHConn();		
	
		
		url = ajax_path + "random_recommendation.php?city_ID=" + cityId;
		
		myConn.connect( url, "POST", request, function( XML ) {
				var element;
				
				element = document.getElementById(elementId);
				element.innerHTML = XML.responseText;
			} 
		);	
}

//-----------------------------------show hotel rate-------------------------------

function doPostHotelRate(hotelId, elementId){
	var ajax_path = "http://www.thaihoteldiscounts.com/ajax/";

	var request = "";
	var url = "";
	var myConn = new XHConn();		
	
		
		url = ajax_path + "rate.php?hotel_ID=" + hotelId;
		
		myConn.connect( url, "POST", request, function( XML ) {
				var element;
				
				element = document.getElementById(elementId);
				element.innerHTML = XML.responseText;
			} 
		);	
}