var wkdArray = new Array('Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat')
var jsNow = new Date();

function fnValidateDate()
	{
		if (document.frmSearch.inDay.selectedIndex==0 && document.frmSearch.inMonth.selectedIndex==0 && document.frmSearch.inYear.selectedIndex==0)
			{
				inDate = new Date(jsNow.getFullYear(), jsNow.getMonth(), jsNow.getDate()+14);
				document.frmSearch.inDay.selectedIndex = inDate.getDate()-1;
				document.frmSearch.inMonth.selectedIndex = inDate.getMonth();
				document.frmSearch.inYear.selectedIndex = inDate.getFullYear()-jsNow.getFullYear();
			}
		
		var inDay = parseInt(document.frmSearch.inDay.options[document.frmSearch.inDay.selectedIndex].value);
		var inMonth = parseInt(document.frmSearch.inMonth.options[document.frmSearch.inMonth.selectedIndex].value-1);
		var inYear = parseInt(document.frmSearch.inYear.options[document.frmSearch.inYear.selectedIndex].value);
		var inDate = new Date(inYear, inMonth, inDay);
		if (inDate<jsNow)
			{
				inDate = new Date(jsNow.getFullYear(), jsNow.getMonth(), jsNow.getDate()+1);
				document.frmSearch.inDay.selectedIndex = inDate.getDate()-1;
				document.frmSearch.inMonth.selectedIndex = inDate.getMonth();
				document.frmSearch.inYear.selectedIndex = inDate.getFullYear()-jsNow.getFullYear();
			}
		if (inDate.getMonth() != inMonth || inDate.getDate() != inDay)
			{
				document.frmSearch.inDay.selectedIndex = inDate.getDate()-1;
				document.frmSearch.inMonth.selectedIndex = inDate.getMonth();
				document.frmSearch.inYear.selectedIndex = inDate.getFullYear()-jsNow.getFullYear();
			}
		var outDay = parseInt(document.frmSearch.inDay.options[document.frmSearch.outDay.selectedIndex].value);
		var outMonth = parseInt(document.frmSearch.inMonth.options[document.frmSearch.outMonth.selectedIndex].value-1);
		var outYear = parseInt(document.frmSearch.inYear.options[document.frmSearch.outYear.selectedIndex].value);
		var outDate = new Date(outYear, outMonth, outDay);
		if (inDate>=outDate)
			{
				outDate = new Date(inDate.getFullYear(), inDate.getMonth(), inDate.getDate()+1);
				document.frmSearch.outDay.selectedIndex = outDate.getDate()-1;
				document.frmSearch.outMonth.selectedIndex = outDate.getMonth();
				document.frmSearch.outYear.selectedIndex = outDate.getFullYear()-jsNow.getFullYear();
			}
		if (outDate.getMonth() != outMonth || outDate.getDate() != outDay)
			{
				document.frmSearch.outDay.selectedIndex = outDate.getDate()-1;
				document.frmSearch.outMonth.selectedIndex = outDate.getMonth();
				document.frmSearch.outYear.selectedIndex = outDate.getFullYear()-jsNow.getFullYear();
			}
		document.getElementById('SetInDay').innerHTML = ' [' + wkdArray[inDate.getDay()] + '] ';
		document.getElementById('SetOutDay').innerHTML = ' [' + wkdArray[outDate.getDay()] + '] ';
		var lenStay = Math.abs(Math.round((outDate-inDate)/86400000));
		document.getElementById('lenStay').innerHTML = lenStay;
	}



function sendRequest(url,callback,postData) 
	{
		var req = createXMLHTTPObject();
		if (!req) return;
		var method = (postData) ? "POST" : "GET";
		req.open(method,url,true);
		req.setRequestHeader('User-Agent','XMLHTTP/1.0');
		if (postData)
			req.setRequestHeader('Content-type','application/x-www-form-urlencoded');
		req.onreadystatechange = function () 
				{
				if (req.readyState != 4) return;
				if (req.status != 200 && req.status != 304) 
					{
		//			alert('HTTP error ' + req.status);
					return;
					}
				callback(req);
			}
		if (req.readyState == 4) return;
		req.send(postData);
	}

var XMLHttpFactories = 
	[
	function () {return new XMLHttpRequest()},
	function () {return new ActiveXObject("Msxml2.XMLHTTP")},
	function () {return new ActiveXObject("Msxml3.XMLHTTP")},
	function () {return new ActiveXObject("Microsoft.XMLHTTP")}
	];

