





/*------------------------------------------------------------------------------------------------
HANDLES UPPER CASE WORDING AND STRIPPING SPECIFIED CHARACTERS
Rules:		This class is already called each time the validation function handles a new
			form elements so there's no need to call it.
			
			If you want to add strings to strip out of your form field, add the string to
			the reArray variable.  Seperate each filtred with ' | ' remembering to include the
			spaces around the pipe.
------------------------------------------------------------------------------------------------*/
function email(ename, evalue)
{
  	if (ename.indexOf('email') != -1) {
		var teste = false;
		var strtest = new String(evalue);
		var index = strtest.indexOf("@");
		
		if (index > 0) {
			var eid = strtest.indexOf(".",index);
		
			if (eid > index+1 && strtest.length > eid+1) {
				teste = true;
			}
			return teste;
		}
	}
}





/*------------------------------------------------------------------------------------------------
HANDLES UPPER CASE WORDING AND STRIPPING SPECIFIED CHARACTERS
Rules:		This class is already called each time the validation function handles a new
			form elements so there's no need to call it.
			
			If you want to add strings to strip out of your form field, add the string to
			the reArray variable.  Seperate each filtred with ' | ' remembering to include the
			spaces around the pipe.
------------------------------------------------------------------------------------------------*/
String.prototype.toCapitalCase = function() {	
	//----------------------------------------------------------------
	// Use this variable to list things to replace with ''
	// Seperate with a ' | ' pipe.  Include the space for readability
	//----------------------------------------------------------------
			reArray		= 	"incl_ | reg_ | blast_ | [ | ]";
	
	
	
	//----------------------------------------------------------------
	//	Replacing and Upper Casing words.  Do not touch below
	var re 		= /\s/;
	var words 	= this
	reArray 	= reArray.split(" | ");
		for (i = 0; i <= reArray.length; i++) {
			words = words.replace(reArray[i], "");
		}
	
	words 		= words.replace(/_/g, " ");
	words 		= words.split(re);
	re 			= /(\S)(\S+)/;
		for (i = words.length - 1; i >= 0; i--) {
			re.exec(words[i]);
			words[i] = RegExp.$1.toUpperCase()
			+ RegExp.$2.toLowerCase();
		}
	return words.join(' ');
	//---------------------------------------------------------------
}
/*----------------------------------------------------------------------------------------------*/



