// +==========================================================+
// |     +++   BOOKING SCRIPTS JAVASCRIPT FUNCTIONS   +++     |
// |       script must be located one level above root        |
// +==========================================================+

// +=========================================================+
// |      +++   FUNCTIONS COMMON TO SEVERAL PAGES   +++      |
// +=========================================================+

//  CREATE GLOBAL VARIABLES
	var startdate;  // global variable 'startdate' declared here
	var surchg;
	var contig = false;
	var onenightOK = false;
	var onenightforced = false;
	var today = new Date();
	var thisyear = today.getFullYear();
	var nextyear = thisyear+1;
//	alert ("This year is " + thisyear);
//  Show message in status bar
//	function prompt(msg) {
//		window.status = "Please enter your " + msg + ".";
//	}
// end of prompt()


// +===================================================+
// |      +++   FUNCTIONS for avail9999.php   +++      |
// +===================================================+

//  Availability list: sets selected index as query on URL, then loads page.
//  Used when going to Availability from Booking, after selecting a date
	function avail() {
		startdate = document.getElementById("startDate");
//		alert ("Selected Index = " +startdate.selectedIndex)
		var selInd = startdate.selectedIndex
		if (selInd == 0) {
			selInd = 1
		}
		var selDate = startdate.options[selInd].text
//		alert ("Destination = " + selDate)
		var selDate = selDate.substring(0,9)
//		alert ("Selected Date = " + selDate)
		var yr = "20" + selDate.substring(7,selDate.length);
//		alert (yr);
		window.location = "index.php?bookYear="+ yr + "&selDate=" + selDate;
//		alert ("Location = " + location + "\r\nAlert from bookingScripts")
		}
// end of avail()


// +==================================================+
// |     +++   FUNCTIONS for bookonline.php   +++     |
// +==================================================+

//Focus on Choice when booking form opens
	function focusChoice() {
		if (!document.getElementById("startDate")) {
			alert ("startDate element not found");
		}
		startdate = document.getElementById("startDate");
		if (startdate.selectedIndex == 0) {
//			alert ("Choice = " + startdate.selectedIndex);
				startdate.focus();
		}
		if (document.form1.duration[5].checked == true) {
			makeAppear()
		}
		cotCheck()
//		if (document.getElementById("bs").value == 0) setDest2();
//		mealCheck()
//		alert ("Choice = " + startdate.selectedIndex + "\n(from focusChoice)");
		startdate.focus();
	}
// End of function 'focusChoice'

// Sets Choice of date to value selected in Availability
//	function setDest2() {
//		alert ("setDest started");
//		if (!document.getElementById("sd1")) return false;  // sd1 is only created if booking selected on full availability page
//		var dest = document.getElementById("sd1").value;  // extracts the date from 'location'
////		alert ("Dest in Booking = " + dest);
//		for (var i = 0; i < startdate.length; i++) {    //
//			if (startdate.options[i].text.substring(0,9) == dest) {  // ('option' includes day. substring gives date portion only)
//				startdate.options[i].selected = true;
////				oneNightCheck();		// added 24/3/09 so that dates selected on availability page are checked for one night rules.
//				return true;
//			}
//		}
//		return false;
//	}

