var aArrayOfPhotos = new Array();

// JavaScript Document
function CalculateRates()
{
	if (document.getElementById)
	{
		var aTotalPrice = 0;
		if (document.getElementById('calcroomtype').value == 'standard')
		{
			aTotalPrice += 	  document.getElementById('calcnbrofsglrooms').value 
							* document.getElementById('calcnbrofnights').value
							* document.getElementById('calcstdsglroomprice').value;
			aTotalPrice +=    document.getElementById('calcnbrofdblrooms').value
							* document.getElementById('calcnbrofnights').value
							* document.getElementById('calcstddblroomprice').value;
		}
		else
		{
			aTotalPrice += 	  document.getElementById('calcnbrofsglrooms').value 
							* document.getElementById('calcnbrofnights').value
							* document.getElementById('calcsupsglroomprice').value;
			aTotalPrice +=    document.getElementById('calcnbrofdblrooms').value
							* document.getElementById('calcnbrofnights').value
							* document.getElementById('calcsupdblroomprice').value;
		}
		document.getElementById('totalprice').innerHTML = aTotalPrice;
	}
}

window.onload = function() {
	/* Associate logo's onclick event with home page */
	var centralsign = document.getElementById('centralsign');
	if (centralsign != null)
	{
		centralsign.onclick = function() 
		{
			window.location='home.php';
			return false;
		}
	}
	
	/* Associate style links's onclick events with the relevant action */
	var functionsadvert = document.getElementById('functionsadvert');
	if (functionsadvert != null)
	{
		functionsadvert.onclick = function() 
		{
			window.location='facilities.php';
			return false;
		}
	}

	var castleprop = document.getElementById('castlemovie');
	if (castleprop != null)
	{
		castleprop.onclick = function() 
		{
			window.location='http://www.castleproperties.biz';
			return false;
		}
	}
	
	var bluelinemediaadvert = document.getElementById('bluelinemediaadvert');
	if (bluelinemediaadvert != null)
	{
		bluelinemediaadvert.onclick = function() 
		{
			window.location='http://www.bluelinemedia.co.uk';
			return false;
		}
	}

	var bookingform = document.getElementById('bookingform');
	if (bookingform != null)
	{
		bookingform.onsubmit = function() 
		{
			if (document.getElementById('name').value == '')
			{
			 	alert('Please enter the name under which you wish to place the booking');
				document.getElementById('name').focus();
				return false;
			}
			else if (   (document.getElementById('addressfield').value == '')
			   		 && (document.getElementById('phone').value == '')
			   		 && (document.getElementById('email').value == ''))
			{
			 	alert('Please complete at least one of the following items so that we may contact you to confirm the booking - email address, postal address or telephone number');
				return false;
			}
			else if (document.getElementById('dateofstay').value == '')
			{
			 	alert('Please enter the date of the first night you wish to stay');
				document.getElementById('dateofstay').focus();
				return false;
			}		 
		}
	}

	var mailinglistform = document.getElementById('mailinglistform');
	if (mailinglistform != null)
	{
		mailinglistform.onsubmit = function() 
		{
			if (document.getElementById('mlemailaddress').value == '')
			{
			 	alert('Please enter a valid email address');
				document.getElementById('mlemailaddress').focus();
				return false;
			}
			else if (document.getElementById('mlname').value == '')
			{
			 	alert('Please enter your name');
				return false;
			}
	 
		}
	}
	
	// Add extra serv aprts thumbnail images functionality
	var thumbnailslist = document.getElementById("thumbnailsserv");
	if (thumbnailslist != null)
	{	
		var picturelinks = thumbnailslist.getElementsByTagName("a");
		for (aIndex = 0; aIndex < picturelinks.length; aIndex++)
		{
			aHashPosition = picturelinks[aIndex].href.indexOf("#");
			aLengthOfLink = picturelinks[aIndex].href.length;
			aLinkTarget = picturelinks[aIndex].href.substring(aHashPosition + 1,aLengthOfLink);
			aArrayOfPhotos[aIndex] = aLinkTarget;
			
			aImageToHide = document.getElementById(aLinkTarget);
			if (aImageToHide != null) 
			{
				picturelinks[aIndex].onclick = JSFnServThumbnailClick;
				if (aIndex > 0) aImageToHide.style.display = 'none';
			}
		}
		
		// Hide any back to top links as they are no longer necessary
		var allLinks = document.getElementsByTagName("a")
		for (aIndex = 0; aIndex < allLinks.length; aIndex++)
		{
			if (allLinks[aIndex].href.indexOf("#header") != -1) allLinks[aIndex].style.display = 'none';
		}
	}

	// Add extra property thumbnail images functionality
	var thumbnailslist = document.getElementById("thumbnails");
	if (thumbnailslist != null)
	{	
		var picturelinks = thumbnailslist.getElementsByTagName("a");
		for (aIndex = 0; aIndex < picturelinks.length; aIndex++)
		{
			aHashPosition = picturelinks[aIndex].href.indexOf("#");
			aLengthOfLink = picturelinks[aIndex].href.length;
			aLinkTarget = picturelinks[aIndex].href.substring(aHashPosition + 1,aLengthOfLink);
			aArrayOfPhotos[aIndex] = aLinkTarget;
			
			aImageToHide = document.getElementById(aLinkTarget);
			aDescToHide = document.getElementById(aLinkTarget.replace("pic", "textforpic"));
			if (aImageToHide != null) 
			{
				picturelinks[aIndex].onclick = JSFnThumbnailClick;
				if (aIndex > 0) 
				{
					aImageToHide.style.display = 'none';
					aDescToHide.style.display = 'none';
				}
			}
		}
		
		// Hide any back to top links as they are no longer necessary
		var allLinks = document.getElementsByTagName("a")
		for (aIndex = 0; aIndex < allLinks.length; aIndex++)
		{
			if (allLinks[aIndex].href.indexOf("#header") != -1) allLinks[aIndex].style.display = 'none';
		}		
	}
	
	// Add extra property thumbnail images functionality
	var links = document.getElementsByTagName("a");
	for (aIndex = 0; aIndex < links.length; aIndex++)
	{
		if (links[aIndex].className == 'popup') 
		{
			links[aIndex].onclick = JSFnPopUpWindow;
		}
	}
	
	// Add extra property table functionality
	var propertytable = document.getElementById("propertytable");
	if (propertytable != null)
	{
		var links = propertytable.getElementsByTagName("a");
		for (aIndex = 0; aIndex < links.length; aIndex++)
		{
			if (links[aIndex].className == "button") links[aIndex].innerHTML = '<input class="button" type="button" value="Details"></input>';
		}

		var propertytablerows = propertytable.getElementsByTagName("tr");
		for (aIndex = 1; aIndex < propertytablerows.length; aIndex++)
		{
			var wheretoclickspan = document.getElementById("propertyclicksspan");	
			wheretoclickspan.innerHTML = "relevant row of";
			propertytablerows[aIndex].getElementsByTagName("td")[0].getElementsByTagName("a")[0].style.textDecoration = "none";		
			propertytablerows[aIndex].getElementsByTagName("td")[0].getElementsByTagName("a")[0].style.color = "#474747";		
			propertytablerows[aIndex].onclick = function() {window.location = this.getElementsByTagName("td")[0].getElementsByTagName("a")[0].href; return false;}
			propertytablerows[aIndex].onmouseover = function() {this.className = 'propertyhover';}
			propertytablerows[aIndex].onmouseout = function() {this.className = '';}
		}
	}
	
	// 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;
	
	// Rollover images in rightcolumn
	navRoot = document.getElementById("rightcolumn");
	if (navRoot != null)
	{
		for (i=0; i< navRoot.childNodes.length; i++) 
		{
			node = navRoot.childNodes[i];
			if (node.nodeName=="A") 
			{
				for (a=0; a< node.childNodes.length; a++) 
				{
					childnode = node.childNodes[a];
					if (childnode.className == "rollover") 
					{
						childnode.onmouseover = function() 
						{
							this.src = this.src.replace(".gif", "%20over.gif");
						}
						
						childnode.onmouseout = function() 
						{
							this.src = this.src.replace("%20over.gif", ".gif");
						}
					}
				}
			}
		}
	}
	
	// Hide/Show Tabs of properties
	aByAddressDiv = document.getElementById('byaddress');
	if (aByAddressDiv != null) aByAddressDiv.style.display = 'none';
	aOptions = document.getElementById('propertytypeoptions');
	if (aOptions != null) 
	{
		var aLinks = aOptions.getElementsByTagName("a");
		for (aIndex = 0; aIndex < aLinks.length; aIndex++)
		{		
			if (aIndex == 0) aLinks[aIndex].style.textDecoration = 'none';
			else aLinks[aIndex].style.textDecoration = 'underline';
		}
	}
}

