window.onload = StandardOnLoad;

function StandardOnLoad()
{
	if (document.getElementById)
	{
		handleExternalLinks(); // Open external links in new window
				
		// Load Google Map
		var aMap = document.getElementById('map');
		if (aMap != null)
		{			
			if (window.GBrowserIsCompatible)
			{			
				loadMap(aMap);	
			}
		}
		
		// Add Date Picker to contact form
		var aContactForm = document.getElementById('contactform');
		if (aContactForm != null)
		{
			setupCalendars();
			
			//aContactForm.onSubmit = validate(aContactForm);
		}
			
	}	
}

// Create Google Map
function loadMap()
{
	var map;
	var icon0;
	var newpoints = new Array();
	var WINDOW_DESCRIPTION = '<div id="google_text_box"><span>Altonburn Cottage</span><br>The cottage is located on a private road just off Altonburn Road below the marker.<br/><form action="http://maps.google.co.uk/maps" method="get"><label for="saddr">For Directions Enter Your Postcode: </label><input type="text" name="saddr" id="saddr" value="" /><input id="google_submit" type="submit" value="Go" /><input type="hidden" name="daddr" value="57.584177906223495,-3.905360698699951" /><input type="hidden" name="hl" value="en" /></form>Or <a href="http://maps.google.co.uk/maps?f=d&saddr=&daddr=57.584177906223495,-3.905360698699951&z=15&q=Altonburn+Cottage,+Nairn">Click Here</a> For Directions.</div>';

	map = new GMap2(document.getElementById("map"));
	map.addControl(new GLargeMapControl());
	map.addControl(new GMapTypeControl());
	map.setCenter(new GLatLng(57.584177906223495, -3.905360698699951), 15);
 
	var icon0 = new GIcon();
	icon0.image = "http://www.google.com/mapfiles/marker.png";
	icon0.shadow = "http://www.google.com/mapfiles/shadow50.png";
	icon0.iconSize = new GSize(20, 34);
	icon0.shadowSize = new GSize(37, 34);
	icon0.iconAnchor = new GPoint(9, 34);
	icon0.infoWindowAnchor = new GPoint(9, 2);
	icon0.infoShadowAnchor = new GPoint(18, 25);
	
	var point = new GPoint(-3.905360698699951,57.584177906223495);

	//Add overlay to map 
	var marker = new GMarker(point,icon0);
	map.addOverlay(marker);
	marker.openInfoWindowHtml(WINDOW_DESCRIPTION);
}
				
// This function calculates the 'Number Of Nights' form box
function calculate(form) {
	var A = form.day.value;
	var B = form.dday.value;
	var C = form.month.value;
	var D = form.dmonth.value;
	
	//alert (B);
	
	A = Number(A);
	B = Number(B);
	var E=0;

	// These if functions calculate the 'Number Of Nights' form box depending on the months selected.
	if(C == "08" || C == "03" || C == "05" && B != ''){
		if(C==D){
			E = (B - A);
		}else {
			F = 30 - A;
			E = F + B;
		}										
	}
	
	if(C == "00" || C == "02" || C == "06" || C == "04" || C == "07" || C == "09" || C == "10" || C == "11" && B != ''){
		if(C==D){
			E = (B - A);
		}else {
			F = 31 - A;
			E = F + B;
		}										
	}
	
	if(C == "01" && B != ''){
		if(C==D){
			E = (B - A);
		}else {
			F = 28 - A;
			E = F + B;
	}			
	}
	
	if(B == '') E = 0;
	
	// Setting the value of the form box 'Number Of Nights' to E.
	form.Nights.value = E;
	
	// Error function to check that the arrival day is before the departure day.
	if(B != '' && C == D && A > B){
		alert ("Error, you have entered an arrival date that is after your departure date, please re-enter your dates.");
		form.day.value = 1;
		form.dday.value = 1;
		form.nights.value = 0;																		
	}
	
	// Assinging values to the arrival year and departure year.
	G = form.year.value;
	H = form.dyear.value;
	
	// Error function to check that the arrival year is before the departure year.
	if(B != '' && G > H){
		alert ("Error, you have entered an arrival year that is after your departure year, please re-enter your year dates.");
		today = new Date();
		CurrentYear = today.getFullYear();
		
		form.year.value = CurrentYear;
		form.dyear.value = CurrentYear;																	
	}

}
		
