//Form Validation
//Version 1.0
///////////////////////////////////////////////

//Make sure the following block is defined on the page
//using this script BEFORE you include this file
/*
var errorString = 'The form cannot be submitted due to the following errors:\n';

//The names (HTML names) of the fields that must contain valid E-mail addresses
var emailFields = new Array('email');

//The visible descriptions of the fields that must contain valid E-mail addresses
var emailFieldNames = new Array('E-mail');

//The names (HTML names) of the fields that cannot be blank
var notNullFields = new Array('name','myCircle','test','myChecks');

//The visible descriptions of the fields that cannot be blank
var notNullFieldNames = new Array('Name','What do you think?','Let us know what you think','Check a couple of these');

//If you wish to disable the submit button when the user clicks submit, set this value to true
var disableSubmit = true;

//If the previous value is true, enter the HTML name of the submit button to disable
var submitButtonName = 'submit';

*/



//Add the following to your <form> tag:
//   onSubmit="return ValidateForm(this);"

//DO NOT MAKE CHANGES BELOW THIS LINE
//---------------------------------------------------------------------------------------------------------

//GLOBALS
var errorMsg = '';

function IsValidEmailFormat(theValue)
{
	//assumes that there must be two characters before the @ symbol, and two after the .
	var indexOfAtSymbol = theValue.indexOf('@');
	var indexOfPeriod = (theValue.substring(indexOfAtSymbol,theValue.length)).indexOf('.');

	//If either character was not found
	if(indexOfAtSymbol == -1 || indexOfPeriod == -1)
		return false;

	//Convert the location of the period to its true position
	indexOfPeriod = indexOfPeriod + indexOfAtSymbol

	//if the there are not at least 2 characters before the @
	if(indexOfAtSymbol < 2)
		return false;

	//else if there are not at least 2 characters after the period
	else if((indexOfPeriod+2) >= theValue.length)
		return false;
	//Else the E-mail address is valid
	else 
		return true;
}



function ValidateEmailAddresses(theForm)
{
	var i=0;
	var valid = true;
	var theValue = "";

	for(i=0; i < emailFields.length; i++)
	{
		//alert('Field name: ' + emailFields[i]);
		if((theValue = theForm.elements[emailFields[i]].value) == '')
		{
			errorMsg = errorMsg + '- ' + emailFieldNames[i] + ' - this field requires a valid E-mail address\n';
			valid = false;
		}
		//else try to validate the Email
		else if(!IsValidEmailFormat(theValue))
		{
			errorMsg = errorMsg + '- ' + emailFieldNames[i] + ' - the address entered is invalid\n';
			valid = false;
		}
	}

	return valid;
}



function ValidateNonNullFields(theForm)
{
	var i=0, j=0;
	var valid = true;
	var checked;
	var obj;

	for(i = 0; i < notNullFields.length; i++)
	{
		//Select the current field
		obj = theForm.elements[notNullFields[i]];
		
		//if its a text box or memo box
		if(obj.type == 'text' || obj.type == 'textarea')
		{
			//if its NULL
			if(obj.value == '')
			{
				valid = false;
				errorMsg = errorMsg + '- ' + notNullFieldNames[i] + ' - You must enter a value\n';
			}
		}
		//else if its a menu
		else if(obj.type == 'select-one' || obj.type == 'select-multiple')
		{
			//if the there is nothing selected
			if(obj.selectedIndex == -1)
			{
				valid = false;
				errorMsg = errorMsg + '- ' + notNullFieldNames[i] + ' - You must select a value\n';
			}
			//Else if the selected value is invalid
			else if(obj.value == 'js_invalid')
			{
				valid = false;
				errorMsg = errorMsg + '- ' + notNullFieldNames[i] + ' - You must select a value\n';
			}
		}

		//if its a radio button or checkbox
		else
		{
			checked = false;

			//loop through all the radio buttons and find one that's checked
			for(j = 0; j < obj.length; j++)
			{
				if(obj[j].checked)
					checked = true;
			}

			//if no value was selected, its an error
			if(!checked)
			{
				valid = false;
				errorMsg = errorMsg + '- ' + notNullFieldNames[i] + ' - You must select a value\n';
			}
		}
	}
	return valid;
}



function ValidateForm(theForm)
{
	var valid = true;

	//if the user wishes to disable the submit button
	if(disableSubmit)
		theForm.elements[submitButtonName].disabled = true; 

	//Prepare the error msg, just incase there is an error
	errorMsg = errorString;

	//Validate all E-mail addresses
	if(!ValidateEmailAddresses(theForm))
		valid = false;
	
	//alert("validating");

	if(!ValidateNonNullFields(theForm))
		valid = false;

	if(!valid)
	{
		alert(errorMsg);
		if(disableSubmit)
			theForm.elements[submitButtonName].disabled = false;
		return false;
	}
	else
	{

		return true;
		theForm.elements[submitButtonName].disabled = false;
	}
}
