/**************************************
  quick documentation
				input (val)
						 checkLen - checks length
						 			- [min] | [max] => val="checkLen" min="4" max="255"
						 noequal - makes sure it is not equal to
						 			- [noequal] => noequal="xyz"
						 equal - makes sure it is equal to
						 			- [equal] => eq="xyz"
										Note: you can allow scripts in there. Initiate this by using "scr:" in the begginning. Casing matters.
										e.g. val="equal" eq="src:documents[0].forms[0].value"
						 email - is email address
						 			- none => val="email"
						 int - is integer
						      - [imin] | [imax] => val="int" imin="4" imax="5"
						 reg - is regular expression // please note that you are responsible for checking your own regular expressions
						      - exp => ??
						 chars - characters only
						      - [min] | [max] => val="chars" min="0" max="244"
						 flt - float
						      - [min] | [max] | [minDec] | [maxDec] => val="flt" min="4" max="255" minDec="2" maxDec="255"
						 checkValAjax - run a check off an Ajax page (most likely to see if the the entry is empty)
						 						  - oPage - this is the ID where the output will be compared to 
													- rPage - this is the URL where the script needs to run
						 							- note: that you still need to run validateForm() function
													- note: it is NOT recommended to use this as a final validator - use it as a way to inform the users that 
																	their input is/isn't correct
				select
						 noequal - make sure it is not equal to
						 		 - [noequal] => noequal="xyz"
this script is a GNU script, created by Umang Beri. For the latest version of this script, go to umaniac.com

**************************************/

