/*
 * Javascript by Troy Lutton 21/09/2007
 */
// test to make sure the date is valid
function isWValidDate(theDate)
{
	// build the regex and run it
	var dateRegExp = /\b(0?[1-9]|[12][0-9]|3[01])[- \/.](0?[1-9]|1[012])[- \/.](19|20)?[0-9]{2}\b/;
	return (theDate.match(dateRegExp));
}

// a function to check if an email is valid
function isValidEmail(email) {

	// is there an email?
	if (email != "") {

		// build the regex filter
		var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

		// test the email
		return filter.test(email);

	} else {
		return false;
	}

}// check_valid_email ()


// function to check the quick select booking form
function checkBookingSearchForm(form)
{
	try
	{
		// remove any old bg colors b4 proceeding
		clearBackgroundStyle(form);

		with (form)
		{
			if (arrivalDate.value == "")
			{
				formFieldFocus(arrivalDate.id);
				window.alert("Please enter an arrival date");
				return false;
			}
			if (departureDate.value == "")
			{
				formFieldFocus(departureDate.id);
				window.alert("Please enter a departure date");
				return false;
			}
			if (!isWValidDate(arrivalDate.value) || !isWValidDate(departureDate.value))
			{
				window.alert("Please ensure that you have entered valid dates ( dd/mm/yyyy )");
				return false;
			}

			if (arrivalDate.value == departureDate.value)
			{
				formFieldFocus(departureDate.id);
				window.alert("You cannot arrive and depart on the same day.  Please check your reservation dates.");
				return false;
			}

			arrivalArr = arrivalDate.value.split('/');
			newArrival = new Date(arrivalArr[2], arrivalArr[1]-1, arrivalArr[0]);
			departureArr = departureDate.value.split('/');
			newDeparture = new Date(departureArr[2], departureArr[1]-1, departureArr[0]);

			if (newArrival > newDeparture)
			{
				window.alert("Please ensure that your arrival date is before your departure date.");
				return false;
			}

			if (property.value == "" || property.value == 0)
			{
				formFieldFocus(property.id);
				window.alert("Please select a property from the list");
				return false;
			}
		}
		return true;
	}
	catch (e)
	{
		window.alert(e.message);
		return false;
	}
}

function checkLocationForm(form)
{
	try
	{
		// remove any old bg colors b4 proceeding
		clearBackgroundStyle(form);

		with (form)
		{
			if (locationProperty.value == "" || locationProperty.value == 0)
			{
				//alert(property.id);
				formFieldFocus(locationProperty.id);
				window.alert("Please select a property from the list");
				return false;
			}
		}
		return true;
	}
	catch (e)
	{
		window.alert(e.message);
		return false;
	}
}

// add some extra functionality to the focus function
function formFieldFocus(id)
{
	document.getElementById(id).style.backgroundColor="#FFE2E2";
	document.getElementById(id).focus();
}

function formFieldBlur(id)
{
	document.getElementById(id).style.backgroundColor="#FFF";
}

// clear out the old bg colors in a given form
function clearBackgroundStyle(form)
{
	for (i = 0;i < form.length; i++)
	{
		form.elements[i].style.backgroundColor="";
	}
}

// Function to handle the selection of a date in a calendar
// responds when a person selects a date
function handleDateCalSelect(type,args,obj)
{
	var textInputID = this.args.textInputID;
	var theCal = this.args.theCal;

	// get the date selected and put it in the text field
	var dates = args[0];
	var date = dates[0];
	var txtDate1 = document.getElementById(textInputID).value = date[2] + "/" + date[1] + "/" + date[0];
	theCal.hide();
}

// creat the calendar namespace to sue
YAHOO.namespace("widget.calendar");

var updateDepartureDate = function(type,args,obj) {

	arrival = document.getElementById('arrival').value;
	departure = document.getElementById('departure').value;

	// blank arrival date, display same month as departing
	if (departure == '') {
		arrivalArr = arrival.split('/');
		obj.cfg.setProperty('pagedate', arrivalArr[1]+'/'+arrivalArr[2]);
		obj.render();

	} /*else {

		// if arrival is before departure update to the same month
		arrivalArr = arrival.split('/');
		arrivalD = new Date(arrivalArr[2], arrivalArr[1]-1, arrivalArr[0]);
		departureD = new Date(departureArr[2], arrivalArr[1]-1, arrivalArr[0]);
		if (departureD > arrivalD) {
		}
	}*/
};

