	// Title: FORM VALIDATOR
	// Author: Ryan Logsdon
	// Contact: rlogsdon@tbconsulting.com
	// Create Date: September 15th, 2004
	// Designed for: Internet Explorer 5.0 or higher
	// Description: Provides a global form validation function utilizing custom attribute tags
	//			   inside form elements and ASP.NET Controls.
	//
	// Modification History:
	// Modified By    |      Date      |      Description of Modification
	// -------------------------------------------------------------------------
	// Ryan Logsdon		   02.04.05			Added "grouping" ability to allow
	//										validation on a page with multiple 
	//										forms.
	//
	// -------------------------------------------------------------------------
	//
	//----------------------------INSTRUCTIONS-----------------------------------//
	// To include in your form simply add this attribute to your <form> tag:
	//		onsubmit="return frmValidator('MyFormName', '', '');"
	// Alternatively you can add it to the onclick event of the control that submits the form:
	//		onclick="return frmValidator('MyFormName', '', '');"
	// ***Example: <form id="myForm" method="post" action="myAction.aspx" onsubmit="return frmValidator('myForm', '', 'lblMyErrorMessage');">
	// The method takes 3 string parameters:
	// strForm - REQUIRED variable that holds the form name
	// strGroup - OPTIONAL variable that holds the name of the group you want to validate
	// strLabel - OPTIONAL variable that holds the name of the label(span) element you want the error message to display in
	// Simply pass empty strings if you don't want that functionality
	//
	// To turn validation on for a form element simply add any of the following attributes to the element tag:	
	// Supported Tags:
	//	1.) valrequired="true" - makes field required
	//	2.) valformat="X" - where X is one of the following:
	//		a. email
	//		b. phone (555-555-5555)
	//		c. number
	//		d. character
	//		e. shortdate
	//		f. longdate
	//		g. zip
	//	3.) valtitle="element tite" - used in error summary pop-up
	//  4.) OPTIONAL: valgroup="myGroup" - for assigning an element to a validation group
	// ***Example: <input type="text" id="myTextBox" valtitle="My Text Box" valrequired="true" valgroup="myGroup" />
	//
	//-------------------------------------------------------------------//
	
	//-----------Global Variable Declaration----------------//		
		var blnValidated = true; //Validation Boolean Flag
		
		//Error Message Header
		var strValidationMessage = "";
		var j = 0; //Recursive Variable for Element Array
		
		//Change your label string message
		var strLabelMessage = "<b>ERROR:</b> Please review all fields with a blue background";
		
		// these are your original color settings
		var strOriginalBG = "#ffffff";
		var strOriginalFont = "#000000";
		
		// change colors reflect the change highlight
		var strHighlightColor = "#d6c8da";
		var strFontColor = "#000000";
		
		//variable to hold first failed element for form focus
		var strFirstFailedElement = "";
	//-----------------------------------------------------//
		
	//master function
	function frmValidator(strForm, strGroup, strLabel)
	{	
		//Reset globals
		strValidationMessage = "The following fields are invalid: ";
		blnValidated = true;
		strFirstFailedElement = "";
		
		//reset bln flag
		
		//if form is blank then error out
		if(strForm == '' || strForm == null)
		{
			alert('No form name passed to EasyValidator');	
			return false;		
		}
		
		//set J equal to number of elements in the calling form
		j = document.getElementById(strForm).elements.length - 1; 		
		
		//Loop through form elements
		for(i = 0; i <= j; i++)
		{		
			// clear backgound and font styles of element
			if(document.getElementById(strForm).elements[i].style.background == strHighlightColor)
			{			
				document.getElementById(strForm).elements[i].style.background = strOriginalBG;
			}
			if(document.getElementById(strForm).elements[i].style.color == strFontColor)
			{
				document.getElementById(strForm).elements[i].style.color = strOriginalFont;	
			}		
			
			//check if the validation is for a group only
			if(strGroup != '' && strGroup != null)
			{
				if(document.getElementById(strForm).elements[i].getAttribute("valgroup") == strGroup)
				{
					//call valformat function
					valFormatCheck(document.getElementById(strForm).elements[i].getAttribute("id"));			
					
					//call val required function
					valRequiredCheck(document.getElementById(strForm).elements[i].getAttribute("id"));
				}
			}
			else
			{
				//call valformat function
				valFormatCheck(document.getElementById(strForm).elements[i].getAttribute("id"));			
				
				//call val required function
				valRequiredCheck(document.getElementById(strForm).elements[i].getAttribute("id"));
			}						
		}
		
		//test for validation flag
		if(blnValidated == false)
		{		
			//output error string in label control if provided
			if(strLabel != '')
			{
				document.getElementById(strLabel).innerHTML = strLabelMessage;
			}
			
			//set focus
			if(strFirstFailedElement != null || strFirstFailedElement != '')
			{				
				document.getElementById(strFirstFailedElement).focus();				
			}						
			
			//alert out validation error summary
			alert(strValidationMessage);
			
			//return false to prevent form post-back
			return false;
		}
		else
		{
			//If there is an error label clear its value
			if(strLabel != '')
			{
				document.getElementById(strLabel).innerHTML = "";
			}
			
			//return true to finish submitting form
			return true;
		}
	}
	
	//function to check an element valrequired attribute and validate accordingly
	function valRequiredCheck(obj)
	{
		if(obj != '')
		{	
			//Check for valRequired="true" attribute of form element
			if(document.getElementById(obj).getAttribute("valrequired") == "true")
			{				
				//check to see if value is null by calling valRequired functions	
				if(valRequired(document.getElementById(obj).value) == false)
				{	
					//set validation flag to false				
					blnValidated = false;
					if(strFirstFailedElement == null || strFirstFailedElement == '')
					{
						strFirstFailedElement = document.getElementById(obj).getAttribute("id");				
					}
					
					//change background and font color to highlight offending elements
					document.getElementById(obj).style.background = strHighlightColor;
					document.getElementById(obj).style.color = strFontColor;
					
					//populate error message string with valTitle or element ID
					if(document.getElementById(obj).getAttribute("valtitle") == null)
					{
						strValidationMessage += "\n" + document.getElementById(obj).getAttribute("id")
												+ ": This item is required";
					}
					else
					{
						strValidationMessage += "\n" + document.getElementById(obj).getAttribute("valTitle")
												+ ": This item is required";
					}					
				}								
			}	
		}
	}
	
	//function to check an elements valformat attribute and validate accordingly
	function valFormatCheck(obj)
	{
		if(obj != '')
		{						
			//Check for valFormat attribute and make sure there is a value to test
			if(document.getElementById(obj).getAttribute("valformat") != null && document.getElementById(obj).value != null && document.getElementById(obj).value != '')
			{				
				//Get valFormat value
				switch(document.getElementById(obj).getAttribute("valformat"))
				{
					//case statement to handle different valFormats
					case 'number':
						//check to see if value is a number by calling valNumber function
						if(valNumber(document.getElementById(obj).value) == false)
						{
							//set validation flag
							blnValidated = false;
							if(strFirstFailedElement == null || strFirstFailedElement == '')
							{
								strFirstFailedElement = document.getElementById(obj).getAttribute("id");				
							}
							
							//change background and font color to highlight offending elements
							document.getElementById(obj).style.background = strHighlightColor;
							document.getElementById(obj).style.color = strFontColor;
					
							//populate error message string with valTitle or element ID
							if(document.getElementById(obj).getAttribute("valtitle") == null)
							{
								strValidationMessage += "\n" + document.getElementById(obj).getAttribute("id")
												+ ": Must contain all numbers";
							}
							else
							{
								strValidationMessage += "\n" + document.getElementById(obj).getAttribute("valTitle")
												+ ": Must contain all numbers";
							}	
						}
						break;
						
					case 'email':
						//check to see if value is a valid e-mail by calling valEmail function
						if(valEmail(document.getElementById(obj).value) == false)
						{
							//set validation flag
							blnValidated = false;
							if(strFirstFailedElement == '' || strFirstFailedElement == null)
							{
								strFirstFailedElement = document.getElementById(obj).getAttribute("id");											
							}
							
							//change background and font color to highlight offending elements
							document.getElementById(obj).style.background = strHighlightColor;
							document.getElementById(obj).style.color = strFontColor;
							
							//populate error message string with valTitle or element ID
							if(document.getElementById(obj).getAttribute("valtitle") == null)
							{
								strValidationMessage += "\n" + document.getElementById(obj).getAttribute("id")
												+ ": Invalid e-mail format";
							}
							else
							{
								strValidationMessage += "\n" + document.getElementById(obj).getAttribute("valTitle")
												+ ": Invalid e-mail format";
							}
						}	
						break;
						
					case 'character':
						//check to see if value is all characters by calling valCharacter function
						if(valCharacter(document.getElementById(obj).value) == false)
						{
							//set validation flag
							blnValidated = false;
							if(strFirstFailedElement == null || strFirstFailedElement == '')
							{
								strFirstFailedElement = document.getElementById(obj).getAttribute("id");				
							}
							
							//change background and font color to highlight offending elements
							document.getElementById(obj).style.background = strHighlightColor;
							document.getElementById(obj).style.color = strFontColor;
							
							//populate error message string with valTitle or element ID
							if(document.getElementById(obj).getAttribute("valtitle") == null)
							{
								strValidationMessage += "\n" + document.getElementById(obj).getAttribute("id")
												+ ": No numbers or special characters allowed";
							}
							else
							{
								strValidationMessage += "\n" + document.getElementById(obj).getAttribute("valTitle")
												+ ": No numbers or special characters allowed";
							}
						}	
						break;
						
					case 'phone':	
						//check to see if value is a valid phone number by calling valPhone function
						if(valPhone(document.getElementById(obj).value) == false)
						{
							//set validation flag
							blnValidated = false;
							if(strFirstFailedElement == null || strFirstFailedElement == '')
							{
								strFirstFailedElement = document.getElementById(obj).getAttribute("id");				
							}
							
							//change background and font color to highlight offending elements
							document.getElementById(obj).style.background = strHighlightColor;
							document.getElementById(obj).style.color = strFontColor;
							
							//populate error message string with valTitle or element ID
							if(document.getElementById(obj).getAttribute("valtitle") == null)
							{
								strValidationMessage += "\n" + document.getElementById(obj).getAttribute("id")
												+ ": Must be in correct format";
							}
							else
							{
								strValidationMessage += "\n" + document.getElementById(obj).getAttribute("valTitle")
												+ ": Must be in correct format";
							}	
						}
						break;	
					case 'zip':	
						//check to see if value is a valid phone number by calling valPhone function
						if(valZip(document.getElementById(obj).value) == false)
						{
							//set validation flag
							blnValidated = false;
							if(strFirstFailedElement == null || strFirstFailedElement == '')
							{
								strFirstFailedElement = document.getElementById(obj).getAttribute("id");				
							}
							
							//change background and font color to highlight offending elements
							document.getElementById(obj).style.background = strHighlightColor;
							document.getElementById(obj).style.color = strFontColor;
							
							//populate error message string with valTitle or element ID
							if(document.getElementById(obj).getAttribute("valtitle") == null)
							{
								strValidationMessage += "\n" + document.getElementById(obj).getAttribute("id")
												+ ": Must be in correct format (11111 or 11111-1111)";
							}
							else
							{
								strValidationMessage += "\n" + document.getElementById(obj).getAttribute("valTitle")
												+ ": Must be in correct format (11111 or 11111-1111)";
							}	
						}
						break;	
					case 'shortdate':	
						//check to see if value is a valid phone number by calling valPhone function
						if(valShortDate(document.getElementById(obj).value) == false)
						{
							//set validation flag
							blnValidated = false;
							if(strFirstFailedElement == null || strFirstFailedElement == '')
							{
								strFirstFailedElement = document.getElementById(obj).getAttribute("id");				
							}
							
							//change background and font color to highlight offending elements
							document.getElementById(obj).style.background = strHighlightColor;
							document.getElementById(obj).style.color = strFontColor;
							
							//populate error message string with valTitle or element ID
							if(document.getElementById(obj).getAttribute("valtitle") == null)
							{
								strValidationMessage += "\n" + document.getElementById(obj).getAttribute("id")
												+ ": Must be a valid date";
							}
							else
							{
								strValidationMessage += "\n" + document.getElementById(obj).getAttribute("valTitle")
												+ ": Must be a valid date";
							}	
						}
						break;	
					case 'longdate':	
						//check to see if value is a valid phone number by calling valPhone function
						if(valLongDate(document.getElementById(obj).value) == false)
						{
							//set validation flag
							blnValidated = false;
							if(strFirstFailedElement == null || strFirstFailedElement == '')
							{
								strFirstFailedElement = document.getElementById(obj).getAttribute("id");				
							}
							
							//change background and font color to highlight offending elements
							document.getElementById(obj).style.background = strHighlightColor;
							document.getElementById(obj).style.color = strFontColor;
							
							//populate error message string with valTitle or element ID
							if(document.getElementById(obj).getAttribute("valtitle") == null)
							{
								strValidationMessage += "\n" + document.getElementById(obj).getAttribute("id")
												+ ": Must be in correct format or a valid date";
							}
							else
							{
								strValidationMessage += "\n" + document.getElementById(obj).getAttribute("valTitle")
												+ ": Must be in correct format or a valid date";
							}	
						}
						break;			
				}
			}
		}
	}
	
	//function to test if value is null or empty string
	function valRequired(strValue)
	{
		strValue = strValue.replace(/\s/g, '');
		if(strValue == null || strValue == '')
		{
			return false;
		}
		else
		{
			return true;
		}
	}
	
	//function to check validity of email string
	function valEmail(strEmail)
	{
		var rePattern = /^\S+@\S+\.\w+$/
		
		if(rePattern.test(strEmail) == false)
		{
			return false;
		}
		else
		{
			return true;
		}
		return true;
	}
	
	//function to validate phone number format ((555)555-5555)
	function valPhone(strPhone)
	{
		var rePattern = /^\(\d{3}\) \d{3}-\d{4}$/
		
		if(rePattern.test(strPhone) == false)
		{
			return false;
		}
		else
		{
			return true;
		}
	}
	
	//function to validate zip code format (55555 or 55555-5555)
	function valZip(strZip)
	{
		var rePattern = /^\d{5}-\d{4}$/
		var otPattern = /^\d{5}$/
		
		if(rePattern.test(strZip) == false && otPattern.test(strZip) == false )
		{
			return false;
		}
		else
		{
			return true;
		}
	}
	
	//function to check validity of Short Date String "00/00/00"
	function valShortDate(strDate)
	{
		var rePattern = /^\d{2}\/\d{2}\/\d{2}$/
		
		if(rePattern.test(strDate) == false)
		{
			return false;
		}
		else
		{
			var blnPass = true;
			var arrDate = strDate.split("/");
			var intMonth = arrDate[0];
			var intDay = arrDate[1];
			var intYear = arrDate[2];
			
			if(intMonth > 12 || intMonth <= 0)
			{
				blnPass = false;
			}
			if(intDay > daysInMonth(intMonth) || intDay <= 0)
			{
				blnPass = false;
			}
			
			return blnPass;
			
		}
		return true;
	}
	
	//function to check validity of Long Date String "mm/dd/yyyy"
	function valLongDate(strDate)
	{
		var rePattern = /^\d{2}\/\d{2}\/\d{4}$/
		
		if(rePattern.test(strDate) == false)
		{
			return false;
		}
		else
		{
			var blnPass = true;
			var arrDate = strDate.split("/");
			var intMonth = arrDate[0];
			var intDay = arrDate[1];
			var intYear = arrDate[2];
			
			if(intMonth > 12 || intMonth <= 0)
			{
				blnPass = false;
			}
			if(intDay > daysInMonth(intMonth) || intDay <= 0)
			{
				blnPass = false;
			}
			if(intYear > 2030 || intYear < 1920)
			{
				blnPass = false;
			}
			return blnPass;
			
		}
		return true;
	}
	
	//takes a month int and returns number of days
	function daysInMonth(intMonth)
	{	
		switch(intMonth)
		{
			case '01': //January
				return 31;
				break;
			case '02': //February
				return 29;
				break;
			case '03': //March
				return 31;
				break;
			case '04': //April
				return 30;
				break;
			case '05': //May
				return 31;
				break;
			case '06': //June
				return 30;
				break;
			case '07': //July
				return 31;
				break;
			case '08': //August
				return 31;
				break;
			case '09': //September
				return 30;
				break;
			case '10': //October
				return 31;
				break;
			case '11': //November
				return 30;
				break;
			case '12': //December
				return 31;
				break;
			default:
				return 31;
				break;
		}
	}
	
	//function to validate phone number format (555-555-5555)
	/*function valPhone(strPhone)
	{
		var rePattern = /^\d{3}-\d{3}-\d{4}$/
		
		if(rePattern.test(strPhone) == false)
		{
			return false;
		}
		else
		{
			return true;
		}
	}*/
	
	//function to make sure value is a number
	function valNumber(strNumber)
	{
		if(isNaN(strNumber))
		{
			return false;
		}
		else
		{
			return true;
		}		
	}	
	
	//function to make sure value is made up entirely of alphabet characters
	function valCharacter(strCharacter)
	{
		var rePattern = /^[a-zA-Z ]+$/
		
		if(rePattern.test(strCharacter) == false)
		{
			return false;
		}
		else
		{
			return true;
		}
	}
	
	function CleanMoneyInput(strNumber)
	{
			
		var pattern = /\$/;
		
		strNumber = strNumber.replace(pattern,"");
		
		pattern = /\,/;
		
		strNumber = strNumber.replace(pattern,"");
		strNumber = strNumber.replace(pattern,"");
		
		return strNumber;
	
	}
	
	//function to make sure value is a number
	function valNumber(strNumber)
	{
		
		strNumber = CleanMoneyInput(strNumber);
		
		if(isNaN(strNumber))
		{
			return false;
		}
		else
		{
			return true;
		}		
	}

