
<!--
//-- This function validates user input into all form fields.
//-- >= or <= etc checks on user entered values are not made in this function.
//--
//-- ELEMENT NAMING CONVENTIONS:
//-- Each element has an identifying name (eg. 'addr_1') and a full name (eg 'Address Line 1')
//-- The identifying name is held in the element.name and the full name is held in the element.id.
//--=============================================================================================================
//-- NOTE 1
//-- validate="" col_header=""
//-- ++++++++    ++++++++++
//-- In this updated version the use of element.id to hold the validation info is replaced by element.validate
//-- and the element.id is used as a normal id.
//-- element.col_header is used as a text description of the element
//--=============================================================================================================
//--
//-- NOTE 2
//-- If the object has an attribute alwaysValidate="<go to this object name>"
//-- Then the field will be validated even if it is disabled or readonly
//--
//--=============================================================================================================
//--
//-- CHECKS PERFORMED:
//-- Format validation checks are requested by appending the type of test required to the element.validate
//-- Eg element.validate = <long name><_test><_test><_test>...
//--    element.validate = 'Postcode_required_ukpost'  Checks for a valid UK postcode.
//--    element.validate= 'County_required_alphabetic' Checks that the county contains only alpha characters.
//--
//-- AUTO CASE MODIFICATION:
//-- The element.validate attributes can also be set to force upper and lower case. See _uppercase, _lowercase
//--
//--   ========== NOTE 1 ==========
//--   For the date validation to work the script date_funcs.js must be included in the HTML or PHP document that uses this form validation script.
//--
//--   ========== NOTE 2 ==========
//--   If you want to display a javascript 'alert' message to say that the form submit was a success then use the hidden field formvalidstatus
//--
//--   ========== NOTE 3 ==========
//--   'informonce' this option should not be used with some other validations
//--   eg. maxlength - which may cause a DB insert/update fail if the inpust string is bigger than the DB field.
//--