function validateForm ( q , options ) {
				 // bground , border , message
				 var i = 0;
				 if ( options.bground == undefined ) {
				 		var bground = '#ffffff';
				 } else {
				 	  var bground = options.bground;
				 } 
				 if ( options.border == undefined ) { 
				 		var border = '0px solid #333333';
				 } else {
				 	  var border  = options.border;
				 }

				 var message = options.message;

				 if ( options.form == 'element' ) {
				 		var form = 'element';
				 } else {
				 	  var form = 'form';
				 }
				 
				 if ( options.ajax == true ) {
				 		var ajax = true;
				 } else {
				 	  var ajax = false;
				 }

				 var j = 0;
				 // alert(q.length);
				 var bool = true;
				 var checker = true;
/**/
				 var classArray;
				 var radioObject = new Object();
				 var len = 0;
				 var inputStyle = "border: 1px solid red; background: #F7FFAF; color = #666;";
				 var message = '';
				 var filter;
				 var foundName;

				 if ( form == 'element' ) {
				 		switch ( q.tagName ) {
									 case 'INPUT':
									 			switch ( q.type ) {
													case 'radio':
													break;
													case 'text':
														
													break;
												}
									 			break;
									 case 'SELECT':
									 			break;
									 case 'TEXTAREA':
									 			break;
						}
				 }

/************** get the radio info **********/
				 for ( i=0; i<q.length; i++) {
				 	  if ( ( q[i].tagName == "INPUT" ) && ( q[i].type == "radio" ) ) {
							for ( var r in radioObject ) {
									foundName = false;
									if ( r == q[i].name ) { // there is a match in the name
										  foundName = true;
											if ( radioObject[r] == "" ) {
												 radioObject[q[i].name] = q[i].getAttribute("val");
											}
									}
							} // end for loop
							if ( !foundName ) {
								 radioObject[q[i].name] = q[i].getAttribute("val");
							}
					 } // end if
				 } // end for loop
/************* end radio *******************/

				    i = 0;
				 		for ( i=0; i<q.length; i++ ) {
							if ( ( q[i].getAttribute("type") == "submit" ) || ( q[i].getAttribute("type") == "file" )  ) {} else {
				 		 	 q[i].style.border = border;
							 q[i].style.background = bground;
							 }

/*************** input *************/

				 		 	 if ( ( q[i].tagName == "INPUT" ) && ( ( q[i].type == "text" ) || ( q[i].type == "password" )) ) {
							 if ( q[i].getAttribute("val") ) {
									classArray = q[i].getAttribute("val").split(" ");
									len = classArray.length ;

									for (var j = 0; j < len; j++ ) {
											
											switch ( classArray[j] ) {

/*********** checkValAjax **********/
						 						case 'checkValAjax':
														 if ( ajax == false ) { break; } // ensure that we need it
														 if ( ( q[i].getAttribute('rPage') ) && ( q[i].getAttribute('oPage') ) ) {
														 /*********** using prototype *******/
														 		new Ajax.Updater(
																		q[i].getAttribute('oPage'),
																		q[i].getAttribute('rPage'),
																		{ parameters: { p1: q[i].value } , method: 'get' , 
																		onCreate: function() { checker = false; }, 
																		onSuccess: function(t) 
																		  {
																					for ( j = 0 ; j < q.length ; j++ ) {
																							if ( ( q[j].getAttribute('rPage') ) && ( q[j].getAttribute('oPage') ) ) {
																								 if ( t.responseText == q[j].getAttribute('noEqualAjax') ) {
																								 		q[j].style.cssText = inputStyle;
																										message = 'No';
																								 		bool = false;
																								 }
																							} // end if
																					}	// end for loop
																					checker = true;
																		  }
																    }
														    );
																/********** // end prototype ******/
														 }
														 break;
/*********** length ****************/
												case 'checkLen':
														 if ( q[i].getAttribute('min') ) {
														 		if ( isInt(q[i].getAttribute('min') ) ) {
																	 var y = q[i].getAttribute('min');
																	 if ( y < 1 ) {
																	 		y = 1;
																	 }
																}
  														  if ( q[i].value.length < y ) {
  														 		q[i].style.cssText=inputStyle;
  																message = 'Value of String must be at least ' + y + ' characters';
  																bool = false;
  																break;
  														  }
														 }


														 if ( q[i].getAttribute('max') ) {
														 		if ( isInt(q[i].getAttribute('max') ) ) {
																	 var y = q[i].getAttribute('max');
																	 if ( y < 1 ) {
																	 		y = 1;
																	 }
																}
  														  if ( q[i].value.length > y ) {
  														 		q[i].style.cssText=inputStyle;
  																message = 'Value of String must be at least ' + y + ' characters';
  																bool = false;
  																break;
  														  }

														 }
														 break;
/************ email ***************/
												case 'email':
														 filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
														 // if ( !q[i].value.match('@') ) {
														 // the following regular expression obtained from quirksmode
														 if ( filter.test(q[i].value) ) { } else {
																q[i].style.cssText = inputStyle;
																message = 'Email address is invalid';
																bool = false;
														 } 
														 break;
/************ match ***************/
												case 'match':
														 // filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
														 // if ( !q[i].value.match('@') ) {
														 // the following regular expression obtained from quirksmode
														 // if ( filter.test(q[i].value) ) { } else {
														 		tempBool = false;
														 		for ( var j=0; j<q.length; j++) {
																		if ( ( q[j].tagName == 'INPUT' ) && ( q[j].getAttribute('name') == q[i].getAttribute('match') ) ) {
																			 tempBool = true;			 
																		}
																}
																if ( tempBool == false ) {
																	 q[i].style.cssText = inputStyle;
																	 bool = false;
																}
																
																// q[i].style.cssText = inputStyle;
																// message = 'Email address is invalid';
																// bool = false;
														 // } 
														 break;
/************** noequal ***********/
												case 'noequal':
														 if ( q[i].getAttribute('noequal') == q[i].value ) {
																// alert(q[i].value + ' ' + q[i].getAttribute['noequal']);
																q[i].style.cssText=inputStyle;
																message = 'This cannot equal ' + q[i].value;
														 		bool = false;
																break;
														 }
														 break;
/************** equal ***********/
												case 'equal':
														 if ( q[i].getAttribute('eq').match(/^scr:/)) {
															 var val = q[i].getAttribute('eq').split(':');
															 var val2 = eval(val[1]);
														 } else {
														 	 var val2 = q[i].getAttribute('eq');
														 } 
														 if ( val2.toLowerCase() != q[i].value.toLowerCase() ) {
																// alert('Value: ' + q[i].value + ' <<>> Attribute: ' + q[i].getAttribute('eq'));
																q[i].style.cssText=inputStyle;
																message = 'This is not equal to ' + q[i].value;
														 		bool = false;
																break;
														 }
														 break;

/************** isInt *************/
												case 'int':
														 filter = /\D/;
														 filter2 = /[a-z]/i;
														 q[i].value = trim(q[i].value);
														 var vMin = parseInt(q[i].getAttribute("imin"));
														 var vMax = parseInt(q[i].getAttribute("imax"));
														 var vVal = parseInt(q[i].value);
														 if ( q[i].value == '' ) {
														 		vVal = 0;
														 }
														 if ( filter.test(vVal) ) {
														 		if ( filter2.test(vVal) ) {
																} else 
														 		if ( ( isNaN(vVal) ) && ( vMin == 0 ) ) {
																	 break;
																}
														 		q[i].style.cssText = inputStyle;
																message = 'Integer is required';
																bool = false;
														 }
														 // alert(q[i].getAttribute("min") + " > " + q[i].value);
														 if ( vMin > vVal) {
														 		// alert(q[i].getAttribute("min") + " > " + q[i].value);
																q[i].style.cssText = inputStyle;
																message = 'Integer is required to have a higher value';
																bool = false;
														 }

														 if ( vMax < vVal ) {
														 		// alert(q[i].getAttribute("max") + " < " + q[i].value);
																q[i].style.cssText = inputStyle;
																message = 'Integer is required to have a lower value';
																bool = false;
														 }
														 break;
/************** reg *************/
										    case 'reg':
														 if ( q[i].getAttribute("exp") ) {
																// filter = /q[i].getAttribute("exp")/i;
																filter = new RegExp(q[i].getAttribute("exp"));
														 		if ( filter.test(q[i].value) ) {} else {
																	 q[i].style.cssText = inputStyle;
																	 message = 'Regular Expression not matched';
																	 bool = false;
																}				 		
														 }
														 break;
											}
									} // end for loop (j)
/***********************************/
							 } // end if "val"
							 } // end if "input"

/*************** select ************/							 

				 		 	 if ( q[i].tagName == "SELECT" ) {
 						 		// classArray.length = 0;
							 		if ( q[i].getAttribute("val") == null ) {}
									else {
										 classArray = q[i].getAttribute("val").split(" ");
	 									 len = classArray.length;
									}

/**/
									for (var j = 0; j < len; j++ ) {

											switch ( classArray[j] ) {
/*********** noequal ****************/
												case 'noequal':
														 // ( q[i].options[selectedIndex].value )
														 // alert(q[i].selectedIndex);
														 if ( q[i].selectedIndex == q[i].getAttribute("noequal") ) {
														 		q[i].style.cssText=inputStyle;
																message = 'This cannot equal ' + q[i].value;
																bool = false;
														 }
														 break;

/*********** xxxxxxx ****************/

											} // end switch

									 } // end for loop of length
/**/
							 } // end if == select

/*************** radio ************/
				 		 	 if ( ( q[i].tagName == "INPUT" ) && ( q[i].type == "radio" ) ) {
							 		if ( radioObject[q[i].name] == "" ) {
										 radioObject[q[i].name] = true;
									} // end for loop of length
									else if ( radioObject[q[i].name] == "req" ) {
										 if ( q[i].checked == true ) {
										 		radioObject[q[i].name] = true;
										 }
										 // radioObject[q[i].name] = true;
									}
/**/
							 } // end if == radio

/************** textarea ************/
				 		 	 if ( q[i].tagName == "TEXTAREA" ) {
 						 		classArray.length = 0;
							 		if ( q[i].getAttribute("val") == null ) {}
									else {
										 classArray = q[i].getAttribute("val").split(" ");
									}
									len = classArray.length;
/**/
									for (var j = 0; j < len; j++ ) {

											switch ( classArray[j] ) {
/*********** noequal ****************/
												case 'noequal':
														 if ( q[i].selectedIndex == q[i].getAttribute("noequal") ) {
														 		q[i].style.cssText=inputStyle;
																message = 'This cannot equal ' + q[i].value;
																bool = false;
														 }
														 break;

/*********** checkLen ***************/
						 					  case 'checkLen':
														 if ( q[i].getAttribute('min') ) {
														 		if ( isInt(q[i].getAttribute('min') ) ) {
																	 var y = q[i].getAttribute('min');
																	 if ( y < 0 ) {
																	 		y = 1;
																	 }
																}
    														if ( q[i].value.length < y ) {
    														 		q[i].style.cssText=inputStyle;
    																message = 'Value of String must be at least ' + y + ' characters';

    																bool = false;
    																break;
    														}
														 }


														 if ( q[i].getAttribute('max') ) {
														 		if ( isInt(q[i].getAttribute('max') ) ) {
																	 var y = q[i].getAttribute('max');
																	 if ( y < 1 ) {
																	 		y = 1;
																	 }
																}
    														if ( q[i].value.length > y ) {
    														 		q[i].style.cssText=inputStyle;
    																message = 'Value of String must be at least ' + y + ' characters';
    																bool = false;
    																break;
    														}
																
														 }

														 break;

											} // end switch

									 } // end for loop of length
/**/
							 } // end if == textarea
		 
				 } // end for loop for all form elements
/*********** radio buttons ************/
				 for ( var r in radioObject ) {
				 		 // alert('in here 2');
				 		 if ( radioObject[r] == "req" ) {
				 		 		for ( i=0; i<q.length; i++) {
										if ( ( q[i].tagName == "INPUT" ) && ( q[i].type == "radio" ) && ( r == q[i].name ) ) { 
											 	  q[i].style.cssText=inputStyle;
													bool = false;
										}
								}
						 } else {
						 	 // alert(r);
						 }
				 }

/**/

          if ( bool == true ) { 
          	 		bool = 'accept';
          } else {
								bool = false;
          }
					return bool;
				 
}

function isInt(x) {
   var y=parseInt(x);
   if (isNaN(y)){ 
	 		// alert(false);
			return false;
	 }
	 	 		// alert(true);
   // return x==y && x.toString()==y.toString();
	 return true;
} 


function trim (str) { return str.replace(/^\s\s*/, '').replace(/\s\s*$/, ''); };