// JavaScript Document

/*
New version of the form validator. It works basically like the old one, but runs through an array of filters instead of being hardcoded with specific tests.
The first part preloads the error image that's used to ID which fields need to be fixed.
I started with three regex validation ruls- email, date, and zip code. They are in the array that's created at the top of the funciton. To add a new regex validation rule, add a new set to the filter array. 'X' in the example below should be the next in sequence from the last exisitng array row. The numbers in brackets should run from 0-3. The part in parenthesis after the equal sign describes the data you need to put it.

filter_array[x] =new Array(); 
filter_array[x][0] = (name of the filter) IT'S NOT USED BUT IS GOOD FOR ID PURPOSES
filter_array[x][1] = (the actual regex expression) DO NOT PUT IN QUOTES
filter array[x][2] = (the text of the generic error message for the validtion rule)
filter_array[x][3] = (the text that's used in the class attribute to indicate which filter to run) THIS ISN'T NECESSARILY USED IN THE CSS FILE. FOR THIS, IT'S JUST USED FOR ID PURPOSES WITHIN THE SCRIPT.

It's currently supporting email, date, zip, and numeric only validation patterns.

As for the script itself... it first runs through the document, grabs all the labels, and gets rid of any previously applied error classes. This ensures that once something has been fixed, it doesn't retain the error icon.
The script then loops through the labels again, grabbing the value of the 'for' attributes and 'class' attributes. If both of those have a value, it searches the class attribute for the text string 'req'. If that comes back positive, it grabs the element and class, appending an error gif to the latter. If positive, it then loops through the array to see what kind of validation needs to be performed based on the class value and the contents of the array. It performs the correct regex test, or failing that warns that a value must be entered. Then comes the big else. If 'req' is not found, it looks again for any test markers in the class name by looping through the array. If those are present without 'req' it looks at the length of the responses. If and only if it's greater than zero, it performs the proper regex testing. There's no else here. The optional test is only run on fields with some kind of regular syntax to check.
The older, more efficient but less automated version of this generic validation is at the bottom of the page and should still work with forms that use either version.
Could run the array creation out of table and populate it in the cfm file that calls this page, but I don't want to.
*/


