var XmlHttpObj; // declare a global  XMLHTTP Request object
var domain_name = document.URL;
var testdomain;
if (domain_name.match("www")){
testdomain = domain_name.substring(11,domain_name.lastIndexOf('/'));
var myUrl = "http://www."+testdomain;
}
else{
testdomain = domain_name.substring(7,domain_name.lastIndexOf('/'));
var myUrl = "http://"+testdomain;
}
// declare a global  XMLHTTP Request object
var XmlHttpObj;

// create an instance of XMLHTTPRequest Object, varies with browser type, try for IE first then Mozilla
function CreateXmlHttpObj()
{
	// try creating for IE (note: we don't know the user's browser type here, just attempting IE first.)
	try
	{
		XmlHttpObj = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			XmlHttpObj = new ActiveXObject("Microsoft.XMLHTTP");
		} 
		catch(oc)
		{
			XmlHttpObj = null;
		}
	}
	// if unable to create using IE specific code then try creating for Mozilla (FireFox) 
	if(!XmlHttpObj && typeof XMLHttpRequest != "undefined") 
	{
		XmlHttpObj = new XMLHttpRequest();
	}
}

// this function called when state of  XmlHttpObj changes
// we're interested in the state that indicates data has been
// received from the server
function StateChangeHandler()
{
	// state ==4 indicates receiving response data from server is completed
	if(XmlHttpObj.readyState == 4)
	{
		// To make sure valid response is received from the server, 200 means response received is OK
		if(XmlHttpObj.status == 200)
		{			
			PopulateCityList(XmlHttpObj.responseXML.documentElement);
		}
		else
		{
			alert("problem retrieving data from the server, status code: "  + XmlHttpObj.status);
		}
	}
}

// populate the contents of the country dropdown list
function PopulateCityList(cityNode)
{
	document.main.cityList.disabled=true;
	var cityList = document.getElementById("cityList");	
	var cityNodes = cityNode.getElementsByTagName('city');
	var idValue ;
	var textValue; 
	var optionItem;
	// populate the dropdown list with data from the xml doc
	for (var count = 0; count < cityNodes.length; count++)
	{
   		textValue = GetInnerText(cityNodes[count]);
		idValue = cityNodes[count].getAttribute("id");
		//alert(textValue);
		optionItem = new Option( textValue, idValue,  false, false);
		cityList.options[cityList.length] = optionItem;
	}
	document.main.cityList.disabled=false;
}

// returns the node text value 
function GetInnerText (node)
{
	 return (node.textContent || node.innerText || node.text) ;
}


//////////////////For Locations Populating/////////////////////////

function CityListOnChange() 
{

try
{
	var cityList = document.getElementById("cityList");
    var selectedCity = cityList.options[cityList.selectedIndex].value ;
	var countryname = document.getElementById("frmcountry").value;	
}
catch(err)
{  
alert("Choose Your Hotel / Villa");  
//alert("selectedCity "+ selectedCity + "Error "+err.description);
}


	var cityList = document.getElementById("cityList");
    var selectedCity = cityList.options[cityList.selectedIndex].value ;
	var countryname = document.getElementById("frmcountry").value;
//	var countryname = document.main.frmcountry;
	var isvilla = document.getElementById("isVilla").value;


//	var requestUrl = myUrl+"/quick_quote_xml.php?active_country="+countryname+'&filter3='+selectedCity;   
	var requestUrl = myUrl+"/quick_quote_xml.php?active_country="+countryname+"&isVilla="+isvilla+"&filter3="+selectedCity; 
//	alert (requestUrl);
	CreateXmlHttpObj();
	
	// verify XmlHttpObj variable was successfully initialized
	if(XmlHttpObj)
	{
        // assign the StateChangeHandler function ( defined below in this file)
        // to be called when the state of the XmlHttpObj changes
        // receiving data back from the server is one such change
		XmlHttpObj.onreadystatechange = StateChangeHandler2;
		
		// define the iteraction with the server -- true for as asynchronous.
		XmlHttpObj.open("GET", requestUrl,  true);
		
		// send request to server, null arg  when using "GET"
		XmlHttpObj.send(null);		
	}
}


// this function called when state of  XmlHttpObj changes
// we're interested in the state that indicates data has been
// received from the server
function StateChangeHandler2()
{
	// state ==4 indicates receiving response data from server is completed
	if(XmlHttpObj.readyState == 4)
	{
		// To make sure valid response is received from the server, 200 means response received is OK
		if(XmlHttpObj.status == 200)
		{			
			PopulateLocationList(XmlHttpObj.responseXML.documentElement);
		}
		else
		{
			alert("problem retrieving data from the server, status code: "  + XmlHttpObj.status);
		}
	}
}

// populate the contents of the country dropdown list
function PopulateLocationList(locationNode)
{

	var locationList = document.getElementById("locationList");
	// clear the country list 
	
	for (var count = locationList.options.length-1; count >-1; count--)
	{
		locationList.options[count] = null;
	}
// alert(locationList);
	var locationNodes = locationNode.getElementsByTagName('location');
// alert(locationNodes);
	var idValue;
	var textValue; 
	var optionItem;
	// populate the dropdown list with data from the xml doc
	for (var count = 0; count < locationNodes.length; count++)
	{
   		textValue = GetInnerText(locationNodes[count]);
		idValue = locationNodes[count].getAttribute("id");

		optionItem = new Option( textValue, idValue,  false, false);
	
	locationList.options[locationList.length] = optionItem;
	}
			
}

function OnCityLoad() 
{
	var CityList = document.getElementById("cityList");
	var countryname = document.getElementById("frmcountry");
//	var countryname = document.main.frmcountry;

	var requestUrl = myUrl+"/quick_quote_xml.php?filter1="+countryname.value;	

	CreateXmlHttpObj();
	if(XmlHttpObj)
	{
		XmlHttpObj.onreadystatechange = StateChangeHandler;	
		XmlHttpObj.open("GET", requestUrl,  true);
		XmlHttpObj.send(null);
	}
}


//Newly Added Js Functions for used for various locations inthe website

	function open_city_tag(){
		document.write('<select id="cityList" name="cityList" class="textblack" onfocus="return CityListOnChange()" onChange="return CityListOnChange()" size="5" style="width:200px;">');
	}
	
	function open_location_tag(){
		document.write('<select name="id" id="locationList" size="5" style="width:200px;" class="textblack">');
	}
	
	function close_select_tag (node){
		 document.write("</select>");
	}	

	function validateForm(frmname){
		if (frmname.id.value=="") {
			alert_message = "Please select a Hotel.";
			alert(alert_message);
			frmname.id.focus();
			return false;
		}
		else if (frmname.id.value=="0")
			return false;
		else
			return true;		
	}	

