 /*
	JS for Chester Zoo welcome page.
*/

function onload_function() {
	changeInfants();
}

function mapscriptfunc_multi() {
	// The page calls this function but it doesn't exist.
}

function checkInfantCotRemoval( inSelect ) {
	if( inSelect.disabled == false && inSelect.value == 'N' ) {
		alert('Are you sure that you do not want a cot? Infants under 2 years old are normally accomodated in a cot. If you remove the cot from your booking please either take your own travel cot or select a larger room type with sufficient beds for the whole family, including your infant.');
	}
}

function checkExtraRooms( inCheckbox )  {
	document.getElementById('extraRooms2').style.display = (inCheckbox.checked) ? '' : 'none';
	document.getElementById('extraRooms3').style.display = (inCheckbox.checked) ? '' : 'none';
}

function checkForm ( frm ) {
	// Blank all vars
	var msg = '';
	var roomAdults = 0;
	var roomChildren = 0;
	var roomCots = 0;

	// Check basics
	msg += (((frm.ParkDate.value=='')||(frm.ParkDate.value=='select')) ? 'Please select your park entry date\n' : '' );
	msg += (((frm.ArrivalDate.value=='')||(frm.ArrivalDate.value=='select')) ? 'Please select the date you will arrive at the hotel\n' : '' );
	msg += ((frm.Nights.value=='0') ? 'You must select the number of nights you will be staying at the hotel\n' : '' );

	// Shuffle rooms user has selected to the topmost select boxes in case
	// they pick them in a weird order
	if( frm.Room1.value=='' && (frm.Room2.value!='' || frm.Room3.value!='') ) {
		if( frm.Room2.value=='' ) {
			frm.Room1.value=frm.Room3.value;
		} else {
			frm.Room1.value=frm.Room2.value;
			frm.Room2.value=frm.Room3.value;
		}
		frm.Room3.value='';
	}
	
	// Tally up room occupants
	for(var i=1; i<=3; i++) {
		eval( 'if( frm.Room' + i + '.value!=\'\' ) { roomAdults += parseInt(frm.Room' + i + '.value.substring(1,2)); roomChildren += parseInt(frm.Room' + i + '.value.substring(2,3)); roomCots += (((frm.inf_room_' + i + '.value==\'Y\')&&(frm.inf_room_' + i + '.disabled==false)) ? 1 : 0);	}' );
	}
	
	// Check against number of tickets
	if( (frm.parkAdults.value!=roomAdults) || ((roomChildren - (parseInt(frm.parkChildren.value) + parseInt((frm.parkInfants2.value - roomCots))))!=0) ) {
		msg+="Please check the number of hotel rooms you have selected and their occupants match the number of tickets you have requested for the park.\n";
		msg+="For the park, you requested " + frm.parkAdults.value + " adult ticket(s), " + frm.parkChildren.value + " child ticket(s), and " + frm.parkInfants2.value + " infant(s).\n";
		msg+="For the hotel, you requested rooms totaling " + roomAdults + " adult bed(s), " + roomChildren + " bed(s) for children or infants, and " + roomCots + " cot(s) for infants.\n";
	}

	// Throw error or send through
	if( msg != '' ) {
		alert( 'Please check the following before submitting: \n\n' + msg);
		return false;
	} else {
		return true;
	}
}

function changeInfants() {
	for(var i=1; i<=3; i++) {
		eval('document.searchForm.inf_room_'+i).disabled = ( (document.searchForm.parkInfants2.value>=i) ? false : true);
		eval('document.searchForm.inf_room_'+i).value = ( (document.searchForm.parkInfants2.value>=i) ? "Y" : "N");
	}
}

Date.prototype.getFormattedDate=function() {
	return this.getDayText() + " " + this.getDate() + " " + this.getMonthText() + " " + this.getFullYear();
}



function hotelArrivalDate( parkArrivalDate )
{
	document.searchForm.ArrivalDate.length = 0;
	
	if(parkArrivalDate != "select" && parkArrivalDate != "")
	{
		var today = new Date();
		today.setDate(today.getDate()-1);
		
		// Turn the chauntry-date passed into here into a JS date object
		var oParkDate = new Date(parkArrivalDate.dateConvert('DDMMMYY', 'DD MMM 20YY'));
		//oParkDate = oParkDate.getYesterday();
		oParkDate.setDate(oParkDate.getDate()-3);
		
		for(var i=0;i<7;i++)
		{
			
			if(oParkDate > today)
			{
				var optionObj = new Option( oParkDate.getFormattedDate(), oParkDate.getChauntryDate() );
				var optionRank = document.searchForm.ArrivalDate.options.length;
				document.searchForm.ArrivalDate.options[optionRank] = optionObj;
				if (i == 3)
				{
					document.searchForm.ArrivalDate.selectedIndex=optionRank;
				}
				
			}
			oParkDate = oParkDate.getTomorrow();
		}
		
		
	}
	else
	{
		document.searchForm.ArrivalDate.length = 1;
		document.searchForm.ArrivalDate.options[0].text = "Choose Park Date";
	}
}