if (document.images) {
		   fix_gif = new Image(15,11);
		   fix_gif.src = "/admin/images/icons/icon_butterfly.gif";
				}

	function validateForm() {	

	var filter_array = new Array();
	filter_array[0] = new Array;
	filter_array[0][0] = "email_filter";
	filter_array[0][1] = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	filter_array[0][2] = 'Please check the email address you entered.';
	filter_array[0][3] = 'email';
	filter_array[1] = new Array();
	filter_array[1][0] = "zip_filter";
	filter_array[1][1] = /^(\d{5}-\d{4})|(\d{5})$/i;
	filter_array[1][2] = 'Please check the zip code you entered.';
	filter_array[1][3] = 'zip';
	filter_array[2] = new Array();
	filter_array[2][0] = "date_filter" 
	filter_array[2][1] = /^(?=\d)(?:(?:(?:(?:(?:0?[13578]|1[02])(\/|-|\.)31)\1|(?:(?:0?[1,3-9]|1[0-2])(\/|-|\.)(?:29|30)\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})|(?:0?2(\/|-|\.)29\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))|(?:(?:0?[1-9])|(?:1[0-2]))(\/|-|\.)(?:0?[1-9]|1\d|2[0-8])\4(?:(?:1[6-9]|[2-9]\d)?\d{2}))($|\ (?=\d)))?(((0?[1-9]|1[012])(:[0-5]\d){0,2}(\ [AP]M))|([01]\d|2[0-3])(:[0-5]\d){1,2})?$/;
	filter_array[2][2] = 'date error';
	filter_array[2][3] = 'date';
	filter_array[3] = new Array();
	filter_array[3][0] = "us phone"; 
	filter_array[3][1] = /^((\(\d{3}\)?)|(\d{3}))([\s-./]?)(\d{3})([\s-./]?)(\d{4})$/;
	filter_array[3][2] = "Please check US phone number syntax.";
	filter_array[3][3] = "us_phone"	
	filter_array[4] = new Array();
	filter_array[4][0] = "numeric only";
	filter_array[4][1] = /^[0-9]*$/i;
	filter_array[4][2] = "Please enter numbers only.";
	filter_array[4][3] = "num";
	filter_array[5] = new Array();
	filter_array[5][0] = "Used for select boxes- alerts on 0 option value";
	filter_array[5][1] = /[^0]$/;
	filter_array[5][2] = "Please make a selection";
	filter_array[5][3] = "select";
	/*filter_array[6] = new Array();
	filter_array[6][0] = 
	filter_array[6][1] =
	filter_array[6][2] =
	filter_array[6][3] =*/	
	/*
	filter_array[7] = new Array();
	filter_array[7][0] = 
	filter_array[7][1] =
	filter_array[7][2] =
	filter_array[7][3] = 
	*/
	
	var labels = document.getElementsByTagName('label');
		for (var i=0; i<=labels.length-1; i++){	
			var labelClass = null;
			var labelfor = null;
			var labelfor=labels[i].getAttribute('for')?labels[i].getAttribute('for'):labels[i].getAttribute('htmlFor');
			var labelClass =labels[i].getAttribute('class')?labels[i].getAttribute('class'):labels[i].getAttribute('className');
			if (labelfor != null && labelClass != null)  {
				if (labelClass.indexOf('req') != -1) {
					fixed_class = labelClass.replace(/error/i,'');
					labels[i].setAttribute("class",fixed_class);
					labels[i].setAttribute("className", fixed_class);				
						}//the innermost if
					}//the outermost if
				}//the loop		
			for (var i=0; i<=labels.length-1; i++){	
			var labelClass = null;
			var labelfor = null;
			var labelfor=labels[i].getAttribute('for')?labels[i].getAttribute('for'):labels[i].getAttribute('htmlFor');
			var labelClass =labels[i].getAttribute('class')?labels[i].getAttribute('class'):labels[i].getAttribute('className');
		if (labelfor != null && labelClass != null) {	
			if (labelClass.indexOf('req') != -1) {
				var curr_error = labelClass + ' error';
				var check_input = document.getElementById(labelfor);
				for (p=0; p<filter_array.length; p++) {
					if (labelClass.indexOf(filter_array[p][3]) != -1) {
						var curr_filter = filter_array[p][1];
						if (curr_filter.test(check_input.value) == false)
						{
								var field_name = labels[i].innerHTML;
								if (field_name.indexOf('<') != -1){
									var extra_stuff = field_name.indexOf('<');
									field_name = field_name.substr(0,extra_stuff);
								}//closes extra stuff class remover if
								alert(filter_array[p][2]);
								labels[i].setAttribute("class",curr_error);
								labels[i].setAttribute("className",curr_error);
								labels[i].appendChild(fix_gif);
								labels[i].scrollIntoView(top);
								the_Y = (document.all)?document.documentElement.scrollTop:window.pageYOffset;
							window.scrollTo(0,the_Y);
							check_input.focus();
							return false;
							break;
															
								
							}//closes regex text fail if
					}
					else{if (check_input.value.length == 0) {
							var field_name = labels[i].innerHTML;
							if (field_name.indexOf('<') != -1){
								var extra_stuff = field_name.indexOf('<');
								field_name = field_name.substr(0,extra_stuff);
									}
							alert('You must complete the ' + field_name + ' field!');
							labels[i].setAttribute("class",curr_error);
							labels[i].setAttribute("className",curr_error);
							labels[i].appendChild(fix_gif);
							labels[i].scrollIntoView(top);
							the_Y = (document.all)?document.documentElement.scrollTop:window.pageYOffset;
							window.scrollTo(0,the_Y);
							check_input.focus();
							return false;
							break;							
							}//closes regular fail statement
							}//closes the else for regular inputs
					//Closes string search
				}//closes array validation loop
				
			}//closes req find loop
			else{
				var curr_error = labelClass + ' error';
				var check_input = document.getElementById(labelfor);
				for (c=0; c<filter_array.length; c++){
				if (labelClass.indexOf(filter_array[c][3]) != -1) {
						var curr_filter = filter_array[c][1];
						if (check_input.value.length > 0 && curr_filter.test(check_input.value) == false)
						{
								var field_name = labels[i].innerHTML;
								if (field_name.indexOf('<') != -1){
									var extra_stuff = field_name.indexOf('<');
									field_name = field_name.substr(0,extra_stuff);
								}//closes extra stuff class remover if
								alert(filter_array[c][2]);
							labels[i].setAttribute("class",curr_error);
							labels[i].setAttribute("className",curr_error);
							labels[i].appendChild(fix_gif);
							labels[i].scrollIntoView(top);
							the_Y = (document.all)?document.documentElement.scrollTop:window.pageYOffset;
							window.scrollTo(0,the_Y);
							check_input.focus();
							return false;
							break;
							
							}//closes regex text fail if
				}//what does this close? an else part? the function fails without it...
						
					
				}//closes array loop for non req regex tests
			
			
			
			}//closes the else part
		}//closes not null label loop
	}//closes label loop
	
	}//closes the function






