
/********************************************************/
// Validate contactus form
/********************************************************/
function ValidateContactUs() {
	var f = document.forms[0];
	if (f.name == "formCU") {
		// Contact details (check name, email)
		if (!ValidateContactDetails) {
			return false;
			}
		}
	else {
		alert("Invalid form reference");
		return false ;
		}
	return true;
	}


/********************************************************/
// Validate email form
/********************************************************/
function ValidateEmail() {
	var f = document.forms[0];
	if (f.name == "formEmail") {
		if (!ValidateContactDetails) {
			return false;
			}
		if (!checkRadio(f.AddRemove)) {
			alert("Please select either Add or Remove.");
			f.AddRemove[0].focus();
			return false;
			}
		}
	else {
		alert("Invalid form reference");
		return false ;
		}
	return true;
	}
	

/********************************************************/
// Validate tournament entry form
/********************************************************/
function ValidateEntry() {
	var f = document.forms[0];
	if (f.name == "formJ") {
		// ===========================
		// Junior Tournament
		// ===========================
		
		if (trim(f.PlayerName.value) == "") {
			alert("Please enter player's name.");
			f.PlayerName.focus();
			return false;
			}
		if (!checkRadio(f.BoyGirl)) {
			alert("Please select a tournament.");
			f.BoyGirl[0].focus();
			return false;
			}
		if (!checkDOB(f.PlayerDD.value,f.PlayerMM.value,f.PlayerYYYY.value)) {
			f.PlayerDD.focus();
			return false;
			}
		if (!checkAge(f.TournamentDate.value,f.PlayerDD.value,f.PlayerMM.value,f.PlayerYYYY.value,10,18)) {
			f.PlayerDD.focus();
			return false;
			}
		}
	else if (f.name == "formH") {
		// ===========================
		// Handicap Tournament
		// ===========================
		
		if (trim(f.Player1Name.value) == "") {
			alert("Please enter name for player 1");
			f.Player1Name.focus();
			return false;
			}
		if (f.Player1Club.value == "") {
			alert("Please enter player 1 club.");
			f.Player1Club.focus();
			return false;
			}
		if (trim(f.Player2Name.value) == "") {
			alert("Please enter name for player 2");
			f.Player2Name.focus();
			return false;
			}
		if (f.Player2Club.value == "") {
			alert("Please enter player 2 club.");
			f.Player2Club.focus();
			return false;
			}
		if (!checkRadio(f.TournType)) {
			alert("Please select a tournament.");
			f.TournType[0].focus();
			return false;
			}
		}
	else if (f.name == "formV") {
		// ===========================
		// Veterans Tournament
		// ===========================		
		
		// Man details
		if (trim(f.ManName.value) == "") {
			alert("Please enter man's name.");
			f.ManName.focus();
			return false ;
			}
		if (f.ManClub.value == "") {
			alert("Please enter man's club.");
			f.ManClub.focus();
			return false;
			}
		if (!checkDOB(f.ManDD.value,f.ManMM.value,f.ManYYYY.value)) {
			f.ManDD.focus();
			return false;
			}
		if (!checkAge(f.TournamentDate.value,f.ManDD.value,f.ManMM.value,f.ManYYYY.value,40,100)) {
			f.ManDD.focus();
			return false;
			}

		// Lady details
		if (trim(f.LadyName.value) == "") {
			alert("Please enter lady's name.");
			f.LadyName.focus();
			return false;
			}
		if (f.LadyClub.value == "") {
			alert("Please enter lady's club.");
			f.LadyClub.focus();
			return false;
			}
		if (!checkDOB(f.LadyDD.value,f.LadyMM.value,f.LadyYYYY.value)) {
			f.LadyDD.focus();
			return false;
			}
		if (!checkAge(f.TournamentDate.value,f.LadyDD.value,f.LadyMM.value,f.LadyYYYY.value,40,100)) {
			f.LadyDD.focus();
			return false;
			}
		}
	else if (f.name == "formD") {
		// ===========================
		// Divisional Tournament
		// ===========================	
		
		// Man details
		if (trim(f.ManName.value) == "") {
			alert("Please enter man's name.");
			f.ManName.focus();
			return false ;
			}
		if (f.ManClub.value == "") {
			alert("Please enter man's club.");
			f.ManClub.focus();
			return false;
			}
		
		// Lady details
		if (trim(f.LadyName.value) == "") {
			alert("Please enter lady's name.");
			f.LadyName.focus();
			return false;
			}
		if (f.LadyClub.value == "") {
			alert("Please enter lady's club.");
			f.LadyClub.focus();
			return false;
			}
		
		// Check tournament
		if (!checkRadio(f.Division)) {
			alert("Please select a division.");
			f.Division[0].focus();
			return false;
			}
		}
	else if (f.name == "formP") {
		// ===========================
		// PartnerSwap Tournament
		// ===========================
			
		// Man details
		if (trim(f.ManName.value) == "") {
			alert("Please enter man's name.");
			f.ManName.focus();
			return false ;
			}
		if (f.ManClub.value == "") {
			alert("Please enter man's club.");
			f.ManClub.focus();
			return false;
			}
		
		// Lady details
		if (trim(f.LadyName.value) == "") {
			alert("Please enter lady's name.");
			f.LadyName.focus();
			return false;
			}
		if (f.LadyClub.value == "") {
			alert("Please enter lady's club.");
			f.LadyClub.focus();
			return false;
			}
		}
	else {
		alert("Invalid form reference");
		return false ;
		}

	// Finally, contact details (name, email, phone)
	if (!ValidateContactDetails) {
		return false;
		}
	if (trim(f.ContactPhone.value) == "") {
		alert("Please enter a phone number.");
		f.ContactPhone.focus();
		return false;
		}
	return true;
	}


