/*
]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]
jsFORM-CHECK version 1.1
AUTO-FORM VALIDATOR 

WRITTEN BY:
Jordan M. Mrazek
jordanm@tea-corp.com
Technology Specialist
Technology Engineers & Architects, LLC
201-226-9333 ext. 12
MCSE


REVISION HISTORY
5/19/2004 started Version 1.0 
5/20/2004 initial development & enhancements to functionality

]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]



THIS CODE-BASE PROCESSES FORM DATA USING THE ELEMENT-LEVEL INSTRUCTIONS DEFINED IN THE PASSED ARRAYS

This code will loop thru the form elements and based on what type the element is,
it will validate their requirements based on the instructions in the element-level array.
This code only checks for the existence of a value, it does not perform numeric vs. alpha-only validation.

this file recieves the following instructions from the form source file;

FORM ARRAY INSTRUCTIONS - ELEMENT-LEVEL INSRUCTION ARRAY [SEE 'MYARRAY' BELOW]
NACK ARRAY
ACK ARRAY
OTHER ARRAY

/* 
INSRUCTIONS - FILL THE ARRAY WITH FORM-SPECIFIC DATA
	
		myarray[0][0]	=	'FIELD' (enter HTML form element name, case-sensitive) 
		myarray[0][1]	=	'requirement type
			values 	0 <not required>

					1 <required>

					2 <required 'IF YES THEN' type, this is required only if the pre-requisite question is affirmative (checks against NACKS array), 
						specify pre-req. form element name in myarray[0][3]> 

					3 <required 'IF NO THEN' type, this is required only if the pre-req. question is affirmative (checks against ACKS array), 
						specify pre-req. form element name in myarray[0][3]> 
					
					4 <required 'IF OTHER THEN' type, this is required only if the pre-req. question is 'other' (checks against other array), 
						specify pre-req. form element name in myarray[0][3]> 
					
					CURRENTLY TYPE 4 IS ONLY ENABLED FOR RADIO ELEMENT TYPES
					
		myarray[0][2]	=	'<response message insert>'	
		myarray[0][3]	=	'<pre-requisite form element (only if MYARRAY[0][1] is > 1)>'	
*/
		
		/*
		This array provides allowed ACK responses for processing fields that are pre-requisite on other fields ('if yes then')
			var Acks=new Array(20);
				Acks[0]='YES';
				Acks[1]='Yes';
				Acks[2]='yes';
				Acks[3]='Y';
				Acks[4]='y';
				Acks[5]='TRUE';
				Acks[6]='True';
				Acks[7]='true';
				Acks[8]='T';
				Acks[9]='t';
				Acks[10]='1';
				Acks[11]='OK';
				Acks[12]='ok';
		
		This array provides allowed NACK responses for processing fields that are pre-requisite on other fields ('if yes then')
			var Nacks=new Array(20);
				Nacks[0]='NO';
				Nacks[1]='No';
				Nacks[2]='no';
				Nacks[3]='0';
				Nacks[4]='FALSE';
				Nacks[5]='False';
				Nacks[6]='false';
				Nacks[7]='F';
				Nacks[8]='f';
		
		This array provides allowed OTHER responses for processing fields that are pre-requisite on other fields ('if other then')
			var Others=new Array(20);
				Others[0]='OTHER';
				Others[1]='Other';
				Others[2]='other';
				Others[3]='';
		*/



/*
alert(myarray[56][0]);
alert(myarray[56][1]);
alert(myarray[56][2]);
alert(myarray[56][3]);
*/





/*	BEGIN jsFORM-CHECK		*/

/* 
define globals
*/
	var selectedvalue='';
	var prereq=0;
	var checkreq=0;


/* 
define a two-dimensioanl array to hold per-element processing instructions
*/

	var myarray=new Array(form.elements.length+10);
	for (i=0; i <form.elements.length+10; i++){
	myarray[i]=new Array(4);
	}

/* 
arrays for pre-requisite questions processing.
*/
	var Acks=new Array(20);
	var Nacks=new Array(30);
	var Others=new Array(30);

	







