
// Bruce Thomas 
function ValidateThisForm( oDocForm ) {
	var strCssBorder 	= '2px solid red';
	var strPassSufix	=  '_confirm';			// Default sufix
	var	sErr			= '';
	//var sDocFormName	= oDocForm.name;
	//var sDocFormName	= oDocForm.toString(); // works if string form name
	// var oForm			= document.forms[ sDocFormName ];
	var oForm = oDocForm;
	var sDocFormName	= oForm.name;
	// alert('sDocFormName'+sDocFormName );

	// loop throught the Required Array to identify the fields
	for (var t=0; t<Required.length;t++) {
		var FieldName 	= Required[t][0].toString();		// Required field name
		var FieldType	= Required[t][1].toString();		// Required type
		var FieldMsg	= Required[t][2].toString();		// Requied error message
		//alert('!!' + FieldName + ' ' + FieldType);
		if(oForm[FieldName]) {
			// alert('exists' + FieldName);
			// Does this field exist in this form?
			if(oForm[FieldName]) {
				var checkThis = oForm[FieldName];
				if (!checkThis.type) {
					// Radio types need a different approach...
					// (see radio below)
					var checkThis = oForm;
				} 
				// Default to return an error
				var bConfirmed 		= false;
				switch (FieldType) {
					case "radio" : 
						// Radio's are an array, so search the form until the collection is found
						// then confirm that one is checked, and exit
						for (var i=0; i<checkThis.elements.length && !bConfirmed; i++ ) {
							if(checkThis[i].name == FieldName) {
								bConfirmed = checkThis[i].checked;
							} 
						}
					break; 

					// All other input types are easily identified by their .type, so
					// if this matched the Required array's type the validate it's value
					case "text" : 
						bConfirmed = (checkThis.value.length>0);
						break; 

					case "numeric" : 
						var filter  = /^([0-9])+$/;
						bConfirmed = filter.test(checkThis.value);
						break; 

					case "select-one" : 
						bConfirmed = (checkThis.value.length>0);
						break; 

					case "email" :
						// this does not take into account CCed emails i.e. contains ";"
						var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
						bConfirmed = filter.test(checkThis.value);
						break; 

					case "confirm" :
						// presumes that the second field is named the same and sufixed with 'confirm'
						bConfirmed = (checkThis.value == oForm[FieldName + strPassSufix].value) && (checkThis.value.length>0);
						break;
					
					case "checkbox" : 
						bConfirmed = (checkThis.checked);
						break; 

					case "dd/mm/yyyy" : 
						var filter  = /^([0-9]{2})+\/(([0-9]{2})+\/)+([0-9]{4})+$/;
						bConfirmed = filter.test(checkThis.value);
						break; 

					case "image-jpg" : 
						var tmp	= checkThis.value.toLowerCase();
						var ext = tmp.substring(tmp.length-3,tmp.length);
						bConfirmed = (ext == "jpg");
						break; 

					case "mime-doc" : 
						var tmp	= checkThis.value.toLowerCase();
						var ext = tmp.substring(tmp.length-3,tmp.length);
						bConfirmed = (ext == "doc" || ext == "pdf" || ext == "txt");
						break; 

					case "none" : 
						bConfirmed = true;
					break; 

					default :
						// presumes a regular expression filter.
						var filter  = FieldType;
						bConfirmed = filter.test(checkThis.value);
				}
			}
			
			if (!bConfirmed) {
				// concatinate the erroring field names.
				sErr += FieldName + '|';
			}
		}
	}
	
	// return the guilty fields as a string
	return sErr;
}


function UpdateDateField( strFormName, strFieldName )
{
	// alert(strFormName+'.'+strFieldName);
	// this is still experimental use with caution!
	// returns a dd/mm/yyyy value to date fields sufixed as _Day, _Month, _Year
	// i.e. date selectors that are select option form field where 
	// strFormName is type="hidden", and "strFormName_Day" etc. are select option fields.
	var frm = document.forms[strFormName].elements[strFieldName];
	var val = document.forms[strFormName];
	var strDayVal 	= '0' + val.elements[strFieldName+'_Day'].value.toString();
	var strMonthVal = '0' + val.elements[strFieldName+'_Month'].value.toString();
	var strYearVal 	= '0' + val.elements[strFieldName+'_Year'].value.toString();
	var strDateValue = right(strDayVal,2) +"/"+right(strMonthVal,2)+"/"+right(strYearVal,4);
	//alert('value:' + strDateValue);
	frm.value = strDateValue;
}

function right( strVal, intLen )
{
	// Same as VBScript string function Right(string,count)
	// Returns string value truncated by "count" charaters from the right
	if (strVal.length > intLen) {
		var theVal 		= strVal.toString();
		var last_index	= theVal.length;
		var first_index = theVal.length - intLen;
		var trunc		= theVal.substring(first_index,last_index);
		//alert(trunc);
		return trunc;
	} else {
		return strVal;
	}
}




