function refresh_bedetails(option) {
		phpscript 		= document.createElement('script');
		phpscript.src 	= 'includes/update_bedetails.php?sitecode=' + option;
		phpscript.type	= 'text/javascript';
		document.getElementsByTagName('head')[0].appendChild(phpscript);
}

//function refresh_bestartseconds(option) {
//	phpscript 		= document.createElement('script');
//	phpscript.src 	= 'includes/update_bestartseconds.php?date=' + option.value;
//	phpscript.type	= 'text/javascript';
//	document.getElementsByTagName('head')[0].appendChild(phpscript);
//}

function check_total_bowlers() {
	var number_of_adults  	= Number(document.forms['form1'].numatt.options[document.forms['form1'].numatt.selectedIndex].value);
	var number_of_juniors 	= Number(document.forms['form1'].numatt_jnr.options[document.forms['form1'].numatt_jnr.selectedIndex].value);
	var total_bowlers 		= number_of_adults + number_of_juniors;
	var maximum_bowlers 	= Number(document.forms['form1'].numatt.options.length) - 1;
	//alert('adults: '+number_of_adults+'\njuniors: '+number_of_juniors+'\ntotal: '+total_bowlers+'\nmax: '+maximum_bowlers);
	if (total_bowlers > maximum_bowlers || total_bowlers == 0) {
		return false;
	} else {
		return true;
	}
}

function check_time() {

 	regexp = /^(\d{1,2}):(\d{2})\s*([ap]m)?$/;
 	if(document.forms[0].bookingtime.value == '') {
 		return false;
 	} else {
 		if(regs = document.forms[0].bookingtime.value.match(regexp)) {
 			if(regs[3]) {
 				// 12-hour time format with am/pm
 				if(regs[1] < 1 || regs[1] > 12) {
 					return false;
 				}
 			} else {
 				// 24-hour time format
 				if(regs[1] > 23) {
 					return false;
 				}
 			}

 			if(regs[2] > 59) {
 				return false;
 			}
 		} else {
 			return false;
 		}
 	}
 	return true;
}

function populateDaysInMonth(month) {
	var i;
	var leadingZero;
	var numElements = document.form1.b_day.length;

	var year = new Date().getFullYear();

	if(month < document.form1.b_month.options[0].value) {
		year = year + 1;
	}
	
	var currentDaySelection = document.form1.b_day.selectedIndex;

	var numDays = (32 - new Date(year, month-1, 32).getDate());
	for (i=0; i < numElements; i++) {
		document.form1.b_day.options[i] = null;
	}

	for(i=0 ; i < numDays ;i++) {
		if(i<9) {
			leadingZero = '0';
		} else {
			leadingZero = '';
		}
		document.form1.b_day.options[i] = new Option(String(leadingZero+(i+1)), i+1);
	}
	
	if(currentDaySelection > (numDays -1)){
		currentDaySelection = numDays - 1;
	}
	document.form1.b_day.selectedIndex = currentDaySelection;
}

function populateMinutes(hour){
	var currentMinutesSelection = document.form1.bookingtime_min.selectedIndex;
	document.form1.bookingtime_min.options.length = 0;
	
	document.form1.bookingtime_min.options[0] = new Option("00", 00);
	if(hour == 22){
		document.form1.bookingtime_min.selectedIndex = 0;
	} else {
		for(i=10; i <= 50; i+=10){
			document.form1.bookingtime_min.options[document.form1.bookingtime_min.options.length] = new Option(String(i), i);
		}
		document.form1.bookingtime_min.selectedIndex = currentMinutesSelection;
	}
}