function Process (form) {

/*	BEGIN PROCESSOR		*/

  for (var e = 0; e < form.elements.length; e++) {
	//alert('1');
    //alert(form.elements[e].name);

	//if (form.elements[e]){
	   // alert('2');
		//alert(myarray.length);
		//alert(form.elements[e].value);
		
		getselectedvalue (form.elements[e]); 
		prereq=0;

		if (selectedvalue == ''){
			//alert(form.elements[e].name);
			//alert('selectedvalue:'+selectedvalue);
		
			  exit_array_loop: /* this loop will run thru the configuration array (myarray) looking to match the current form element index's name in order to get processing instructions */
			  for (var x = 0; x < myarray.length; x++) {
			  		
						if (myarray[x][0] !=''){
				
							if (form.elements[e].name == myarray[x][0]){
								//alert('4');
							    //alert(myarray[x][0]);
				
								checkreq=myarray[x][1];
								//alert(checkreq);
								if (checkreq !='' || checkreq > 0){
										var el = form.elements[e];
									    if (el.type == 'text' || el.type == 'textarea' || el.type == 'password' || el.type == 'file' ) { 
											if (checkreq==1){
											      if (el.value == '') {
												        //alert(x);
											        alert(myarray[x][2]+' is required. Please provide an answer to continue, or enter N/A if not applicable.');
													//alert('Please fill out the text field ' + el.name);
											        el.focus();
											        return false;
											      }
											}
											
											if (checkreq==2 || checkreq==3 || checkreq==4){
											  /*
												  This field has a pre-requisite field. 
												  The following will look at the value 
												  of the specified prerequisite field 
												  (myarray[0][3]) 
											  */
		
												var checkprereq=myarray[x][3];
												if (checkprereq !=''){
												  //alert (myarray[x][0]);
												  //alert (myarray[x][1]);
													//alert('checkprereq:'+checkprereq);
			
													exit_prereq_loop: /* this loop will run thru the form elements, looking for the pre-requisite form element in order to check the selected answer*/
													for (var p = 0; p < form.elements.length; p++) {
														if (form.elements[p].name == myarray[x][3]){
																//alert(form.elements[p].value);
																 
																 getselectedvalue (form.elements[p]); 
																 
															 getreqtype (selectedvalue, checkreq);

																//alert('return prereq:'+prereq);
																//alert('pre-req selectedvalue:'+selectedvalue);
														  
															  if (prereq==1){
																//alert('need');
															      if (el.value == '') {
																        //alert(x);
															        alert(myarray[x][2]+' is required. Please provide an answer to continue.');
																	//alert('Please fill out the text field ' + el.name);
															        el.focus();
															        return false;
															      }
															  }
														break exit_prereq_loop; /* found the pre-requisite field, no need to continue */
														}
													}
												}
											}
											
										}
									    else if (el.type.indexOf('select') != -1) {
									      if (el.selectedIndex == -1) {
										    alert(myarray[x][2]+' is required. Please select an answer to continue.');
									        //alert('Please select an answer for '+myarray[x][2]+' to continue.');
									        //alert('Please select a value of the select field ' + el.name);
									        el.focus();
									        return false;
									      }
									    }
									    else if (el.type == 'radio') {
										
										  if (checkreq==1){
											//alert('radio required 1');
											  /*
												  This field is required. The following code will 
												  prompt for a value if there is more than one group element
											  */
											  var group = form[el.name];
										      var checked = false;
										      if (!group.length)
										        checked = el.checked;
										      else
										        for (var r = 0; r < group.length; r++)
										          if ((checked = group[r].checked))
										            break;
										      if (!checked) {
										        /*
												alert(x);
										        alert(myarray[x][0]);
										        alert(myarray[x][1]);
										        alert(myarray[x][2]);
										        alert(myarray[x][3]);
												*/
										        alert(myarray[x][2]+' is required. Please choose an answer to continue.');
												//alert('Please choose an answer for '+myarray[x][2]+' to continue.');
												//alert('Please check one of the radio buttons ' + el.name);
										        el.focus();
										        return false;
										      }
										  }
		
										  
										  if (checkreq==2 || checkreq==3 || checkreq==4) {
											  /*
												  This field has a pre-requisite field. 
												  The following will look at the value 
												  of the specified prerequisite field 
												  (myarray[0][3]) 
											  */
		
											var checkprereq=myarray[x][3]
											if (checkprereq !=''){
												//alert('Field:'+myarray[x][0]);
												//alert('checkprereq:'+checkprereq);
		
											  exit_prereq_loop: /* this loop will run thru the form elements, looking for the pre-requisite form element in order to check the selected answer*/
											  for (var p = 0; p < form.elements.length; p++) {
													if (form.elements[p].name == myarray[x][3]){
															
															//alert('pre-req. Field '+form.elements[p].name);
															
															getselectedvalue (form.elements[p]); 
																//alert('pre-req selectedvalue:'+selectedvalue);

															 getreqtype (selectedvalue, checkreq);
																//alert('return prereq:'+prereq);
																 
																 
															
															//selectedvalue = form.elements[p].value;
															//alert('prereq selectedvalue:'+selectedvalue);
													  
														//alert('prereq:'+prereq);
													  
													  if (prereq==1){
														//alert('need');
														  var group = form[el.name];
					
													      var checked = false;
													      if (!group.length)
													        checked = el.checked;
													      else
													        for (var r = 0; r < group.length; r++)
													          if ((checked = group[r].checked))
													            break;
													      if (!checked) {
													        alert(myarray[x][2]+' is required. Please choose an answer to continue.');
													        //alert('Please select an answer for '+myarray[x][2]+' to continue.');
															//alert('Please check one of the radio buttons ' + el.name);
													        el.focus();
													        return false;
													      }
					
													  }
									    			
													break exit_prereq_loop; /* found the pre-requisite field, no need to continue */
													}
												}
											  
											  }
											  
											  
										  }
									    }
									    else if (el.type == 'checkbox') {
									      var group = form[el.name];
									      if (group.length) {
									        var checked = false;
									        for (var r = 0; r < group.length; r++)
									          if ((checked = group[r].checked))
									            break;
									        if (!checked) {
										        alert(myarray[x][2]+' is required. Please choose an answer to continue.');
										      //alert('Please select an answer for '+myarray[x][2]+' to continue.');
									          //alert('Please check one of the checkboxes ' + el.name);
									          el.focus();
									          return false;
									        }
									      }
									    }
						
								}
		
			    			break exit_array_loop;
							}
				
						}
		
			  }
		}

	//}
  }
  return true;
}