//Old comments for the older, more explicit validation. I don't want to erase it yet.

	//A new validation function. The first part preloads an image that is placed next
	//to fields that have not been filled in correctly. The second part grabs all the
	//label elements, loops through them, and gets the for attribute values and the 
	//class attribute values. If the class-req, it performs a validation, returning
	//an alert on an error, refocusing, and adding an image. 
	//Need to put the image next to the input, rather than in the label.
		
	//The first loop is to reset the classes off of the error class. This was done so that
	//if there is more than one required field, the error class doesn't stick after the
	//respondent fills in one field but not the other. (It might be confusing if the error
	//class stuck even after the question was answered.) The two var statements set the 
	//labelClass and labelfor vars to null. This was done because of a cross-browser 
	//problem. IE doesn't get != null, but does get != '', and vice-versa for firefox. This
	//way, null is null for each browser. The if statement that checks for null was added
	//because the label loop is really long, but not all labels have classes. Without this
	//check, the code would look for a variable without properties.
	//Both loops- the one that resets the classes and the one that does the actual 
	//validation- use these var and if statements.
	//Added a few lines to store the current class and append or remove just the 'error'
	//part, so that if there are more class settings that 'req' in a label, those settings
	//are preserved.
	//Added a couple lines to reposition the page after the on focus. The screen should
	//now put the current label at the top of the screen, ensuring that it is still 
	//visible after the onfocus. (The onfocus was putting the field at the very top of the
	//screen rather than the label, which was cut off on the scroll.) Had trouble doing this
	//with IE6- hence the lines with scrollTop and scrollTo. Need someone to look at it in
	//IE7.
	//Added a couple of lines to capture the actual text of a label and to use that in
	//the alert message. There's an if statement in there that gets rid of anything including
	//and after a '<'. The if part is in there because of the image add on a validation fail.
	//Add regex to check email syntax? If id is set to email, run it through the test...
	//Branch it off around line 70? If it's not email proceed...if it is an email field, run
	//the regex on it...

//Works now based on labels to validate required emails,zips,dates, and general text fields.

/*Added a new loop. It looks for labels with classes that aren't required. It goes through those and looks for email, date, and zip validations. If they are due for one of those checks, that check is dependent on whether anything has been put in the field. If it's blank, there's no check. If there's something in there, whatever it is has to conform to he regex before the form can be submitted.*/