function ViewByAddress(aThis)
{
	aByAddressDiv = document.getElementById('byaddress');
	aByTypeDiv = document.getElementById('bytype');
	if (aByAddressDiv != null) aByAddressDiv.style.display = 'block';
	if (aByTypeDiv != null) aByTypeDiv.style.display = 'none';	
	
	aOptions = document.getElementById('propertytypeoptions');
	if (aOptions != null) 
	{
		var aLinks = aOptions.getElementsByTagName("a");
		for (aIndex = 0; aIndex < aLinks.length; aIndex++)
		{		
			aLinks[aIndex].style.textDecoration = 'underline';
		}
	}
	aThis.style.textDecoration = 'none';
	
	return false;	
}

function ViewByType(aThis)
{
	aByAddressDiv = document.getElementById('byaddress');
	aByTypeDiv = document.getElementById('bytype');
	if (aByAddressDiv != null) aByAddressDiv.style.display = 'none';
	if (aByTypeDiv != null) aByTypeDiv.style.display = 'block';
	
	aOptions = document.getElementById('propertytypeoptions');
	if (aOptions != null) 
	{
		var aLinks = aOptions.getElementsByTagName("a");
		for (aIndex = 0; aIndex < aLinks.length; aIndex++)
		{		
			aLinks[aIndex].style.textDecoration = 'underline';
		}
	}
	aThis.style.textDecoration = 'none';
	
	return false;
}