//============================================
//Check start Date to decide whether one night booking is permitted more than 30 days ahead
function oneNightCheck() {
//	alert ("Starting oneNightCheck");
	onenightOK = false;
//	alert ("One Night OK (97) = " + onenightOK);
	var startdate = document.getElementById("startDate");
// variables here were originally 'stDate' not 'startdate.  'startdate' is a global variable.
	var selIndex = startdate.selectedIndex
//	alert ("sel = " +selIndex);
	if (selIndex == 0) {
//		alert ("sel = zero"); //should return zero from here
		document.getElementById("one").style.display = "none";
		document.getElementById("labelone").style.display = "none";
		return false;
	}
	var selDate = startdate.options[selIndex].text
	selDate = selDate.substr(0, 9);  //  cuts the day off the start Date string
//	alert ("Date = " +selDate);
	selDay = selDate.substr(0, 2);           //  numerical day
	selMonth = selDate.substr(3, 3);         //  month short name
	selYear = "20" +selDate.substr(7, 2);    //  four digit year
	if (selMonth == "Jan") selMonth = "01";
	if (selMonth == "Feb") selMonth = "02";
	if (selMonth == "Mar") selMonth = "03";
	if (selMonth == "Apr") selMonth = "04";
	if (selMonth == "May") selMonth = "05";
	if (selMonth == "Jun") selMonth = "06";
	if (selMonth == "Jul") selMonth = "07";
	if (selMonth == "Aug") selMonth = "08";
	if (selMonth == "Sep") selMonth = "09";
	if (selMonth == "Oct") selMonth = "10";
	if (selMonth == "Nov") selMonth = "11";
	if (selMonth == "Dec") selMonth = "12";
//	alert (selDay + " " + selMonth + " " + selYear);
	var today = new Date();
//	alert (today);
	var bookDate = new Date(selYear,selMonth-1,selDay);  // numerical version of date, format 2009 10 21
//	alert ("Book Date = " + bookDate);
	var bookAheadMS = bookDate - today;
	var bookAheadDays = bookAheadMS/86400000;
//	alert (bookAheadDays);
//	Number of days ahead that single night booking is allowed (default 30).
	if (document.getElementById("aDays").value) {
		var ahead = document.getElementById("aDays").value;  // from database via hidden input in 'bookingform'
	} else {
		var ahead = 30;
	}
//		alert ("Number of days ahead that single night booking is allowed = " + ahead);
	var aheadMS = ahead * 86400000;
//	alert ("AheadMS = " +aheadMS);
	if (selMonth <= "03" || selMonth >= "10" || bookAheadMS <= aheadMS || contig === true) {
		onenightOK = true;
//		alert ("One Night OK (145) = " + onenightOK);
//		alert ("Month = " + selMonth + "\nAhead = " + bookAheadDays);
		document.getElementById("one").style.display = "inline";
		document.getElementById("labelone").style.display = "inline";
		document.getElementById("ahead").style.display = "none";
//		document.getElementById("one").checked = "checked";
//		alert ("Winter season or less than " + ahead + " days ahead");
	} else {
		onenightOK = false;
		if (document.form1.duration[0].checked === true && onenightforced === false) {
			alert ("Sorry, we do not take one night bookings more than 30 days ahead");
		}
		document.getElementById("one").style.display = "none";
		document.getElementById("labelone").style.display = "none";
		document.getElementById("ahead").style.display = "inline";
		document.getElementById("two").checked = "checked";
	}
	availCheck();

}

//============================================
// Check that no booked dates are selected
	function availCheck() {
//	alert ("One Night OK (169) = " + onenightOK);
//		alert ("Starting availability check")
		if (!document.getElementById) return false;
		var startdate = document.getElementById("startDate");  // Hidden becasue there is a global variable 'startdate
		var selIndex = startdate.selectedIndex;
//		alert ("selIndex Index: " + selIndex);
		if (selIndex == 0) {
			return false
		}
//		alert ("Index non-zero")
		var ordArray = ["first", "second", "third", "fourth", "fifth", "sixth", "seventh", "eighth", "ninth", "tenth", "eleventh", "twelfth", "thirteenth", "fourteenth"];
		var numArray = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen"];
//		alert("Line 171 Choice index= " + selIndex)
		var k = startdate[selIndex].text.substring(0,9)
//		alert ("Line 173 Start Date = " + startDate)
		var i = getSelectedButton(document.form1.duration)
		if (i == 5) {
			i = document.form1.period.value
			i--;
		}
		var l = availArray.length
		for (var j = 0; j < l; j++) {  //  loops though array from start to find selected date
			if (k == availArray[j]) {   //  finds selected date
//				if (availArray[j-1] == "booked" || availArray[j+1] == "booked") {
				if (availArray[j+1] == "booked" || onenightOK === true) {
					contig = "true";
					if (availArray[j+1] == "booked") {
//						var oneNightOnly = 1;
		document.getElementById("one").style.display = "inline";
		document.getElementById("labelone").style.display = "inline";
		document.getElementById("ahead").style.display = "none";
//		document.getElementById("one").checked = "checked";
						if (!document.getElementById("one").checked) {
							alert("Sorry, only one night available \nIf you wish to stay for longer \nPlease choose another date");
							document.getElementById("one").checked = "checked";
//							break;
							elem = document.getElementById("inner");
							elem.scrollTop = 0;
							startdate.focus();
							onenightforced = true;
							return false;
						}
					}
				} else {
					contig = false;
//		document.getElementById("one").checked = "false";
		document.getElementById("one").style.display = "none";
		document.getElementById("labelone").style.display = "none";
//		document.getElementById("two").checked = "checked";
//					alert ("Contiguous booking (availCheck) " + contig);

				}
				for (var z = j+1; z <= j+i; z++) {  //  continue loop for 'i' dates to check for any already booked
					if (availArray[z] == "booked" && contig == "false") {
						alert ("The " + ordArray[z-j] + " night of your selected period is already booked \nPlease select other dates or a shorter stay")
//						alert ("Sorry, only " + numArray[z-(j+1)] + " nights of your selected period are available \nPlease select other dates")
//						window.location.hash = 'top_of_page'
						elem = document.getElementById("inner");
						elem.scrollTop = 0;
						startdate.focus();
						return false;
					}
				}
//				alert ("returning true from availCheck");
				break;
			}
		}
		return true
	}

	function getSelectedButton(buttonGroup) {
		for (var i = 0; i < buttonGroup.length; i++) {
			if (buttonGroup[i].checked) {
				return i
			}
		}
		return 0
	}