function check_booking_form() {
	var errs = '<b><span class="alt">Please check the following:</b>';
	var error_state = '0';
//	if(document.forms[0].bowl.options[document.forms[0].bowl.selectedIndex].value == "") {
//		errs += '<br>Please select a bowl';
//		document.getElementById("err").innerHTML = errs;
//		errs = "";
//		return false
//	}
	if(check_total_bowlers() == false) {
		errs += '<br>Please enter a valid number of bowlers (1 - 12 maximum).';
		error_state = '1';
	}

	var bookingdate_day 	= document.forms['form1'].b_day.options[document.forms['form1'].b_day.selectedIndex].value;
	var bookingdate_month 	= document.forms['form1'].b_month.options[document.forms['form1'].b_month.selectedIndex].value;
	var bookingdate_year 	= document.forms['form1'].b_year.options[document.forms['form1'].b_year.selectedIndex].value;

	var selectedTimeHr 		= document.forms['form1'].bookingtime_hr.options[document.forms['form1'].bookingtime_hr.selectedIndex].value;
	var selectedTimeMins 	= document.forms['form1'].bookingtime_min.options[document.forms['form1'].bookingtime_min.selectedIndex].value;
	var bookingdate			= new Date(bookingdate_year, bookingdate_month-1, bookingdate_day);
	var bookingdate_time	= new Date(bookingdate_year, bookingdate_month-1, bookingdate_day, selectedTimeHr, selectedTimeMins);
	var todayfulldate 		= new Date();
	var todaydate			= new Date(todayfulldate.getFullYear(),todayfulldate.getMonth(),todayfulldate.getDate());
	var todaydatetime		= new Date(todayfulldate.getFullYear(),todayfulldate.getMonth(),todayfulldate.getDate(), (todayfulldate.getHours())+1, todayfulldate.getMinutes());

	if (bookingdate_time.getTime() < todaydatetime.getTime()) {
		errs += '<br>Please select a start time at least 1 hour in the future.';
		error_state = '1';
	} 

	if (bookingdate.getTime() == todaydate.getTime()) {
		if((selectedTimeHr == "22") && (selectedTimeMins != "00")) {
			errs += "<br>Sorry, you are not able select a start time later than 10pm";
			error_state = '1';
		}
	}

	if ((bookingdate_month==4 || bookingdate_month==6 || bookingdate_month==9 || bookingdate_month==11) && bookingdate_day==31) {
		errs += "<br>Please enter a valid date";
		error_state = '1';
	}
	if (bookingdate_month == 2) { // check for february 29th
		var isleap = (bookingdate_year % 4 == 0 && (bookingdate_year % 100 != 0 || bookingdate_year % 400 == 0));
		if (bookingdate_day>29 || (bookingdate_day==29 && !isleap)) {
			errs += "<br>Please enter a valid date";
			error_state = '1';
		}
	}

/*
Not in use atm due to having used combo boxes for time instead of free txt
	if(check_time() == false) {
		errs += '<br>Please enter a valid preferred start time.';
		error_state = '1';
	}
*/
	if(error_state == '1') {
		errs += "</span>";
		document.getElementById("err").innerHTML = errs;
		document.getElementById("err_bottom").innerHTML = errs;
		errs = "";
		return false
	} else {
		errs = "";
		document.getElementById("err").innerHTML = errs;
		document.getElementById("err_bottom").innerHTML = errs;
		return true
	}
}

// The old pre compliance credit card validation script
function isCreditCard(cardNo) {
	var my_regexp = /[^\d+]/;

	if(cardNo.match(my_regexp)) {
		return false;
	}

	if (cardNo.length > 19) {
		return false;
	}

	if ((cardNo == 0) || (cardNo == '')) {
		return false;
	}

	var sum = 0;
	var mul = 1;
	var len = cardNo.length;

	for (i = 0; i < len; i++) {

		digit = cardNo.substr(len - i - 1, 1);
		tproduct = digit * mul;

		if (tproduct >= 10) {
			sum += (tproduct % 10) + 1;
		} else {
			sum += tproduct;
		}

		if (mul == 1) {
	  		mul++;
		} else {
			mul--;
		}
	}

	if ((sum % 10) == 0) {
		return true;
	} else {
		return false;
	}
}

// The new compliant credit card validation script
function luhnCheck(s) {

  var i, n, c, r, t;

  // First, reverse the string and remove any non-numeric characters.

  r = "";
  for (i = 0; i < s.length; i++) {
    c = parseInt(s.charAt(i), 10);
    if (c >= 0 && c <= 9)
      r = c + r;
  }

  // Check for a bad string.

  if (r.length <= 1)
    return false;

  // Now run through each single digit to create a new string.
  // Even digits are multiplied by two, odd digits are left alone.

  t = "";
  for (i = 0; i < r.length; i++) {
    c = parseInt(r.charAt(i), 10);
    if (i % 2 != 0)
      c *= 2;
    t = t + c;
  }

  // Finally, add up all the single digits in this string.

  n = 0;
  for (i = 0; i < t.length; i++) {
    c = parseInt(t.charAt(i), 10);
    n = n + c;
  }

  // If the resulting sum is an even multiple of ten (but not zero),
  // the card number is good.

  if (n != 0 && n % 10 == 0) {
    return true;
  } else {
    return false;
  }
}

