// JavaScript Document

//----------------------------------------------------------------------------------------
//  Prototype: bool validateEmail(chr email)
//
//  Description: Pass in an e-mail address you wish to validate.
//  Function Call: emailValid = validateEmail(document.forms[0].txtEmail.value);
//----------------------------------------------------------------------------------------
function validateEmail(email)
{
	if((email.indexOf("@") == -1) || (email.indexOf(".") == -1) || (email.indexOf(" ") != -1))
	{
		return false;
	}
	else
	{
		return true;
	}
}


//----------------------------------------------------------------------------------------
//  Prototype: bool validateCharacters(chr stringToValidate, chr charactersToCheckFor)
//
//  Description: Pass in a string you wish to validate as the first parameter,
//               and the allowed characters as the second parameter.
//  Function Call: phoneValid = validateCharacters(document.forms[0].txtPhone.value, "0123456789");
//----------------------------------------------------------------------------------------
function validateCharacters(stringToValidate, charactersToCheckFor)
{
	for(i=0;i<stringToValidate.length;i++)
	{
		if(charactersToCheckFor.indexOf(stringToValidate.charAt(i).toLowerCase()) == -1)
		{	
			//--- The -1 means that the number in (stringToValidate) was not found in (charactersToCheckFor).
			return false;
		}
	}
	
	//--- If we make it out of the for loop without having returned false, we know its safe to return true;
	return true;
}



//----------------------------------------------------------------------------------------
//  Prototype: bool validateForm()
//
//  Description: Function validates a form before submitting the form.
//  Function Call:  (To be placed in the HTML Form tag) onSubmit="return validateForm();"
//----------------------------------------------------------------------------------------
function validateForm()
{
	var returnValue;
	var errorMessage;
	var fieldToFocusOn;
	
	returnValue = true;
	fieldToFocusOn = "";	
	errorMessage = "Please fill in the following required fields:\n\n";


	if (document.forms[0].firstName.value == "")
	{
		errorMessage += "First Name\n";
		returnValue = false;
		
		if(fieldToFocusOn == "")
		{
			fieldToFocusOn = "firstName";
			document.forms[0].firstName.focus();
		}
	}
	
	
	if (document.forms[0].lastName.value == "")
	{
		errorMessage += "Last Name\n";
		returnValue = false;
		
		if(fieldToFocusOn == "")
		{
			fieldToFocusOn = "lastName";
			document.forms[0].lastName.focus();
		}
	}
	
	
	if (document.forms[0].company.value == "")
	{
		errorMessage += "Company Name\n";
		returnValue = false;
		
		if(fieldToFocusOn == "")
		{
			fieldToFocusOn = "company";
			document.forms[0].company.focus();
		}
	}
	
	
	if (document.forms[0].phone.value == "")
	{
		errorMessage += "Phone Number\n";
		returnValue = false;
		
		if(fieldToFocusOn == "")
		{
			fieldToFocusOn = "phone";
			document.forms[0].phone.focus();
		}
	}
	else
	{
		//--- Check the data to make sure it is in the correct format.
		phoneValid = validateCharacters(document.forms[0].phone.value, "0123456789-().");
		
		if(phoneValid != true)
		{
			errorMessage += "Phone Number (must conatin numerical data / no letters or special characters)\n";
			returnValue = false;
			
			if(fieldToFocusOn == "")
			{
				fieldToFocusOn = "phone";
				document.forms[0].phone.focus();
			}
		}
		
		
	}



	//--- Check Email Address.
	//--- This code is to be placed inside the validateForm function.
	//--- It also assumes that the text field that is being checked is named "emailAddress".
	if (document.forms[0].emailAddress.value == "")
	{
		errorMessage += "Email Address\n";
		returnValue = false;
		
		if(fieldToFocusOn == "")
		{
			fieldToFocusOn = "emailAddress";
			document.forms[0].emailAddress.focus();
		}
	}
	else
	{
		//--- Check the data to make sure it is in the correct format.
		emailValid = validateEmail(document.forms[0].emailAddress.value);
		
		if(emailValid != true)
		{
			errorMessage += "E-Mail Address (must be in proper email format, for example: name@company.com)\n";
			returnValue = false;
			
			if(fieldToFocusOn == "")
			{
				fieldToFocusOn = "emailAddress";
				document.forms[0].emailAddress.focus();
			}
		}
	}


	//--- Check to see if we submit the form or display an error -------------------------------------------
	if(returnValue == false)
	{
		alert(errorMessage);
		return false;
	}
	else
	{
		return true;
	}
	
}