	//FormaValidation parameters:
	//formName - name of the form being validated - string
	//fieldName - name of the field being validated - string
	//fieldAlias - name of the field as it appears tp the user - example First Name
	//fieldType - type of the fields being validated
		//the following field types are allowed:
		/*
		1) text 		      
		2) person name       
		3) email 		      
		4) phone number 	  
		5) zip code 		  
		6) date should have 3 fields separated by |  month|date|year - this order is a must
option values must be numeric (no january but 1, no zeros either such as 01, just 1) 
for CF, pass in the date as one field
		7) credit card number 
		8) price              
		9) dropdowns		JS ONLY  
		*/
	//errorType - type of the validation
	//the following types are allowed:
		//1) required
		//2) format check
	//errorMessage - message for the error, if empty, then uses default message
	//errorMessage could have tree pipes ||| that will be replaced with the name of 
	//the field
	function FormValidation(formName, fieldName, fieldAlias, fieldType, errorType, errorMessage)
	{	
		//make sure that correct number of arguments are passed into the function
		if(arguments.length != 6)
		{
			customalert('Validation failed. Wrong number of parameters for the validation function.');
			return false;
		}
		//switch for different kinds of fieldTypes
		switch (fieldType)
		{	
			case 1:
				return TextValidation(formName, fieldName, fieldAlias, errorType, errorMessage);
			case 2:
				return PersonNameValidation(formName, fieldName, fieldAlias, errorType, errorMessage);
			case 3:
				return EmailValidation(formName, fieldName, fieldAlias, errorType, errorMessage);
			case 4:
				return PhoneValidation(formName, fieldName, fieldAlias, errorType, errorMessage);
			case 5:
				return ZipValidation(formName, fieldName, fieldAlias, errorType, errorMessage);
			case 6:
				return DateValidation(formName, fieldName, fieldAlias, errorType, errorMessage);
			case 7:
				return CreditCardValidation(formName, fieldName, fieldAlias, errorType, errorMessage);
			case 8:
				return PriceValidation(formName, fieldName, fieldAlias, errorType, errorMessage);
			case 9:
				return DropdownValidation(formName, fieldName, fieldAlias, errorType, errorMessage);
			default:
				alert('Incorrect fieldType value specified');
				return false;
		}
	}
	
	function TextValidation(formName, fieldName, fieldAlias, errorType, errorMessage)
	{
		//with the following try catch block, see if this field is defined and is accessible
		try
		{
			var temp = eval(formName+'.'+fieldName+'.value');
		}
		//form or field incorrectly indicated
		catch(e)
		{
			customalert('Incorrect formName or fieldName for |||',fieldAlias);
			return false;
		}
		//can proceed here since we did not get error message above
		if (errorType == 1)
		{
			if(trim(temp) == '')
			{
				customalert((trim(errorMessage) == '')?'||| is blank, please make sure to enter this field.':errorMessage,fieldAlias);
				createfocus(formName, fieldName)
				return false;
			}
			//if field is not empty, then return true
			else
			{
				return true;
			}
		}
		//if errorType is anything but 1, then return error
		else
		{
			customalert('||| is being checked as TEXT field, it can only be checked for being empty or not',fieldAlias);
			return  false;
		}
	}
	
	function PersonNameValidation(formName, fieldName, fieldAlias, errorType, errorMessage)
	{
		//with the following try catch block, see if this field is defined and is accessible
		try
		{
			var temp = eval(formName+'.'+fieldName+'.value');
		}
		//form or field incorrectly indicated
		catch(e)
		{
			customalert('Incorrect formName or fieldName for |||',fieldAlias);
			return false;
		}
		//can proceed here since we did not get error message above
		if (errorType == 1)
		{
			//call text validation since it is the same and already defined
			return TextValidation(formName, fieldName, fieldAlias, errorType, errorMessage);
		}
		//format check
		else if (errorType == 2)
		{
			//person name should not have any characters but regular letters and/or `'-
			var RegExpression = /^([a-zA-z(\s)?]+[\'\`\-]?[a-zA-z(\s)?]+)*$/;
			if(temp.match(RegExpression)==null)
			{
				customalert((trim(errorMessage) == '')?'||| has invalid characters. ||| can contain letters of English alphabet and apostrophe and dash':errorMessage,fieldAlias);					
				createfocus(formName, fieldName);
				return false;
			}
			else
			{
				return true;
			}
		}
		//if errorType is anything but 1 or 2, then return error
		else
		{
			customalert('||| is being checked as PERSON NAME field, it can only be checked for being empty or not and its format',fieldAlias);
			return  false;
		}
	}
	
	function EmailValidation(formName, fieldName, fieldAlias, errorType, errorMessage)
	{
		//with the following try catch block, see if this field is defined and is accessible
		try
		{
			var temp = eval(formName+'.'+fieldName+'.value');
		}
		//form or field incorrectly indicated
		catch(e)
		{
			customalert('Incorrect formName or fieldName for |||',fieldAlias);
			return false;
		}
		//can proceed here since we did not get error message above
		if (errorType == 1)
		{
			//call text validation since it is the same and already defined
			return TextValidation(formName, fieldName, fieldAlias, errorType, errorMessage);
		}
		//format check
		else if (errorType == 2)
		{
			//blah.blah@blahb.com
			var RegExpression = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
			if(temp.match(RegExpression)==null)
			{
				customalert((trim(errorMessage) == '')?'||| is in invalid format. ||| should be of format john@doe.com or similar':errorMessage,fieldAlias);					
				createfocus(formName, fieldName);
				return false;
			}
			else
			{
				return true;
			}
		}
		//if errorType is anything but 1 or 2, then return error
		else
		{
			customalert('||| is being checked as EMAIL field, it can only be checked for being empty or not and its format',fieldAlias);
			return  false;
		}
	}
	
	function PhoneValidation(formName, fieldName, fieldAlias, errorType, errorMessage)
	{
		//with the following try catch block, see if this field is defined and is accessible
		try
		{
			var temp = eval(formName+'.'+fieldName+'.value');
		}
		//form or field incorrectly indicated
		catch(e)
		{
			customalert('Incorrect formName or fieldName for |||',fieldAlias);
			return false;
		}
		//can proceed here since we did not get error message above
		if (errorType == 1)
		{
			//call text validation since it is the same and already defined
			return TextValidation(formName, fieldName, fieldAlias, errorType, errorMessage);
		}
		//format check
		else if (errorType == 2)
		{
			//phone, no dashes no parentheseis just numbers
			var RegExpression = /^([0-9]?)(\s|-)?(\()?[0-9]{3}(\))?(\s|-)?[0-9]{3}(\s|-)?[0-9]{4}$/;
			if(temp.match(RegExpression)==null)
			{
				customalert((trim(errorMessage) == '')?'||| has invalid characters. ||| should be of the following format (212) 222-2222':errorMessage,fieldAlias);					
				createfocus(formName, fieldName);
				return false;
			}
			else
			{
				return true;
			}
		}
		//if errorType is anything but 1 or 2, then return error
		else
		{
			customalert('||| is being checked as PHONE field, it can only be checked for being empty or not and its format',fieldAlias);
			return  false;
		}
	}
	
	function ZipValidation(formName, fieldName, fieldAlias, errorType, errorMessage)
	{
		//with the following try catch block, see if this field is defined and is accessible
		try
		{
			var temp = eval(formName+'.'+fieldName+'.value');
		}
		//form or field incorrectly indicated
		catch(e)
		{
			customalert('Incorrect formName or fieldName for |||',fieldAlias);
			return false;
		}
		//can proceed here since we did not get error message above
		if (errorType == 1)
		{
			//call text validation since it is the same and already defined
			return TextValidation(formName, fieldName, fieldAlias, errorType, errorMessage);
		}
		//format check
		else if (errorType == 2)
		{
			//either 12345 or 12345-1234
			var RegExpression = /(^[0-9]{5}$)|(^[0-9]{5}-[0-9]{4}$)/;
			if(temp.match(RegExpression)==null)
			{
				customalert((trim(errorMessage) == '')?'||| has invalid characters. ||| should be of the following format wither 12345 or 12345-1234':errorMessage,fieldAlias);					
				createfocus(formName, fieldName);
				return false;
			}
			else
			{
				return true;
			}
		}
		//if errorType is anything but 1 or 2, then return error
		else
		{
			customalert('||| is being checked as ZIP field, it can only be checked for being empty or not and its format',fieldAlias);
			return  false;
		}
	}
	
	function DateValidation(formName, fieldName, fieldAlias, errorType, errorMessage)
	{
		//with the following try catch block, see if this field is defined and is accessible
		try
		{
			var nameArray = fieldName.split("|");
			var m = eval(formName+'.'+nameArray[0]+'.selectedIndex');
			m = eval(formName+'.'+nameArray[0]+'['+m+'].value');
			var d = eval(formName+'.'+nameArray[1]+'.selectedIndex');
			d = eval(formName+'.'+nameArray[1]+'['+d+'].value');
			var y = eval(formName+'.'+nameArray[2]+'.selectedIndex');
			y = eval(formName+'.'+nameArray[2]+'['+y+'].value');
			var mydate = m+"/"+d+"/"+y;
		}
		//form or field incorrectly indicated
		catch(e)
		{
			customalert('Incorrect formName or fieldName for |||',fieldAlias);
			return false;
		}
		//can proceed here since we did not get error message above
		//format check includes all 3 date fields being required
		if (errorType == 2)
		{	
			if(!DropdownValidation(formName, nameArray[0], 'Month part of the date', 1, errorMessage))
			{return false;}			
			if(!DropdownValidation(formName, nameArray[1], 'Day part of the date', 1,errorMessage))
			{return false;}
			if(!DropdownValidation(formName, nameArray[2], 'Year part of the date', 1, errorMessage))
			{return false;}

			if(!isDate(mydate))
			{
				customalert((trim(errorMessage) == '')?'||| is invalid date.':errorMessage,fieldAlias);					
				createfocus(formName, nameArray[0]);
				return false;
			}			
			else
			{
				return true;
			}
		}
		
		//if errorType is anything but 1 or 2, then return error
		else
		{
			customalert('||| is being checked as DATE field, it can only be checked for being empty or not and its format',fieldAlias);
			return  false;
		}
	}
	
	function CreditCardValidation(formName, fieldName, fieldAlias, errorType, errorMessage)
	{
		//with the following try catch block, see if this field is defined and is accessible
		try
		{
			var temp = eval(formName+'.'+fieldName+'.value');
		}
		//form or field incorrectly indicated
		catch(e)
		{
			customalert('Incorrect formName or fieldName for |||',fieldAlias);
			return false;
		}
		//can proceed here since we did not get error message above
		if (errorType == 1)
		{
			//call text validation since it is the same and already defined
			return TextValidation(formName, fieldName, fieldAlias, errorType, errorMessage);
		}
		//format check
		else if (errorType == 2)
		{
			 if(checkCC(temp))
			 {
			 	return true;
			 }
			 else
			 {
			 	customalert((trim(errorMessage) == '')?'||| has invalid characters. ||| should be of correct format':errorMessage,fieldAlias);					
				createfocus(formName, fieldName);
				return false;
			 }
		}
		//if errorType is anything but 1 or 2, then return error
		else
		{
			customalert('||| is being checked as CREDIT CARD field, it can only be checked for being empty or not and its format',fieldAlias);
			return  false;
		}
	}
	
	function PriceValidation(formName, fieldName, fieldAlias, errorType, errorMessage)
	{
		//with the following try catch block, see if this field is defined and is accessible
		try
		{
			var temp = eval(formName+'.'+fieldName+'.value');
		}
		//form or field incorrectly indicated
		catch(e)
		{
			customalert('Incorrect formName or fieldName for |||',fieldAlias);
			return false;
		}
		//can proceed here since we did not get error message above
		if (errorType == 1)
		{
			//call text validation since it is the same and already defined
			return TextValidation(formName, fieldName, fieldAlias, errorType, errorMessage);
		}
		//format check
		else if (errorType == 2)
		{
			//12121 or 121212.00
			temp = temp.replace(/\,/g,'');
			var RegExpression = /(^[0-9]*$)|(^[0-9]+(\.){1}[0-9]{1,2}$)/;
			if(temp.match(RegExpression)==null)
			{
				customalert((trim(errorMessage) == '')?'||| is in invalid numeric value.':errorMessage,fieldAlias);					
				createfocus(formName, fieldName);
				return false;
			}
			else
			{
				eval(formName+'.'+fieldName+'.value = temp');
				return true;
				
			}
		}
		//if errorType is anything but 1 or 2, then return error
		else
		{
			customalert('||| is being checked as PRICE field, it can only be checked for being empty or not and its format',fieldAlias);
			return  false;
		}
	}
	
	
	function DropdownValidation(formName, fieldName, fieldAlias, errorType, errorMessage)
	{
		//with the following try catch block, see if this field is defined and is accessible
		try
		{
			var temp = eval(formName+'.'+fieldName+'.selectedIndex');
		}
		//form or field incorrectly indicated
		catch(e)
		{
			customalert('Incorrect formName or fieldName for |||',fieldAlias);
			return false;
		}
		//can proceed here since we did not get error message above
		if (errorType == 1)
		{				
			//give error if this dropdown is selected the first option and its value is ''
			//in all cases means that something else is selected
			if(temp == 0 && eval(formName+'.'+fieldName+'['+temp+'].value') == 0)
			{
				customalert((trim(errorMessage) == '')?'||| not selected. Please make sure to select |||':errorMessage,fieldAlias);					
				createfocus(formName, fieldName);
				return false;
			}
			else
			{
				return true;
			}
		}		
		//if errorType is anything but 1 , then return error
		else
		{
			customalert('||| is being checked as DROPDOWN field, it can only be checked for being empty or not',fieldAlias);
			return  false;
		}
	}
	
	
	//help functions not related to specific field validation but used
	//throughout the function scope, most general functions
	function trim(str) {
	  //check from the beginning of the string
	  while (str.substring(0,1) == ' ') {
	    str = str.substring(1,str.length);
	  }
	  //check from the end of the string
	  while (str.substring(str.length-1,str.length) == ' ') {
	    str = str.substring(0,str.length-1);
	  }
	  return str;
	}
	
	function customalert(messageText, fieldAlias)
	{
		alert(messageText.replace(/\|{3}/g,fieldAlias));
	}
	
	function createfocus(formName, fieldName)
	{
		eval(formName+'.'+fieldName+'.focus()');
	}
	
	function checkCC(tempVal)
	{
		if (tempVal.length == 0)
				return true;
			var white_space = " -";
			var creditcard_string="";
			var check_char;
		
			for (var i = 0; i < tempVal.length; i++)
			{
				check_char = white_space.indexOf(tempVal.charAt(i));
				if (check_char < 0)
					creditcard_string += tempVal.substring(i, (i + 1));
			}	
		
			if (creditcard_string.length < 13 || creditcard_string.length > 19)
				return false;
		
			if (creditcard_string.charAt(0) == "+")
				return false;
		
			if (!_CF_checkinteger(creditcard_string))
				return false;
		
			var doubledigit = creditcard_string.length % 2 == 1 ? false : true;
			var checkdigit = 0;
			var tempdigit;
		
			for (var i = 0; i < creditcard_string.length; i++)
			{
				tempdigit = eval(creditcard_string.charAt(i));
		
				if (doubledigit)
				{
					tempdigit *= 2;
					checkdigit += (tempdigit % 10);
		
					if ((tempdigit / 10) >= 1.0)
						checkdigit++;
		
					doubledigit = false;
				}
				else
				{
					checkdigit += tempdigit;
					doubledigit = true;
				}
			}	
			return (checkdigit % 10) == 0 ? true : false;
	}
	
	function _CF_checkinteger(object_value)
	{
		if (object_value.length == 0)
			return true;
	
		var decimal_format = ".";
		var check_char = object_value.indexOf(decimal_format);
	
		if (check_char == -1)
			return _CF_checknumber(object_value);
		else
			return false;
	}
	
	function _CF_checknumber(object_value)
	{
		if (object_value.length == 0)
			return true;
	
		var start_format = " .+-0123456789";
		var number_format = " .0123456789";
		var check_char;
		var decimal = false;
		var trailing_blank = false;
		var digits = false;
	
		check_char = start_format.indexOf(object_value.charAt(0));
	
		if (check_char == 1)
			decimal = true;
		else if (check_char < 1)
			return false;
	
		for (var i = 1; i < object_value.length; i++)
		{
			check_char = number_format.indexOf(object_value.charAt(i));
			if (check_char < 0)
				return false;
			else if (check_char == 1)
			{
				if (decimal)
					return false;
				else
					decimal = true;
			}
			else if (check_char == 0)
			{
				if (decimal || digits)	
					trailing_blank = true;
			}
			else if (trailing_blank)
				return false;
			else
				digits = true;
		}	
	
		return true
	}
	
	function isDate(dateString) 
	{ 
	    if(isNaN(Date.parse(dateString))) return false; 
	    dateString = dateString.replace(/(\/|,| )0([1-9])/g,'$1$2'); 
	
	
	    //line below is preferred line in place of the above line 
	    //dateString = dateString.replace(/(\/|,| |^)0([1-9]($)?)/g,'$1$2'); 
	
	
	    var date = new Date(dateString).getDate(); 
	    var reg = RegExp("(/" + date + "/)|([, ]" + date + 
	              "[, ])|^(" + date + "[, ])|([, ]" + date + ")$"); 
	    return dateString.match(reg) != null; 
	
	
	} 