function getselectedvalue (sl){
/*
This function reads a form element and returns it's value
*/	
selectedvalue='';

	if (sl.type == 'text' || sl.type == 'textarea' || sl.type == 'password' || sl.type == 'file' ) { 
		selectedvalue = sl.value;
	}
	
	else if (sl.type == 'radio') {
	//alert('sl.name:'+sl.name);
	
	    var aPreReqRadios = document.getElementsByName(sl.name);	
		for (var w = 0; w < aPreReqRadios.length; w++) {
	            
			 if (aPreReqRadios[w].checked) selectedvalue = aPreReqRadios[w].value;
		}
		//alert('in lookup lookup selected:'+selectedvalue);
	}
	
	else if (sl.type == 'checkbox') {
	
	    var aPreReqRadios = document.getElementsByName(sl.name);	
		for (var w = 0; w < aPreReqRadios.length; w++) {
	            
			 if (aPreReqRadios[w].checked) selectedvalue = aPreReqRadios[w].value;
		}
	}
									
	else if (sl.type.indexOf('select') != -1) {
			selectedvalue = sl.options[sl.selectedIndex].value;
	}
	
return 	selectedvalue;

}

function getreqtype (selectedvalue, checkreq){
//alert('getreqtype selectedvalue'+selectedvalue);
	if (checkreq==2){ 
		/*This is 'if yes then's, compare against a list 
		of negative responses, and clear requirement if 
		negative answers were picked in the perequisite question 
		(makes an answer required on everything but the list of NACKS)*/

		//alert('checkreq:'+checkreq);

		prereq=1; /*make required*/
		for (var z = 0; z < Nacks.length; z++) {
			/* check the list of NACK responses */
				//alert('NACK:'+Nacks[z]);
		
			if (selectedvalue == Nacks[z]){
				//alert('match');
				//alert (Nacks[z]);
				/* the pre-requisite field provided an allowed NO response.  */
				prereq=0; /*clear if NACK received in previous question*/
				break;
			}
		}
	//alert('prereqtype'+prereq);
	}
	
	if (checkreq==3){
		/*This is "IF NO THEN's", compare against a list 
		of affirmative responses, and clear requirement if 
		affrimative answers were picked in the perequisite question 
		(makes an answer required on everything but the list of ACKS)*/

		//alert('checkreq:'+checkreq);

		prereq=1;
		for (var z = 0; z < Acks.length; z++) {
			/* check the list of ACK responses */
			if (selectedvalue == Acks[z]){

				/* the pre-requisite field provided an allowed YES response.  */
				prereq=0; /*clear if ACK received in previous question*/
				break;
			}
		}
	}

	if (checkreq==4){
		/*This is "IF OTHER THEN's", compare against a list 
		of OTHERS responses, and clear requirement if 
		'other' answers were picked in the pre-requisite question 
		(makes an answer required on OTHERS answers )*/

		//alert('checkreq:'+checkreq);

		prereq=0;
		for (var z = 0; z < Others.length; z++) {
			/* check the list of OTHERS responses */
			if (selectedvalue == Others[z]){

				prereq=1; /*require on OTHER received in previous question*/
				break;
			}
		}
	}

return prereq, checkreq;
}



