window.onload = jsFnOnload;

function jsFnOnload()
{
	if (document.getElementById)
	{
		/* Associate logo's onclick event with home page */
		var logo = document.getElementById("logo");
 		if (logo != null) {
			logo.onclick = function() {
				window.location = 'index.php';
				return false;
			}
		}

		// Remove confirm
		var remove = document.getElementsByTagName("a");
 		if (remove != null) 
		{
			for(aremindex=0;aremindex<remove.length;aremindex++)
			{
				if(remove[aremindex].className=='rem')
				{
					remove[aremindex].onclick = JSFnRemoveClick;
				}
			}
		}
				
		var menu = document.getElementById("menu");
 		if (menu != null) 
		{
			var lis = menu.getElementsByTagName("li");
			for(aliindex=0;aliindex<lis.length;aliindex++)
			{
				lis[aliindex].onmouseover = JSFnMenuLiOver;
				lis[aliindex].onmouseout = JSFnMenuLiOut;
			}
		}		

		// Do Booking Form Verification
		var aBookingForm = document.getElementById('bookingformproper');
		if (aBookingForm != null) aBookingForm.onsubmit = BookingSpecificChecks;
		
		// Do Customer Information Form Verification
		var aBookingForm3 = document.getElementById('bookingformproper3');
		if (aBookingForm3 != null) aBookingForm3.onsubmit = Booking3SpecificChecks;
	
		var aContactForm = document.getElementById('contactform');
		if (aContactForm != null) aContactForm.onsubmit = JSFnValidateContactForm;
		
		var aImageZoom = document.getElementById('largeimage');
		if (aImageZoom != null) aImageZoom.onclick = JSFnImageZoom;
	}
	
}

function JSFnRemoveClick()
{
	var removed = confirm("Are you sure you want to remove this item?");

	return removed;
}

function JSFnMenuLiOver()
{
	this.className = 'hover';
}

function JSFnMenuLiOut()
{
	this.className = '';
}

function JSFnMenuLiClick()
{
	window.location = this.childNodes[0].href;
	//alert(this.childNodes[0]);
}

function JSFnGoBack()
{
	history.go(-1);
}

function JSFnImageZoom()
{
	var aWindow = window.open(this.href, 'productbig', 'menubar=no, resizeable=no, toolbar=no, width=600px, height=600px')
	aWindow.focus();
	return false;
}

var aContactRequiredFields = new Array ("name","Please enter your name to continue");

function JSFnValidateContactForm()
{
	var aEmail = document.getElementById('email');
	var aTelephone = document.getElementById('telephone');

	if ((aEmail != null) && (aTelephone != null))
	{
		if ((aEmail.value == '') && (aTelephone.value == ''))
		{
			alert('You must provide either your telephone number or email address to continue.');
			return false;
		}
	}

	return JSFnValidateForm(aContactRequiredFields);
}

function JSFnValidateForm(aRequiredFields)
{
	for (aIndex = 0; aIndex < aRequiredFields.length; aIndex = aIndex + 2)
	{
		currElement = document.getElementById(aRequiredFields[aIndex]);
		if (currElement != null)
		{
			if  (   (   (currElement.type == 'text')
				     && (currElement.value == ''))
				 || (   (currElement.type == 'password')
				     && (currElement.value == ''))
				 || (   (currElement.type == 'checkbox')
				     && (currElement.checked == false))
				 || (   (currElement.type == 'file')
				     && (currElement.value == ''))
				 || (   (currElement.type == 'textarea')
				     && (currElement.value == ''))
				 || (   (currElement.type == 'select-one')
				     && (currElement.value == '')))
			{
				alert(aRequiredFields[aIndex + 1]);
				return false;
			}
			else if (currElement.type == 'radio')
			{
				aIndex = aIndex + 2;
				if (!currElement.checked)
				{
					currElement = document.getElementById(aRequiredFields[aIndex]);
					if ((currElement.type == 'radio') && (!currElement.checked))
					{
						alert(aRequiredFields[aIndex + 1]);
						return false;
					}
				}
			}
		}
	}
	return true;
}