function check_CC_form() {
	var errs = '<span class="alt"><b>Your details are not complete, please check the following: <br></b>';
	var error_state = '0';

	if(document.frmData.elements['forename'].value == "") {
		errs += '<br>First name';
		error_state = '1';
	}
	if(document.frmData.elements['surname'].value == "") {
		errs += '<br>Surname';
		error_state = '1';
	}
	if(document.frmData.elements['address1'].value == "") {
		errs += '<br>The first line of your address';
		error_state = '1';
	}
	if(document.frmData.elements['postcode'].value == "") {
		errs += '<br>Postcode';
		error_state = '1';
	}
	if(document.frmData.elements['phone_number'].value == "") {
		errs += '<br>Telephone number';
		error_state = '1';
	}

	var emailFilter = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.([a-zA-Z]{2,4})$/;
	var email_1_check = '0';
	var email_2_check = '0';
	var email_1 = document.frmData.elements['email_address'].value;
	var email_2 = document.frmData.elements['email_address_test'].value;
	if (!email_1){ //check the first email box for content
		errs += "<br>Email address";
		error_state = '1';
	} else {
		if (!email_1.match(emailFilter)) { //check the first email box for a valid email address
			errs += "<br>Email address is not valid, please re-enter your email address";
			error_state = '1';
		} else {
			email_1_check = '1';
		}
	}

	if (!email_2){ //check second box for content
		errs += "<br>Confirmation email address";
		error_state = '1';
	} else {
		if (!email_2.match(emailFilter)) {  //check the second email box for a valid email address
			errs += "<br>Confirmation email address is not valid, please re-enter your email address";
			error_state = '1';
		} else {
			email_2_check = '1';
		}
	}

	//if email addresses are valid, check they match
	if(email_1_check == '1' && email_2_check == '1') {
		if(email_1 != email_2) {
			errs += "<br>Your email addresses do not match, please check you have entered both correctly";
			error_state = '1';
		} else {
			error_state = '0';
		}
	}

	if(document.frmCardholder.elements['CCType'].value == "") {
		errs += '<br>Credit/debit card type';
		error_state = '1';
	}
	if(document.frmCardholder.elements['CCNumber'].value == "") {
		errs += '<br>Credit/debit card number';
		error_state = '1';
	}
	if ((document.frmCardholder.elements['ExpDateMM'].value == "") || (document.frmCardholder.elements['ExpDateYY'].value == "")) {
		errs += '<br>Credit/debit card expiry date';
		error_state = '1';
	}
	if(document.frmCardholder.elements['ch_name'].value == "") {
		errs += '<br>Name on credit/debit card';
		error_state = '1';
	}

	if((document.frmCardholder.elements['CCType'].options[document.frmCardholder.elements['CCType'].selectedIndex].value == "Switch") && (document.frmCardholder.elements['IssueNumber'].value == "")) {
		errs += '<br>Issue number';
		error_state = '1';
	}

	if(!luhnCheck(document.frmCardholder.elements['CCNumber'].value)) {
		document.frmCardholder.elements['CCNumber'].focus();
		errs += '<br><br>Please enter a valid credit card number<br>';
		error_state = '1';
		ccvalid = false;
	} else {
		ccvalid = true;
	}

	if (document.frmCardholder.elements['tandc'].checked == false) {
		//alert('Please confirm that you have read our Terms and conditions before proceeding.');
		document.frmCardholder.elements['tandc'].focus();
		errs += '<br>Please confirm that you have read our Terms and conditions before proceeding';
		error_state = '1';
	}

	if (document.frmCardholder.elements['cancelcharge'].checked == false) {
		//alert('Please confirm that you have read our Cancellation Policy before proceeding.');
		document.frmCardholder.elements['cancelcharge'].focus();
		errs += '<br>Please confirm that you have read our Cancellation Policy before proceeding';
		error_state = '1';
	}

	if((error_state == '1') || (!ccvalid)) {
		errs +="</span>";
		document.getElementById("err").innerHTML = errs;
		document.getElementById("errs_bottom").innerHTML = errs;
		document.location.href="#a_errors";
		errs = "";
		return false;
	} else {
		errs = "";
		document.getElementById("err").innerHTML = errs;
		document.getElementById("errs_bottom").innerHTML = errs;

		// Compliance code again,
		// Create variables with the six digits and the last four digits.
		var s = document.frmCardholder.elements['CCNumber'].value;
		var f = s.substring(0, 6);
		var l = s.substring(s.length-4);

		// Determine the length of the card number.

		var len = s.length;

		// If a longer than sixteen digit card use a longer padding string of asterisks.

		if (len>16){
			var p="**********";
		} else {
			var p="******";
		}

		document.frmCardholder.elements['CCNumber'].value=f+p+l;

		// Submit the form that does not contain any card holder data.
		// (neither full or partial details)

		document.frmData.submit();
	}
}

function check_card_type() {
		if((document.frmCardholder.elements['CCType'].options[document.frmCardholder.elements['CCType'].selectedIndex].value == "Maestro") || (document.frmCardholder.elements['CCType'].options[document.frmCardholder.elements['CCType'].selectedIndex].value == "AMEX")) {
		document.getElementById("ccStartDate").style.display = "";
		document.getElementById("ccIssue").style.display = "";

	} else {
		document.getElementById("ccStartDate").style.display = "none";
		document.getElementById("ccIssue").style.display = "none";

	}

}

