// JavaScript Document
<!--
/*************************************************************
Company:Daniel Burton Dean | DBD
Author:Saurabh Ajmera
Email:sajmera@dbdnet.com
**************************************************************/
//Global Variables
//var fieldNameAlt;

function validatePhoneNumber(elementValue)
//this function will validate a US phone number against a regular expression
{  
	 
	 var phoneNumberPattern = /^\(?(\d{3})\)?[- ]?(\d{3})[- ]?(\d{4})$/;  
	 return phoneNumberPattern.test(elementValue);  
} 

function validateAgencyCode(elementValue)
//this function will validate a agency code against a regular expression
{
	var agencyCodeRegEx=/^\d{1,4}$/;
	return agencyCodeRegEx.test(elementValue);
}  

function validateAgencyName(elementValue)
//this function will validate a agency name against a regular expression
{
	var agencyNameRegEx = /(!|@|\$|%|\^|\*|=|\+|\{|\}|\[|\]|\?)+/g;
	return (!agencyNameRegEx.test(elementValue));
}

function validateName(elementValue)
//this function will validate a persons name against a regular expression
{

	var NameRegEx = /([0-9]|!|@|#|\$|%|\^|&|\*|=|\+|\{|\}|\[|\]|\?)+/g;
	return (!NameRegEx.test(elementValue));
}

function validateEmail(elementValue)
//this code will validate an email address against a regular expression
{
	var EmailRegEx = /^([a-zA-Z0-9_\-\.])+@((([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]))|((([a-zA-Z0-9\-])+\.)+([a-zA-Z\-])+))$/;
	return EmailRegEx.test(elementValue);
}

function validateURL(elementValue)
//this function will validate if the url entered by the user is valid.
{
	var urlRegEx = /[A-Za-z0-9\.-]{3,}\.[A-Za-z]{3}/;
	return urlRegEx.test(elementValue);
}

function getAltName(fieldName)
//when given an actual field name this function will return an alternate name.
{
	var fieldNameAlt;
	switch(fieldName) 
	{
		case 'txtName':
			fieldNameAlt = 'Your Name';
			break;
		case 'txtEmail':
			fieldNameAlt = 'E-mail Address';
			break;
		case 'txtAgencyName':
			fieldNameAlt = 'Agency Name';
			break;
		case 'txtAgencyCode':
			fieldNameAlt = 'Agency Code No.';
			break;
		case 'txtPhone':
			fieldNameAlt = 'Phone No.';
			break;
		case 'txtWebsite':
			fieldNameAlt = 'Website';
			break;
		case 'txtaComments':
			fieldNameAlt = 'Comments';
			break;
					
	}//end of the switch fieldName
	return fieldNameAlt;
}//end of getAltName

//standard string trim function
function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}



function ValidatePage()
{
	//alert("entering validatepage");
	var Validated = true;
	//var ErrorCounter = 0;
	//var stringOfBlankErrors = '';
	//var stringOfOtherErrors = '';
	//var finalErrorString = '';
	var thisForm = document.forms[0];
	var thisFormItem;
	//var doneThisField;
	//var checkedThisRadio;
	//var ThisRadioChecked;
	var errorString = 'The following fields must be entered:\n';
	var validationErrorString = 'The following fields does not contain valid data:\n';
	var incompleteError = false;
	var validationError = false;
	
	
	
	for (i = 0; i < thisForm.elements.length; i++) 
	{			
			
		thisFormItem = thisForm.elements[i];
		doneThisField = false;
		ThisRadioChecked = false;
	
		var fieldName = thisFormItem.name;
		var fieldNameAlt;
		
		
		if (fieldName != 'Submit') 
		{
			//create user-friendly field names
			
			fieldNameAlt = getAltName(fieldName);
			if((trim(thisFormItem.value) == "" || thisFormItem.value == null) && (!thisFormItem.value) && thisForm.elements[i].type != 'radio' && fieldName != 'txtWebsite' && fieldName != 'txtaComments')
			{
				incompleteError = true;
				errorString += "You must enter a value for the \'"+fieldNameAlt+"\' field!\n";
				//return errorString; 
				
			}
			
			
			
			//following switch structure is for validation
			

			if(!incompleteError)
			{
				switch(thisFormItem.name) 
				{
					case 'txtName':
						if(!validateName(thisFormItem.value))
						{
							validationError = true;
							validationErrorString += "Your Name must be alphabetic. No numbers or special characters\n";
						}
						
						break;
					case 'txtEmail':
						if(!validateEmail(thisFormItem.value))
						{
							validationError = true;
							validationErrorString += "You must enter a valid e-mail address.\n";
						}
						
						break;
					case 'txtAgencyName':
						if(!validateAgencyName(thisFormItem.value))
						{
							validationError = true;
							validationErrorString += "The Agency Name must be alphanumeric. No special characters.\n";
						}
						
						break;
					case 'txtAgencyCode':
						if(!validateAgencyCode(thisFormItem.value))
						{
							validationError = true;
							validationErrorString += "The Agency Code No. must be 1-4 digits\n";
						}
						
						break;
					case 'txtPhone':
						if(!validatePhoneNumber(thisFormItem.value))
						{
							validationError = true;
							validationErrorString += "The Phone number must be a valid US phone number\n  Eg:(xxx)-xxx-xxxx or xxx-xxx-xxxx or xxxxxx-xxxx or xxxxxxxxxx.\n";
						}
						
						break;
					case 'txtWebsite':
						if((thisFormItem.value) && (thisFormItem.value != "http://"))
						{
							if(!validateURL(thisFormItem.value))
							{
								validationError = true;
								validationErrorString += "The Website URL is invalid.\n";
							}
						}
						break;
					case 'txtaComments':
						if(!validateAgencyName(thisFormItem.value))
						{
							validationError = true;
							validationErrorString += "The Comments contain invalid characters.\n";
						}
						break;
								
				}//end of the switch fieldName
			}
			
			
								
		}//end of if (fieldName != 'Submit') 
	}//end of the for loop
	//display the error
	if(incompleteError)
	{
		alert(errorString);
		return false;
	}
	//display validation errors
	if(validationError)
	{
		alert(validationErrorString);
		return false
	}
	return true;
}
			
			
			
-->