// JavaScript Document
var validateFormTestConfirm = true;

function MMCvalidateForm(form, fields, test) {
	test = (String(test)=="undefined")?false:test;
	//form = form.name (str), fields = "field1|Field 1,field2|Field 2" (str), test (boolean) populates fields
	f = document[form];
	fA = fields.split(",");
	
	if (test) {
		var vFTCa = (validateFormTestConfirm)?confirm("You are currently in test mode.\rDo you want to enter test values?"):vFTCa;
	}
	validateFormTestConfirm = false;
	//test string to insert in test mode
	tS = "Test Value";
	//set highlight colour for incomplete fields
	hC = "#FF9999";
	//set test colour for completed fields
	tC = "#33EEFF";
	//loop through required fields
	for (var i=0; i<fA.length; i++) {
		fAsplit = fA[i].split("|");
		//check field exists
		if (String(f[fAsplit[0]])=="undefined") {
			if (test) {
				alert("The form field " + fAsplit[0] + " is undefined");
				return false;
				break;
			}
		//check for radio buttons
		} else if ((f[fAsplit[0]].length > 0) && f[fAsplit[0]][0].type == "radio") {
			var validated = false;
			//loop through radio buttons
			for(var j=0; j<f[fAsplit[0]].length; j++) {		
				if (f[fAsplit[0]][j].checked == true) {
					validated = true;
					break;
				}
			}
			if (!validated) {
				//highlight radio button and focus it
				f[fAsplit[0]][0].style.backgroundColor = hC;
				alert("Please select an option for '" + fAsplit[1] + "'.");
				f[fAsplit[0]][0].focus();
				return false;
				break;
			} 
		}
		//check text & drop-downs
		if (f[fAsplit[0]].value == "" || f[fAsplit[0]].value == 0) {
			//insert test string if in test mode
			if (test && vFTCa) {
				f[fAsplit[0]].style.backgroundColor = tC;
				f[fAsplit[0]].value = (f[fAsplit[0]].value == "")?tS:f[fAsplit[0]].options[1].value;
				f[fAsplit[0]].value = (String(f[fAsplit[0]].name).toLowerCase().indexOf("email")!=-1)?"email@"+f[fAsplit[0]].value.replace(/\s+/g,"")+".com":f[fAsplit[0]].value;
			} else {
				//highlight incomplete field and focus
				f[fAsplit[0]].style.backgroundColor = hC;
				alert("Please enter a value into '" + fAsplit[1] + "'.");
				f[fAsplit[0]].focus();
				return false;
				break;
			}
		//if field name contains 'email' check structure of entered value fits x@x.xx
		} else if ((String(f[fAsplit[0]].name).toLowerCase().indexOf("email")!=-1) && (f[fAsplit[0]].value.indexOf("@")<1 || f[fAsplit[0]].value.indexOf(" ")!=-1 || f[fAsplit[0]].value.substr(f[fAsplit[0]].value.indexOf("@")+1,1) == "." ||(f[fAsplit[0]].value.lastIndexOf(".")<f[fAsplit[0]].value.indexOf("@")) || f[fAsplit[0]].value.lastIndexOf(".")>=f[fAsplit[0]].value.length-2)) {
			//highlight incomplete field and focus
			f[fAsplit[0]].style.backgroundColor = hC;
			alert("Please enter a valid email address into '" + fAsplit[1] + "'.");
			f[fAsplit[0]].focus();
			return false;
			break;
		//if field name contains 'password' check it matches 'confirm'
		} else if (String(f[fAsplit[0]].name).toLowerCase().indexOf("password")!=-1) {
			if ((f["confirm"+fAsplit[0]].type == "password") && f[fAsplit[0]].value != f["confirm"+fAsplit[0]].value) {
				alert("'" + fAsplit[1] + "' does not match 'Confirm " + fAsplit[1] + "'.");
				f[fAsplit[0]].focus();
				return false;
				break;
			}
		} else if (String(f[fAsplit[0]].name)=="agreeterms") {
			if (!f[fAsplit[0]].checked) {
				alert("You must agree to the terms and conditions before continuing.");
				f[fAsplit[0]].focus();
				return false;
				break;
			}
		}
	}
	if (test && vFTCa) {
		return confirm("Do you want to submit?");
	} else {
		return true;
	}
	//return false;
}