// Bruce Thomas 
// Version 1.0 (2 July 2005) 		- File created
// Version 1.1 (12 october 2005) 	- modified to allow multiple form support
// Version 1.2 (17 October 2005)	- Added check for "mine-doc" i.e. DOC, PDF or TXT files

// Description:
// A generic javascript function to check required form fields.
// Supports multiple forms on a single page, and using a single array of all form fields.

// Dependancies:
// 1. onSubmit event to call this function
// 2. Array (Called 'Required') containing:
//    field name, data type & error message
//    for each field that requires validation

// Data Type Identifiers:
//	"checkbox", 	Required to continue
//	"radio",		Collection of radio buttons
//	"password",		Equal values for both fields 
//					(presumes 2nd field has identical name, 
//					 sufixed with 'confirm' 
//					e.g. myPass and myPassconfirm)
//	"select-one",	Selected option that has a value
//	"text", 		Generic text box
//	"email", 		Email Address syntax
//	"numeric", 		Interger check (NO punctuation!)
//	"dd/mm/yyyy", 	Input filter, does not check for legal date
//  {reg-expres},	A legal regular expression to be used for validation.
// "image-jpg"		!!!!!!!!!!!!!!!!!!!
// "mime-doc"		Check file extension for DOC, PDF or TXT files.


// To-Do-List:
// 1. Need to add a legal date validator rule.
// 2. Would be great to have a 'checkboxes' type that ensures
//    at least one checkbox in a collection is chosen

function ValidateThisForm( oDocForm ) {
	var strCssBorder 	= '2px solid red';
	var strPassSufix	=  'confirm';			// Default sufix
	var	sErr			= '';
	var sDocFormName	= oDocForm.name.toString();
	var oForm			= document.forms[ sDocFormName ];

	// loop throught the Required Array to identify the fields
	for (var t=0; t<Required.length;t++) {
		var FieldName 	= Required[t][0];		// Required field name
		var FieldType	= Required[t][1];		// Required type
		var FieldMsg	= Required[t][2];		// 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;
				if (FieldType == '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;
						} 
					}
				// All other input types are easily identified by their .type, so
				// if this matched the Required array's type the validate it's value
				} else if (FieldType == 'text' ) {
					bConfirmed = (checkThis.value.length>0);
				} else if (FieldType == 'select-one' ) {
					bConfirmed = (checkThis.value.length>0);
				} else if (FieldType == 'numeric' ) {
					var filter  = /^([0-9])+$/;
					bConfirmed = filter.test(checkThis.value);
				} else if (FieldType == 'email' ) {
					var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
					bConfirmed = filter.test(checkThis.value);
				} else if (FieldType == 'password' ) {
					// presumes that the second field is named the same and sufixed with 'confirm'
					bConfirmed = (checkThis.value == oForm[FieldName + strPassSufix].value) && (checkThis.value.length>0);
				} else if (FieldType == 'checkbox' ) {
					bConfirmed = (checkThis.checked);
				} else if (FieldType == 'dd/mm/yyyy' ) {
					var filter  = /^([0-9]{2})+\/(([0-9]{2})+\/)+([0-9]{4})+$/;
					bConfirmed = filter.test(checkThis.value);
				} else if (FieldType == 'image-jpg' ) {
					var tmp	= checkThis.value.toLowerCase();
					var ext = tmp.substring(tmp.length-3,tmp.length);
					bConfirmed = (ext == "jpg");
				} else if (FieldType == 'mime-doc' ) {
					var tmp	= checkThis.value.toLowerCase();
					var ext = tmp.substring(tmp.length-3,tmp.length);
					bConfirmed = (ext == "doc" || ext == "pdf" || ext == "txt");
				} else if (FieldType == 'none' ) {
					bConfirmed = true;
				} else {
					// presumes a regular expression filter.
					var filter  = FieldType;
					bConfirmed = filter.test(checkThis.value);
				}
			}
			
			if (!bConfirmed) {
				// This field has not validated, so get it's error message
				// and try to highlight the form field on the page. This requires
				// that the form is wrapped in a div tag, and that there is a 
				// valid CSS definition for 'strCssBorder'
				sErr += FieldMsg + '\n';
				if (document.getElementById(FieldName) && FieldType != 'radio'){
					var css = document.getElementById(FieldName);
					css.style.border = strCssBorder.toString();
				} 
			}
		}
	}
	
	if(sErr.length>0) {
		// the error sting contains something... so alert the users, and return false
		sErr = "Errors have been detected!\n------------------------------------\n" + sErr;
		alert(sErr);
		return false;
	}
	return true;
}


function UpdateDateField( strFormName, strFieldName )
{
	//alert(strFormName+'.'+strFieldName);
	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 )
{
	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;
}




