<!--//--><![CDATA[//><!--

function genericValidate(myForm){
	
	var result = true;
	
	if (form_fields == undefined) {
		alert("Error: form meta is missing 8-(");
		return false;
	}
	// loop through all fields
	for (var i=0;i<form_fields.length;i++){
		
		var field_result = true;
		
		var cm = form_fields[i];
		var cf = myForm[cm[0]];
		
		// compulsory fields
		if (isCompulsory(cm)){
			//  is empty
			if (isEmpty(cf, cm)) {
				field_result = false;
			}
			// still default text ??
			
		}
	
	
		if (getType(cm) == "email" && !isEmpty(cf, cm)){
			// bad email
			if (!checkEmail(cf.value)) {
				field_result = false;
			}
		}
		
		if (field_result == false) {
			result = false;
		}
		flagError(cm[0], field_result);
		
	}
	return result;
}

function endValidation(result){
	// proceed or advise of errors
	if (result){

	} else {
		alert("The form is not complete.\nErrors are outlined in red.");
	}
}

function flagError(fieldName, isHealthy){
	var highlight = document.getElementById(fieldName + "-error");
	if (isHealthy){
		highlight.setAttribute("class","form-error-disabled");
		highlight.setAttribute("className","form-error-disabled");
	} else {
		highlight.setAttribute("class","form-error");
		highlight.setAttribute("className","form-error");
	}
}

function getType(cm){
	return cm[2];
}

function isCompulsory(cm){
	if (cm[4] == true) return true;
	return false;
}

function isEmpty(cf, cm){
	
	if (getType(cm) == "string" || getType(cm) == "email"){
		if (cf.value == "") return true;
	} else if (getType(cm) == "radio"){
		if (getRadioValue(cf) == null) return true;
	} else if (getType(cm) == "dropdown") {
		if (getDropdownValue(cf) == "") return true;
	}
	
	return false;
}



// text fields
function restrictLength(field, maxlimit) {

	if (field.value.length > maxlimit) {
	// if too long...trim it!
		field.value = field.value.substring(0, maxlimit);
	}
}

function clearField(field, orig){
	if (field.value == orig) field.value = "";
}

function restrictToNumbers(field){
	var input_value = field.value;
	var validChars = "-.0123456789";
	var len = input_value.length;
	for (var i = len - 1; i >= 0; i--){
		if (validChars.indexOf(input_value.charAt(i)) == -1){
			var front_bit = input_value.slice(0,i)
			var back_bit = input_value.slice(i+1);
			input_value = front_bit + back_bit;
		}
	}
	field.value = input_value;
}


// radio buttons
function getRadioValue(cf){
	for (var i = 0; i < cf.length; i++){
		if (cf[i].checked) return cf[i].value;
	}
	return null;
}


// drop down menus ** not multiple selects
function getDropdownValue(cf){
	return cf.options[cf.selectedIndex].text;
}

// emails
function checkEmail(strng) {
	
	if (strng == "") return false;

	var emailFilter=/^.+@.+\..{2,3}$/;
	
	if (!(emailFilter.test(strng))) { 
		return false;
	} else {
		//test email for illegal characters
		var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/
		if (strng.match(illegalChars)) return false;
	}
	return true;
}


// check postcode
function checkPostcode (strng) {
	if (isNaN(strng)) return false;
	if (strng.length != 4) return false;
	return true;
}

// phone number - strip out delimiters and check for 8 - 10 digits
function checkPhone (strng) {
	var stripped = strng.replace(/[\(\)\.\-\+\ ]/g, ''); //strip out acceptable non-numeric characters
	if (isNaN(parseInt(stripped))) {
		return false;
	}
	
	if (stripped.length < 8 || stripped.length > 12) return false;

	return true;
	
}

// password - between 6-8 chars, uppercase, lowercase, and numeral
function checkPassword (strng) {
	var error = "";
	if (strng == "") {
		error = "You didn't enter a password.\n";
	}

	var illegalChars = /[\W_]/; // allow only letters and numbers

	if ((strng.length < 6) || (strng.length > 8)) {
		error = "The password is the wrong length.\n";
	} else if (illegalChars.test(strng)) {
		error = "The password contains illegal characters.\n";
	} else if (!((strng.search(/(a-z)+/)) && (strng.search(/(A-Z)+/)) && (strng.search(/(0-9)+/)))) {
		error = "The password must contain at least one uppercase letter, one lowercase letter, and one numeral.\n";
	}  
	return error;
}    

// username - 4-10 chars, uc, lc, and underscore only.
function checkUsername (strng) {
	var error = "";
	if (strng == "") {
		error = "You didn't enter a username.\n";
	}

	var illegalChars = /\W/; // allow letters, numbers, and underscores
	if ((strng.length < 4) || (strng.length > 10)) {
		error = "The username is the wrong length.\n";
	} else if (illegalChars.test(strng)) {
		error = "The username contains illegal characters.\n";
	} 
	return error;
}       


//--><!]]>