//<![CDATA[

function isNumeric(value) {
  if (value == null || !value.toString().match(/^[-]?\d*\.?\d*$/)) return false;
  return true;
}

function validateForm()
{
	var retVal = true;
	
	var validemail = validateEmail($("#id_email").val(), $("#id_confirm_email").val());
	
	//Email
	if (!validemail){
		$("#id_email, #id_confirmEmail").addClass("error");
		retVal = false;
		$("#emailError").show();
	}else{
		$("#id_email, #id_confirm_email").removeClass("error");
		$("#emailError").hide();
	}
	
	//firstname
	var currentObject = $("#id_nameFirst");
	var nameError = false;
	
	if (currentObject.val().length == 0){
		currentObject.addClass("error");
		retVal = false;
		nameError = true;
	}else{
		currentObject.removeClass("error");
	}
	
	//Lastname
	currentObject = $("#id_nameLast");
	
	if (currentObject.val().length == 0){
		currentObject.addClass("error");
		retVal = false;
		nameError = true;
	}else{
		currentObject.removeClass("error");
	}
	
	if(nameError){
		$("#nameError").show();
	}else{
		$("#nameError").hide();
	}
	

	// Phone Stuff
	valueToTest = $("#id_phoneNumber").attr("value");
	valueToTest = valueToTest.replace(/^1/, "");
	valueToTest = valueToTest.replace(/[^0123456789]/g, "");
	
	currentObject = $("#id_phoneNumber");
	
	if (valueToTest.length < 10){
		currentObject.addClass("error");
		retVal = false;
		$("#phoneNumberError").show();
	}else{
		currentObject.removeClass("error");
		$("#phoneNumberError").hide();
	}

	//Company Name
	currentObject = $("#id_companyName");

	if (currentObject.val().length == 0){
		currentObject.addClass("error");
		retVal = false;
		$("#companyNameError").show();
	}else{
		currentObject.removeClass("error");
		$("#companyNameError").hide();
	}
	
	//The confidentiality check box
	// Note since the thing we are checking is not the thing that we are highlighting we need to not use the above idiom.
	if( $("#id_confidentialityAgreement").attr("checked") == false){
		$("#confidentialityAgreementBox").addClass("error");
		retVal = false;
	}else{
		$("#confidentialityAgreementBox").removeClass("error");		
	}
	
	if( $("#id_bestWay").attr("value") == ""){
	    $("#id_bestWay").addClass("errorForChoice");
	    retval = false;
	}else{
	    $("#id_bestWay").removeClass("errorForChoice");
	}
	if( $("#id_bestTime").attr("value") == ""){
	    $("#id_bestTime").addClass("errorForChoice");
	    retval = false;
	}else{
	    $("#id_bestTime").removeClass("errorForChoice");
	}
	if( $("#id_contactUrgency").attr("value") == ""){
	    $("#id_contactUrgency").addClass("errorForChoice");
	    retval = false;
	}else{
	    $("#id_contactUrgency").removeClass("errorForChoice");
	}
		
	
	//only show the box if there is a problem.
	if(!retVal){
		$("#submissionErrorTop, #submissionErrorBottom").show("slow");
	}
	
	return retVal;
}

function validateEmail(email1, email2)
{	
	reTest = /[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/;
	if(email1 == undefined || email1 === "") return false;
	//return (email1 == email2 && email1.match(reTest));
	return (email1.match(reTest));
}


$(document).ready(function(){ $("form").submit(function(){ return validateForm();});});
//]]>