// init the various calendars
YAHOO.widget.calendar.init = function()
{
	// create the departure calendar
	YAHOO.widget.calendar.departureDate = new YAHOO.widget.Calendar("departureDate","departureContainer",{ title:"Choose a date:", close:true, iframe:true, mindate:new Date() } );
	var calInfo = {"args":{"textInputID":"departure", "theCal":YAHOO.widget.calendar.departureDate}};
	YAHOO.widget.calendar.departureDate.selectEvent.subscribe(handleDateCalSelect, calInfo, true);
	YAHOO.widget.calendar.departureDate.render();

	// Listener to show the Calendar when the button is clicked
	YAHOO.util.Event.addListener("showDepartureDate", "click", YAHOO.widget.calendar.departureDate.show, YAHOO.widget.calendar.departureDate, true);
	YAHOO.util.Event.addListener("departure", "click", YAHOO.widget.calendar.departureDate.show, YAHOO.widget.calendar.departureDate, true);

	// create the arrive calendar
	YAHOO.widget.calendar.arrivalDate = new YAHOO.widget.Calendar("arrivalDate","arrivalContainer",{ title:"Choose a date:", close:true, iframe:true, mindate:new Date()} );
	var calInfo = {"args":{"textInputID":"arrival", "theCal":YAHOO.widget.calendar.arrivalDate}};
	YAHOO.widget.calendar.arrivalDate.selectEvent.subscribe(handleDateCalSelect, calInfo, true);
	YAHOO.widget.calendar.arrivalDate.selectEvent.subscribe(updateDepartureDate, YAHOO.widget.calendar.departureDate, true);
	YAHOO.widget.calendar.arrivalDate.render();

	// Listener to show the Calendar when the button is clicked
	YAHOO.util.Event.addListener("showArrivalDate", "click", YAHOO.widget.calendar.arrivalDate.show, YAHOO.widget.calendar.arrivalDate, true);
	YAHOO.util.Event.addListener("arrival", "click", YAHOO.widget.calendar.arrivalDate.show, YAHOO.widget.calendar.arrivalDate, true);

}

// run the calendars
YAHOO.util.Event.onDOMReady(YAHOO.widget.calendar.init);


// set up some of the form fields from the enquiry form
function setEnquiryFormDetails(adults, children)
{
	document.forms['enquiryForm'].enquiryNumAdults.value = adults;
	document.forms['enquiryForm'].enquiryNumChildren.value = children;
}


// check the availability form has been filled correctly
function checkAvailabilityForm(form)
{
	// make sure a radio is selected
	if (radioValueSelected(form.roomValues))
	{
		// set up the booking room info
		return true; //setBookedRoomInfo(form);
	}
	else
	{
		window.alert("Please ensure that you have selected a room type and package");
		return false;
	}
}

function setBookedRoomInfo(form)
{
	try
	{
		// get the room type
		var roomValues = getSelectedRadioValue(form.roomValues);
		var roomType = roomValues.charAt(0);

		//alert(document.getElementById("ShortDesc[" + roomType + "]").innerHTML);

		// extract and assign the details about the room to the form
		document.getElementById("ShortDesc").value = document.getElementById("ShortDesc[" + roomType + "]").innerHTML;
		document.getElementById("LongDesc").value = document.getElementById("LongDesc[" + roomType + "]").innerHTML;
		document.getElementById("MaxOccupants").value = document.getElementById("MaxOccupants[" + roomType + "]").innerHTML;
		return true;
	}
	catch (e)
	{
		window.alert(e.message);
		return false;
	}
}

function getSelectedRadioValue(buttonGroup)
{
	// if the button group is an array (one button is not an array)
	for (var i = 0; i < buttonGroup.length; i++)
	{
		if (buttonGroup[i].checked)
		{
			return buttonGroup[i].value;
		}
	}
}

function radioValueSelected(buttonGroup)
{
	// if the button group is an array (one button is not an array)
	for (var i = 0; i < buttonGroup.length; i++)
	{
		if (buttonGroup[i].checked)
		{
			return true;
		}
	}

	// none selected
	return false;
}

function checkSignupDetails (form)
{
	try
	{
		with (form)
		{
			formFieldBlur(EmailName.id);
			formFieldBlur(EmailAddress.id);

			if (EmailName.value == "")
			{
				formFieldFocus(EmailName.id);
				window.alert("Please enter your name.");
				return false;
			}
			if (EmailAddress.value == "")
			{
				formFieldFocus(EmailAddress.id);
				window.alert("Please enter your email address.");
				return false;
			}
			if (!isValidEmail(EmailAddress.value))
			{
				formFieldFocus(EmailAddress.id);
				window.alert("The entered email address is not.");
				return false;
			}
		}
		return true;
	}
	catch (e)
	{
		window.alert(e.message);
		return false;
	}
}


