<!--

// isLetter (c)
// isDigit (c)
// isEmpty(s)
// isWhitespace (s)
// isAlphanumeric (s)
// isEmail (s)
// validate_length (s, len)
// validate_contact(f)
// validate_registration(f)
// validate_login(f)


function isLetter (c)
{   
	return ( ((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z")) )
}

function isDigit (c)
{   
	return ((c >= "0") && (c <= "9"))
}

function isEmpty(s)
{   
	// Check whether string s is empty.
	return ((s == null) || (s.length == 0))
}

function isWhitespace (s)
{   
	// Returns true if string s is empty or 
	// whitespace characters only.
	var i;
	// whitespace characters
	var whitespace = " \t\n\r";
    // Is s empty?
    if (isEmpty(s)) return true;

    // Search through string's characters one by one
    // until we find a non-whitespace character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (whitespace.indexOf(c) == -1) return false;
    }

    // All characters are whitespace.
    return true;
}

function isAlphanumeric (s)
{   
	var i;
    // Search through string's characters one by one
    // until we find a non-alphanumeric character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number or letter.
        var c = s.charAt(i);
        if (! (isLetter(c) || isDigit(c) ) ) return false;
    }

    // All characters are numbers or letters.
    return true;
}

function isEmail (s)
{   
	// Email address must be of form a@b.c -- in other words:
	// * there must be at least one character before the @
	// * there must be at least one character before and after the .
	// * the characters @ and . are both required
	//
	// For explanation of optional argument emptyOK,
	// see comments of function isInteger.
   
    // is s whitespace?
	// cant get this to work so comment out
    // if (isWhitespace(s)) return false;
    
    // there must be >= 1 character before @, so we
    // start looking at character position 1 
    // (i.e. second character)
    var i = 1;
    var sLength = s.length;

    // look for @
    while ((i < sLength) && (s.charAt(i) != "@"))
    { 
		i++;
    }

    if ((i >= sLength) || (s.charAt(i) != "@")) 
	{
		return false;
	}
    else 
	{
		i += 2;
	}
	
    // look for .
    while ((i < sLength) && (s.charAt(i) != "."))
    { 
		i++;
    }

    // there must be at least one character after the .
    if ((i >= sLength - 1) || (s.charAt(i) != ".")) 
	{
		return false;
	}
    else 
	{
		return true;
	}
}

function validate_length (s, len)
{
	var sLength = s.length;
	
	if (sLength > len)
	{
		return false;
	}
	else
	{
		return true;
	}
}
 

function validate_contact(f)
{
	var msg;
	var empty_fields = "";
	var errors = "";
	
	for(var i=0;i < f.length; i++) 
	{
		var e = f.elements[i];		
		if ( (e.value == null) || (e.value == "") ) 
		{
			empty_fields += "\n              " +e.name;
			continue;
		}
	}
	
	msg = "_______________________________________________________________\n\n";
	msg += "The form was not submitted because of the following error(s). \n";
	msg += "Please correct these error(s) and re-submit.\n";
    msg += "_______________________________________________________________\n\n";
	
	
	if ( !(isEmail (f.email.value)) )
	{
		errors += "invalid email";
		msg += "- You must enter a valid email address\n\n";
	}
	
	if (empty_fields || errors)  
	{
		if (empty_fields)
		{
			msg += "- The following required field(s) are empty:" + empty_fields + "\n";
		}
		alert(msg);
		return false;
	}
	
	
}

function validate_nutrition(f)
{
	var msg;
	var empty_fields = "";
	var errors = "";
	
	for(var i=0;i < f.length; i++) 
	{
		var e = f.elements[i];		
		if ( (e.value == null) || (e.value == "") ) 
		{
			empty_fields += "\n              " +e.name;
			continue;
		}
	}
	
	msg = "_______________________________________________________________\n\n";
	msg += "The form was not submitted because of the following error(s). \n";
	msg += "Please correct these error(s) and re-submit.\n";
    msg += "_______________________________________________________________\n\n";
	
	
	if ( !(isEmail (f.email.value)) )
	{
		errors += "invalid email";
		msg += "- You must enter a valid email address\n\n";
	}
	
	if (empty_fields || errors)  
	{
		if (empty_fields)
		{
			msg += "- The following required field(s) are empty:" + empty_fields + "\n";
		}
		alert(msg);
		return false;
	}
	
	
}

function validate_registration(f) {
	var msg;
	var empty_fields = "";
	var errors = "";

	
	if (f.firstname.value == null || f.firstname.value == "") {
		empty_fields += "\n first name";
	}
	if (f.lastname.value == null || f.lastname.value == "") {
		empty_fields += "\n last name";
	}
	if (f.address1.value == null || f.address1.value == "") {
		empty_fields += "\n address line one";
	}
	if (f.town.value == null || f.town.value == "") {
		empty_fields += "\n town";
	}
	if (f.county.value == null || f.county.value == "") {
		empty_fields += "\n county";
	}
	if (f.country.value == null || f.country.value == "") {
		empty_fields += "\n country";
	}
	if (f.postcode.value == null || f.postcode.value == "") {
		empty_fields += "\n post code";
	}
	
	msg = "_______________________________________________________________\n\n";
	msg += "The form was not submitted because of the following error(s). \n";
	msg += "Please correct these error(s) and re-submit.\n";
    msg += "_______________________________________________________________\n\n";
	
	if (f.username.value.length > 32) {
		errors += "username too big";
		msg += "- The username is greater than 32 characters\n";
	}
	
	if (f.username.value.length < 5) {
		errors += "username too small";
		msg += "- The username is less than 5 characters long\n";
	}
	
	if ( !(isAlphanumeric (f.username.value)) )
	{
		errors += "username not alphanumeric";
		msg += "- The username contains non alphanumeric character(s) \n";
	}
	
	if (f.password.value.length > 32) {
		errors += "password too big";
		msg += "- The username is greater than 32 characters\n";
	}
	
	if (f.password.value.length < 5) {
		errors += "password too small";
		msg += "- The password is less than 5 characters long\n";
	}
	
	if ( !(isAlphanumeric (f.password.value)) )
	{
		errors += "username not alphanumeric";
		msg += "- The password contains non alphanumeric character(s) \n";
	}
	
	if ( !(isEmail (f.email.value) ) )
	{
		errors += "invalid email";
		msg += "- You must enter a valid email address\n";
	}
	
	if (empty_fields || errors)  {
		if (empty_fields)
		{
			msg += "- The following required field(s) are empty:" + empty_fields + "\n";
		}
		alert(msg);
		return false;
	}
	
}


function validate_ppp(f)
{
	var msg;
	var empty_fields = "";
	var errors = "";
	var mem_num = f.ppp_membership_number.value;
	var grp_num = f.ppp_group_number.value;
	//if ( ( (mem_num == "") || (mem_num == null) ) && ( (grp_num == "") || (grp_num == null) ) )
	if ( isEmpty(mem_num) && isEmpty(grp_num) )
	{
		empty_fields += "\n empty error";
	}

	msg = "_______________________________________________________________\n\n";
	msg += "The form was not submitted because of the following error(s). \n";
	msg += "Please correct these error(s) and re-submit.\n";
    msg += "_______________________________________________________________\n\n";
	
	if ( !isEmpty(mem_num) )
	{
		for (var j = 0; j < 7; j++)
	    {   
	        // Check that current character is number.
	        var c = f.ppp_membership_number.value.charAt(j);
	        if (! isDigit(c) )
			{
				errors += "no digits";
			}
		}
		
		var char8 = f.ppp_membership_number.value.charAt(7);
	    if (! (isLetter(char8) ) )
		{
			errors += "no letter";
		}
			
		if (f.ppp_membership_number.value.length != 8) 
		{
			errors += "too long";
		}
	}
	
	if ( !isEmpty(grp_num) )
	{
		for (var j = 0; j < 4; j++)
	    {   
	        // Check that current character is number.
	        var c = f.ppp_group_number.value.charAt(j);
	        if (! isDigit(c) )
			{
				errors += "no digits";
			}
		}
			
		if (f.ppp_group_number.value.length != 5) 
		{
			errors += "too long";
		}
	}
	
	if (empty_fields || errors)  
	{
		if (empty_fields)
		{
			//msg += "- The following required field(s) are empty:" + empty_fields + "\n";
			msg += "- You must enter a Member or Group number\n";
		}
		
		if (errors)
		{
			msg += "- That is not a valid PPP membership number\n";
		}
		
		alert(msg);
		return false;
	}

}


function validate_login(f)
{
	var msg;
	var empty_fields = "";
	var errors = "";
	
	for(var i=0;i < f.length; i++) 
	{
		var e = f.elements[i];		
		if ( (e.value == null) || (e.value == "") ) 
		{
			empty_fields += "\n              " +e.name;
			continue;
		}
	}
	
	msg = "_______________________________________________________________\n\n";
	msg += "The form was not submitted because of the following error(s). \n";
	msg += "Please correct these error(s) and re-submit.\n";
    msg += "_______________________________________________________________\n\n";
	
	
	if (f.username.value.length > 32) {
		errors += "username too big";
		msg += "- The username is greater than 32 characters\n";
	}
	
	if (f.username.value.length < 5) {
		errors += "username too small";
		msg += "- The username is less than 5 characters long\n";
	}
	
	if ( !(isAlphanumeric (f.username.value)) )
	{
		errors += "username not alphanumeric";
		msg += "- The username contains non alphanumeric character(s) \n";
	}
	
	if (f.password.value.length > 32) {
		errors += "password too big";
		msg += "- The username is greater than 32 characters\n";
	}
	
	if (f.password.value.length < 5) {
		errors += "password too small";
		msg += "- The password is less than 5 characters long\n";
	}
	
	if ( !(isAlphanumeric (f.password.value)) )
	{
		errors += "username not alphanumeric";
		msg += "- The password contains non alphanumeric character(s) \n";
	}
	
	if (empty_fields || errors)  
	{
		if (empty_fields)
		{
			msg += "- The following required field(s) are empty:" + empty_fields + "\n";
		}
		alert(msg);
		return false;
	}
	
	
}

//-->