/*------------------------------------------------------------------------------------------------
VALIDATES ENTIRE FORM
Rules: 	The required form field names must start with "incl_" if they are to be required
		The script automatically strips out "incl_" and builds the error alert for fields
		not completed properly.

PHP Checkbox Array: 	The script automatically takes in to consideration the array requirement
						for PHP to handle Checkbox elements of the same name.
------------------------------------------------------------------------------------------------*/
function validate(f)
{
	
	var textReqColor 	= "#ffffcc";
	var radioReqColor 	= "red";
	var checkReqColor 	= "red";
	
	var e  = '';
	var t  = '';
	var s  = '';
	var r  = '';
	var c  = '';
	var ta = '';
	var arrayname 	= '';
	var error 		= "no";
	var fieldlist 	= '';
	var emailerr	= '';
	var password	= '';
/*--------------------------------------------------------------------------------------------
//								Start Looping through Elements								//
--------------------------------------------------------------------------------------------*/
for (var i = 0; i < document[f].elements.length; i++)
{	
		var j = document[f].elements[i];
		/*------------------------------------------------------------------------------------
		//					Continue only if the Field is required							//
		------------------------------------------------------------------------------------*/
		if (j.name.indexOf("incl_") == 0 || j.name == "password" || j.name == "passwordconfirm") {
			
			//Strip everthing out we need
			jtitle = j.name.toCapitalCase();			
		
			/*----------------------------------------------------------------------------
			//						Text/Textarea Fields								//
			----------------------------------------------------------------------------*/
			if ( j.type == 'text' || j.type == 'textarea' ) {				
				if (j.name == "password") {
					password = j.value;
				} else if (j.name == "passwordconfirm") {
					passwordconfirm = j.value;
				} else if (j.value == '') {
					e += "Please fill out " + jtitle + "\n";
					document[f].elements[i].style.backgroundColor = textReqColor;
					document[f].elements[i].style.border = "1px solid";
				} else if (j.name.indexOf('email') != -1 && !email(j.name, j.value)) {
			  		emailerr += jtitle + " is an incorrect format\n";
			  	}
			}
			/*--------------------------------------------------------------------------*/
			
			
		
			/*----------------------------------------------------------------------------
			//						Select Fields										//
			----------------------------------------------------------------------------*/
				// Single Select Fields
			else if ( j.type == 'select-one' ) {
				if (j.selectedIndex == 0) {
					e += "Please select a valid " + jtitle + "\n";
					document[f].elements[i].style.backgroundColor = textReqColor;
					document[f].elements[i].style.border = "1px solid";
				}
			}
				// Multiple Select Fields
			else if ( j.type == 'select-multiple' ) {
				if (j.selectedIndex == -1) {
					e += "Please select a valid " + jtitle + "\n";
					document[f].elements[i].style.backgroundColor = textReqColor;
					document[f].elements[i].style.border = "1px solid";
				}
			}
			/*--------------------------------------------------------------------------*/
			
			
			
		
			/*----------------------------------------------------------------------------
			//						Checkbox Fields										//
			----------------------------------------------------------------------------*/
			else if (j.type == 'checkbox') {	
				if ( fieldlist.indexOf(j.name) < 0 ) {
					arrayname 	= (document.getElementsByName(j.name).length - 1);
					resetarray 	= "no";
					//If there's an array of checkboxes, loop through
					if ( arrayname > 1 ) 
					{
						a = new Number(0);
						for ( a = a; a <= arrayname; a++ ) {
							var ja 	= document[f].elements[i];
								if ( fieldlist.indexOf(ja.name) != -1 ) {
									a = arrayname;
								} else {
									if (ja.type == "checkbox") {
										if (ja.checked == true) {
											error 	= "no";
											jtitle 	= ja.name.toCapitalCase();
											a 		= arrayname + 1;
										} else {
											error	= "yes";
										}
										i++;
									}
								}
						}
						fieldlist += "," + ja.name;
						i--;
					// Single Checkbox field
					} else {
						if (j.type == "checkbox" && j.checked == false) {
							error 		= "yes";
						}
					}
					
					if (error == "yes") {
							fieldlist 	+= "," + j.name;
							var j 		= document[f].elements[i];
								/*-------------------------------------------------------------------------------
									If there are checkbox errors, highlight the checkbox group. IE Only
								-------------------------------------------------------------------------------*/
								i = new Number(i);
								a = new Number(document.getElementsByName(j.name).length);
								a = i-a+1;
								for ( c = i; c >= a; c-- ) {
									if (document[f].elements[c].type == "checkbox") {
										document[f].elements[c].style.border = "1px solid " + checkReqColor;
										document[f].elements[c].style.mozBorderRadius = "1px !important";
										document[f].elements[c].style.mozBorderBottomColors = "#919498 #cecece";
									}
								}
								c = 0;
								a = 0;
								/*-----------------------------------------------------------------------------*/
							jtitle 		= j.name.toCapitalCase();
							e 			+= "You must select from " + jtitle + "\n";
					}
						
				}
			
			error 			= "no";
			arrayname		= '';
			
			}
			/*--------------------------------------------------------------------------*/
			
			
			
		
			/*----------------------------------------------------------------------------
			//						Radio Buttons										//
			----------------------------------------------------------------------------*/
			else if (j.type == "radio") {
				rlength = document.getElementsByName(j.name).length;
				if ( fieldlist.indexOf(j.name) < 0 ) {
					fieldlist 	+= "," + j.name;
					var j 		= document[f].elements[i];
					jtitle 		= j.name.toCapitalCase();
						// Run through the radio buttons
						for (r = 0; r <= rlength; r++) {
							if (document[f].elements[r + i].checked == true) {
								error = "no";
								r = rlength + 1;
							}
						}
							i++;
							if (error = "yes") {
								e += "You must select from " + jtitle + "\n";
							}
					fieldlist 	+= "," + j.name;
				}
			}
			/*--------------------------------------------------------------------------*/
		}
		/*---------------------------- End Required Check ------------------------------*/
		fieldlist += "," + j.name;
}
/*------------------------------------ End Main Loop -----------------------------------*/
			fieldlist = "";
			if (emailerr != "") {
				e += "\n-------------------------------------------------------\n" + emailerr + "\n";
			}
			
			if (password && password == "") {
				if (password != passwordconfirm) {
					alert("You did not successfully verify your password.  Please try again");
					return false;
				} else if (password == "") {
					e += "\n-------------------------------------------------------\nPlease enter your Password\n";
				}
			}
			
			if ( e ) {
				alert(e);
				return false;
			} else {
				return true;
			}
}

