// Trilogyhair.co.uk
// 10n6.com / Matthew Harrington
// ©2010 10n6



function checkform(formname,fields,linkto,returnval){
var fieldslist = fields.split(",");
var fieldsnum = fieldslist.length;
var formerrors = 0;
for(i = 0; i < fieldslist.length; i++){
var testeditor = fieldslist[i];
var testeditorcut = (testeditor.length)-1;
var editorcheck = 0;

if(testeditor.charAt(testeditorcut) == "*"){
checkstring = testeditor.substring(0,testeditorcut);
check = document.getElementById(checkstring).value;
editorcheck = 1;
} else if(testeditor.charAt(testeditorcut) == "!"){
editorcheck = 2; // Dropdown not equal
positionequals = testeditor.indexOf('=');
checkstring = testeditor.substring(0,positionequals);
check = document.getElementById(checkstring).value;
var checkvalue = ""+ testeditor.substring((positionequals + 1),testeditorcut);
} else if(testeditor.charAt(testeditorcut) == "@"){

checkstring = testeditor.substring(0,testeditorcut);
check = "" + document.getElementById(checkstring).value;
editorcheck = 3; // Email Address

}else{
checkstring = testeditor;
check = document.getElementById(fieldslist[i]).value;
editorcheck = 0;
}

if(check == ""){
		if(editorcheck == 1){
		document.getElementById(checkstring+"_wrap").style.border = "1px solid red";
		document.getElementById(checkstring+"_wrap").style.backgroundColor = "yellow";
		} else{
		document.getElementById(checkstring).style.border = "1px solid red";
		document.getElementById(checkstring).style.backgroundColor = "yellow";
		}
		formerrors++;
	} else{
		if(editorcheck == 1){
		document.getElementById(checkstring+"_wrap").style.border = "0px solid silver";
		document.getElementById(checkstring+"_wrap").style.backgroundColor = "white";
		} else if(editorcheck == 2){
			if(check == checkvalue){
				document.getElementById(checkstring).style.border = "1px solid red";
				document.getElementById(checkstring).style.backgroundColor = "yellow";
				formerrors++;
			} else{
				document.getElementById(checkstring).style.border = "1px solid silver";
				document.getElementById(checkstring).style.backgroundColor = "white";
			}
		} else if(editorcheck == 3){
		 	apos = check.indexOf("@");
			dotpos = check.lastIndexOf(".");
			
		if (apos<1 || dotpos-apos<2){
				document.getElementById(checkstring).style.border = "1px solid red";
				document.getElementById(checkstring).style.backgroundColor = "yellow";
				formerrors++;
			} else{
				document.getElementById(checkstring).style.border = "1px solid silver";
				document.getElementById(checkstring).style.backgroundColor = "white";
			}
		
		
		}  else{
		document.getElementById(fieldslist[i]).style.border = "1px solid silver";
		document.getElementById(fieldslist[i]).style.backgroundColor = "white";
		}
	}
}


	if(formerrors > 0){
		Alert = "Please check/modify/enter information into the highlighted fields in the form.";
		document.getElementById('Alert').innerHTML = Alert;
		document.getElementById('Alert').style.display = "block";
		return false
	} else{
		Alert = "";
		document.getElementById('Alert').innerHTML = Alert;
		document.getElementById('Alert').style.display = "none";
		document.getElementById('form_do').value = "1";
		document.getElementById(formname).action = linkto + "?m="+returnval;
		document.getElementById(formname).submit();
		return true
	}
}