function createXMLHTTPObject() 
	{
		var xmlhttp = false;
		for (var i=0;i<XMLHttpFactories.length;i++) 
			{
				try 
					{
						xmlhttp = XMLHttpFactories[i]();
					}
				catch (e) 
					{
						continue;
					}
				break;
			}
		return xmlhttp;
	}
	

function fnPopulateCtl(jsdata,jsCtrl,jsstart,jsdefaultvalue) 
	{
		if (jsdata.length >0)
			{
				var jsarydata = jsdata.split("|")
				for (i = jsCtrl.options.length; i >= 0; i--) 
					{
					jsCtrl.options[i] = null; 
					}
				for (i = jsstart; i < jsarydata.length; i++) 
					{
					jsCtrl.options[i-jsstart] = new Option(jsarydata[i]);
					jsCtrl.options[i-jsstart].value = jsarydata[i]; 
					}
				// select first item (prompt) for sub list
				if (jsCtrl.options.length>0)
					{
					jsCtrl.selectedIndex = 0;
					for (i = 0; i < jsCtrl.options.length; i++) 
						{
						  if (jsCtrl.options[i].value == jsdefaultvalue) 
							  {
							    jsCtrl.selectedIndex = i;
							  }
						}
					}
			}
	}

function fnClearCtl(jsCtrl) 
	{
		for (i = jsCtrl.options.length; i >= 0; i--) 
			{
				jsCtrl.options[i] = null; 
			}
		jsCtrl.options[0] = new Option('Loading....');
		jsCtrl.options[1] = new Option('zzzzzzzzzzzzzzzzzzzzzzzzz');
		jsCtrl.options[2] = new Option('');
		jsCtrl.options[3] = new Option('');
		jsCtrl.options[4] = new Option('');
		jsCtrl.selectedIndex = 0;
	}

function CountryLoad() 
	{
		addLoadEvent(CountryLoad2);
	}

function CountryLoad2() 
	{
		sendRequest('http://www.hotel.com.au/_SearchPanel/Country.asp?startCountry=' + document.frmSearch.startCountry.value,CountryLoadProcess);
	}

function addLoadEvent(func) 
	{
		var oldonload = window.onload;
		if (typeof window.onload != 'function') 
			{
				window.onload = func;
			} 
		else 
			{
				window.onload = function() 
					{
						if (oldonload) 
							oldonload();
						func();
					}
			}
	}

function CountryLoadProcess(req) 
	{
		fnPopulateCtl(req.responseText,document.frmSearch.Country,0,document.frmSearch.startCountry.value);
		CountryChange();
	}

function CountryChange() 
	{
		fnClearCtl(document.frmSearch.City);
		fnClearCtl(document.frmSearch.SuburbLandmark);
		if (document.frmSearch.Country.value == "More Countries...")
			{
			fnClearCtl(document.frmSearch.Country);
			sendRequest('http://www.hotel.com.au/_SearchPanel/Country.asp?ysnAll=1',CountryLoadProcess);
			}
		else
			sendRequest('http://www.hotel.com.au/_SearchPanel/City.asp?startCity=' + document.frmSearch.startCity.value + '&Country=' + escape(document.frmSearch.Country.value) ,CountryChangeProcess);
	}

function CountryChangeProcess(req) 
	{
		fnPopulateCtl(req.responseText,document.frmSearch.City,0,document.frmSearch.startCity.value);
		CityChange();
	}

function CityChange() 
	{
		fnClearCtl(document.frmSearch.SuburbLandmark);
		if (document.frmSearch.City.value == "More Cities...")
			{
			fnClearCtl(document.frmSearch.City);
			sendRequest('http://www.hotel.com.au/_SearchPanel/City.asp?ysnAll=1&Country=' + escape(document.frmSearch.Country.value) ,CountryChangeProcess);
			}
		else
			sendRequest('http://www.hotel.com.au/_SearchPanel/SuburbLandmark.asp?Country=' + escape(document.frmSearch.Country.value) + '&City=' + escape(document.frmSearch.City.value), CityChangeProcess);
	}

function CityChangeProcess(req) 
	{
		// document.getElementById('testspace').innerHTML = req.responseText
		if (req.responseText.indexOf("|")>0)
			{
				document.getElementById('SuburbLandmarktitle').innerHTML = req.responseText.substr(0,req.responseText.indexOf("|"))
				document.getElementById('SuburbLandmarkrow').style.display = '';
			}
		else
			{
				document.getElementById('SuburbLandmarktitle').innerHTML = req.responseText
				document.getElementById('SuburbLandmarkrow').style.display = 'none';
			}
		fnPopulateCtl(req.responseText,document.frmSearch.SuburbLandmark,1)
	}