function checkpaymentForm(form)
{
	try
	{
		// remove any old bg colors b4 proceeding
		clearBackgroundStyle(form);

		with (form)
		{
			if (Given.value == "")
			{
				formFieldFocus(Given.id);
				window.alert("Please enter your given name");
				return false;
			}
			if (Surname.value == "")
			{
				formFieldFocus(Surname.id);
				window.alert("Please enter your surname");
				return false;
			}
			if (Phone.value == "")
			{
				formFieldFocus(Phone.id);
				window.alert("Please enter your phone number");
				return false;
			}
			if (Email.value == "")
			{
				formFieldFocus(Email.id);
				window.alert("Please enter email address");
				return false;
			}
			if (!isValidEmail(Email.value))
			{
				formFieldFocus(Email.id);
				window.alert("The entered email address is not.");
				return false;
			}

			// credit card stuff
			if (CardHolder.value == "")
			{
				formFieldFocus(CardHolder.id);
				window.alert("Please enter credit card name");
				return false;
			}
			if (CardNumber.value == "")
			{
				formFieldFocus(CardNumber.id);
				window.alert("Please enter credit card number");
				return false;
			}
			if (!cardNumberIsValid(CardNumber.value))
			{
				formFieldFocus(CardNumber.id);
				window.alert("Incorrect Credit Card Number entered, credit card must have 16 numbers");
				return false;
			}
			if (CardType.value == "")
			{
				formFieldFocus(CardType.id);
				window.alert("Please select the credit card type from the list");
				return false;
			}
			if (CardExpiryMonth.value == "" || CardExpiryYear.value == "")
			{
				formFieldFocus(CardExpiryMonth.id);
				window.alert("Please select the credit card expiry month and year");
				return false;
			}
			if (CardCCV.value == "")
			{
				formFieldFocus(CardCCV.id);
				window.alert("Please enter credit card credit card verification number");
				return false;
			}
		}

		// confirmation message
		var msg = "CONFIRMATION \n\n";
		msg += "Please confirm that you wish to pay for the following \n\n";
		msg += "Arrive: " + document.getElementById('purchaseArrive').value + " \t Depart: " + document.getElementById('purchaseDepart').value + " \n\n";
		msg += "Total Purchase Price: $" + document.getElementById('purchasePrice').value + " \n\n";
		msg += "This money will be deducted from your credit card if you proceed";

		return window.confirm(msg);
	}
	catch (e)
	{
		window.alert(e.message);
		return false;
	}
}


function cardNumberIsValid(cardNumber)
{
	try
	{
		// test for a 16 digit credit card number
		var myregexp = /[0-9]{16}/;
		var tempNumber = cardNumber.replace(/ /g, '');
		return tempNumber.match(myregexp);
	}
	catch (e)
	{
		window.alert(e.message);
	}
}


// creat the calendar namespace to sue
YAHOO.namespace("YAHOO.widget.enquiry.calendar");

