// BEGIN function to pre-validate the registration form
function validate_r(theForm) {

	var errorNum = -1; // No errors
	var errorText = new Array;
	errorText[0] = "You must enter a player name and email address to register."
	errorText[1] = "If you are a rookie please enter your profile information. The 'College(s)' field is optional. If you are not a rookie, please retype your player name, making sure it contains no extra spaces."
	errorText[2] = "You must provide a valid email address for every referral."
	errorText[3] = "You must provide a name for every referral."
	
	var nameVal = clearEmpty(theForm.elements['form_id'].value);
	var emailVal = clearEmpty(theForm.elements['email'].value);


	if (!nameVal) { 
		errorNum = 0; // User hasn't entered name
	} else {
		var nameArray = nameVal.split(" "); // See if full name is entered
		if (nameArray.length > 1) {
			var sexEl = theForm.elements['sex'];
			var locVal = clearEmpty(theForm.elements['location'].value);
			if (((!sexEl[0].checked) && (!sexEl[1].checked)) || (!locVal)) {
				errorNum = 1; // Rookie hasn't entered profile information
			}
		}
	}


		if (!emailVal) { 
		errorNum = 0; // User hasn't entered name
	} else {
		var nameArray = nameVal.split(" "); // See if full name is entered
		if (nameArray.length > 1) {
			var sexEl = theForm.elements['sex'];
			var locVal = clearEmpty(theForm.elements['location'].value);
			if (((!sexEl[0].checked) && (!sexEl[1].checked)) || (!locVal)) {
				errorNum = 1; // Rookie hasn't entered profile information
			}
		}
	}

// Check referral fields only if other errors don't exist and player is not a rookie
	if ((errorNum == -1) && (nameArray.length == 1)) {

		var errorEA, errorEP; 
		var rName, rEmail, vChar;
		
		for (var i = 1; i < 4; i++) {
			errorEA = true; // Email with no at sign keep testing until proven false
			errorEP = true; // Email with no period, keep testing until proven false
			rName = 'refername' + i;
			rEmail = 'refermail' + i;
			rName = clearEmpty(theForm.elements[rName].value);
			rEmail = clearEmpty(theForm.elements[rEmail].value);
			if (rEmail.length > 0) {
				for (var j = 0; j < rEmail.length; j++) {
					vChar = rEmail.charAt(j);
					if (vChar == "@") { errorEA = false; }
					if (vChar == ".") { errorEP = false; }
				}
				if ((errorEA) || (errorEP) || (rEmail.length < 5)) {
					errorNum = 2;
					break;
				} else if (rName.length == 0) {
					errorNum = 3;
				}
			}
		}
	}
	
	if (errorNum != -1) {
		alert (errorText[errorNum]);
		return false;
	} else {
		var joinEl = theForm.elements['joining'];
		var season = theForm.elements['season'].value;
		if (joinEl[0].checked) {
			writeCookie(season, nameVal, 30)
		} else {
			removeCookie(season);
		}
		prepCookie(nameVal);
		return true;
	}

} // END validate_r(theForm)


// BEGIN function to remove leading and trailing spaces from a value
function clearEmpty(val) {
	var cVal = ""
	var vChar;
	for (var i = 0; i < val.length; i++) {
		vChar = val.charAt(i);
		if ((vChar != " ") && (vChar != "\n") && (vChar != "\t")) { 
			cVal = val.substr(i);
			break;
		}
	}
	for (var i = val.length; i > 0; i--) {
		vChar = val.charAt(i-1);
		if ((vChar != " ") && (vChar != "\n") && (vChar != "\t")) { 
			cVal = val.substr(0,i);
			break;
		}
	}
	return cVal;
}// END clearEmpty(val)


