// JavaScript Document

var dropDownOpen = false; 

function openLink(url) {
	window.open(url, '', 'scrollbars=auto, toolbar=yes, location=yes, status=yes, menubar=yes, directories=yes, resizable=1');
}

function showTextbox(fieldName) {
	if(document.getElementById(fieldName).style.visibility == "visible") {
		document.getElementById(fieldName).style.display = "none";
		document.getElementById(fieldName).style.visibility = "hidden";
	}
	else {
		document.getElementById(fieldName).style.display = "block";
		document.getElementById(fieldName).style.visibility = "visible";
	}
}

function openSearchResults() {
	var checkin = document.getElementById("checkin").value;
	var searchword = document.getElementById("searchword").value;
	var adults = document.getElementById("adults").value;
	var children = document.getElementById("children").value;
	var rooms = document.getElementById("rooms").value;
	var bookingcode = "";
	var iata = "";
	var pageconfig = document.getElementById("pageconfig").value;
	if(document.getElementById("bookingcode") != null) {
		bookingcode = document.getElementById("bookingcode").value;
		iata = document.getElementById("iata").value;
	}

	//window.open('http://www.stamford.com.au/page.asp?e_page=767104&pageconfig=' + pageconfig + '&checkin=' + checkin + '&searchword=' + searchword +'&adults=' + adults + '&children=' + children + '&rooms=' + rooms + '&bookingcode=' + bookingcode + '&iata=' + iata + '', '', 'scrollbars=yes, toolbar=yes, location=yes, status=yes, menubar=yes, directories=yes, resizable=1');
	document.location.href = 'http://www.stamford.com.au/page.asp?e_page=767104&pageconfig=' + pageconfig + '&checkin=' + checkin + '&searchword=' + searchword +'&adults=' + adults + '&children=' + children + '&rooms=' + rooms + '&bookingcode=' + bookingcode + '&iata=' + iata + '';
	return true;
}

function GetXmlHttpObject() {
	var xmlHttp=null;
	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e) {
		// Internet Explorer
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
} 

var searchReq = GetXmlHttpObject();

//Called from keyup on the search textbox.
//Starts the AJAX request.
function searchSuggest() {
	if (searchReq.readyState == 4 || searchReq.readyState == 0) {
		var str = escape(document.getElementById('searchword').value);
		searchReq.open("GET", 'http://www.stamford.com.au/clients/Stamford/stamfordsearch.asp?search=' + str, true);
		searchReq.onreadystatechange = handleSearchSuggest; 
		searchReq.send(null);
	}
	if(document.getElementById('searchword').value == "") {
		if(document.getElementById("currencyID") != null){
			document.getElementById("currencyID").style.visibility = "visible";
		}
		document.getElementById('search_suggest').style.backgroundColor = ""
		document.getElementById('search_suggest').style.zIndex = "10"
	}
}

function searchAll() {
	if (searchReq.readyState == 4 || searchReq.readyState == 0) {
		searchReq.open("GET", '/clients/Stamford/stamfordsearch.asp?searchall=true');
		searchReq.onreadystatechange = handleSearchSuggest; 
		searchReq.send(null);
	}
}

//Called when the AJAX response is returned.
function handleSearchSuggest() {
	if (searchReq.readyState == 4) {
		var ss = document.getElementById('search_suggest')
		ss.innerHTML = '';
		var str = searchReq.responseText.split(",");
		if(dropDownOpen == false) {
			for(i=0; i < str.length; i++) {
				//List all matching suggestions
				var suggest = '<div class="location_out" onclick="javascript:setSearch(this.innerHTML);" onmouseover="javascript: locationOver(this)" onmouseout="javascript:locationOut(this)">' + str[i] + '</div>';
				//Hide hotel finder drop downs
				if(document.getElementById("currencyID") != null){
					document.getElementById("currencyID").style.visibility = "hidden";
				}
				ss.style.backgroundColor = "#fff"
				ss.style.zIndex = "30"
				ss.innerHTML += suggest;
				dropDownOpen = true;
			}
		}
		else {
			document.getElementById('search_suggest').innerHTML = '';
			if(document.getElementById("currencyID") != null){
				document.getElementById("currencyID").style.visibility = "visible";
			}
			document.getElementById('search_suggest').style.backgroundColor = ""
			document.getElementById('search_suggest').style.zIndex = "10"
			dropDownOpen = false;
		}
	}

}

//Click function
function setSearch(value) {
	document.getElementById('searchword').value = value;
	document.getElementById('search_suggest').innerHTML = '';
	if(document.getElementById("currencyID") != null){
		document.getElementById("currencyID").style.visibility = "visible";
	}
	document.getElementById('search_suggest').style.backgroundColor = ""
	document.getElementById('search_suggest').style.zIndex = "10"
	dropDownOpen = false;
}

//Mouse over function
function locationOver(div_value) {
	div_value.className = 'location_over';
}
//Mouse out function
function locationOut(div_value) {
	div_value.className = 'location_out';
}