function BookingSpecificChecks()
{
	Result = true;
	
	// Check dates are valid
	StartDate = document.getElementById('StayStartDay').value + '/';
	StartDate += document.getElementById('StayStartMonth').value + '/';
	StartDate += document.getElementById('StayStartYear').value;
	
	EndDate = document.getElementById('StayEndDay').value + '/';
	EndDate += document.getElementById('StayEndMonth').value + '/';
	EndDate += document.getElementById('StayEndYear').value;
	
	aStartDate = new Date();
	aEndDate = new Date();
	aTodaysDate = new Date();
	
	aStartDate.setFullYear(document.getElementById('StayStartYear').value, document.getElementById('StayStartMonth').value - 1, document.getElementById('StayStartDay').value);
	aEndDate.setFullYear(document.getElementById('StayEndYear').value, document.getElementById('StayEndMonth').value - 1, document.getElementById('StayEndDay').value);

	if ((!isDate(StartDate)) || (!isDate(EndDate)))
	{
		Result = false;
	}
	else
	{		
		if (aStartDate >= aEndDate)
		{
			Result = false;
		}
	}
	
	if (Result == false) alert('The start and end dates for the booking must be valid dates and the end date must be after the start date - please try again');
	else		
	{	
		/*if (aStartDate <= aTodaysDate)
		{
			alert('Unfortunately, you cannot book online less than 24 hours in advance. Please telephone or email us to arrange your booking.');
			Result = false;
		}*/
		
		if (aStartDate < aTodaysDate)
		{
			alert('Unfortunately, you cannot book for dates in the past. Please telephone or email us to arrange your booking.');
			Result = false;
		}
		
		// Add A Year On
		aTodaysDate.setFullYear(aTodaysDate.getFullYear() + 1);
		if (   (aStartDate > aTodaysDate) || (aEndDate > aTodaysDate))
		{
			alert('Unfortunately, you cannot book online for more than a year in advance. Please telephone or email us to arrange your booking.');
			Result = false;
		}
		// Have they tried to book both a specific room
		else if ((document.getElementById('RoomID').value == ''))
		{
			alert('Unfortunately, you must indicate which apartment you are interested in booking.');
			Result = false;	
		}
		// Have they forgotten to tick the terms checkbox
		else if (!document.getElementById('ReadTerms').checked)
		{
			alert('Unfortunately, you must accept our terms and conditions to continue.');
			Result = false;	
		}
	}

	return Result;
}

function Booking3SpecificChecks()
{
	Result = true;
	
	if (document.getElementById('FirstName').value == '')
	{
		alert('Unfortunately, you must enter your first name to continue.');
		Result = false;	
	}
	else if (document.getElementById('Surname').value == '')
	{
		alert('Unfortunately, you must enter your surname to continue.');
		Result = false;	
	}
	else if (document.getElementById('CustomerEmail').value == '')
	{
		alert('Unfortunately, you must enter your email address to continue.');
		Result = false;	
	}
	else if (document.getElementById('BillingAddress').value == '')
	{
		alert('Unfortunately, you must enter your address to continue.');
		Result = false;	
	}
	else if (document.getElementById('BillingPostCode').value == '')
	{
		alert('Unfortunately, you must enter your postcode to continue.');
		Result = false;	
	}
	else if (document.getElementById('ContactNumber').value == '')
	{
		alert('Unfortunately, you must enter a contact telephone number to continue.');
		Result = false;	
	}
	
	return Result;
}

function isDate(sDate) 
{
	// Expects dd/mm/yyyy
	var re = /^\d{1,2}\/\d{1,2}\/\d{4}$/
	if (re.test(sDate)) 
	{
		var dArr = sDate.split("/");
		var d = new Date(dArr[2], dArr[1] - 1, dArr[0]);
		return d.getMonth() + 1 == dArr[1] && d.getDate() == dArr[0] && d.getFullYear() == dArr[2];
	}
	else 
	{
		return false;
	}
}