function check_cancel_form() {
	var errs = '<span class="alt"><b>Your details are not complete, please check the following: <br></b>';
	var error_state = '0';

	var emailFilter = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.([a-zA-Z]{2,4})$/;
	var email_1_check = '0';
	var email_2_check = '0';
	var email_1 = document.forms[0].email_address.value;
	var email_2 = document.forms[0].email_address_test.value;
	if (!email_1){ //check the first email box for content
		errs += "<br>Email address";
		error_state = '1';
	} else {
		if (!email_1.match(emailFilter)) { //check the first email box for a valid email address
			errs += "<br>Email address is not valid, please re-enter your email address";
			error_state = '1';
		} else {
			email_1_check = '1';
		}
	}

	if (!email_2){ //check second box for content
		errs += "<br>Confirmation email address";
		error_state = '1';
	} else {
		if (!email_2.match(emailFilter)) {  //check the second email box for a valid email address
			errs += "<br>Confirmation email address is not valid, please re-enter your email address";
			error_state = '1';
		} else {
			email_2_check = '1';
		}
	}

	//if email addresses are valid, check they match
	if(email_1_check == '1' && email_2_check == '1') {
		if(email_1 != email_2) {
			errs += "<br>Your email addresses do not match, please check you have entered both correctly";
			error_state = '1';
		} else {
			error_state = '0';
		}
	}

	if((error_state == '1')) {
		errs +="</span>";
		//document.getElementById("err").innerHTML = errs;
		document.getElementById("errs_bottom").innerHTML = errs;
		document.location.href="#a_errors";
		errs = "";
		return false;
	} else {
		errs = "";
		//document.getElementById("err").innerHTML = errs;
		document.getElementById("errs_bottom").innerHTML = errs;
		return true;
	}

	return true;

}

function check_cancelentry_form() {
	var errs = '<span class="alt"><b>Your details are not complete, please check the following: <br></b>';
	var error_state = '0';

	var refFilter 		= /^([a-zA-Z]{3})/;
	var numFilter		= /^([0-9]{1,8})/;
	var bookingBowl		= document.forms[0].bookingBowl.value;
	var bookingNum		= document.forms[0].bookingNum.value;
	var booking_day 	= document.forms[0].b_day.value;
	var booking_month 	= document.forms[0].b_month.value;
	var booking_year 	= document.forms[0].b_year.value;

	if(document.forms[0].surname.value == "") {
		errs += '<br>Please enter your surname';
		error_state = '1';
	}

	if((document.forms[0].bookingNum.value == "") || (document.forms[0].bookingBowl.value == "")) {
		errs += '<br>Please enter your booking reference in full';
		error_state = '1';
	} else {
		if (!bookingBowl.match(refFilter)) { //check the booking ref had 2 characters at the beginning ie LEE
				errs += "<br>Please enter only A-Z characters for the bokking reference prefix";
				error_state = '1';
		}
		if (!bookingNum.match(numFilter)) { //Check numerical part of booking reference ie 1234567890
				errs += "<br>Please enter numbers only the number part of your booking reference";
				error_state = '1';
		}
	}
	//Date validation : Check that date is in the future
	var d = new Date();
	var bookingDate = new Date();
	bookingDate.setFullYear(booking_year,booking_month-1,booking_day)
	if(d > bookingDate) {
			errs += "<br>Sorry, you cannot cancel bookings in the past";
			error_state = '1';
	}

	// Check that date selected is a valid date including leap years
	if ((booking_month==4 || booking_month==6 || booking_month==9 || booking_month==11) && booking_day==31) {
		errs += "<br>Please enter a valid date";
		error_state = '1';
	}
	if (booking_month == 2) { // check for february 29th
		var isleap = (booking_year % 4 == 0 && (booking_year % 100 != 0 || booking_year % 400 == 0));
		if (booking_day>29 || (booking_day==29 && !isleap)) {
			errs += "<br>Please enter a valid date";
			error_state = '1';
		}
	}




	if(error_state == '1') {
		errs +="</span>";
		document.getElementById("errs_bottom").innerHTML = errs;
		//document.location.href="#a_errors";
		errs = "";
		return false;
	} else {
		errs = "";
		document.getElementById("errs_bottom").innerHTML = errs;
		return true;
	}

}

function toggleClass(t_id, t_class) {
	var d = document;
	var c = toggleClass.arguments;
	d.getElementById(c[0]).className = t_class;
}


function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