function setDateValue() {
	var f=document.Search;
	var now=new Date();
	var start
	
	start=new Date(f.year.options[f.year.selectedIndex].value,f.month.options[f.month.selectedIndex].value-1,f.day.options[f.day.selectedIndex].value);
	start.setHours(23);
	start.setMinutes(59);
	start.setSeconds(59);

	if (start.getMonth()!=f.month.options[f.month.selectedIndex].value-1){
		alert("Check In date is not a valid date");
		return false;
	}	
	if (start<now){
		alert("Check In date is early than today");
		return false;
	}

	var cidate =  document.getElementById("year").value + "-" + document.getElementById("month").value + "-" + document.getElementById("day").value
	document.getElementById("checkin").value = cidate;

	if(document.getElementById("dynField") != null) {
		if(document.getElementById("dynField").value == "Please select...") {
			document.getElementById("dynField").value = "";
		}
		document.getElementById("searchword").value = document.getElementById("dynField").value;

		var strSearchWord = document.getElementById("searchword").value.toLowerCase();

		if(strSearchWord.indexOf("auckland") == -1 && strSearchWord.indexOf("new zealand") == -1  && strSearchWord.indexOf("stamford plaza auckland") == -1 && strSearchWord.indexOf("nz") == -1) {
			//regular page config
			document.getElementById("pageconfig").value = "C82C6325-B321-4604-8A06-B938DE";
		}
		else {
			//Auckland page config (NZ dollar)
			document.getElementById("pageconfig").value = "346AAC29-13A2-4178-AAF0-330B9E";
		}
	}

	return openSearchResults();
}

function checkData(){
	var f=document.Search;
	var now=new Date();
	var start, finish
	
	start=new Date(f.Y1.options[f.Y1.selectedIndex].value,f.M1.options[f.M1.selectedIndex].value-1,f.D1.options[f.D1.selectedIndex].value);
	finish=new Date(f.Y2.options[f.Y2.selectedIndex].value,f.M2.options[f.M2.selectedIndex].value-1,f.D2.options[f.D2.selectedIndex].value);
	start.setHours(23);
	start.setMinutes(59);
	start.setSeconds(59);
	finish.setHours(23);
	finish.setMinutes(59);
	finish.setSeconds(59);
	
	if (start.getMonth()!=f.M1.options[f.M1.selectedIndex].value-1){
		alert("Check In date is not a valid date");
		return false;
	}	
	if (start<now){
		alert("Check In date is early than today");
		return false;
	}
	if (finish.getMonth()!=f.M2.options[f.M2.selectedIndex].value-1){
		alert("Check Out Date is not a valid Date");
		return false;
	}	
	if (finish<now){
		alert("Check Out Date is early than today");
		return false;
	}
	if (finish<start){
		alert("Check Out Date is early than the Check In Date");
		return false;
	}
	return true;
}


function clearVal(){
	var f=document.Search;
	f.HotelName.value="";
	f.City.value="";
	f.State.value="";
	f.RateLow.value="";
	f.RateHigh.value="";
	f.NearestAirport.value="";
	f.NearestCity.value="";
	f.NearestLandmark.value="";
}
function UpdateCheckout(){
	var f=document.Search;
	var start, finish
	var iCount, iextraday = 2
	start=new Date(f.Y1.options[f.Y1.selectedIndex].value,f.M1.options[f.M1.selectedIndex].value-1,f.D1.options[f.D1.selectedIndex].value);
	finish=new Date(f.Y1.options[f.Y1.selectedIndex].value,f.M1.options[f.M1.selectedIndex].value-1,(parseInt(f.D1.options[f.D1.selectedIndex].value)+iextraday));
	//alert(start)
		for(iCount = 0; iCount < f.D1.length; iCount++) { 
			if (f.D1.options(iCount).value == start.getDate()){
				f.D1.selectedIndex=iCount;
				}
			if (f.D1.options(iCount).value == finish.getDate()){
				f.D2.selectedIndex=iCount;
				break;
				}
				
		}
		for(iCount = 0; iCount < f.M1.length; iCount++) { 
			if (f.M1.options(iCount).value == (start.getMonth()+1)){
				f.M1.selectedIndex=iCount;
				}
			if (f.M1.options(iCount).value == (finish.getMonth()+1)){
				f.M2.selectedIndex=iCount;
				break;
				}
				
		}
		for(iCount = 0; iCount < f.Y1.length; iCount++) { 
			if (f.Y1.options(iCount).value == start.getYear()){
				f.Y1.selectedIndex=iCount;
				}
			if (f.Y1.options(iCount).value == finish.getYear()){
				f.Y2.selectedIndex=iCount;
				break;
				}
				
		}
	}

function show_calendar(type){
	var f=document.forms[0];
	
	var now=new Date();
	var month = now.getMonth()+1;
	var day = now.getDate();
	var year = now.getYear();
	var clientday;
	var bookingMonth;
	var bookingYear;
	
	//For Netscape, the year is 100 for 2000 	
	if (year<1900)
		year=2000+year-100;
	clientday=year+(month<10?'0'+month:month)+(day<10?'0'+day:day);
	if (type=="in"){
		bookingMonth=f.M1.options[f.M1.selectedIndex].value;
		bookingYear=f.Y1.options[f.Y1.selectedIndex].value;
	}
	else
	{
		bookingMonth=f.M2.options[f.M2.selectedIndex].value;
		bookingYear=f.Y2.options[f.Y2.selectedIndex].value;
	}	
	
	window.open("calendar.asp?type="+type + "&month="+bookingMonth+"&year="+bookingYear+"&clientday="+clientday, "Calendar", 
		"width=300,height=210,status=no,resizable=no,top=200,left="+(screen.width-350));
}