/* JavaScript: Class PAAS Form Validation (alias: jsFormValidation)functions: to provide generic validation of input and selection (etc) fields.			on entry, err{a1} is forced hiddent and local message is removed. 					lbl{a1} colour changed to none					txt{a1} highlight is off	returns: 	true = field validates successfully			false = error detected, err{A1} is unhidden and populated with local message					lbl{a1} is turned to red					txt{A1} is highlighted	created: 2009-07-13 pb, rwlast updated: 2009-07-21 pb - added clear method				2009-07-29 pb - added select, check, length, dateRangemethods:	validator.check	validator.clear	supported check types;text - must be text, not optional	text rules:	must be (everything but semicolon(;)). reserved for multivalue field content	- optional text - can be blank, if not blank, cannot contain semicolon	select - has a selection been madecheck - has a check box or radio button been checked offnumber - is value blank? if not, is value a number?value - has to contain the correct or calculated valuelength - is value greater then max allowable length	format is (validator.check('length',id o/r this, length))	maxvalue - is value greater then max allowable value		format is (validator.check('maxvalue',id o/r this, value))minvalue - is value greater then min allowable value		format is (validator.check('minvalue',id o/r this, value))daterange - special note, the two values past to the class MUST by the IDs of the two String dat fields			- see how the date are formated in English.......			CHECK TYPES NOT CODED YET	email - phone number - postal code -class initialized in sfrmCGIVarsusage: validator.check(validationType(see supported check types), fieldId, 'm'|'o'(Optional vs mandatory), if undefined,						then the option becomes 'm'. for length, the option is the max field lengthvalidator.clear('fieldId');messages are sef-contained (initially)*//* ********************************************************************** */// 2009-07-09 pb use this model when creating new functions in the javascript libraryfunction functionModel(thisStringPerhaps){  try  {  }  catch(e)  {    alert('catch exception detected in *functionModel* ' + e.message);  }}//// use the above block as a function model///* ********************************************************************** */function formValidation(){		try    {	    // prefix all local vars (hide them) with var	   var lclErrorStore = false;	   var lclFieldId = ''	   var lclFieldIdSuffix = '';	   var lclFieldValue = '';	   var lclCheckType = '';	   var lclOption = '';	   var lclSelect = '';	   	   var m1 = 'This field cannot be blank';	   var m2 = 'This field cannot contain a semicolon';	   var m3 = "No selection made"	   var m4 = "This field is not a number"	   var m5 = "Invalid currency format. Format is $nn,nnn.nn"	   var m6 = "This field cannot contain imbedded spaces"   	   var m7 = "This field exceeds the maximum length of ";    	   var m8 = "Dates are not compatible";    	   var m9 = "An expected value not found: ";    	   var m10 = "Field exceeds value of: ";    	   var m11 = "Field must be greater than: ";	   	   	   this.check = function validateItem(thisCheckType, thisFieldId, thisOption)	     {	     try{				if (thisCheckType == undefined) // if the users did not specify anything, then default to text.	     	{	     		thisCheckType = 'text';     		}	     	if (thisCheckType.constructor == String)	     	{	     		     			     		if (thisOption == undefined) // validate the option parameter	     		{	     			lclOption = 'm';     			}     			else     			{				 	if (thisOption != 'm' && thisOption != 'o')					{ lclOption = 'm'; }    				     				else {lclOption = thisOption; }     			}    			    	    			    	if (thisFieldId.constructor == String)    			    	{    		     		lclFieldId = thisFieldId;		     		lclFieldIdSuffix = thisFieldId;		     	}		     	else		     	{		     		lclFieldId = thisFieldId.name;		     		lclFieldIdSuffix = lclFieldId;		     		//alert(lclFieldIdSuffix);		     	}		     		     		if (!wrappers$()) return false; // no wrapper??? exit	     			     	//alert(fieldIdSuffix);		     	// before doing anything, check to make sure there is a proper wrapper defined		     	// for this input area. Must have lbl{A1}, txt{A1} and err{A1} spans addressable		     				     lclFieldValue = my$(); 			     lclCheckType = thisCheckType.toLowerCase();			     //alert(thisCheckType);			     clearErrorState(); // turn any error idicators off			     			     switch(lclCheckType)			     {				          case 'text': ///////// text starts here 					    // if mandatory, cannot be blank					    if (lclOption == 'm')					    {						    if (lclFieldValue == '') 					          {      						          	setErrorState(m1)						          return false;					          }	         			          // cannot contain semiColon (reserved for multivalue fields)	         			          /* 2009-11-26 pb, removed					         if (lclFieldValue.indexOf(';') > -1) 					          {      						          	setErrorState(m2)						          return false;				          	}				          	*/						    }					    else if(lclOption == 'o')					    {		     									    		if (lclFieldValue != '') 					    		{						    		 	// cannot contain semiColon (reserved for multivalue fields)						          if (lclFieldValue.indexOf(';') > -1) 					          {      						          	setErrorState(m2)						          return false;				          	}					    		}					    }				         				          return true;			          break;			          			          case 'select': ///////// select (from a drop down) starts here			          				          	lclSelect = document.getElementById(lclFieldId).selectedIndex;			          	if (!isSelected(lclFieldId))			          	{ 						   	setErrorState(m3);							return false; }			          				          	if(getSelectedText(lclFieldId) == "--Select--")						{						   	setErrorState(m3);							return false; }												return true;			          break;			              			          case 'check': ///////// check box or radio button			          				          	if (!isChecked(lclFieldId))			          	{ 						   	setErrorState(m3);							return false; 						}			          							return true;			          break;			          			          case 'number':			          					    if (lclFieldValue == '') 				          {      					          	setErrorState(m1)					          return false;				          }					    if (isNaN(lclFieldValue))			          	{			          	   	setErrorState(m4);							return false; 			          	}			          													return true;			          break;			             			          case 'nospace': // cannot contain imbedded blanks						// 2009-07-28 pb																		var tmp = trim(lclFieldValue);						if (tmp.indexOf(' ') < 0)							{							return true;							}												setErrorState(m6)						return false;							          break;			             			          case 'length': // thisOption contain the length allowed						// 2009-07-29 pb						lclOption = thisOption			          																		if (lclFieldValue.length <= lclOption)						{							return true;							}												setErrorState(m7 + lclOption)						return false;							          break;			          case 'currency':						if (isNaN(lclFieldValue))						{							if (isValidCurrency(lclFieldValue))														{ 	// reformt into current format								setText(lclFieldId, formatCurrency(lclFieldValue));									return true; 							}						    	setErrorState(m5)						    	return false;						}												if (isValidCurrency(lclFieldValue))						{ 							// reformt into current format							setText(lclFieldId, formatCurrency(lclFieldValue));								return true; 						}												setErrorState(m5)						return false;															          break			              			          case 'value':    			          						// lclFieldId is field name						// thisOption contain the value macth												if (getText(lclFieldId) == thisOption)						{							return true;						}												setErrorState(m9 + thisOption) // date fields are not compatible				     	return false;																			          break    			          case 'maxvalue':    			          						// lclFieldId is field name						// thisOption contain the value macth					    if (isNaN(getText(lclFieldId)))					    {						    setErrorState(m4); // not a number					     	return false;					    }												if (isNaN(thisOption))						{							alert('catch exception in javascript class library: jsFormValidation. function: maxvalue '+thisOption+' is not a number ');							return false;						}												if (forceNumeric(thisOption) < forceNumeric(getText(lclFieldId))) 						{ 							setErrorState(m10 + thisOption); // date fields are not compatible					     	return false;						}												return true;													          break;    			          case 'minvalue':    			          						// lclFieldId is field name						// thisOption contain the value macth					    if (isNaN(getText(lclFieldId)))					    {						    setErrorState(m4); // not a number					     	return false;					    }												if (isNaN(thisOption))						{							alert('catch exception in javascript class library: jsFormValidation. function: minvalue '+thisOption+' is not a number ');							return false;						}												if (forceNumeric(thisOption) >= forceNumeric(getText(lclFieldId))) 						{ 							setErrorState(m11 + thisOption); // date fields are not compatible					     	return false;						}												return true;													          break;	              			          case 'daterange':    			          						// lclFieldId is date 1 (ie start date)						// thisOption contain the second date field Id												var startDate = getText(lclFieldId);						var endDate = getText(thisOption);												// we are assuming that these are the string names of the two						// date fields that we are comparing and that the first field is the Start Date						// and the second field is the End Date												if (Date.parse(endDate) < Date.parse(startDate))						{							setErrorState(m8) // date fields are not compatible				     		return false;						}												return true;							          break;			     }		     }	    		return lclErrorStore;		    		}	    		catch(e)	    		{ alert('catch exception in javascript class library: jsFormValidation. function: check error: '+e.message); }	         }		   }	   catch(e)	   {         alert('catch exception in javascript class library: jsFormValidation. function: formValidation error: '+e.message);     }		  // private function to get the value of the field referenced in the fieldId	function my$()	{	   try	   {			// alert(lclFieldId+' = '+document.getElementById(lclFieldId).value);		      return document.getElementById(lclFieldId).value;	      }     	 catch(e)      	{ alert('catch exception in javascript class library: jsFormValidation. function: my$ error: '+e.message); }	}		// private function to set error state for current field	function setErrorState(errorText)	{	   try	   {	   	// 2009-11-24 pb, replace plain error text with image and mouse over text		// <img src="fullPath+'/'error.gif" alt="" border=0>		// spanText('err'+lclFieldIdSuffix , errorText);		spanText('err'+lclFieldIdSuffix , '<img src="' + fullPath + '/err.gif?openImageResource" title="' + errorText + '" border="0">');	    	unHide('err'+lclFieldIdSuffix);	    	color('lbl'+lclFieldIdSuffix, 'red');   		var className = getClass(lclFieldId); // do not highlight protected fields		if (className.indexOf('Shielded') > -1 || className.indexOf('Protected') > -1) {}		else {highlight(lclFieldId); } 				return true;	      }     	 catch(e)      	{ alert('catch exception in javascript class library: jsFormValidation. function: setErrorState error: '+e.message); }	}	// private function to clear error state for current field	function clearErrorState()	{	   try	   {         	hide('err'+lclFieldIdSuffix);         	colorOff('lbl'+lclFieldIdSuffix);         	highlightOff(lclFieldId);         	spanText('err'+lclFieldIdSuffix , '');		return true;	      }     	 catch(e)      	{ alert('catch exception in javascript class library: jsFormValidation. function: clearErrorState error: '+e.message); }	}		 // private function(s) to test if all wrapper spans are addressable	function wrappers$()	{	   try	   {	   		if (wrapper$$())	   		{ return true; }	   		else	   		{ alert('javascript class library, jsFormValidation cannot locate wrapper SPAN tags');	   		}			return false;     	      }     	 catch(e)      	{ alert('catch exception in javascript class library: jsFormValidation. function: wrappers$ error: '+e.message); }	}		function wrapper$$()	{	try{			//alert(lclFieldIdSuffix);			try{	   		 		if(getObject('lbl'+lclFieldIdSuffix) == undefined)		      		{ 						alert('validator method failure ... your label field "lbl'+lclFieldIdSuffix+'" is undefined'); 						return false; 					}		      	} 				catch(e) 				{					alert('catch exception in javascript class library: jsFormValidation. function: wrappers$$ error: '+e.message +' trying to locate field ' + 'lbl'+lclFieldIdSuffix);				}		          			try{		      if(getObject(lclFieldId) == undefined)		      { alert('validator method failure ... your label field "'+lclFieldId+'" is undefined'); return false; }		      } catch(e) {alert('catch exception in javascript class library: jsFormValidation. function: wrappers$$ error: '+e.message +' trying to locate field ' +lclFieldId);}		      		      try{		      if(getObject('err'+lclFieldIdSuffix) == undefined)		      { alert('validator method failure ... your label field "err'+lclFieldIdSuffix+'" is undefined'); return false; }    		      } catch(e) {alert('catch exception in javascript class library: jsFormValidation. function: wrappers$$ error: '+e.message +' trying to locate field ' + 'err'+lclFieldIdSuffix);}	      		      return true;	}	catch(e) 	{ alert('catch exception in javascript class library: jsFormValidation. function: wrappers$$ field: ' +lclFieldId + ' error: '+e.message); }		} 			function isValidCurrency(thisString)	{	try{	    	thisString = trim(thisString);    		// return /^\$?[1-9][0-9]{0,2}(,[0-9]{3})*(\.[0-9]{2})?$/.test(thisString);    		return RegExp(/^\$?[0-9\,]+(\.\d{2})?$/).test(String(thisString).replace(/^\s+|\s+$/g, ""));    		    	}	catch(e) 	{ alert('catch exception in javascript class library: jsFormValidation. function: isValidCurrency error: '+e.message); }		}			function clear(fieldId)	{	try{			// 2009-09-14 pb, added suport for String vs This		if (fieldId.constructor == String)		{			object = getObject(fieldId)		}		else		{			object = fieldId		}				lclFieldIdSuffix = object.name.thisFieldId;     	if (!wrappers$()) return false; // no wrapper??? exit		clearErrorState(); // turn any error idicators off	     			 		    	}	catch(e) 	{ alert('catch exception in javascript class library: jsFormValidation. function: clear error: '+e.message); }		}		/* CLEAR method */		this.clear = function clear(thisFieldId)	{	try{		// 2009-07-30 pb				if (thisFieldId.constructor == String)			{				lclFieldId = thisFieldId;     		}     		else     		{     			     			lclFieldId = getId(thisFieldId); // get the Id of the object;     		     			     		}     		lclFieldIdSuffix = lclFieldId;     		if (!wrappers$()) return false; // no wrapper??? exit		     clearErrorState(); // turn any error idicators off	     			 		         	}	catch(e) 	{ 		alert('catch exception in javascript class library: jsFormValidation. function: clear error: '+e.message); }		}		}
