
/**
 * Set up variables to hold the parameters - therefore reduces the chances of spelling mistakes
 */
var isMandatory		    = 0;
var isEmail			    = 1;
var isCBMandatory	    = 2;
var isRadioMandatory	= 3;
var isDropdownMandatory = 4;
var isTandCsMandatory	= 5;
var MESSAGE_MANDATORY_ERROR 	        = "The following field is mandatory";
var MESSAGE_MANDATORY_CB_ERROR 	        = "Please select a checkbox";
var MESSAGE_MANDATORY_RADIO_ERROR 	    = "Please select a radiobox";
var MESSAGE_MANDATORY_DROPDOWN_ERROR 	= "Please select from the drop down list";
var MESSAGE_MANDATORY_TANDCS_ERROR	    = "Please confirm you have read and agree to the Terms and Conditions";
var MESSAGE_EMAIL_ERROR		            = "The email address is not of the form \"name@address\"";
var fId = 0;


function SKM_validateForm(formID) {

	var foundError = false;
	/**
	 * Loop through the element list until an error found
	 */
	for (var i = 0; !foundError && i < fieldList.length; i++) {
		/**
		 * Assign to "obj" the HTML object with the name of the first element of the array
		 */
		var obj = MM_findObj(fieldList[i][0]);
		var name = fieldList[i][1];
		/**
		 * Only proceed if the HTML object was found
		 */
		if (obj != null) {
			/**
			 * Loop through the parameter list until an error found
			 * starting at 2 - i.e. ignore field name and English name
			 */
			for (var j = 2; !foundError && j < fieldList[i].length; j++) {
				/**
				 * Switch-case statement is more effecient and easier to read than "if, if else, else"
				 */
				switch (fieldList[i][j]) {
					case isMandatory:
						foundError = check_Mandatory(obj, name);
						break;
					case isEmail:
						foundError = check_Email(obj, name);
						break;
					case isCBMandatory:
						foundError = check_MandatoryCB(obj, name);
						break;
					default:
						break;
				}
			}
		} else {
			alert("ADMIN: object not found \"" + fieldList[i][0] + "\"");
		}
	}
	if (!foundError) {
		return true;
	}
	else {
	    return false
	}
}

/**
 * check_Mandatory
 */
function check_Mandatory(obj, name) {
	var error = false;
	if (obj.value == null || obj.value == "") {
		error = true;
		alert(MESSAGE_MANDATORY_ERROR + "\n" + name);
		obj.focus();
	}
	return error;
}

/**
 * check_MandatoryCB
 */
function check_MandatoryCB(obj, name) {
	var strCB= "";
	var error = false;
	var foundChecked = false;
	for (var i = 0; !foundChecked && i < obj.length; i++) {
		if (obj[i].checked) {
			foundChecked = true;
			strCB = strCB + obj[i].value + ", "
			document.form.Enquirystring.value = strCB
		}
	}
	if (!foundChecked) {
		error = true;
		alert(MESSAGE_MANDATORY_CB_ERROR + "\n" + name);
	}
	return error;
}

/**
 * check_Email
 * Acceptable entries <name>@<address>
 *	where "name" & "address" can be made from letters, numbers and "_-." only
 */
function check_Email(obj, name) {
	var error = false;
	var v = obj.value;
	v = v.replace(/[a-zA-Z0-9_.-]/g,"A");	//convert all letters, numbers, & "_", "." & "-" to "A"
	v = v.replace(/AA+/g,"A");				//convert "AA" to "A"
	if (v != "A@A") {
		error = true;
		alert(MESSAGE_EMAIL_ERROR);
		obj.focus();
	}
	return error;
}
/**
 * check_T's & C's confirmed
 *	Single checkbox validation
 */
function check_MandatoryTandCs(obj, name) {
	var error = false;
	var foundChecked = false;
	
		if (obj.checked) {
		foundChecked = true	
	}
		if (!foundChecked) {
		error = true;
		alert(MESSAGE_MANDATORY_TANDCS_ERROR);
	}
	return error;
}


/**
 * check radio box selected
 *
 */
function check_MandatoryRadio(obj, name) {
 	var error = false;
	var foundChecked = false;

	for (var i = 0; !foundChecked && i < obj.length; i++) {
		if (obj[i].checked) {
			foundChecked = true	
	}
	}
		if (!foundChecked) {
		error = true;
		alert(MESSAGE_MANDATORY_RADIO_ERROR + "\n" + name);
	}

	return error;
}
/**
 * check item selected from drop down list
 *
 */
function check_MandatoryDropdown(obj, name) {
	var error = false;
	var foundChecked = false;
	
	if (!obj.options[0].selected) {
			foundChecked = true	
	}
		if (!foundChecked) {
		error = true;
		alert(MESSAGE_MANDATORY_DROPDOWN_ERROR + "\n" + name);
	}
	return error;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}