/*function validateFormx() {
		var email_filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
		var zip_filter = /^(\d{5}-\d{4})|(\d{5})$/i
		var date_filter = /^(?=\d)(?:(?:(?:(?:(?:0?[13578]|1[02])(\/|-|\.)31)\1|(?:(?:0?[1,3-9]|1[0-2])(\/|-|\.)(?:29|30)\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})|(?:0?2(\/|-|\.)29\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))|(?:(?:0?[1-9])|(?:1[0-2]))(\/|-|\.)(?:0?[1-9]|1\d|2[0-8])\4(?:(?:1[6-9]|[2-9]\d)?\d{2}))($|\ (?=\d)))?(((0?[1-9]|1[012])(:[0-5]\d){0,2}(\ [AP]M))|([01]\d|2[0-3])(:[0-5]\d){1,2})?$/;
		var labels = document.getElementsByTagName('label');
		for (var i=0; i<=labels.length-1; i++){	
			var labelClass = null;
			var labelfor = null;
			var labelfor=labels[i].getAttribute('for')?labels[i].getAttribute('for'):labels[i].getAttribute('htmlFor');
			var labelClass =labels[i].getAttribute('class')?labels[i].getAttribute('class'):labels[i].getAttribute('className');
			if (labelfor != null && labelClass != null)  {
				if (labelClass.indexOf('req') != -1) {
					fixed_class = labelClass.replace(/error/i,'');
					labels[i].setAttribute("class",fixed_class);
					labels[i].setAttribute("className", fixed_class);				
						}//the innermost if
					}//the outermost if
				}//the loop		
			
			
		for (var i=0; i<=labels.length-1; i++){	
			var labelClass = null;
			var labelfor = null;
			var labelfor=labels[i].getAttribute('for')?labels[i].getAttribute('for'):labels[i].getAttribute('htmlFor');
			var labelClass =labels[i].getAttribute('class')?labels[i].getAttribute('class'):labels[i].getAttribute('className');
			
			if (labelfor != null && labelClass != null) {
				if (labelClass.indexOf('req') != -1) {
					var curr_error = labelClass + ' error';
					var check_input = document.getElementById(labelfor);	
				//Add validation regex array loop here.	
				//Then switch the index value to the array spot
					if (labelClass.indexOf('email') != -1) { 
						if (email_filter.test(check_input.value) == false)
						{
							var field_name = labels[i].innerHTML;
								if (field_name.indexOf('<') != -1){
									var extra_stuff = field_name.indexOf('<');
									field_name = field_name.substr(0,extra_stuff);
								}
					
							alert('Please check the email address in the ' + field_name + ' field!');
							labels[i].setAttribute("class",curr_error);
							labels[i].setAttribute("className",curr_error);
							labels[i].appendChild(error_gif);
							labels[i].scrollIntoView(top);
							the_Y = (document.all)?document.documentElement.scrollTop:window.pageYOffset;
							window.scrollTo(0,the_Y);
							check_input.focus();
							return false;
							break;
							}//Closes email fail statement
							}//Closes email string search
							//Close a hypothetical regex loop here.
						
						
						else if (labelClass.indexOf('zip') != -1) {
							if (zip_filter.test(check_input.value) == false)
								{
								var field_name = labels[i].innerHTML;
								if (field_name.indexOf('<') != -1){
									var extra_stuff = field_name.indexOf('<');
									field_name = field_name.substr(0,extra_stuff);
								}
							alert('Please check the zip code in the ' + field_name + ' field!');
							labels[i].setAttribute("class",curr_error);
							labels[i].setAttribute("className",curr_error);
							labels[i].appendChild(error_gif);
							labels[i].scrollIntoView(top);
							the_Y = (document.all)?document.documentElement.scrollTop:window.pageYOffset;
							window.scrollTo(0,the_Y);
							check_input.focus();
							return false;
							break;
							}//closes zip fail statement
						}//closes zip string search
						
						else if (labelClass.indexOf('date') != -1) {
							if (date_filter.test(check_input.value) == false)
								{
								var field_name = labels[i].innerHTML;
								if (field_name.indexOf('<') != -1){
									var extra_stuff = field_name.indexOf('<');
									field_name = field_name.substr(0,extra_stuff);
								}
								alert('Please check the date in the ' + field_name + ' field!');
								labels[i].setAttribute("class",curr_error);
								labels[i].setAttribute("className",curr_error);
								labels[i].appendChild(error_gif);
								labels[i].scrollIntoView(top);
								the_Y = (document.all)?document.documentElement.scrollTop:window.pageYOffset;
								window.scrollTo(0,the_Y);
								check_input.focus();
								return false;
								break;
							}//closes date fail statement 
						}//closes date string search
						
						else{
							if (check_input.value.length == 0) {
								var field_name = labels[i].innerHTML;
								if (field_name.indexOf('<') != -1){
									var extra_stuff = field_name.indexOf('<');
									field_name = field_name.substr(0,extra_stuff);
									}
								alert('You must complete the ' + field_name + ' field!');
								labels[i].setAttribute("class",curr_error);
								labels[i].setAttribute("className",curr_error);
								labels[i].appendChild(error_gif);
								labels[i].scrollIntoView(top);
								the_Y = (document.all)?document.documentElement.scrollTop:window.pageYOffset;
								window.scrollTo(0,the_Y);
								check_input.focus();
								return false;
								break;
								}//closes regular fail statement
						}//Closes else string search
				
				}//Ends the check on different types of requireed fields
				else{//else looks for syntax checks that aren't required. 
				//they only check if the length of the response is greater than 0
				//they don't reset the label class but still show the error icon
					
					var curr_error = labelClass + ' error';
					var check_input = document.getElementById(labelfor);
							
					if (labelClass.indexOf('email') != -1 && check_input.value.length > 0) { 
						if (email_filter.test(check_input.value) == false)
						{
							var field_name = labels[i].innerHTML;
								if (field_name.indexOf('<') != -1){
									var extra_stuff = field_name.indexOf('<');
									field_name = field_name.substr(0,extra_stuff);
								}
					
							alert('Please check the email address in the ' + field_name + ' field!');
							//labels[i].setAttribute("class",curr_error);
							//labels[i].setAttribute("className",curr_error);
							labels[i].appendChild(error_gif);
							labels[i].scrollIntoView(top);
							the_Y = (document.all)?document.documentElement.scrollTop:window.pageYOffset;
							window.scrollTo(0,the_Y);
							check_input.focus();
							return false;
							break;
							}//Closes email fail statement
							}//Closes email string search
							
							
						if (labelClass.indexOf('zip') != -1 && check_input.value.length > 0) { 
						if (zipr.test(check_input.value) == false)
						{
							var field_name = labels[i].innerHTML;
							if (field_name.indexOf('<') != -1){
								var extra_stuff = field_name.indexOf('<');
								field_name = field_name.substr(0,extra_stuff);
								}
					
							alert('Please check the zip code in the ' + field_name + ' field!');
							//labels[i].setAttribute("class",curr_error);
							//labels[i].setAttribute("className",curr_error);
							labels[i].appendChild(error_gif);
							labels[i].scrollIntoView(top);
							the_Y = (document.all)?document.documentElement.scrollTop:window.pageYOffset;
							window.scrollTo(0,the_Y);
							check_input.focus();
							return false;
							break;
							}//Closes zip fail statement
							}//Closes zip string search
							
						if (labelClass.indexOf('date') != -1 && check_input.value.length > 0) { 
						if (date_filter.test(check_input.value) == false)
						{
							var field_name = labels[i].innerHTML;
								if (field_name.indexOf('<') != -1){
									var extra_stuff = field_name.indexOf('<');
									field_name = field_name.substr(0,extra_stuff);
								}
					
							alert('Please check the date in the ' + field_name + ' field!');
							//labels[i].setAttribute("class",curr_error);
							//labels[i].setAttribute("className",curr_error);
							labels[i].appendChild(error_gif);
							labels[i].scrollIntoView(top);
							the_Y = (document.all)?document.documentElement.scrollTop:window.pageYOffset;
							window.scrollTo(0,the_Y);
							check_input.focus();
							return false;
							break;
							}//Closes date fail statement
							}//Closes date string search


				
				}//closes else statment for non-required syntax checks
				//add else to for non-req fields?
			
			}//closes the outermost if
			}//closes the loop
			return true;
		}// closes the function
		*/
		