/********************************************************/
// Validate contactdetails
/********************************************************/
function ValidateContactDetails() {
	var f = document.forms[0];
	if (trim(f.ContactName.value) == "") {
		alert("Please enter a contact name.");
		f.ContactName.focus();
		return false;
		}
	if (!checkEmail(trim(f.ContactEmail.value))) {
		alert("Please enter a valid email address.");
		f.ContactEmail.focus();
		return false;
		}
	return true;
	}


/********************************************************/
// Trim blanks from string
/********************************************************/
function trim(str) {
	return str.replace(/^\s+|\s+$/g,'');
	}


/********************************************************/
// Check for a leap year
/********************************************************/
function LeapYear(pYear) {
	var LeapYear = (((pYear%4==0) && (pYear%100!=0)) || (pYear%400==0));
	return LeapYear;
	}



/********************************************************/
// Check for valid email address
/********************************************************/
function checkEmail(pEmail) {
	// Check for valid syntax using regexp pattern matching
	// OLD: var pattern=/[A-Za-z0-9._-]+@[a-zA-Z_\.]+?\.[a-zA-Z]{2,3}/
	var pattern = /[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)*@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+/
	if (!pattern.test(pEmail)) {
		return false;
		}
	else {
		return true;
		}
	}


/********************************************************/
// Check for a valid date of birth
/********************************************************/
function checkDOB(pDay,pMonth,pYear) {
	// Check for valid syntax using regexp pattern matching
	// OLD: var pattern2 = /\d\d/
	// OLD: var pattern4 = /\d\d\d\d/
	var pattern2 = /[0-9]{1,2}/
	var pattern4 = /[12]{1}[0-9]{3}/

	if (!pattern2.test(pDay) || !pattern2.test(pMonth) || !pattern4.test(pYear)) {
		alert("Invalid date entered - please try again.");
		return false;
		}

	// Store days of month in array (adjust if leap year)
	var days = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
	if (LeapYear(pYear)) days[1]=29;

	// Check for valid date
	if (pMonth<1 || pMonth>12 || pDay<1 || pDay>days[pMonth-1]) {
		alert("Invalid date entered - please try again.");
		return false;
		}
	return true;
	}


/********************************************************/
// Check eligibility for age-related tournaments
/********************************************************/
function checkAge(pTournamentDate,pDOB_Day,pDOB_Month,pDOB_Year,pMinAge,pMaxAge) {
	// Convert DOB to numeric string
	var iBirthDate = parseInt(pDOB_Year*10000)+parseInt(pDOB_Month*100)+parseInt(pDOB_Day);

	// Check that date entered is acceptable
	if (iBirthDate > pTournamentDate) {
		alert("You seem to be a figment of my future - please try again!");
		return false;
		}
	if (iBirthDate + parseInt(pMinAge*10000) > pTournamentDate) {
		alert("Sorry, you are too young for this tournament.");
		return false;
		}
	if (iBirthDate + parseInt(pMaxAge*10000) <= pTournamentDate) {
		alert("Sorry, you are too old for this tournament.");
		return false;
		}
	return true;
	}


/********************************************************/
// Check radio button has been selected
/********************************************************/
function checkRadio(pButton) {
	for (i=0; i<pButton.length; i++) {
		if (pButton[i].checked) break;
		}
	return (i<pButton.length);
	}


/********************************************************/
// Set savekey
/********************************************************/
function saveKeySet() {

	// No. of days since Jan 1st
	// NB. Javascript works in milliseconds
	var oneday=1000*60*60*24;

	var tday= new Date();
	tday.setHours(12);
	tday.setMinutes(0);
	tday.setSeconds(0);

	var jan1= new Date('January 1, '+tday.getFullYear());
	jan1.setHours(12);
	jan1.setMinutes(0);
	jan1.setSeconds(0);

	var f = document.forms[0];
	f.saveKey.value=Math.round(Math.abs(tday-jan1)/oneday);
	}