// init the various calendars
YAHOO.widget.enquiry.calendar.initBookingFormCals = function()
{
	// create the arrive calendar
	YAHOO.widget.enquiry.calendar.enquiryArrivalDate = new YAHOO.widget.Calendar("enquiryArrivalDate","enquiryArrivalContainer",{ title:"Choose a date:", close:true, iframe:true } );
	var calInfo = {"args":{"textInputID":"enquiryArrival", "theCal":YAHOO.widget.enquiry.calendar.enquiryArrivalDate}};
	YAHOO.widget.enquiry.calendar.enquiryArrivalDate.selectEvent.subscribe(handleDateCalSelect, calInfo, true);
	YAHOO.widget.enquiry.calendar.enquiryArrivalDate.render();

	// Listener to show the Calendar when the button is clicked
	YAHOO.util.Event.addListener("showEnquiryArrivalDate", "click", YAHOO.widget.enquiry.calendar.enquiryArrivalDate.show,YAHOO.widget.enquiry.calendar.enquiryArrivalDate, true);
	YAHOO.util.Event.addListener("enquiryArrival", "click", YAHOO.widget.enquiry.calendar.enquiryArrivalDate.show, YAHOO.widget.enquiry.calendar.enquiryArrivalDate, true);

	// create the arrive calendar
	YAHOO.widget.enquiry.calendar.enquirydepartureDate = new YAHOO.widget.Calendar("enquirydepartureDate","enquirydepartureContainer",{ title:"Choose a date:", close:true, iframe:true } );
	var calInfo = {"args":{"textInputID":"enquirydeparture", "theCal":YAHOO.widget.enquiry.calendar.enquirydepartureDate}};
	YAHOO.widget.enquiry.calendar.enquirydepartureDate.selectEvent.subscribe(handleDateCalSelect, calInfo, true);
	YAHOO.widget.enquiry.calendar.enquirydepartureDate.render();

	// Listener to show the Calendar when the button is clicked
	YAHOO.util.Event.addListener("showEnquirydepartureDate", "click", YAHOO.widget.enquiry.calendar.enquirydepartureDate.show,YAHOO.widget.enquiry.calendar.enquirydepartureDate, true);
	YAHOO.util.Event.addListener("enquirydeparture", "click", YAHOO.widget.enquiry.calendar.enquirydepartureDate.show, YAHOO.widget.enquiry.calendar.enquirydepartureDate, true);
}

// run the calendars
YAHOO.util.Event.onDOMReady(YAHOO.widget.enquiry.calendar.initBookingFormCals);


// check the form fields in the enquiry form before submission
function checkBookingEnquiryForm(form)
{
	with (form)
	{
		try
		{
			// remove any old bg colors b4 proceeding
			clearBackgroundStyle(form);

			if (document.getElementById('firstname').value == "")
			{
				formFieldFocus('firstname');
				window.alert("Please enter your given name");
				return false;
			}
			if (document.getElementById('surname').value == "")
			{
				formFieldFocus('surname');
				window.alert("Please enter your surname");
				return false;
			}
			if (document.getElementById('phone').value == "")
			{
				formFieldFocus('phone');
				window.alert("Please enter your phone number");
				return false;
			}
			if (document.getElementById('email').value == "")
			{
				formFieldFocus('email');
				window.alert("Please enter your email address");
				return false;
			}
			if (!isValidEmail(document.getElementById('email').value))
			{
				formFieldFocus('email');
				window.alert("Please enter a valid email address");
				return false;
			}

			if (enquiryArrival.value == "")
			{
				formFieldFocus(enquiryArrival.id);
				window.alert("Please enter your arrival date");
				return false;
			}
			if (enquirydeparture.value == "")
			{
				formFieldFocus(enquirydeparture.id);
				window.alert("Please enter departure date");
				return false;
			}
			if (enquiryNumAdults.value == "")
			{
				formFieldFocus(enquiryNumAdults.id);
				window.alert("Please the number of adults");
				return false;
			}
			if (enquiryNumChildren.value == "")
			{
				formFieldFocus(enquiryNumChildren.id);
				window.alert("Please enter the number of children");
				return false;
			}
			if (Smoking.value == "")
			{
				formFieldFocus(Smoking.id);
				window.alert("Please enter whether you wish to have a smoking or non smoking room");
				return false;
			}

			return true;
		}
		catch (e)
		{
			window.alert(e.message);
			return false;
		}

	}
}



// function to check the quick select booking form
function checkBookingDatesForm(form)
{
	try
	{
		// remove any old bg colors b4 proceeding
		clearBackgroundStyle(form);

		with (form)
		{
			if (arrivalDate.value == "")
			{
				formFieldFocus(arrivalDate.id);
				window.alert("Please enter an arrival date");
				return false;
			}
			if (departureDate.value == "")
			{
				formFieldFocus(departureDate.id);
				window.alert("Please enter a departure date");
				return false;
			}
			if (!isWValidDate(arrivalDate.value) || !isWValidDate(departureDate.value))
			{
				window.alert("Please ensure that you have entered valid dates ( dd/mm/yyyy )");
				return false;
			}

			arrivalArr = arrivalDate.value.split('/');
			newArrival = new Date(arrivalArr[2], arrivalArr[1]-1, arrivalArr[0]);
			departureArr = departureDate.value.split('/');
			newDeparture = new Date(departureArr[2], departureArr[1]-1, departureArr[0]);

			if (newArrival > newDeparture)
			{
				window.alert("Please ensure that your arrival date is before your departure date.");
				return false;
			}
		}
		return true;
	}
	catch (e)
	{
		window.alert(e.message);
		return false;
	}
}