<!--

// Form Global Variables
var alertErrorMess   = new Boolean(true);
var lastErrorMessage = new String("");

// Valid Char Declerations
var ALPHA_LOWER     = 1;
var ALPHA_UPPER     = 2;
var NUMERIC         = 3;
var SPACE           = 4;
var HYPHEN          = 5;
var PLUS            = 6;
var BRACKETS        = 7;
var ANGLED_BRACKETS = 8;
var APOSTROPHE      = 9;
var INVERTED_COMMA  = 10;
var COMMA           = 11;
var PERIOD          = 12;

var validCharsArray = new Array();
validCharsArray[ALPHA_LOWER]     = "abcdefghijklmnopqrstuvwxyz";
validCharsArray[ALPHA_UPPER]     = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
validCharsArray[NUMERIC]         = "0123456789";
validCharsArray[SPACE]           = " ";
validCharsArray[HYPHEN]          = "-";
validCharsArray[PLUS]            = "+";
validCharsArray[BRACKETS]        = "()";
validCharsArray[ANGLED_BRACKETS] = "<>";
validCharsArray[APOSTROPHE]      = "'"; 
validCharsArray[INVERTED_COMMA]  = "\""; 
validCharsArray[COMMA]           = ",";
validCharsArray[PERIOD]          = ".";

// <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
//                                                      Initialisation Functions
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
// Set Alert Error Message
function setAlertErrorMessageVar(setValue)
{
	alertErrorMess = setValue;
}



// <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
//                                                             General Functions
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
// Display Error Message
function displayErrorMessage()
{
	if (alertErrorMess)
		alert(lastErrorMessage);
}

// Set cursor to go to a certian part of the form
function goToFormField(formElement, focusOnField, selectField)
{
	if (focusOnField)
		formElement.focus(); 
	
	if (selectField)
		formElement.select(); 
}



// <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
//                                              Main String Validation Functions
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
// Determines whether a string conists of completely legal chars
function doesStringConsistOfValidChars(str, validChars)
{
	// Iterates through string to see if any character does not exist in the 
	// predefined string validChars
	for (var i=0; i<str.length; i++)
	{
		if (validChars.indexOf(str.charAt(i))==(-1))
			return false;
	}
	
  return true;
}

// Validate a string against a list of char types 
function isFieldValid(stringToValidate, okCharsArray, fieldName)
{
	var comleteValidationList = new String("");
	
	// Create valid character list 
	for(var i=0; i<okCharsArray.length; i++)
		comleteValidationList += validCharsArray[(okCharsArray[i])];
	
	if (!doesStringConsistOfValidChars(stringToValidate, comleteValidationList))
	{
		lastErrorMessage =   fieldName 
											 + " can only consist from a combination of the following characters:\n\n"
											 + comleteValidationList;		
		return false;
	}
		
	return true;
}



// <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
//                                                  Other String Check Functions
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
// Checks to see if fields are empty
function isFieldEmpty(fieldEntry, errMess)
{
	if (fieldEntry == "")
	{
   		lastErrorMessage = "All details must be completed !\n\nPlease enter " + errMess + ".";
			return true;
	}
	
	return false;
}

////////////////////////////////////////////////////////////////////////////////
// create error string for incorrect email address
function createErrorMessForInvalidEmail(emailAdd, fieldName, errNum, i, errMess)
{
	lastErrorMessage =   fieldName + "is invalid!\n"
	                   + "The email address \"" 
										 + emailAdd + "\" " + errMess + "."
										 + "\n\n"
										 + "Technical info: Error Number " + errNum + " at character location " + i;   
}

// Function to check that an email entered contains a @ and a .
function isStringEmailAddress(email, fieldName)
{
	// Check that email address consist of @ and at least one .
	if (email.indexOf('@',0)==-1)
	{	
		createErrorMessForInvalidEmail(email, fieldName, 1, 0, " does no contain @");
		return false;
	}
	
	// Check that email address consist of @ and at least one .
	if (email.indexOf('.',0)==-1)
	{
		createErrorMessForInvalidEmail(email, fieldName, 2, 0, " does no contain a period (.)");
		return false;
	}	
	
	var i;
	var countBefore;	
		
 	// count up to @
	for(i=0, countBefore=0; email.charAt(i)!='@'; i++, countBefore++)
	{}

	// no characters before @
	if (countBefore == 0)
	{
 		createErrorMessForInvalidEmail(email, fieldName, 3, i, 
																	"does no contain any characters before @");
		return false;
	}
	
	countBefore=0;
	
	// Check validity after @
	for(i=i+1; i<email.length; i++)
	{
		// if space return error
		if (email.charAt(i)==' ')
		{
 			createErrorMessForInvalidEmail(email, fieldName, 4, i, "contains spaces");			
			return false;
		}
		
		// if period
		else if (email.charAt(i)=='.')
		{
			// no chars before .
			if (countBefore == 0)
			{
 				createErrorMessForInvalidEmail(email, fieldName, 5, i, 
																			 "does not contain any chars before a specific period");					
				return false;
			}
			// if period is encounterd then set countBefore back to zero 
			countBefore=0;
		}

		// if not digit or alphabetical character or hyphen			
		else if (!isFieldValid(email.charAt(i), Array(NUMERIC, ALPHA_LOWER, ALPHA_UPPER, HYPHEN), 
				"Email Address"))	
		{
			createErrorMessForInvalidEmail(email, fieldName, 6, i, 
																		 "consists of illegal characters");	
			return false;
		}	
		
		// check that first char is not a hyphen after @ or .
		else if ((countBefore==0) && (email.charAt(i)=='-'))
		{
 			createErrorMessForInvalidEmail(email, fieldName, 7, i, 
																		 "contains an illegal hyphen");			
			return false;
		}
		
		else
			countBefore++;
   }

	// no chars after last . (i.e. Email Address Can't finish with a period)
	if (countBefore == 0)
	{
 		createErrorMessForInvalidEmail(email, fieldName, 8, i, 
																	 "finishes with a period. Remove the last character of "+fieldName);				
		return false;
	}
	// last char is a hyphen
	if ((email.charAt(i-1)=='-'))
	{	
 		createErrorMessForInvalidEmail(email, fieldName, 9, i, 
																	 "finishes with a hyphen. Remove the last character of "+fieldName);			
		return false;
	}
	
   return true;
}

