// 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 = "#FFFF66";	//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") {			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[fAsplit[0]].value != f["confirm"+fAsplit[0]].value) {				alert("'" + fAsplit[1] + "' does not match 'Confirm " + fAsplit[1] + "'.");				f[fAsplit[0]].focus();				return false;				break;			}		}	}	if (test && vFTCa) {		return confirm("Do you want to submit?");	} else {		return true;	}	//return false;}*/function resetFields(whichform) {  for (var i=0; i<whichform.elements.length; i++) {    var element = whichform.elements[i];    if (element.type == "submit") continue;    if (!element.defaultValue) continue;    element.onfocus = function() {    if (this.value == this.defaultValue) {      this.value = "";     }    }    element.onblur = function() {      if (this.value == "") {        this.value = this.defaultValue;      }    }  }}function validateForm(whichform) {	// remove any error messages	var spans = document.getElementsByTagName("span");	for (var i=0; i<spans.length; i++) {		if (spans[i].getAttribute("className") == "errorMsg") {			spans[i].parentNode.removeChild(spans[i]);			i--;		}	}	//test mode	if (whichform.className.indexOf("test") != -1) {		if (!confirm("You are in test mode. Do you want to continue?")) return false;	}	var validated = true;	for (var i=0; i<whichform.elements.length; i++) {		var element = whichform.elements[i];		if (element.className.indexOf("required") != -1) { //if this field is mandatory			if (element.name.toLowerCase().indexOf("email") != -1) {				if (whichform.className.indexOf("test") != -1) element.value = "Test@Value.com";				if (!isEmail(element)) {					createErrorMsg("Please complete, using a valid email address!", element);					validated = false;				}			//call custom validation function and error messages			/*} else if (condition) {				if (!isFunction(element)) {					createErrorMsg("", element);					validated = false;				}			*/					} else if (element.name.toLowerCase().indexOf("keyphrase") != -1) {				if (whichform.className.indexOf("test") != -1) element.value = "Test Keyphrase";				if (!isKeyphrase(element)) {					createErrorMsg("Please type at least TWO words into this box!", element);					validated = false;				}													} else {				if (whichform.className.indexOf("test") != -1) element.value = "Test Value";				if (!isFilled(element)) {					createErrorMsg("Please fill in  "+element.title+"!", element);					validated = false;				}			}		}	}	if (whichform.className.indexOf("test") != -1) {		if (!confirm("Do you want to submit this form?")) return false;	}	return validated;}function createErrorMsg(msg, element) {	var errorMsg = document.createElement("span");	errorMsg.setAttribute("class","errorMsg");	errorMsg.setAttribute("className","errorMsg");	var emText = document.createTextNode(msg);	errorMsg.appendChild(emText); 	element.parentNode.insertBefore(errorMsg,element);	}function isFilled(field) {  if (field.value.length < 1 || field.value == field.defaultValue) {    return false;  } else {    return true;  }}function isEmail(field) {  if (field.value.substr(field.value.indexOf("@")+1,1) == "." || field.value.lastIndexOf(".") < field.value.indexOf("@") || field.value.lastIndexOf(".") >= field.value.length-2 || field.value == field.defaultValue) {    return false;  } else {    return true;  }}function isKeyphrase(field) {	var words = field.value.split(" ");	if (words.length <= 1 || field.value == field.defaultValue) {		return false;	} else {		return true;	}}function prepareForms() {  for (var i=0; i<document.forms.length; i++) {    var thisform = document.forms[i];    resetFields(thisform);    thisform.onsubmit = function() {      return validateForm(this);    }  }}addLoadEvent(prepareForms);
