
// ****************************
// Registration form validation
// Matt Millross
// 10th Feb 2003 || 14:06
// ****************************


//check mandatory fields for null input, not enough input, etc
function checkNulls(field){
if (field.value == "" || field.value == null)
	{
	alert("Mandatory fields MUST be entered\nPlease amend and retry") //message to alert user
	return false; //dont return
	}
return true; // return if we're ok
}

//check that email address is a proper email address
function checkEmailAddressValidity(email){

mailChars=/^[^@]+@[^@]+.[a-z]{2,}$/; //regexp thing

emailString=document.forms['registration'].email.value;

	// search email text for regular exp matches
	if ((emailString.search(mailChars)==-1) || emailString == "" || emailString == null || emailString.indexOf(".") == -1) 
	{
	alert("A valid e-mail address is required.\nPlease amend and retry");
	return false; //dont return
	}
return true; //return if we're ok
}
function checkEmailAddressValidity2(email){

mailChars=/^[^@]+@[^@]+.[a-z]{2,}$/; //regexp thing

emailString=document.forms['update'].email.value;

	// search email text for regular exp matches
	if ((emailString.search(mailChars)==-1) || emailString == "" || emailString == null || emailString.indexOf(".") == -1) 
	{
	alert("A valid e-mail address is required.\nPlease amend and retry");
	return false; //dont return
	}
return true; //return if we're ok
}

//check that passwords match
function checkPasswordMatch(p, p2){

pass = document.forms['registration'].password.value
pass2 = document.forms['registration'].passwordCheck.value

if (pass.length < 6 || pass2.length < 6)
	{
	alert("Password must be greater than 6 characters in length")
	return false;
	}

if (p.value != p2.value)
	{
	alert("The passwords entered do not match\nPlease amend and retry") //password dont match alert
	return false; //dont return
	}
return true; // return if we're ok
}

function checkPassword(p, p2){

pass = document.forms['update'].password.value
pass2 = document.forms['update'].password2.value

if (pass != "" && pass2 != "")
{
	if (pass.length < 6 || pass2.length < 6)
		{
		alert("Password must be greater than 6 characters in length")
		return false;
		}
	
	if (p.value != p2.value)
		{
		alert("The passwords entered do not match\nPlease amend and retry") //password dont match alert
		return false; //dont return
		}
}
return true; // return if we're ok
}

function checkUsername(){
// Check that only basic ASCII characters are in the username (0-127).

u = document.forms['registration'].username.value;

if ((u.indexOf(' ') !== -1) || u.length < 3 || u.length > 20)
	{
	alert("Username cannot contain spaces and must be 3-20 characters in length\nPlease amend and retry")
	return false;
	}
	
for (i=0; i<u.length; i++) 
	{
	if (u.charCodeAt(i)>127) 
		{
		alert("Entered username contains invalid characters.\nPlease amend and retry");
		return false;
	   	}
	}
return true;
}

//check the whole form against the functions
function checkItAll(form){
if (checkNulls(form.username)){
	if (checkNulls(form.password)){
		if (checkNulls(form.passwordCheck)){
			if (checkPasswordMatch(form.password, form.passwordCheck)){
				if (checkEmailAddressValidity(form.email)){
					if (checkUsername(form.username)){
						return true; //form submits if we're ok
						}
					}
				}
			}
		}
	}
return false; //dont do anything if one of them fails
}

function checkItAllScript(form){
if (checkNulls(form.title)){
	if (checkNulls(form.short1)){
		if (checkNulls(form.long1)){
			if (checkNulls(form.script)){
			return true; //form submits if we're ok
		}
	}
}
}
return false; //dont do anything if one of them fails
}

function checkItUpdateDetails(form){
if (checkPassword(form.password, form.password2)){
		if (checkEmailAddressValidity2(form.email)){
				return true; //form submits if we're ok
		}
	}
return false; //dont do anything if one of them fails
}
