// This function trims off leading and trailing spaces from strings.
/* This function is called by the onBlur event handler from the text inputs in the form.
	The fld.value recieved from the text input call is placed into the str variable.
	While there is a space at either end of the string the function trims one end or the
	other and places the new string into the variable str.
	The var str is then placed back into the fld.value. 
*/  
function trim(fld)   
	{ 	
	var str = fld.value     
	while (str.substr(0,1) == " " || str.substr(str.length - 1) == " ")       
		{       
		if (str.substr(0,1) == " ")         
			{         
			str = str.substr(1)         
			}       
		else         
			{         
			str = str.substr(0, str.length - 1)         
			}
		}
		fld.value = str
	}
	
// This function changes the initial character to uppercase.
/* This function is called by the onBlur event handler from the text inputs in the form. The fld.value recieved from the text input call is placed into the str variable. The string is then split into two new strings. the first string is the first character only and the second string is the remainder of the original string. The first string is then changed to uppercase. The two strings are then rejoined into the fld value. 
*/ 
function initial(fld)
	{
	var str=fld.value;
	var str1=str.substr(0,1);
	var str2=str.substr(1,str.length);
		str1 = str1.toUpperCase();
	fld.value= str1 + str2;
	}
	
//This function loops through each element in the form.
/* This function is called by the onClick event handler from the button 'Validater' in the form. The form elements array recieved from the button input function call is then looped through, and the form elements in turn call the fieldcheck function on themselves. 
*/
function validate(frm)
	{
	
	validatethefield(frm.elements.firstname);
	validatethefield(frm.elements.lastname);
	
	frm.elements.email_from_website.valid = false;
	frm.elements.phone.valid = false;
	frm.elements.email.valid = false;
	
	    if (frm.elements.phone.value == "" && frm.elements.email.value == "")
	    {
	    frm.elements.phone.valid = true;
	    frm.elements.email.valid = true;
	    frm.elements.email_from_website.valid = false;
	    }
	    else if (frm.elements.phone.value !== "" && frm.elements.email.value == "")
	    {
	    frm.elements.email_from_website.valid = true;
	    frm.elements.email.valid = true;
	    validatethefield(frm.elements.phone);
	    }
	    else if (frm.elements.phone.value == "" && frm.elements.email.value !== "")
	    {
	    frm.elements.email_from_website.valid = true;
	    frm.elements.phone.valid = true;
	    validatethefield(frm.elements.email);
	    }
	    else if (frm.elements.phone.value !== "" && frm.elements.email.value !== "")
	    {
	    frm.elements.email_from_website.valid = true;
	    validatethefield(frm.elements.phone);
	    validatethefield(frm.elements.email);
	    }
	    
		validatethefield(frm.elements.message);
		
		if (runreport(frm) == true)
		{
		return true;
		}
		else
		{
			return false;
		}
	}
//This function checks the field values for characters and syntax.	
/* This function is in the form of a switch. It switches the valid propery of the field upon which it has been called to true if the value comparisons performed by the regular expresions are true. To make the test certain the valid property is set to false before the comparisons begin. As the validate loop is calling the function on all the fields in the form elements array in turn, each will be compared to its own relavent regular expressions. For this reason the switch value is set to fld.name and contains many cases, one for each fld.value that is to be compared.*/	
function validatethefield(fld)
	{
	fld.valid = false;	
	switch (fld.name)
		{
//These cases have all been stacked, as they will all be compared to the same following regular expressions.
		case "firstname":
		case"lastname":
		
//These are the regular expressions that the fld.values will be compared to.
			var name1RegExp = /^[A-Z]{1}/i
			var name2RegExp = /[a-z]$/i
			var name3RegExp = /[\s\d!"£$%^&*()_+=-@:;'<>?#~\/]/			
//These are the comparisons taking place in an if statement.
				if(name1RegExp.test(fld.value) == true && name2RegExp.test(fld.value) == true && name3RegExp.test(fld.value) == false)
				{
//Here the valid property of the field is being switched to true. 'IF' the comparisons results are true.
				fld.valid = true;
				}
		break;
	
		case"phone":
			var phoneRegExp = /^[0-9]{5}/
			var phone1RegExp = /[0-9]{6}$/
				if(phoneRegExp.test(fld.value) == true && phone1RegExp.test(fld.value) == true)
				{
				fld.valid = true;
				}			
		break;
		
		case"email":
			var email1RegExp = /[@]{1}/
			var emai12RegExp = /.co.uk|.aero|.arpa|.biz|.cat|.com|.coop|.edu|.info|.int|.jobs|.mobi|.museum|.name|.net|.org|.pro|.travel|.eu$/ 
				if(email1RegExp.test(fld.value) == true && emai12RegExp.test(fld.value) == true)
				{
				fld.valid = true;
				}			
		break;
		
		case"message":
			
				if (fld.value.length > 1)
				{
				fld.valid = true;
				}			
		break;
		
		}
	}
	

	
//This function checks the valid property of each field after the comparisons have been made in function fieldcheck.
/* This function is called by the onClick event handler of the 'validater' button. It is called after the validate function. It uses an if else statement to see if the named fields have a valid property of false. If the valid property of the field is false, an alert box is displayed to inform the user of an invalid enrty in the form and the focus is placed in the offending input. Should all the fields valid propertys be true the function will reach its end which is the else part. This displays an alert box informing the user that (as all fields are valid) the form is valid and they may procced to submit the form. The value of the submit button is also changed to 'Submit Now' as up to now its value has been 'Disabled'.
 */	
	
function runreport(frm)
	{
	
	var alertheader =	"=================================\n"+
						"| THERE IS AN ERROR |\n"+
						"=================================\n"						
	var alertfooter =   "=================================\n"	
	
	if(frm.firstname.valid== false)
		{
		alert(alertheader + "| First Name |\n"+							
							"Please make an entry.\n"+
							"Enter only one name. Use letters only and no spaces.\n" + alertfooter);
		frm.firstname.focus();
		}	
	else if(frm.lastname.valid== false)
		{
		alert(alertheader + "| Last Name |\n"+							
							"Please make an entry.\n"+
							"Enter only one name. Use letters and no spaces.\n" + alertfooter);
		frm.lastname.focus();
		}
	else if(frm.email_from_website.valid == false)
		{
		alert(alertheader + "| Telephone Or Email |\n"+
							"Please enter either a phone number or email.\n"+ alertfooter);
		frm.email.focus();
		}	
	else if(frm.phone.valid== false)
		{
		alert(alertheader + "| Telephone |\n"+
							"You have chosen to enter a phone number.\n"+
							"Enter 5 numbers, a space then 6 numbers.\n"+ alertfooter);
		frm.phone.focus();
		}	
	else if(frm.email.valid== false)
		{
		alert(alertheader + "| E-Mail |\n"+
							"You have chosen to enter an Email.\n"+
							"Use letters and numbers\n"+
							"and include '@' and '.com' Or your relevent suffix\n"+ alertfooter);
		frm.email.focus();
		}
	else if(frm.message.valid== false)
		{
		alert(alertheader + "| Message |\n"+
							"Please make an entry.\n"+
							"After all the message is the whole point.\n"+ alertfooter);
		frm.message.focus();
		}		
	else
		{
			return true;
		}
	}