function isStringAlphaHyphenCommaApostrophePeriodSpace(str, fieldName)
{
	if (!isFieldValid(str, 
									 Array(ALPHA_LOWER, ALPHA_UPPER, HYPHEN, PERIOD, COMMA, SPACE, APOSTROPHE), 
									 fieldName))
		return false;
	
	return true;
}
////////////////////////////////////////////////////////////////////////////////

// Does string comprise of all legal telephone characters 
function isStringTelNum(str, fieldName)
{
	if (!isFieldValid(str, Array(NUMERIC, HYPHEN, SPACE, BRACKETS), fieldName))
		return false;
	
	return true;
}

// Check first character is an alphabetical character
function isFirstCharAlpha(str, fieldName)
{
	if (!isFieldValid(str.charAt(0), Array(ALPHA_LOWER, ALPHA_UPPER), fieldName))
	{
		lastErrorMessage = "The first character in your " + fieldName + " must be an alphabetical character.";
		return false;
	}

	return true;
}

// Checks that string is big enough 
function isStringBigEnough(str, fieldName, minLength)
{
	if (str.length < minLength)
	{	
		lastErrorMessage =   fieldName + " must consist of at least " + minLength
		                   + " characters";
		return false;
	}
	return true;
}



// <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
//                                                  Main Form Checking Functions
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
function hasFormFieldBeenFilledIn(formElement, fieldName, minLength)
{
	// If form element is empty
	if (isFieldEmpty(formElement.value, fieldName))
	{
		displayErrorMessage();	
		goToFormField(formElement, true, false);
		return false;
	}
	
	// Ensures that form element has enough characters 
	if (!isStringBigEnough(formElement.value, fieldName, minLength))
	{
		displayErrorMessage();	
		goToFormField(formElement, true, false);
		return false;	
	}	
	
	return true;
}

function isFormContactNameValid(formElement, fieldMustExist, fieldName)
{
	// if - field does not need to be filled in and is also empty  
	if ( (fieldMustExist == false) && (isFieldEmpty(formElement.value, fieldName)) )
		return true; 
	
	// form element contains something (whether it must exist or not) 
	
	// is form field empty
	if (!hasFormFieldBeenFilledIn(formElement, fieldName, 2))
		return false;
	
	// is first character in form field a non alphabetical char
	if (!isFirstCharAlpha(formElement.value, fieldName))
	{
		displayErrorMessage();
		goToFormField(formElement, true, true);
		return false;
	}

	// Does contact name consist of all legal chars
	if (!isStringAlphaHyphenCommaApostrophePeriodSpace(formElement.value, fieldName))
	{
		displayErrorMessage();
		goToFormField(formElement, true, true);
		return false;	
	}
	
	return true;
}

function isFormEmailAddressValid(formElement, fieldMustExist, fieldName)
{
	// if - field does not need to be filled in and is also empty  
	if ( (fieldMustExist == false) && (isFieldEmpty(formElement.value, fieldName)) )
		return true; 

	// form element contains something (whether it must exist or not) 	
	
	// is form field empty
	if (!hasFormFieldBeenFilledIn(formElement, fieldName, 5))
		return false;
		
	if (!isStringEmailAddress(formElement.value, fieldName))
	{						 
		displayErrorMessage();
		goToFormField(formElement, true, true);
		return false;
	}
	
	return true;
}

function isFormTelNumValid(formElement, fieldMustExist, fieldName)
{
	// if - field does not need to be filled in and is also empty  
	if ( (fieldMustExist == false) && (isFieldEmpty(formElement.value, fieldName)) )
		return true; 

	// form element contains something (whether it must exist or not) 	
	
	// is form field empty
	if (!hasFormFieldBeenFilledIn(formElement, fieldName, 4))
		return false;
		
	if (!isStringTelNum(formElement.value, fieldName))
	{						 
		displayErrorMessage();
		goToFormField(formElement, true, true);
		return false;
	}
	
	return true;
}
//-->