//Global Variables
	var today = new Date();
	var pageLoaded = false;
	var iFrameLoaded = false;
        
        var currentYear = new Date();
        var nextYear = new Date();
        currentYear = currentYear.getFullYear();
        nextYear = currentYear+1;
	
	function iFrameLoad()
	{
		iFrameLoaded = true;
		doOnload();
	}
	
	function pageLoad()
	{
		pageLoaded = true;
		doOnload();
	}
	
	function doOnload()
	{
		if (iFrameLoaded && pageLoaded)
		{
			sizeIFrame();
			hotelSearch(window.location.search);
		}
	}
	
	
	
	function setTodaysDate() 
	{
		
			queryString = window.location.search;
			if (queryString)
			{
				var URLparams = getURLParams(queryString);
				
				// Set date / nights
				var dateParts = URLparams.checkin.split('/');
			
				if (dateParts.length > 0)
				{
					var todayDay = document.getElementById("check_in_day");
					todayDay.options[dateParts[0]-1].selected = true;	
	
					var todayMonth = document.getElementById("check_in_month");
					todayMonth.options[dateParts[1]-1].selected = true;
					
					var todayYear = document.getElementById("check_in_year");
					todayYear.options[dateParts[2] - currentYear].selected = true;
				}
				
				var nights =  document.getElementById("nights");
				nights.options[URLparams.noOfNights - 1].selected = true;
				
				// Set hotel(s)
				var hotelList = document.getElementById("listhotels");
				for (i = 0; i < hotelList.options.length; i++)
				{
					if (hotelList.options[i].value == URLparams.hotelList)
					{
						hotelList.options[i].selected = true;
						break;
					}
				}
					
				// Set state(s)
				var stateList = document.getElementById("state");
				for (i = 0; i < stateList.options.length; i++)
				{
					if (stateList.options[i].value == URLparams.state)
					{
						stateList.options[i].selected = true;
						break;
					}
				}
				
				// Set no. of rooms
				var noRoomsList = document.getElementById("numrooms");
				for (i = 0; i < noRoomsList.options.length; i++)
				{
					if (noRoomsList.options[i].value == URLparams.noOfRooms)
					{
						noRoomsList.options[i].selected = true;
						break;
					}
				}			
			}
			else
			{
			
				//Set Day
				var todayDay = document.getElementById("check_in_day");
				todayDay.options[today.getDate()-1].selected = true;		
				//Set Month
				var todayMonth = document.getElementById("check_in_month");
				todayMonth.options[today.getMonth()].selected = true;
				//Set Year
				var todayYear = document.getElementById("check_in_year");
				
				if (document.getElementById("check_in_year").value == String(currentYear)) {
					todayYear.options[0].selected = true;
				} else if (document.getElementById("check_in_year").value == String(nextYear)) {
					todayYear.options[1].selected = true;		
				} else {
					alert('Error with Date Validation - Please email support@pebbledesign.com.au');
					//break;
				}
			}
		validateDate();
		
		dateCalculator();
	}

	function filterhotels() {
		//Filter Hotels By Selected ID
		//this.options[this.selectedIndex].value
		var state = document.getElementById("state");
		var hotel = document.getElementById("listhotels");
		
		var stateSelected = state.options[state.selectedIndex].value;
	
		while (hotel.firstChild) {
			hotel.removeChild(hotel.firstChild);
		}
		switch(stateSelected) {
		case "":
			//hotel.options.length = 0;
			hotel.options[0] = new Option('all properties','333810,334338,334127');
			hotel.options[1] = new Option('grand mercure rockford palm cove','333810');
			hotel.options[2] = new Option('rockford adelaide','334338');
			hotel.options[3] = new Option('novotel rockford darling harbour','334127');
		 	break;
		case "QLD":
			hotel.options[0] = new Option('all properties','333810');		
			hotel.options[1] = new Option('grand mercure rockford palm cove','333810');	
		 	break;
		case "NSW":
			hotel.options[0] = new Option('all properties','334127');		
			hotel.options[1] = new Option('novotel rockford darling harbour','334127');		
		 	break;
		case "SA":
			hotel.options[0] = new Option('all properties','334338');		
			hotel.options[1] = new Option('rockford adelaide','334338');		
		 	break;		  
		default:
			//hotel.options.length = 0;
			hotel.options[0] = new Option('all properties','333810,334338,334127');
			hotel.options[1] = new Option('grand mercure rockford palm cove','333810');
			hotel.options[2] = new Option('rockford adelaide','334338');
			hotel.options[3] = new Option('novotel rockford darling harbour','334127');
		 	break;
		}

	}
	
	
	function getURLParams(queryString)
	{
		var p = unescape(queryString);
		var params = new Array();
		
		//If values have been passed from another page, strip those values and place insert them into the API
		if  (p != "") {
			//alert('Checking');
			var qsParm = new Array();
			var query = p.substring(1);
			var parms = query.split(';');
			
			for (var i=0; i<parms.length; i++) {
				var pos = parms[i].indexOf(':');
				
				if (pos > 0) {
					var key = parms[i].substring(0,pos);
					var val = parms[i].substring(pos+1);
					qsParm[key] = val;
				}
			}
			
			params.state = qsParm["s"];
			params.hotelList = qsParm["h"];
			params.checkin = qsParm["ci"];
			params.checkout = qsParm["co"];
			params.noOfRooms = qsParm["n"];
			params.noOfNights = qsParm["nn"];
		}
		return params;
		
	}
	
	function hotelSearch(queryString) 
	{			
		if (!YAHOO.util.CrossFrame)
			return;
			
		var state;
		var hotels;
		var checkin;
		var checkout;
		var numrooms;
		//if page has been posted 
		//location.search = "";
		
		//var p = unescape(x);
		
		//If values have been passed from another page, strip those values and place insert them into the API
		if  (queryString != "") {
			var params = getURLParams(queryString)
			state = params.state;
			hotels = params.hotelList;
			checkin = params.checkin;
			checkout = params.checkout;
			numrooms = params.noOfRooms;
		} 
		else 
		{
			if (validateDate())
			{
				//If the Search has called from Book.html insert local values into the API
				//alert('using on page variables');			
				state = document.getElementById('state').value;
				hotels = document.getElementById('listhotels').value;
				checkin = document.getElementById('checkin').value;
				checkout = document.getElementById('checkout').value;
				numrooms = document.getElementById('numrooms').value; 			
			}
		}
		
		appCall = "getFlexApp('hotelSearch2b').hotelSearch('" + state + "', '" + hotels + "', '" + checkin + "', '" + checkout + "', '" + numrooms + "')";
		//alert(appCall);
		YAHOO.util.CrossFrame.send("http://1300hotels.com/hotelsearch/rockford/proxy.html",
		"frames['searchframe']",
		appCall);
		
	}
	
	function postBooking() {
		dateCalculator();
	
		if (validateDate()) { 
			var state = document.getElementById('state').value;
			var hotels = document.getElementById('listhotels').value;
			var checkin = document.getElementById('checkin').value;
			var checkout = document.getElementById('checkout').value;
			var numrooms = document.getElementById('numrooms').value; 
			var numberNights = document.getElementById('nights').value; 
			//POST values to booking page
/** LOCAL ACCESS **/
			var url = "http://www.rockfordhotels.com.au/book.html?s:" + state + ";h:" +hotels+ ";ci:" +checkin+ ";co:" +checkout+ ";n:" + numrooms  + ";nn:"+numberNights;
/********************/
/** REMOTE ACCESS **
			var url = "/book.html?s+" + state + "&h+" +hotels+ "&ci+" +checkin+ "&co+" +checkout+ "&n+" + numrooms;			
/** REMOTE ACCESS **/			
			//location.href = url;
			document.location = url;
			
		}
		//else
		//	alert("Error validating booking data");
	}
	
	function validateDate() 
	{
		var date = new Date();
		var day = parseInt(document.getElementById("check_in_day").value);
		var dateValid = true;
		date.setFullYear(document.getElementById("check_in_year").value, document.getElementById("check_in_month").value, day );	
		//If date is set in the past make date todays
		if (date < today) {
			dateValid = false;	 
			document.getElementById("error").style.display="block";
			setTimeout("document.getElementById('error').style.display='none'",5000);
		}
		
		//If date does not exist put to last valid date.
		if (date.getDate() === day) {
		} else {
			document.getElementById("error").style.display="block";
			setTimeout("document.getElementById('error').style.display='none'",5000);
			dateValid = false;	
		}
		// if date failed validation set default to todays date
		if (!dateValid) {
			setTodaysDate();
		}
	
		return dateValid;
	}
	
	function dateCalculator() {
		//Declaring Global Variables
		var checkinDate = new Date();
		var checkoutDate = new Date();
		var numberNights = parseInt(document.getElementById("nights").value);
		var checkinZeroD = "";
		var checkoutZeroD = "";
		var checkinZeroM = "";
		var checkoutZeroM = "";		
		checkinDate.setFullYear(document.getElementById("check_in_year").value, document.getElementById("check_in_month").value, document.getElementById("check_in_day").value); 
		checkoutDate.setFullYear(document.getElementById("check_in_year").value, document.getElementById("check_in_month").value, document.getElementById("check_in_day").value); 
		//Add Number of nights to the check out date
		checkoutDate.setDate(checkoutDate.getDate()+ numberNights);
		//Add zero's to days if below zero
		if (checkinDate.getDate() < 10) {
			checkinZeroD = "0";
			}
		if (checkoutDate.getDate() < 10) {
			checkoutZeroD = "0";
			}
		if (checkinDate.getMonth() < 9) {
			checkinZeroM = "0";
			}
		if (checkoutDate.getMonth() < 9) {
			checkoutZeroM = "0";
			}	
		//Reformat and Display Checkout Date - Convert Month Format
		document.getElementById("checkin").value = checkinZeroD + checkinDate.getDate() + '/' + checkinZeroM + (checkinDate.getMonth()+1) + '/' + checkinDate.getFullYear() ;
		document.getElementById("checkout").value = checkoutZeroD + checkoutDate.getDate() + '/' + checkoutZeroM + (checkoutDate.getMonth()+1) + '/' + checkoutDate.getFullYear() ;
	}
		
function sizeIFrame()
	{
	var winW = 0, winH = 0;
	if( typeof( window.innerWidth ) == 'number' )
		{
		//Non-IE
		winW = window.innerWidth;
		winH = window.innerHeight;
		} 
	else if( document.documentElement && ( document.documentElement.clientWidth ||	document.documentElement.clientHeight ) )
		{
		//IE 6+ in 'standards compliant mode'
		winW = document.documentElement.clientWidth;
		winH = document.documentElement.clientHeight;
		} 
	else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) )
		{
		//IE 4 compatible
		winW = document.body.clientWidth;
		winH = document.body.clientHeight;
		}		
	document.getElementById('searchframe').height = 680;
	}
