
	function funEmailIsValid(strEmail) {

		// return either true or false
		var blnEmail = true;
		strEmail = new String(strEmail);

		lblEmailCheck: {

			// is this empty?
			if (strEmail == "") {
				// leave as valid
				break lblEmailCheck;
			}
			var intLen = strEmail.length;

			// check '@ sign
			var intOffA = strEmail.indexOf("@");
			if (intOffA == intLen-1 || intOffA <= 0) {
				blnEmail = false;
				break lblEmailCheck;
			} 
			// check full-stop
			var intOffP = strEmail.indexOf(".",intOffA+1);
			if (intOffP == -1 || intOffP == intLen-1) {
				blnEmail = false;
				break lblEmailCheck;
			}
			// check ASCII codes 97->122, 48->57, 64 or 95 OR 46 or 39
			strEmail = strEmail.toLowerCase();
			for (var i=0; i<intLen; i++) {
				var intAscii = strEmail.charCodeAt(i);
				if ((intAscii < 48 && intAscii != 45 && intAscii != 46 && intAscii != 39) || (intAscii == 32) || (intAscii > 122) || (intAscii > 57 && intAscii < 97 && intAscii != 64 && intAscii != 95)) {
					blnEmail = false;
					break lblEmailCheck;
				}
			}
		}

		return blnEmail;	
	}

	var popUpID = null;

	function getPopUp(strFileName, intWidth, intHeight) {
		if (intWidth == null) intWidth = 500;
		if (intHeight == null) intHeight = 475;
		var featuresString = "height=" + intHeight + ",width=" + intWidth + ",scrollbars=yes,resizable=yes";
		if (popUpID && !popUpID.closed && popUpID.close) popUpID.close();
		popUpID = window.open(strFileName, "PopUp", featuresString);
		popUpID.focus();
		return popUpID;
	}

	function doPopUp(strFileName, intWidth, intHeight) {
		var tempID = getPopUp(strFileName, intWidth, intHeight);
	}

	function imagePopUp(strFileName, intWidth, intHeight) {
		var newWin = getImagePopUp(strFileName, intWidth, intHeight);
		newWin.focus();
	}

	function getImagePopUp(strFileName, intWidth, intHeight) {
		if (intWidth == null) { intWidth = 500; }
		if (intHeight == null) { intHeight = 475; }
		featuresString = "height=" + intHeight + ",width=" + intWidth + ",scrollbars=no,resizable=yes"
		var objWin = window.open(strFileName, "PopUp", featuresString);
		objWin.focus();
		return objWin;
	}



	function funDateIsValid(strDate) {
		//check string, return true if date, false if not
		strDate 	= new String(strDate);
		var fldlen 	= strDate.length;
		var blnDate = true;

		lblDateCheck: {
			if (strDate == "") {
				break lblDateCheck;
			}

			if (fldlen != 10) {
				blnDate = false;
				break lblDateCheck;
			}
			var fldday 		= 0;
			var fldmon 		= 0;
			var fldyear 	= 0;
			var monthvalid 	= new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
			fldday 			= new Number(strDate.substring(0, 2));
			fldmon 			= new Number(strDate.substring(3, 5));
			fldyear 		= new Number(strDate.substring(6,10));
		
			if (fldyear < 1895) {
				blnDate = false;
				break lblDateCheck;
			}
			if (fldmon < 1 || fldmon > 12) {
				blnDate = false;
				break lblDateCheck;
			}
			var numyear = fldyear / 4;
			var numyearr = Math.round(numyear);
			if (numyear == numyearr){ 
				monthvalid[1] = 29;
			}
			if (fldday < 1 || fldday > eval(monthvalid[fldmon - 1])) {
				blnDate = false;
				break lblDateCheck;
			}
		}

		return blnDate;
	}

	function validateUserInput(intFormType) {
		var blnValid = true; var strError = "";
		var f = document.forms[0];
		var codeGuest = "33";
		var codeCandidate = "34";
		var codeFull = "35";
		var codeAdmin = "36";

		switch (intFormType) {


		case 1:			//editing player
			//surname and firstname may not be blank
			if (f.Firstname.value == "" || f.Surname.value == "") {
				blnValid = false;
				strError += "\nFirstname and surname are compulsory fields";
			}

			//email must be valid
			var strEmail = f.Email.value;
			if (funEmailIsValid(strEmail)==false) {
				blnValid = false;
				strError += "\nInvalid email address";
			}

			//DOB must be valid
			var strDOB = f.DOB.value;
			if (funDateIsValid(strDOB)==false) {
				blnValid = false;
				strError += "\nInvalid date of birth, please enter as dd/mm/yyyy";
			}

			break;


		case 2:			//changing password
			//check old and new are both filled in, and that password correctly repeated
			if (f.currentPassword.value == "" || f.newPassword.value == "") {
				blnValid = false;
				strError += "\nPlease enter both your current and a new password";
			} else if (f.repeatPassword.value != f.newPassword.value) {
				blnValid = false;
				strError += "\nPlease re-enter your new password, they do not match";
			}
			break;


		case 3:			// admin details
			//if new, must have entered surname and firstname
			if ((f.playerNew.value == "True") && (f.Firstname.value == "" || f.Surname.value == "")) {
				blnValid = false;
				strError += "\nFirstname and surname are compulsory fields for a new player";
			}
			//must have selected player type
			var intPlayerType = f.PlayerRef.value;
			if (intPlayerType == "0" || intPlayerType == "") {
				blnValid = false;
				strError += "\nPlease select what type of member this is";
			} else {
				//check player type
				var checkChange = false;
				if ((intPlayerType == codeGuest) && (f.origPlayerRef.value != codeGuest)) {
					checkChange = confirm("Saving a member as type 'Guest' will remove any club specific data\ni.e. Position, match manager and webmaster\n\nOkay to continue?");
					if (checkChange == false) {
						blnValid = false;
						strError += "\nUpdate cancelled by user";
					}
				}
			}
			break;


		case 4:		// sending email to player
			if (f.Subject.value == "" || f.Message.value == "") {
				blnValid = false;
				strError += "\nSubject and message are both compulsory to send an email";
			}
			break;


		case 5:		//sending personal details to webmaster for login help	
			if (f.YourName.value == "" || f.YourEmail.value == "") {
				blnValid = false;
				strError += "\nName and Email are compulsory fields";
			}
			if (funEmailIsValid(f.YourEmail.value) == false) {
				blnValid = false;
				strError += "\nInvalid email address";
			}
			break;


		case 6:		//search form
			if (f.Surname.value == "" && f.Firstname.value == "" && f.selGA.value == "" && f.selPT.value == "" && f.selYear.value == "") {
				blnValid = false;
				strError += "\nPlease enter at least one criteria to search by";
			}
			break;

		default:
			blnValid = false;
			strError += "\nUnknown form type of " + intFormType + ", so failing validation";
		}


		if (blnValid) {
			return true;
		} else {
			alert("DATA ERROR\n" + strError);
			return false;
		}

	}


