

var errorBorderColour = '#900';
var passBorderColourLeft='#d1d1d1';
var passBorderColourTop='#d1d1d1';
var passBorderColourRight='#d1d1d1';
var passBorderColourBottom='#d1d1d1';

// change field styles if validated or not
function changeFieldStyles(fld, validationValue) {
	if(validationValue) {
		fld.style.borderLeftColor = passBorderColourLeft;
		fld.style.borderTopColor = passBorderColourTop;
		fld.style.borderRightColor = passBorderColourRight;
		fld.style.borderBottomColor = passBorderColourBottom;
	} else {
		fld.style.borderColor = errorBorderColour;
	}
}
//A simple Trim implementation
function trim (str) {
	//Replace all whitespace characters with nothing
	//and return the new "trimmed" string
	return str.replace(/^\s*|\s*$/g,"");
}


/* ASK A QUESTION
-------------------------------------------------------------------------------------------------------------------------------------------------------------*/

//Form validation func
function validateForm(theForm) {
	theForm.form_type.value = "j";
	var reason = "";
	//Set a flag to store whether the form is valid or not
	var valid = true;

	//Check to see if the name has been filled in
	if (trim(theForm.form_contact_name.value).length == 0 || trim(theForm.form_contact_name.value) == "Your name") {
		//Tell the user there was a problem with their name
		//alert ("You have not provided your name.");
		reason += "* You have not provided your name.\n";
		
		//Change the appearance of the field
		//theForm.form_contact_name.className = "form-error";
		changeFieldStyles(theForm.form_contact_name, false);
		
		//Place the cursor in the name field
		//theForm.form_contact_name.focus();
		
		//Form is invalid
		valid = false;
	} else {
		//Field is valid so clear error class
		//theForm.form_contact_name.className = "";
		changeFieldStyles(theForm.form_contact_name, true);
	}
	
	//Check to see if the email has been filled in
	if (trim(theForm.form_contact_email.value).length == 0) {
		//Tell the user there was a problem with their name
		//alert ("You have not provided your E-mail address.");
		reason += "* You have not provided your E-mail address.\n";
		
		//Change the appearance of the field
		//theForm.form_contact_email.className = "form-error";
		changeFieldStyles(theForm.form_contact_email, false);
		
		//Place the cursor in the name field
		//theForm.form_contact_email.focus();
		
		//Form is invalid
		valid = false;
	} else {
		//Perform Email Validity Check
		if (theForm.form_contact_email.value.match(/^[\.\-\w]+@[\.\-\w]+\.[^\.\W\d]{2,4}$/i) == null) {
			//Invalid Email addy format
			//alert ("Your E-mail address appears to be invalid.");
			reason += "* Your E-mail address appears to be invalid.\n";
			//theForm.form_contact_email.className = "form-error";
			changeFieldStyles(theForm.form_contact_email, false);
			//theForm.form_contact_email.focus();
			
			valid = false;
		} else {
			//clear the error class
			theForm.form_contact_email.className = "";
			changeFieldStyles(theForm.form_contact_email, true);
		}
	}
	
	//Confirm the message
	if (trim(theForm.form_contact_message.value).length == 0) {
		//alert ("You have not added your message.");
		reason += "* You have not added your question.\n";
		//theForm.form_contact_message.className = "form-error";
		changeFieldStyles(theForm.form_contact_message, false);
		//theForm.form_contact_message.focus();
		
		valid = false;
	} else {
		changeFieldStyles(theForm.form_contact_message, true);
	}

if (reason !== "") {
   alert("Please correct the fields described below and that are marked in red:" + "\n\n" + "" + reason + "\nThank you." );
   valid = false;
} 
	
	return valid;
	
}

//And finally, handle the server's response
function handleFormSubmit(resultText, resultXML) {
	//alert(resultText  + " --- " + resultXML);
	
	if (resultText) {
		resultText +="<br/><br/>";
		var theForm = document.getElementById("form-message");
		var replaceEle = document.createElement("div");
		replaceEle.innerHTML = resultText;
		
		theForm.parentNode.replaceChild(replaceEle, theForm);
	}
}

function AJAXSubmitForm (form, debug, resultFunc, validationFunc) {
	document.MM_returnValue = submitForm(form, debug, resultFunc, validationFunc);
	
}

/* EMAIL SUBSCRIPTION
-------------------------------------------------------------------------------------------------------------------------------------------------------------*/

function validateFormOnSubmit(theForm) {
var reason = "";
var error = "";
var target1 = "cm-f-hrihdt";
var target2 = "cm-name";
var target3 = "cm-biddrd-biddrd";
var value1 = $("#cm-f-hrihdt").val();
var value2 = $("#cm-name").val();
var value3 = $("#cm-biddrd-biddrd").val();

	reason += validateFirstName(target1, value1);
	reason += validateLastName(target2, value2);
	reason += validateEmail(target3, value3);
  
  if (reason != "") {
    alert("Please correct the fields described below and that are marked in red:\n\n" + "" + reason + "\nThank you." );
    return false;
  }
	return true;
}

function validateFirstName(fld, value) {

	theTargetField = document.getElementById(fld);

    var error = "";
	
    if (value == "" || value.length < 2 || value == "First name") {
		
        changeFieldStyles(theTargetField, false)
        error = "* Please include your first name.\n";
    } else {
        changeFieldStyles(fld, true);
    }
    return error;
}
function validateLastName(fld, value) {

	theTargetField = document.getElementById(fld);

    var error = ""; 
    if (value == "" || value.length < 2 || value == "Last name") {
        changeFieldStyles(theTargetField, false)
        error = "* Please include your last name.\n";
    } else {
        changeFieldStyles(theTargetField, true);
    }
    return error;
}
function validateEmail(fld, value) {

	theTargetField = document.getElementById(fld);

    var error="";
    var fld = trim(value); // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
   
    if (value == "" || value == "email@address") {
        error = "* Please include an email address.\n";
		changeFieldStyles(theTargetField, false);
    } else if (!emailFilter.test(value)) { //test email for illegal characters
        error = "* Please include a valid email address.\n";
		//$("#error-email").html(error);
		changeFieldStyles(theTargetField, false);
    } else if (fld.value.match(illegalChars)) {
        error = "* Your email address contains illegal characters.\n";
		//$("#error-email").html(error);
		changeFieldStyles(theTargetField, false);
    } else {
	   $("#error-email").html('');
	   changeFieldStyles(theTargetField, true);
    }
    return error;
}