// ===========================================
// Check that Arrival Date has been selected
	function checkStartDate() {
//		alert("checkStartDate (before if statement)")
		if (startdate.selectedIndex == 0) {
			alert("Please select your preferred arrival date")
			window.location.hash = 'top_of_page'
			startdate.focus()
			return false
		}
//		alert ("returning true from checkChoice");
		return true
	}
// end of checkChoice() (Arrival date)

// =============================================================

// Display box for duration entry if 'other' selected by visitor
	function makeAppear() {
//		alert ("Running 'makeAppear()'")
//		durFlag = ""
		document.getElementById("box").style.display = ""
		document.form1.period.focus()
	}

// Hide box if duration is 5 nights or less
	function makeDisappear() {
		document.form1.period.value = ""
		document.getElementById("box").style.display = "none"
	}


// ==============================================================
//  Check that valid duration options are set
	function checkDuration() {
//		alert ("Function checkDuration called")

//		This is the 'normal' situation where duration <= 5 nights
		if (document.form1.duration[5].checked == false) {
			return true
		}

//		If visitor clicks 'other', check that valid entries are made
		if (document.form1.duration[5].checked == true) {
			var nites = document.form1.period.value
//			alert ("Nites = " + nites)

			if (nites == "") {
				alert ("You have not selected the number of nights")
				document.form1.period.focus()
				return false
			}

			var re = /^(([1-9]{1})|([1]{1}[0-4]{1}))$/  // re is 1-14 only
			if (!re.test(nites)) {
				alert ("Please enter a number from 1 to 14 (inclusive)")
				document.form1.period.blur()
				document.form1.period.focus()
				document.form1.period.select()
				return false
			}

			return true
		}
	}
//  End of checkDuration