function JSFnServThumbnailClick()
{
	// Hide all other images
	var aImageToHide = '';
	for (aIndex = 0; aIndex < aArrayOfPhotos.length; aIndex++)
	{
		aImageToHide = document.getElementById(aArrayOfPhotos[aIndex]);
		if (aImageToHide != null) aImageToHide.style.display = 'none';
	}
	
	// Show the relevant image
	var aHashPosition = this.href.indexOf("#");
	var aLengthOfLink = this.href.length;
	var aLinkTarget = this.href.substring(aHashPosition + 1,aLengthOfLink);
	var aImageToShow = document.getElementById(aLinkTarget);
	if (aImageToShow != null) aImageToShow.style.display = 'block';
	
	// Override the link
	return false;
}

function JSFnThumbnailClick()
{
	// Hide all other images
	var aImageToHide = '';
	for (aIndex = 0; aIndex < aArrayOfPhotos.length; aIndex++)
	{
		aImageToHide = document.getElementById(aArrayOfPhotos[aIndex]);
		aDescToHide = document.getElementById(aArrayOfPhotos[aIndex].replace("pic", "textforpic"));
		if (aImageToHide != null) aImageToHide.style.display = 'none';
		if (aDescToHide != null) aDescToHide.style.display = 'none';
	}
	
	// Show the relevant image
	var aHashPosition = this.href.indexOf("#");
	var aLengthOfLink = this.href.length;
	var aLinkTarget = this.href.substring(aHashPosition + 1,aLengthOfLink);
	var aImageToShow = document.getElementById(aLinkTarget);
	var aDescToShow = document.getElementById(aLinkTarget.replace("pic", "textforpic"));
	if (aImageToShow != null) aImageToShow.style.display = 'block';
	if (aDescToShow != null) aDescToShow.style.display = 'block';
	
	// Override the link
	return false;
}

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 and type
		else if ((document.getElementById('RoomID').value != '') && (document.getElementById('RoomType').value != ''))
		{
			alert('Unfortunately, you must choose either a specific room number or a room type - not both.');
			Result = false;	
		}
		// Have they forgotten to book either a specific room and type
		else if ((document.getElementById('RoomID').value == '') && (document.getElementById('RoomType').value == ''))
		{
			alert('Unfortunately, you must choose either a specific room number or a room type.');
			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;
	}
}

function JSFnPopUpWindow()
{
	aWindow = window.open(this.href, 'popup', 'width=400px,height=600px,toolbars=no,scrollbars=yes');
	aWindow.focus();
	return false;
}