// Validate the form function
function validate(form) {
	// if statements check whether values are present in the relevant form fields	
	if (form.FirstName.value.length == "") {
	form.FirstName.value = prompt("You forgot to enter your firstname, please enter it below." , "");
	return false;
	}

	if (form.Surname.value.length == "") {
	form.Surname.value = prompt("You forgot to enter your surname, please enter it below." , "");
	return false;
	}

	if (form.Email.value.length == "") {
	form.Email.value = prompt("You forgot to enter your email address, please enter it below." , "");
	return false;
	}


	if (form.Telephone.value.length == "") {
	form.Telephone.value = prompt("You forgot to enter your telephone number, please enter it below." , "");
	return false;
	}

	// Check whether the telephone is valid i.e a number
	if (form.Telephone.value.length != ""){
	   var i;
	   for (i = 0; i < form.Telephone.value.length; i++){ 
	   
			// Declaring required variables
			var digits = "0123456789";
			
			// Check that current character is number.
			var c = form.Telephone.value.charAt(i);
			if ((((c < "0") || (c > "9")) && (c != " "))){
				form.Telephone.value = prompt("Error, you have entered the following invalid character: " + c + "  Only numerical characters are allowed. Please enter a valid telephone number" , "");
				return false;
			}
		}
	}

	// Check whether the length of the tel number is correct
	if(form.Telephone.value.length < 11 && form.tel.value.length != 0){
	 form.Telephone.value = prompt("The telephone number you entered is too short, it should be at least 11 numerals. Please enter it again." , "");
	 return false;
	}  

	// Check whether the value for number of adults is present in the form field
	if (form.adult.value.length == "") {
	form.adult.value = prompt("You forgot to enter the amount of adults that will be staying, please enter the amount below." , "");
	return false;
	}
	  
	// Check whether the number of adults is a number
	if (form.adult.value.length != ""){
	   var i;
	   for (i = 0; i < form.adult.value.length; i++){ 
	   
			// Declaring required variables
			var digits = "0123456789";
			
			// Check that current character is number.
			var c = form.adult.value.charAt(i);
			if (((c < "0") || (c > "9"))){
				form.adult.value = prompt("Error, please enter a valid number for the amount of adults staying." , "");
				return false;
			}
		}
	}

	if (form.child.value.length == "") {
	form.child.value = prompt("You forgot to enter the amount of children that will be staying, please enter it below. If no children are staying enter the number 0." , "");
	return false;
	}

	// Check whether the value for the number of nights is present in the form field
	if (form.nights.value.length == "") {
	alert("You forgot to enter the number of nights that you will be staying, please select the dates from the form." , "");
	return false;
	}
    
  return true;
}

/* -------------- DATE PICKER -------------- */

// Change select form input values
function catcalc(cal) {
	var date = cal.date;
	var aContactForm = document.getElementById('contactform');

	var fieldday = document.getElementById("day");
	fieldday.value = date.print("%d");

	fieldmonth = document.getElementById("month");
	fieldmonth.value = date.print("%m");

	fieldyear = document.getElementById("year");
	fieldyear.value = date.print("%Y");
	
	calculate(aContactForm);
}

// Change select form input values
function catcalcb(cal) {
	var date = cal.date;
	var aContactForm = document.getElementById('contactform');

	var fieldday = document.getElementById("dday");
	fieldday.value = date.print("%d");

	fieldmonth = document.getElementById("dmonth");
	fieldmonth.value = date.print("%m");

	fieldyear = document.getElementById("dyear");
	fieldyear.value = date.print("%Y");
	
	calculate(aContactForm);
}

function setupCalendars()
{
	// Arrival Day
	Calendar.setup({
		inputField : "day",
		ifFormat : "%e",
		button : "f_trigger_c",
		align : "Tl",
		onUpdate : catcalc
	});

	// Arrival Month
	Calendar.setup({
		inputField : "month",
		ifFormat : "%m",
		disableFunc: true
	});

	// Arrival Year
	Calendar.setup({
		inputField : "year",
		ifFormat : "%Y",
		disableFunc: true
	});
	
	
	// Departure Day
	Calendar.setup({
		inputField : "dday",
		ifFormat : "%e",
		button : "f_trigger_b",
		align : "Tl",
		onUpdate : catcalcb
	});

	// Departure Month
		Calendar.setup({
		inputField : "dmonth",
		ifFormat : "%m",
		disableFunc: true
	});

	// Departure Year
	Calendar.setup({
		inputField : "dyear",
		ifFormat : "%Y",
		disableFunc: true
	});		
}
/* ------------------------------------------ */

// function makes sure that external links open in new window
function handleExternalLinks()
{
	var hostName = window.location.hostname;
	var links = document.getElementsByTagName("a");
	for(var i = 0; i < links.length; i++)
	{
		if(links[i].href.indexOf(hostName) == -1)
		{
			var curTitle = (links[i].getAttribute("title")) ? links[i].getAttribute("title") + " - ": "";
			links[i].setAttribute("target", "_blank");
			links[i].setAttribute("title",  curTitle + "opens in new window");
		}
	}
}
	