var loops = 0;
var tot1 = null;
// =============================
//	CHECK TOTAL NUMBER OF PEOPLE
//	var totPers1 = 1  // used to set the lower occupancy rate ?
	function totCheck() {
		var totPers = 0
		var totAds = 0
		var maxPers = 5
		for (var i = 0; i < document.form1.adults.length; i++) {
			if (document.form1.adults.options[i].selected) {
				totAds = parseInt(document.form1.adults.options[i].value)
			}
		}

		var totTeens = 0
		for (var i = 0; i < document.form1.teens.length; i++) {
			if (document.form1.teens.options[i].selected) {
				totTeens = parseInt(document.form1.teens.options[i].value)
			}
		}

		var totChild1 = 0
		for (var i = 0; i < document.form1.child1.length; i++) {
			if (document.form1.child1.options[i].selected) {
				totChild1 = parseInt(document.form1.child1.options[i].value)
			}
		}

		var totChild2 = 0
		for (var i = 0; i < document.form1.child2.length; i++) {
			if (document.form1.child2.options[i].selected) {
				totChild2 = parseInt(document.form1.child2.options[i].value)
			}
		}

		var totBaby = 0
		for (var i = 0; i < document.form1.baby.length; i++) {
			if (document.form1.baby.options[i].selected) {
				totBaby = parseInt(document.form1.baby.options[i].value)
			}
		}

		if (totBaby > 0) {
			maxPers = maxPers + totBaby
//			alert ("Total number of people allowed = " + maxPers)
		}

		totPers = totAds + totTeens + totChild1 + totChild2 + totBaby

		if (totPers > maxPers) {
			if (loops == 0 || totPers >= tot1) {  // warning appears only once unless numbers are increased
			alert ("You have entered " + totPers + " people. \nThe total number of guests is limited to five (plus one baby)")
			loops++;
			document.form1.adults.focus();
			tot1 = totPers;  //  tot1 set to current number, to check on change next time thro' cycle
			return false;
			}
		}
		getStartDateMonth();  //  sets value of 'surchg'
		var totKids = totPers - totAds
		if (totAds == 1 && surchg == 1) {
			if (totKids == 0) {
				alert ("Single Occupancy may incur a 50% surcharge \nMarch to October inclusive")
			} else {
				alert ("One adult with children may incur a surcharge \nMarch to October inclusive")
			}
			return true
		}
//		alert ("Total = " + totPers)
		return true
	}

//============================================
	function getStartDateMonth() {
		if (startdate.selectedIndex != 0) {
			var startDate = startdate.value.substr(0, 9);
			startDate = startdate.value.substr(0, 2) + " " + startdate.value.substr(3, 3) + " 20" + startdate.value.substr(7, 2);
			var startMS = Date.parse(startDate);
			startDate = new Date(startMS);
			var startMonth = startDate.getMonth();
//			alert (startMonth);
			if (startMonth < 2 || startMonth > 9) {
				surchg = 0;
			} else {
				surchg = 1;
			}
			return true;
		} else {
			return false;
		}
	}

// ===========================================
// Display COT SELECTION box if baby checked
	function cotCheck() {
		if (document.form1.baby.options[0].selected) {
			document.form1.cot.checked = false
			document.getElementById("cotBox").style.display = "none"
		} else {
			document.getElementById("cotBox").style.display = ""
		}
//		alert ("Cot check done")
	}
// end of function cotCheck()

// ============================================
// Display dietary reminder if evening meal selected
	function mealCheck() {
//		alert ("started mealCheck")
		if (document.form1.evgMeal[0].checked) {
			document.getElementById("courses2").style.display = "none"
			document.getElementById("dietReq").style.display = "none"
		}
		if (document.form1.evgMeal[1].checked) {
			document.getElementById("courses2").style.display = ""
			document.getElementById("dietReq").style.display = ""
		}
		if (document.form1.evgMeal[2].checked) {
			document.getElementById("courses2").style.display = "none"
			document.getElementById("dietReq").style.display = ""
		}
//		alert ("End of mealCheck")
	}


// ===============================
//	FORMCHECK for main booking form
// Checks code right through by calling functions.
	function formCheck(form1) {
//		alert ("Starting FormCheck")
		if (checkDuration(document.form1.duration)) {
			if (checkStartDate(startdate)) {
				if (availCheck()) {
					if (totCheck()) {
//						alert ("Returning True from formCheck");
						return true
					}
				}
			}
		}
//		alert ("returning false from formCheck");
		return false
	}

// ==========================================
// RESET: Clear form and focus on Chosen Date
	function allowReset() {
		if (confirm("Clear the form ?")) {
			document.getElementById("startdate").selectedIndex = "0"; //  added 31 Jul 08
			document.getElementById("box").style.display = "none"
			document.getElementById("cotBox").style.display = "none"
			startdate.options[0].selected = true
			window.location.hash = 'top_of_page'
			return true
		}
//		else {
//			return false
//		}
		return false
	}


// ========================================================
// SEND: Confirm Submit (sending form to 'Review Booking')
	function allowSend() {
//		alert ("Starting AllowSend")
		if (formCheck(document.form1)) {
			document.getElementById("bs").value = "1";
			return true;
//			if (window.confirm("Continue to next stage ?")) {
//				javascript:document.form1.submit()
//			}
		}
		return false;
	}

