/* FORM VALIDATION 1.6 - GENERIC SCRIPTS - copyright NOV 2007        */
/* Digit&#064;l &#073;diom Ltd  www.digitalidiom.co.uk                         */

/* ================================================================= */
/* 1.6 Added a global Hi-Lite & Border color for error fields        */
/* 1.5 add selection box and focus to first error field              */
/* Added FireFox Exception Bug Fix as follows                        */
/* obj.setAttribute('autocomplete','off');                           */  
/* Or, you can simply set autocomplete="off" in HTML.                */
/* --------------------------------------------                      */
/* If field is marked as mandatory in rules - check for any input    */
/* If field is not empty, validate input string against regExp rules */
/* MT = mandatory true : MF = mandatory false                        */
/*                                                                   */
/* Rules are enter on the actual target HTML page                    */
/* ================================================================= */

/* EXAMPLE of Rules ARRAY                                            */
/* FORM VALIDATION rules - 1 per form field                          */
/* Mandtory | friendly name | form-field id | regExp | error msg     */
/* E.G. BELOW ====================================================== */
/* 'MT###FIRST NAME###firstName###/^([\\w]|\\s)*$/###Error found',   */
/* Insert a new rule for each field that needs verifying             */
/* 3 ### separates the rule values - don't forget  comma end of line */
/* Reg expressions need special characters escaping with backslashes */
/* use Regular Expressions for all validation rules                  */
/* ================================================================= */

/* Global Variables */

	var arrFieldRulesSplit;
	var blnShowErrorMsg;
	var strErrorMsg;
	var strHiLite = "#FFDDDD";
	var strBorderColor = "3px solid #CC0000";
	

/* ================================================================= */

	function validateForm(formID)
	// the main serial list of functions / checks 

	{
		f = document.forms[formID];

		f.setAttribute('autocomplete','off');

		blnShowErrorMsg = 0; arrFieldIDList = new Array();

		strErrorMsg = "Oops! - Some corrections are required.\n===============================\n\n";

		sortArrayRules(formID);

		(blnShowErrorMsg < arrFieldRules.length)?errorFieldFocus(formID):"";
		
		(blnShowErrorMsg < arrFieldRules.length)?alert(strErrorMsg):f.submit();
		
		return false;
	}


/* ================================================================= */
// loop rules to see which fields are mandatory - Style fields as required
	function doMandatoryFieldCheck(formID)
	{
		f = document.forms[formID];
		for (i=0;i<arrFieldRules.length;i++)
		{
			arrFieldRulesSplit = arrFieldRules[i].split("###");
			if (arrFieldRulesSplit[0] == "MT")
			{
				//f.elements[arrFieldRulesSplit[2]].style.borderLeft = strBorderColor ;
				f.elements[arrFieldRulesSplit[2]].style.backgroundColor = strHiLite;
			}

		}

	}
/* ================================================================= */


	function errorFieldFocus(frmID) // put first error field in focus
	{	
			document.forms[frmID].elements[arrFieldIDList[0]].focus();
	}


/* ================================================================= */

	function sortArrayRules(currentForm)
	
	{
		f = document.forms[currentForm];
		
		for (i=0;i<arrFieldRules.length;i++)
		{
			arrFieldRulesSplit = arrFieldRules[i].split("###"); // split into array on hashes ###
			regExpr = arrFieldRulesSplit[3]; // get reg exp from rules array


			// TEST FIELD TYPE to take it's value
			// ===========================================

			fldID = arrFieldRulesSplit[2];

			if (f.elements[fldID].type == 'text') 
			{
				strInput = f.elements[fldID].value;
			}
			else if (f.elements[fldID].type == 'textarea') 
			{
				strInput = f.elements[fldID].value;
			}				
			else if ((f.elements[fldID].type) == 'select-one')
			{
				strInput = f.elements[fldID].value;
			}
			
			
			// if input contains something OR is mandatory - validate field
			if ((!strInput == null || !strInput == "") || arrFieldRulesSplit[0] == "MT")
			
			{
			
				// TEST if field has passed or not 
				// field test - search for reg exp rule in the filed value
				blnFieldMatched = eval("strInput.search("+regExpr+")")!=-1;

				// if field is not matched - change its style and append to error message
				if (!blnFieldMatched) // error found
				{
					//f.elements[fldID].style.borderLeft = strBorderColor ;
					f.elements[fldID].style.backgroundColor = strHiLite ;


					// keep array of invalid fields - push new field ID into array
					arrFieldIDList[arrFieldIDList.length] = (arrFieldRulesSplit[2]);

					// build error messages (string)
					strErrorMsg += (arrFieldRulesSplit[1] + " : " + arrFieldRulesSplit[4] + "\n");

				}
				if (blnFieldMatched) // field passed
				{
					// change field back to default appearance
					//f.elements[fldID].style.borderLeft = "1px solid #333" ;
					f.elements[fldID].style.backgroundColor = "white" ;

				blnShowErrorMsg++;
				}

			}
			
			else if (arrFieldRulesSplit[0] == "MF" && strInput == "") // not mandatory and empty so ignore
			{
				// set field back to default color / appearance
				//f.elements[fldID].style.borderLeft = strBorderColor ;
				f.elements[fldID].style.backgroundColor = "white" ;

				blnShowErrorMsg++;
			}

		}


	}

/* ================================================================ */
/* FORM VALIDATION - GENERIC SCRIPTS - copyright                    */
/* Digit&#064;l &#073;diom Ltd  www.digitalidiom.co.uk                        */
/* ================================================================ */


/* ====================================================== */
/* Form field ##########        */
function fh()
{
document.write('<input type="hidden" id="hdPop" name="hdPop" value="007" />');
}