function validate_form(form) {
	
//alert('function validate_form IN');

//	if(!window.removeWhiteSpace){
//		alert('ERROR... function removeWhiteSpace has not been included.\n\nSee strings.js');
//	}

	cursorwait();
	
	var error_count_flag = 0;
	var informed_count_flag = 0;

//var ago = false;
	var stop_on_error = false; //-- May want to stop checking for more errors and report what has alread been found.
	
	var formvalidstatusObj = MM_findObj('formvalidstatus');
	if(!formvalidstatusObj){
		alert('The hidden field formvalidstatus is not present in the form\nIt is required to display the form success message.');
		error_count_flag++;
  	}else{
		formvalidstatusObj.value = 'formok';
	}
	
	var objInform = MM_findObj('informonce');
	if(!objInform){
		alert('The hidden field informonce is not present in the form\nIt is required to display the form \'inform about error once\' message.');
		error_count_flag++;
  	}
	
	var returnVal=true;
	var formEls=form.elements;
	var currEl,currName,currId,currType,currVal,currField,minimum,maximum,temp;
	var errMsg="";
	var returnErrMsg="";
	var firstErr=-1;
	var pos = 0;
	
	var notWhitespace=/\S/;
	var notAlpha=/[^a-z \-\.\,']/gi;
	var notAlphaNumeric=/[^ a-zA-Z0-9]/gi;
	var notText=/[^ a-z0-9\-\.\,]/gi;
	var notAddress=/[^\w \&\-#\.\,\/]/gi;
	var hasSpaces=/\s/g;
	var notYN=/[^NYny]/;
	var notCYN=/[^CNYcny]/; //-- Yes No Cancelled
	var notInt=/\D/g;
	var isDecimal=/^\d+(\.\d+)?$/;
	var isCC=/^\d{4}(-\d{4}){3}$/;
	var isUSZip=/^\d{5}(-\d{4})?$/;
	var isUKPost=/[a-zA-Z]{1,2}[\d][0-9a-zA-Z]? [\d][a-zA-Z]{2}/;
	var isUSPhone=/^\d{3}[-\.]\d{3}[-\.]\d{4}$/;
	var isEmail=/^\w(\.?[\w-])*@\w(\.?[\w-])*\.[a-z]{2,6}(\.[a-z]{2})?$/i;
	var isCurrency=/^\d+(\.\d{2})?$/;
	var notComment=/[^a-zA-Z0-9\.\,;:%&#$@!\^-_~`"'\[\]\{\}\*\/\?\(\)]/i;
	var notNumberWithSpaces=/[^ 0-9\.]/gi;
							
	var informvalue = '';
	var showmessage = false;
	
	var elementIsInRow = false;
	var continueValidation = true;

	var requirements=new Array("required","informonce","alphabetic","address","alphanumeric","nospace",
							 "integer","decimal","decimalorblank",
							 "whiteoutminlength","minlength","minlenorzero","minlenorzeroexspaces","maxlength","maxlenexspaces",
							 "ccnumber","uszip","ukpost","ukpostorblank",
							 "usphone","email","emailorblank","currency","percent","comment","isYN","isCYN",
							 "numberwithspaces", "usdate","ukdate","ukdateorblank","ukdateminval","ukdatemaxval","minval","maxval","notzeroandnotblank","text",
							 "nottext");
	
	var currName='';
	var currType='';
	var formElsLen = formEls.length;

	for(var i=0; i<formElsLen; i++){
		
		//--   'informonce' this option should not be used with some other validations
		//--   eg. maxlength - which may cause a DB insert/update fail if the inpust string is bigger than the DB field.
		var disable_inform_once = false;
		
		var got_error = false;

		errMsg=""; //-- Blank the err message at the start of each loop

		currEl=formEls[i];
		
		if(!currEl.alwaysValidate){
			alwaysValidate = false;
			var go_to_this_field_Obj = '';
		}else{
			alwaysValidate = true;
			var go_to_this_field_name=currEl.alwaysValidate;
			var go_to_this_field_Obj = MM_findObj(go_to_this_field_name);
		}
				
		currName='disabled';
		currType='disabled';

		//-- Only check the form objects if they are NOT disabled ---------------
		if((!currEl.disabled && !currEl.readonly) || alwaysValidate){
			currName=currEl.name;
			currType=currEl.type;

			currValue=Trim(currEl.value);
//if(i>48 && i<60){alert(i+" "+currType);}		
			if(currType=='text'){
				currEl.value = currValue; //-- Trim every field to remove leading and trailing white space.
			}
//if(i>48 && i<60){alert('x');}

			currLen=parseInt(currValue.length,10);

			currId=currEl.id;

			//-- The validation test traditionally used the 'id' attribute to specify the validation required.
			//-- Replace the id attrib validation with a 'validate' attribute (if it exists).
			currValidate=currEl.validate;
			var using_attribute_validate = false;
			if(currValidate){
				currField=currId.indexOf("_")!=-1?currId.split("_")[0]:currId;
				currField=currField.replace(/0/g," ");
				currId = currValidate;
				using_attribute_validate = true;
			}
					
			//-- Remove double underline
			currId = currId.replace(/__/g,"_");

			//-- If the current field exists in a row from a table then extract the row number from the field name.
			elementIsInRow = false;
			var nameSplitArray = currName.split("_");
			var lastPart = '';

//if(i>50 && i<60){alert("-b8-"+i);}			

			if(nameSplitArray.length > 0){
				lastPart = nameSplitArray[nameSplitArray.length -1];
				if(!isNaN(lastPart)){
					//alert(currName+' '+lastPart);
					elementIsInRow = true;
				}
			}
			
//if(i>50 && i<60){alert("-b9-"+i);}

			//-- If the current element is in a row (ie it has a row number as part of it element.name name="address_1")
			//-- then check to see if the 'row_changed_' field exists.
			//-- If it does then is the row flaged as 'changed'? ie = 'y'
			//-- If the row exists but is not changed then do not perform any validation on the current field.
			continueValidation = true;
			if(elementIsInRow == true){
				n = 'row_changed_'+lastPart;
				row_changedObj = MM_findObj(n);
				if(row_changedObj){
					if(row_changedObj.value == 'n'){
						continueValidation = false;
					}
				}
			}
			
//if(i>50 && i<60){alert("-b-"+i);}

			if(continueValidation == true){

				if(using_attribute_validate == false){
					currField=currId.indexOf("_")!=-1?currId.split("_")[0]:currId;
					//currField=currId.indexOf("_")!=-1?currId.split("_")[0]:currid;//-- The el.name may be 'user_id' but the long name would be 'User ID'
					currField=currField.replace(/0/g," ");
				}
				
				if(currEl.col_header){
					currField = currEl.col_header;
				}
				
				if(currField.length == 0) {
					currField=currName;
				}
	
				temp=0;
				
				var context=new Object;
	
				for(var j=0;j<requirements.length;j++) 
				{
					if(currId.indexOf(requirements[j]) != -1){
						context[requirements[j]] = true;
					} else {
						context[requirements[j]] = false;
					}
				}

				switch(currType.toLowerCase())
				{
					case "text":
					case "textarea":
					case "password":
					case "file":
					
					if(context.nottext){
						//-- A (default?) string that should not exist when submit is executed.
						var text_bit = currId.split("nottext_")[1];
						text_bit_lc = text_bit.toLowerCase();
						if(text_bit_lc.indexOf(currValue.toLowerCase()) > -1){
							errMsg+="The field \""+currField+"\" should not contain the text \'"+text_bit+"\'.\n";
							error_count_flag++;
						}
					}
	
					if(context.minlength || context.whiteoutminlength){
						
						minimum=currId.split("minlength_")[1];
						minimum=parseInt(stripvalueout(currName,currType,minimum,'number'),10);
	
						if(isNaN(minimum)){
							alert('Script error in field '+currField+'. The minlength value is not a number');
						}else{
							
							if(context.whiteoutminlength){
								//-- remove blank spaces before doing the minlength check.
								newCurrValue = removeWhiteSpace(currValue);
								currLen=parseInt(newCurrValue.length,10);			
							}
							
							if(currLen < minimum) {
								errMsg+="The field \""+currField+"\"";
								if(currLen>0){
									errMsg+= " only";
								}
								errMsg+= " contains "+currLen+" character";
								if(currLen!=1){
									errMsg+= "s";
								}
								errMsg+= ".\n";
								errMsg+= "It should have a minimum number of "+minimum+" character";
								if(minimum!=1){
									errMsg+= "s";
								}
								errMsg+= ".\n";
								error_count_flag++;
							}
						}
					}
			
					if(context.minlenorzero || context.minlenorzeroexspaces){
						
						if(context.minlenorzeroexspaces){
							//-- Remove the spaces before counting the characters.
							hold_currValue = removeWhiteSpace(currValue);
							currLen=parseInt(hold_currValue.length,10);
							minimum=currId.split("minlenorzeroexspaces_")[1];
						}else{
							minimum=currId.split("minlenorzero_")[1];
						}
	
						minimum=parseInt(stripvalueout(currName,currType,minimum,'number'),10);
	
						if(isNaN(minimum)){
							alert('Script error in field '+currField+'. The minlenorzero value is not a number');
						}else{
						   if(currLen < minimum && currLen > 0) {
							errMsg+="The field \""+currField+"\"";
							if(currLen>0){
								errMsg+= " only";
							}
							errMsg+= " contains "+currLen+" character";
							if(currLen!=1){
								errMsg+= "s";
							}
							errMsg+= ".\n";
							errMsg+= "It should have zero or a minimum number of "+minimum+" character";
							if(minimum!=1){
								errMsg+= "s";
							}
							errMsg+= ".\n";
							error_count_flag++;
						   }
						}					
						
					}				
	
					if(context.maxlength){	
							
						maximum=currId.split("maxlength_")[1];
						maximum=parseInt(stripvalueout(currName,currType,maximum,'number'),10);
						
						if(isNaN(maximum)){
							disable_inform_once = true;
							alert('Script error in field '+currField+'. The maxlength value is not a number');
						}else{
							if(currLen > maximum) {
								disable_inform_once = true;
								errMsg+="The field \""+currField+
										"\" must have a maximum number of characters of "+maximum+".\n"+
										"\""+currField+"\" contains "+currLen+" characters.\n";
								error_count_flag++;
							}					
						}
					}
					if(context.maxlenexspaces){
					//-- Remove the spaces before counting the characters.

						hold_currValue = removeWhiteSpace(currValue);
						currLen=parseInt(hold_currValue.length,10);

						maximum=currId.split("maxlenexspaces_")[1];
						maximum=parseInt(stripvalueout(currName,currType,maximum,'number'),10);

						if(isNaN(maximum)){
							alert('Script error in field '+currField+'. The maxlenexspaces value is not a number');
						}else{
							if(currLen > maximum) {
								errMsg+="The field \""+currField+
										"\" must have a maximum number of characters of "+maximum+".\n"+
										"\""+currField+"\" contains "+currLen+" characters.\n";
								error_count_flag++;
							}
						}
					}
					
//if(i>50 && i<60){alert("-c-"+i);}

					if(context.minval){
						minimum=currId.split("minval_")[1];
						minimum = stripvalueout(currName,currType,minimum,'number');
						
						if(isNaN(parseFloat(currValue))){
							got_error = true;
						}

						if(!minimum) {				
							got_error = true;
						}
						
						if(parseFloat(currValue) < parseFloat(minimum)) {				
							got_error = true;
						}
						
						if(got_error == true){
							if(isNaN(minimum)){
								alert('Script error in field '+currField+'. The value is not a number');
							}else{
								errMsg+="The field \""+currField+"\" must have a minimum value of at least "+minimum+".\n";			
								error_count_flag++;
							}
						}
					}
					
					if(context.notzeroandnotblank){

						//-- Must be a non zero number. Can be positive or negative.
						var got_error = false;
						
						if(isNaN(currValue)){
							got_error = true;
							errMsg+="Error in field '"+currField+"'. The value entered is not a number.";
							error_count_flag++;
						}

						if(currValue.length == 0){
							got_error = true;
							errMsg+="Error in field '"+currField+"'.\nThe field is blank, please enter a value.";
							error_count_flag++;
						}else{
							if(currValue == 0){
								got_error = true;
								errMsg+="Error in field '"+currField+"'\nThe value cannot be zero.";
								error_count_flag++;
							}
						}
					}
				
					if(context.maxval){
						var got_error = false;
						
						maximum=currId.split("maxval_")[1];
						maximum=stripvalueout(currName,currType,maximum,'number');
						
						if(isNaN(parseFloat(currValue))){
							got_error = true;
						}

						if(!maximum) {				
							got_error = true;
						}
		
						if(parseFloat(currValue) > parseFloat(maximum)){
							got_error = true;
						}

						if(got_error == true){
							if(isNaN(maximum)){
								errMsg+="Error in field '"+currField+"'.\nThe value entered is not a number.";
							}else{
								errMsg+="The field '"+currField+"' must have a maximum value of "+maximum+".\n";						
							}
							error_count_flag++;
						}
		
					}
	
					if(context.required && (currValue.length==0||!notWhitespace.test(currValue))){
						errMsg+="The required field \""+currField+"\" was left blank.\n";
						error_count_flag++;
						break;
					}
	
					if(context.ukdate){
						got_error = false;
						if(currValue.length > 0) {
							if(isUKdate(currValue)==false){
								if(context.ukdateorblank) {
									if(currValue.length!=0 && notWhitespace.test(currValue) && currValue!='dd/mm/yyyy') {
										got_error = true;
									}
								}else{
									got_error = true;
								}	
							}
						}else{
							if(context.required && !context.ukdateorblank){
								got_error = true;
							}
						}
						if(got_error == true) {						
							errMsg+="The field \""+currField+"\"  must contain a valid date in the format dd/mm/yyyy.\n";
							error_count_flag++;
						}
					}
					
					if(context.ukdateminval){
//alert('ukdateminval');
						if(currValue.length > 0) {
							if(isUKdate(currValue)){
								ukdateminval=Trim(currId.split("ukdateminval_")[1]);
								if(isUKdate(ukdateminval)){
									date1 = currValue;
									date2 = ukdateminval;
									// -------------------------------------------------------------------
									// compareTwoDates(date1,date1format,date2,date2format)
									//   Compare two date strings to see which is greater.
									//   Returns:
									//   2 if date1 is the same as date2
									//   1 if date1 is greater than date2
									//   0 if date2 is greater than date1
									//  -1 if either of the dates is in an invalid format
									// -------------------------------------------------------------------
									if(compareTwoDates(date1,"dd/mm/yyyy",date2,"dd/mm/yyyy") == 0){
										errMsg+="The field \""+currField+"\"  must contain a date not less than "+date2+".\n";
										error_count_flag++;
										stop_on_error = true;
										break;
									}
								}else{
									alert('Script error in field '+currField+'. The minimum date value is not a date');
								}
							}
						}
					}
					
//if(i>50 && i<60){alert("-d-"+i);}

					if(context.ukdatemaxval){
//alert('ukdatemaxval');
						if(currValue.length > 0) {
							if(isUKdate(currValue)){
								ukdatemaxval=Trim(currId.split("ukdatemaxval_")[1]);
								if(isUKdate(ukdatemaxval)){
									date1 = currValue;
									date2 = ukdatemaxval;
									// -------------------------------------------------------------------
									// compareTwoDates(date1,date1format,date2,date2format)
									//   Compare two date strings to see which is greater.
									//   Returns:
									//   2 if date1 is the same as date2
									//   1 if date1 is greater than date2
									//   0 if date2 is greater than date1
									//  -1 if either of the dates is in an invalid format
									// -------------------------------------------------------------------
									if(compareTwoDates(date1,"dd/mm/yyyy",date2,"dd/mm/yyyy") == 1){
										errMsg+="The field \""+currField+"\"  must contain a date not greater than "+date2+".\n";
										error_count_flag++;
										stop_on_error = true;
										break;
									}
								}else{
									alert('Script error in field '+currField+'. The maximum date value is not a date');
								}
							}
						}
					}
					
					if(context.numberwithspaces && notNumberWithSpaces.test(currValue)){
						errMsg+="The field \""+currField+"\" should only contain numbers (spaces are allowed).\n";
						error_count_flag++;
					}
				
					if(context.alphabetic && notAlpha.test(currValue) || context.alpha&&notAlpha.test(currValue)){
					  	errMsg+="The field \""+currField+"\" contains non-alphabet characters.\n";
						error_count_flag++;
					}
					if(context.alphanumeric && notAlphaNumeric.test(currValue)){
					  	errMsg+="The field \""+currField+"\" contains illegal characters.\nOnly numbers 0 to 9 or letters A to Z or a to z are allowed.\n";
						error_count_flag++;
					}
			//-----------------				
					
					if(context.address && notAddress.test(currValue)){
						errMsg+="The field \""+currField+"\" contains illegal characters.\n";
						error_count_flag++;
					}
					if(context.isYN){
						var trimmedCurrValue = currValue.replace(/^\s*|\s*$/g,""); //-- trim()
						if(notYN.test(currValue)&&trimmedCurrValue.length > 0){
						  	errMsg+="The field \""+currField+"\" should only contain \'y\' or \'n\'.\n";
							error_count_flag++;
						}				
					}
					if(context.isCYN){
						var trimmedCurrValue = currValue.replace(/^\s*|\s*$/g,""); //-- trim()
						if(notCYN.test(currValue)&&trimmedCurrValue.length > 0){
						  	errMsg+="The field \""+currField+"\" should only contain \'y\' or \'n\'. or \'c\'.\n";
							error_count_flag++;
						}				
					}
					if(context.nospace && hasSpaces.test(currValue)){
					  	errMsg+="The field \""+currField+"\" should not contain any spaces.\n";
						error_count_flag++;
					}
					if(context.integer && notInt.test(currValue)){
					  	errMsg+="The field \""+currField+"\" should contain only the digits 0-9.\n";
						error_count_flag++;
					}
					if(context.decimal && !isDecimal.test(currValue)){
						got_error = false;
						if(context.decimalorblank) {
							if(currValue.length!=0&&notWhitespace.test(currValue)) {
								got_error = true;
							}
						} else {
							got_error = true;
						}
						
						if(got_error == true) {
							currValue = Trim(currValue);
							error_count_flag++;
							if(currValue.length == 0){
								errMsg+="The field \""+currField+"\" has been left blank.\n";
							}else{
								errMsg+="The value you entered in the field \""+currField+"\" is not a number.\n";
							}
						}
					}

//if(i>50 && i<60){alert("-e-"+i);}

	//				if(context.minlength&&currValue.length<minimum)
	//				{
	//				  errMsg+="The field \""+currField+"\" must contain at least "+minimum+" characters.\n";
	//				}
	//				if(context.minlenorzero&&currValue.length<minimum&&currValue.length>0)
	//				{
	//				  errMsg+="The field \""+currField+"\" must contain none or the minimum of "+minimum+" characters.\n";
	//				}
	//				if(context.maxlength&&currValue.length>maximum)
	//				{
	//				  errMsg+="The field \""+currField+"\" must contain at most "+maximum+" characters.\n";
	//				}
					
					if(context.ccnumber&&!isCC.test(currValue)){
					 	errMsg+="The value in the field \""+currField+"\" must be in the form \"XXXX-XXXX-XXXX-XXXX\".\n";
						error_count_flag++;
					}
					if(context.uszip&&!isUSZip.test(currValue)){
					  	errMsg+="The field \""+currField+"\" does not contain a valid 5 or 9 digit ZIP code.\n";
						error_count_flag++;
					}
					if(context.ukpost&&!isUKPost.test(currValue)){
						got_error = false;
						if(context.ukpostorblank) {
							if(currValue.length!=0&&notWhitespace.test(currValue)) {
								got_error = true;
							}
						}else{
							got_error = true;
						}
						
						if(got_error == true) {
							error_count_flag++;
							errMsg+="The field \""+currField+"\" does not contain a valid UK Postcode.\n";
						}
					}
					if(context.usphone&&!isUSPhone.test(currValue)){
					  	errMsg+="The field \""+currField+"\" does not contain a valid USA/Canadian telephone number.\n";
						error_count_flag++;
					}
					if(context.email&&!isEmail.test(currValue)){
						got_error = false;
						if(context.emailorblank) {
							if(currValue.length!=0&&notWhitespace.test(currValue)) {
								got_error = true;
							}
						} else {
							got_error = true;
						}
						
						if(got_error == true) {
							errMsg+="The field \""+currField+"\" does not contain a valid email address.\n";
							error_count_flag++;
						}
					}
					if(context.currency&&currValue!=""){
					  currValue=currValue.replace(/[$ге\,]/g,"");		  
					  currValue=currValue.replace(/&pound;/g,"");
						if(!isCurrency.test(currValue)){
							errMsg+="The field \""+currField+"\" does not contain a valid amount.\n";
							error_count_flag++;
					  	}
					}else{				
					  form.elements[i].value=currValue;
					}			
					break;
					
//if(i>50 && i<60){alert("-f-"+i);}

				  case "checkbox":
					//-- Check a group of check boxes (not sure if this bit works)
						if(context.required){
							temp=0;

							for(var n=0;n<form[currId].length;n++){
								if(n<form[currId].length-1){
									i++;
								}
								if(form[currId][n].checked){
									temp++;
								}
							}
							
							if(!temp&&!context.minlength){
								errMsg+="You must check at least one of the \""+currField+"\" checkboxes.\n";
								error_count_flag++;
							}
							if(context.minlength&&temp<minimum){
								errMsg+="You must check at least "+minimum+" of the \""+currField+"\" checkboxes.\n";
								error_count_flag++;
							}
							if(context.maxlength&&temp>maximum){
								disable_inform_once = true;
								errMsg+="Please check no more than "+maximum+" of the \""+currField+"\" checkboxes.\n";
								error_count_flag++;
							}
						}
						break;
			
				  case "radio":
					if(context.required){
						temp=false;
						for(var n=0;n<form[currId].length;n++){
							if(n<form[currId].length-1){
								i++;
							}
							if(form[currId][n].checked){
								temp=true;
							}
					  	}
						if(!temp){
							errMsg+="You must select one of the \""+currField+"\" radiobuttons.\n";
							error_count_flag++;
						}
					}
					break;
			
				  case "select-one":
				  
//alert("select-one "+currField+" "+currEl.selectedIndex);

					if(context.required && currEl.selectedIndex==0){
						//-- Drop down box is required and the index is zero (first entry in the list).

						currValue = currEl.options[currEl.selectedIndex].value;
//alert("select-one "+currField+" "+currEl.selectedIndex+" "+currValue);
						//string1.toLowerCase().indexOf(string2.toLowerCase(), fromNum ) 
						pos = currValue.toLowerCase().indexOf('select one', 0);		
						if(pos == -1){
							pos = currValue.toLowerCase().indexOf('select-one', 0);
						}
						if(pos == -1){
							pos = currValue.toLowerCase().indexOf('select_one', 0);
						}
						if(pos == -1){
							pos = currValue.toLowerCase().indexOf('selectone', 0);
						}
						if(pos == -1){
							pos = currValue.toLowerCase().indexOf('select', 0);
						}
						if(pos == -1){
							//-- The value is the same as the index (see tenant_logon_ok.php select your room.
							pos = currValue.toLowerCase().indexOf('0', 0);
						}

						if(pos != -1) { //-- indexOf returns -1 if not found
							errMsg+="You must select one of the \"" +currField+ "\" options.\n";
							error_count_flag++;
							break;
						}
				
						if(currValue == '') {
							errMsg+="The value in the Drop Down list \""+currField+"\" is blank.\n";
							error_count_flag++;
							break;
						}	
					}
					break;
		
				  case "select-multiple":
					if(context.required||context.minlength||context.maxlength){
						disable_inform_once = true;
					  	temp=0;
					  	for(n=0;n<currEl.length;n++){
							if(currEl.options[n].selected){
						  		temp++;
							}
						}
					}else{
						break;
					}
					
					if(!temp&&!context.minlength){
						minimum=1;
					}
					if(context.minlength&&temp<minimum || context.required&&!temp){
	  					errMsg+="You must select at least "+minimum+" of the \""+currField+"\" options.\n";
						error_count_flag++;
					}
					if(context.maxlength&&temp>minimum){
						disable_inform_once = true;
					  	errMsg+="You must select no more than "+maximum+" of the \""+currField+"\" options.\n";
						error_count_flag++;
					}
					break;
			
				  case "submit":
				  case "reset":
				  case "button":
	//			  case "file":
				  case "image":
				  case "hidden":
				  break;
				  
				  default:
					alert("         ERROR!!\nWe should never get here!");
					break;
			
				}
			} //-- end of if continue validation = true
		} //-- end of if object is not disabled ---

//if(i>50 && i<60){alert("-g-"+i);}

		//-- Any errors found so far?
		var errsFound = false;
		var n = '\n';
	
		if(errMsg.length > 0){
			//if(errMsg.indexOf(n)>-1){
				errsFound = true;
			//}
		}
		
		//-- Are there any errors?
		if(errsFound == true){
			var ignore_this_error = false;
			//-- If the field is part of a row that has been marked for deletion
			//-- then do not check any values.
			//-- All array names must be suffixed with '_<row number>'
			var array_of_parts = currName.split('_')
			var array_ctr = array_of_parts.length;
			if(array_ctr > 0){
				var last_val = array_of_parts[(array_ctr -1)];
				if(!isNaN(last_val)){
					//-- It looks like the current variable is from one row of a record set.
					n = 'delete_row_'+last_val;
					var delObj = MM_findObj(n);
					if(typeof(delObj)=='object'){
						if(delObj){
							if(delObj.checked == true){
								ignore_this_error = true;
							}
						}/*else{
							//-- object is null.
							ignore_this_error = true;
						}*/
					}
				}
			}
			
//if(ignore_this_error == true){
//	alert('ignore_this_error == true');
//}else{
//	alert('ignore_this_error == false');
//}
//
//if(disable_inform_once == false){
//	alert('disable_inform_once == false');
//}else{
//	alert('disable_inform_once == true');
//}

			if(ignore_this_error == false){

				//-----------------	
				if(context.informonce && disable_inform_once == false){				
					//-- First check to see if the user has already been informed once that the field is blank.
	
//alert('GOT INFORMONCE errMsg=='+errMsg+'== currType='+currType+'  currField='+currField);
					
					informvalue = objInform.value;
					showmessage = false;
						
					if(informvalue.length == 0){
						showmessage = true;
					} else {
						if(informvalue.indexOf(currName) < 0){					
							showmessage = true;
						}
					}					
	
					//-- If the object name is already in the hidden 'informonce field - do not report this as an error.
					if(showmessage == true) {
						objInform.value = objInform.value + ' ' + currName; //-- put the current field name into the hidden informonce field. 
						
						returnErrMsg+=errMsg+"You will only be informed about this once.\n\n";
						informed_count_flag++;
	
						if(currType.toLowerCase()=='checkbox' || currType.toLowerCase()=='radio'){
							if(firstErr<0){firstErr=i-(form[currId].length-1);}
						}else{
							if(firstErr<0){firstErr=i;}
						}
				   }
	
				}else{
			
					returnErrMsg+=errMsg;
	
					if(currType.toLowerCase()=='checkbox' || currType.toLowerCase()=='radio'){
						if(firstErr<0){firstErr=i-(form[currId].length-1);}
					}else{
						if(firstErr<0){firstErr=i;}
					}				
				}
				//-----------------	
			} //-- if(ignore_this_error == false){

		}
		
		if(stop_on_error == true){
			//-- Quit the for loop.
			break;
		}

	} //-- end of for loop (for(var i=0; i<formEls.length; i++)) ---------------------
	
//if(i>50 && i<60){alert("-h-"+i);}

  	returnVal=firstErr<0;
	
 	if(!returnVal){
		
		var ObjMM_formUsage = MM_findObj('MM_formUsage');
		if(!ObjMM_formUsage){
			alert('ERROR... The hidden field \'MM_formUsage\' does not exist in the current page.');
		} else {
			ObjMM_formUsage.value = 'ignoreme';		
		}
		
		//-- How many errors were found?
		var ncount = 0;
		var n = '\n';
		pos = returnErrMsg.indexOf(n);
		while ( pos != -1 ) {
		   ncount++;
		   pos = returnErrMsg.indexOf(n,pos+1);
		}
		
		if(ncount>0) {
			if(ncount==1) {
				returnErrMsg="There was a problem with the following field:\n\n" + returnErrMsg;  
			} else {
				returnErrMsg="There were problems with the following fields:\n\n" + returnErrMsg;  
			}

//alert(firstErr);

			var fename = form.elements[firstErr].name;
			var notused = highlighterrorfield(fename);
			var fenameObj = MM_findObj(fename);
			if(fenameObj){
				fenameObj.focus();
			}

			alert(returnErrMsg);

			//-- If the only error is an 'informed once' error then do not stop the form data from being inserted/updated.
			if(error_count_flag  > 0 && error_count_flag != informed_count_flag){
				formvalidstatusObj.value = 'formnotvalid';
			}
			
		}

	}
	
	if(formvalidstatusObj.value == 'formok'){
		returnVal=true;
	}else{
		returnVal=false;
	}
	
	cursordefault();

//alert('function validate_form --OUT--');

	return returnVal;
}


function stripvalueout(currName, currType, theval, valtype) {
//-- strip out the first value in the string 'thevalue'.
//-- Underscore '_' is used as a separator and may be the first char.

//alert('function stripvalueout theval='+theval+' valtype='+valtype+' currName='+currName+' currType='+currType);

	if(theval){
		if(theval.length > 0){
	
			if(theval.substr(0,1) == '_') {
				var ln=theval.length -1;
				theval=theval.substr(1,ln);
			}
		
			theval=theval.split("_")[0];
				
			if(valtype == 'number') {		
				//-- The value may be held in a form object.
				if(isNaN(theval)) {
					var obj = MM_findObj(theval);
					if(obj) {theval = obj.value}
				}
			}
		}else{
			theval = false;
		}
	}else{
		theval = false;
	}
//alert('function stripvalueout OUT');	
	
	return theval;
}

function highlighterrorfield(theField){
//-- Highlight fields that contain errors.
//-- Change the object class to a class with the same text positioning but with a blue (as opposed to yellow) background.
//--
//-- NOTE the input param can be either the field object or the field name.

//alert('highlighterrorfield IN');

	if(typeof(theField)=='string'){
		var fieldObj = MM_findObj(theField);
	}else{
		var fieldObj = theField;
	}

	var highlighterror = false;

	if(fieldObj){

		if(objectIsVisible(fieldObj)){

			var errclassname = fieldObj.className;

			var ln = errclassname.length;
			var holdclassname = errclassname.toLowerCase();
			
			highlighterror = false;
			
			if(ln>5){
				var class_check = holdclassname.substr(ln-5,5);

				if(class_check.toLowerCase() !='error'){
					highlighterror = true;
					errclassname += 'Error';
					fieldObj.className = errclassname;
					
					var n = fieldObj.name;
					setTimeout("doTheFocus('"+n+"');",100);
					
	//alert(fieldObj.name+' errclassname='+errclassname);
				}
			}
		}else{
			highlighterror = true;
		}
	}
	
//alert('highlighterrorfield( OUT');
	
	return highlighterror;
}

function doTheFocus(objName){
	
	//alert(objName);
	
	obj = MM_findObj(objName);
	obj.focus();
	
}

function resetErrorFieldClass(fieldObj){
//-- reset fields previously highlighted as containing errors.
//-- See also 'function highlighterrorfield(fieldObj)'

//alert('function resetErrorFieldClass');

	if(fieldObj){
		var errclassname = fieldObj.className;

		if(errclassname){
			var ln = errclassname.length;
			var holdclassname = errclassname.toLowerCase();
			
			if(ln>5){
				if(holdclassname.substr(ln-5,5)=='error'){
					errclassname=errclassname.substr(0,ln-5)
					fieldObj.className = errclassname;
				}
			}
		}
	}
}

function valid(obj, validation, header){
	//-- This function creates two additional attributes for a form object.
	//-- 1. validate - which holds the validation described in function validate_form(form) above
	//-- 2. col_header - which holds a description of the field.
	//--
	//-- NOTE: IE will allow an object attribute to be created simply by including it
	//--       in the description eg <input type="text" validate="minlen_5" col_header="Your Name" />
	//--       But a number of other browsers will ignore the additional attributes unless they
	//--       are explicitly created as <input type="text" onfocus="this.validate='minlen_5' this.col_header='Your Name'" />
	
	if(typeof(obj.validate)=='undefined'){
		obj.validate = validation;
		obj.col_header = header;
	}
}
	

-->