// ========================================================
// SEND: Confirm Submit (sending form to 'Review Booking')
	function allowSend2() {
//		alert ("Starting AllowSend2")
		if (formCheck2(document.form1)) {
//			alert ("bol2 Script: formCheck completed OK")
			if (window.confirm("Continue to next stage ?")) {
				javascript:document.form1.submit()
			}
		}
//		else {
//			return false
//		}
	}

// ========================================================
// BACK button on Review.php
function backToBooking() {
//	alert ("Back function called");
	history.go(-1);
}

// +===============================================+
// |     +++   FUNCTIONS for addName.php   +++     |
// +===============================================+

//Focuses on Name when form opens
	function focusName() {
//		alert ("Starting focusName");
		document.form2.firstName.focus()
		i == 0;
	}
// End of function 'focusName'

	function nameCheck() {
//		alert ("Starting NameCheck")
		if (firstNameCheck()) {
			if (lastNameCheck()) {
				return true;
			} else {
				return false;
			}
		}
		return false;
	}

//	Set global variables
	var firstName;
	var lastName;
	var i =0;

	function firstNameCheck() {
//		return true;  // by-passes this check when NOT commented out
		var dformf = document.getElementById("firstName");
		var re = /^(([a-z])([a-z]+))((\-|\s){1,3}([a-z])([a-z]+))?$/i
		if (dformf.value != "" && re.test(dformf.value)) {
//			alert ("firstName valid");
			firstName = RegExp.$2.toUpperCase() + RegExp.$3.toLowerCase() + RegExp.$5 + RegExp.$6.toUpperCase() + RegExp.$7.toLowerCase()
//			alert ("First Name is " + firstName);
			dformf.value = firstName;
//			i = 0;
			return true;
		} else {
			alert ("Please check and re-enter your first name");
			dformf.focus();
			dformf.select();
			return false;
		}
	}

	function lastNameCheck() {
//		return true;
		var dforml = document.getElementById("lastName");
		var re = /^([adeilnouv]{0,3}\s{0,3}[adeilmnoruv]{0,3}[\s|\'])?(([a-z])([a-z]+))((\-|\s){1,3}([a-z])([a-z]+))?$/i
		if (dforml.value != "" && re.test(dforml.value)) {
//			alert ("lastName valid");
			if (RegExp.$1.toLowerCase() == "o'") {
				var qwe = "O'";
			} else if (RegExp.$1.toLowerCase() == "m'") {
				var qwe = "M'";
			} else {
				var qwe= RegExp.$1.toLowerCase();
			}
			lastName = qwe + RegExp.$3.toUpperCase() + RegExp.$4 + RegExp.$6 + RegExp.$7.toUpperCase()  + RegExp.$8;
			dforml.value = lastName;
			var fullName = firstName + " " + lastName;
//			alert (fullName);
			document.getElementById("fullName").value = fullName;
			return true;
		} else {
			if (i < 2) {
				alert ("Please check and re-enter your last name");
				dforml.focus();
				dforml.select();
				if (dforml.value != "") i++;
				alert ("i = " + i);
			}
			return false;
		}
	}
// End of name CHECKING FUNCTIONS

// ===========================================
// Display Other Title box if 'Other' selected
	function otherTitleCheck() {
		if (document.form2.title.options[6].selected == false) {
//			alert ("Other NOT selected")
			document.getElementById("otherTitleBox").value = ""
			document.getElementById("otherTitleBox").style.display = "none"
		} else {
			document.getElementById("otherTitleBox").style.display = ""
			document.getElementById("otherTitleBox").focus()
			document.getElementById("otherTitleBox").select()
		}
		alert ("otherTitleCheck done")
	}
//  End of function otherTitleCheck()

// ===========================================
// Display additional info requests if appropriate
	function howFind() {
//		alert("Selected Index is " + document.form2.how.selectedIndex)
//	Hide all possible display options
		document.getElementById("recd").style.display = "none";
		document.getElementById("route").style.display = "none";
		document.getElementById("otherSE").style.display = "none";
//	If 'recommended' display prompt & box asking for name
		if (document.getElementById("how").selectedIndex == 1) {
			document.getElementById("recd").style.display = "";
			document.getElementById("recdTxt").focus();
		}
//	If Google ask for key words
		if (document.getElementById("how").selectedIndex > 3 && document.getElementById("how").selectedIndex <6) {
			document.getElementById("route").style.display = "";
			document.getElementById("routeTxt").focus();
		}
//	If other Search Engine ask for its name and keywords
		if (document.getElementById("how").selectedIndex == 6) {
			document.getElementById("otherSE").style.display = "";
			document.getElementById("se").focus();
		}
//	If a listing site (selected index > 6), no current action (all commented out)
//		if (document.getElementById("how").selectedIndex > 6) {
//			alert ("Greater than six");
//		}
	}

// ===========================================
//	CHECKS FOR email EMPTY.  E-mail IS REQUIRED.
//	VALIDATES E-MAIL ADDRESS IF PRESENT
//	Returns TRUE if E-Mail is good
	function noEmail() {
//		alert ("Starting noEmail");
		if (!document.getElementById("mail0")) {
			alert ("Returning False");
			return false;
		}
		if (document.getElementById("mail0").value == "") {
			alert("Please enter your e-mail address")
			document.getElementById("mail0").focus()
			return false
		}	else if (document.getElementById("mail0").value != "") {
				var re = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/
				if (re.test(document.form2.email.value)) {
					return true
				}
				alert("Invalid e-mail address")
				document.getElementById("mail0").focus()
				document.getElementById("mail0").select()
				return false
		}
	}
// end of function noEmail()

// =============================
// CHECKS that email is the same as email
	function checkEmail() {
		if (document.getElementById("mail0").value != document.getElementById("mail1").value) {
			alert ("Your e-mail entries differ \nPlease check and amend")
			document.getElementById("mail0").focus()
			document.getElementById("mail0").select()
			return false
		}
	}
// end of function checkEmail()

// =============================
//	CHECKS FOR < 9 DIGITS IN Tel
	function badTel() {
		if (document.getElementById("tel0").value == "") {
			alert ("Please enter your phone number")
			document.getElementById("tel0").focus()
			return false
		} else {
			var digits = document.getElementById("tel0").value.length
			if (digits < 10) {
				alert("Invalid phone number \nYou have only entered " + digits + " digits \nPlease enter your full telephone number \n(with Country code if outside UK)")
				document.getElementById("tel0").focus()
				document.getElementById("tel0").select()
				return false
			} else {
				return true
			}
		}
	}
// end of function badTel()

// =====================
	function termsCheck() {
		var termsRead = document.getElementById("terms");
//		alert ("TermsRead is " + termsRead.value);
		if (termsRead.checked) termsRead.value="Yes";
//		alert ("TermsRead is " + termsRead.value);
		if (termsRead.value != "Yes") {
			alert ("Please confirm that you have read and accepted our terms and conditions");
			return false;
		} else {
//			alert ("Terms Read");
			return true;
		}
	}
// end of function termsCheck

// =====================
	function lastCheck() {
		if (!document.getElementById("form2")) {
//			alert ("Form2 not present");
			return false;
		} else {
//			alert ("Form2 found");
			lastform = document.getElementById("form2");
		}
//		alert ("Starting lastCheck")
		if (nameCheck()) {
//			alert ("Name Check OK")
			if (noEmail()) {
//				alert ("E-mail OK");
				if (badTel()) {
//					alert ("Tel OK");
					if (termsCheck()) {
//						alert ("Terms read OK");
//						if (window.confirm("Go ahead and make this booking ?")) {
							javascript:document.form2.submit();
//							return true;
//						}
					}
				}
			}
		}
		return false;
	}

	function test() {
		alert("test started")
	}

// +==============================================+
// |     +++   FUNCTIONS FOR email.php   +++      |
// +==============================================+
// Focus on subject when opening
	function focusemail() {
		if (document.getElementById("form1")) {
//			alert ("Trying to focus")
			document.form1.subject.focus()
//			alert ("Focused")
		}
	}

// Focus on text box
	function focusText() {
		document.form1.message.focus()
	}

//==================================================
// Alert for those who just want to check availability
	function availAlert() {
		if (document.getElementById("bS")) {
			var bS = document.getElementById("bS").value;
//			alert ("bS = " + bS);
			if (window.confirm ("If you just want to check availability and/or pricing for " + thisyear + ", click ''OK'\nThe Availability and Pricing page will open.\n\nIf you want to contact us about something else, click 'Cancel'\nYou will then be able to complete the form and contact us\n")) {
				document.getElementById("j").value = "1";
				window.location = "../booking/index.php?bookYear= " + thisyear;
				bS = "0";
				return false;
			}
		}
	}

//==================================================
// Check AVAILABILITY on line if Subject is Availability
	function checkAvail() {
//		alert ("Started checkAvail")
		var chkAv = (document.form1.subject.options.selectedIndex)
		if (chkAv == "2") {
			if (window.confirm("You can check availability on line ! \nTo check availability NOW, click 'OK' \n\nIf you want to enquire about something else,\npress 'Cancel' TWICE and change the Subject\n")) {
//\n\nIf you prefer to send your message and wait for a reply, Click 'Cancel'
				document.getElementById("j").value = "1";
				window.location = "../booking/index.php?bookYear= " + thisyear;
				return false
			} else {
				document.getElementById("j").value = "0";
				if (window.confirm("Send message now ?")) {
					return true;
				} else {
					return false;
				}
			}
		} else {
//			alert ("Subject is not 'Availability'")
			return true;
		}
	}

//==================================================
// Check Subject has been entered
	function checkSubject() {
		var subj =document.getElementById("subj");
		var subjIndex = (document.form1.subject.options.selectedIndex)
		if (subjIndex == "0") {
			alert ("Please select a Subject\nfrom the drop-down list");
			subj.focus();
			return false;
		} else {
			return true;
		}
	}

//==================================================
// Check name has been entered
	function checkname() {
//		alert ("Starting checkname");
		var uname = document.getElementById("fullName");
		if (uname.value == "") {
			alert ("Please enter your name");
			uname.focus();
			return false;
		}
		return true;
	}

//==================================================
// Check email is valid
	function checkemail() {
		var msg = document.getElementById("msg");
		var email = document.getElementById("email");
		var emailval =email.value;
		if (emailval == "") {
			alert("Please enter your e-mail address");
			email.focus();
			return false;
		} else {
				if (emailval.indexOf("localhost") != -1) return true;
				var re = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
				if (re.test(emailval)) {
					msg.focus();
					return true;
				}
				alert("Invalid e-mail address");
				email.focus();
				email.select();
				return false;
		}
		return true;
	}

//==================================================
// Check that text box is not empty and does not contain any links ('http://')
	function checkText() {
		var msg = document.getElementById("msg");
		var msgval = msg.value;
		if (msgval == "") {
			alert ("Please include your message");
			msg.focus();
			return false;
		}
//		alert (txt);
//		if (indexOf("http://") != -1) {
		if (msgval.match(/^.*http(s)?(:\/\/)?.*/)) {
			alert ("Sorry, no links permitted \nPlease remove them before sending this message");
			msg.focus();
			return false;
		}
		return true
	}

//==================================================
// Check right through the form
	function allowSendMail() {
		if (checkSubject()) {
			if (checkname()) {
				if (checkemail()) {
					if (checkText()) {
						if (checkAvail()) {
//							if (window.confirm("Go ahead and send this message ?")) {
								javascript:document.form1.submit();
								return true
//							}
						}
					}
				}
			}
		}
		return false
	}

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

function displayButtons() {
//	alert ("Running displayButtons");
	var showspan = document.getElementsByTagName("span");
	for (var i=0; i<showspan.length; i++) {
		if (showspan[i].className == "hidespan") {
			showspan[i].style.display = "inline";
		}
	}
}

function hideBox() {
	var hidebox = document.getElementById("box");
	hidebox.style.display = "none";
}

// +==============================================+
// |   +++   FUNCTIONS FOR paypalFull.php   +++   |
// +==============================================+

function focusFullName() {
	document.form1.fullName.focus()
}
// End of function 'focusFullName'
