
/**********************************************************

This file is a catch-all javascript validation for an HTML form.  If you use the naming conventions outlined below for your form elements (namely textboxes and textareas), and use the ValidateForm function in the onsubmit event of your form, all validation is taken care of for you.

To use this file, do the following:

	1.Include the file on your page by using the following line in the <head>:
		<script language="javascript" src="formvalidation.js"></script>
	
	2.Then in your form tag, add the following (where MyForm is the name of your form):
		onsubmit="return ValidateForm('MyForm')"
	
	3.Note: if using an image to call the submit of the form, use the following line:
		<a href="javascript:if(ValidateForm('MyForm')){document.MyForm.submit();}">

	4.Textboxes in your form must be named with "_xy" at the end, where x is the type of data in the field, and y designates if it is required or not.  Use "r" for required, "o" for optional.

Data types & their designators:
-regular text		_t
-email       		_e
-number      		_n
-telephone   		_p	//not handled yet
-web address 		_w	//not handled yet
-postal code 		_??	//not handled yet
-date        		_d

ie: -for a required textbox, end the name with "_tr"
    -optional textboxes don't need anything (or "_to" if you really want to)
    -for a required email field, end the name with "_er"
    -for an optional email field, end the name with "_eo"
    -etc...

**********************************************************/

function ValidateForm(pForm){
	vForm = eval("document." + pForm);
	
	for(i=0;i<vForm.length;++i){
		vElement = vForm.elements[i];
		vSuffix = vElement.name.substr(vElement.name.length-3, 3);
		vChar1 = vSuffix.substr(0, 1);
		vChar2 = vSuffix.substr(1, 1);
		vChar3 = vSuffix.substr(2, 1);
		
		if(vChar1=="_"){ //check if element is required
			if(vChar3=="r" && vElement.value==""){
				alert("Please enter all required fields.");
				vElement.focus();
				return false;
			}
			
			if(vChar2=="e"){ //validate email address
				vValidEmail = true;
				
				if(vElement.value.length>0){
					for(j=0; j<=vElement.value.length-1; ++j){
						if(vElement.value.charAt(j) == " "){vValidEmail = false;}
					}
						
					vCount = 0;
					for(k=0; k<=vElement.value.length-1; ++k){
						if(vElement.value.charAt(k) == "@"){++vCount;}
					}
					if(vCount != 1){vValidEmail = false;}
						
					if(vElement.value.indexOf("@") < 1){vValidEmail = false;}
					if(vElement.value.indexOf("@") == vElement.value.length-1){vValidEmail = false;}
					if(vElement.value.indexOf(".") < 1){vValidEmail = false;}
					if(vElement.value.indexOf(".") == vElement.value.length-1){vValidEmail = false;}
				}
				
				vInvalidChars = "!*&/\?+=)(^%$#:;";
				
				for(m=0; m<vInvalidChars.length; ++m){
					if(vElement.value.indexOf(vInvalidChars.charAt(m)) > -1){vValidEmail = false;}
				}
				
				if(vValidEmail==false){
					alert("Please enter a valid email address.");
					vElement.focus();
					return false;
				}
			}
			
			if(vChar2=="n" && isNaN(vElement.value)==true){ //validate number field
				alert("You have entered an invalid number in a number field.");
				vElement.focus();
				return false;
			}
			
			if(vChar2=="d" && isNaN(Date.parse(vElement.value))){ //validate date field
				alert("You have entered an invalid date in a date field.");
				vElement.focus();
				return false;
			}
		}
	}
	
	return true;
}

