function validateFormOnSubmit(theForm) {
var reason = "";

	reason += validateappointmentdate(theForm.appointmentdate);
	reason += validateName(theForm.Name);
	reason += validateEmail(theForm.Email);
	reason += validatePhone(theForm.Phone);
	reason += validateQuestion(theForm.Question);
      
  if (reason != "") {
    alert("Following field(s) need to be Filled:\n" + reason);
    return false;
  }
  if(checkValue(theForm)) { 
	insitePost(theForm); 
  }
  setTimeout(function(){theForm.submit();},500); // increase this value if it's not tracking properly
  return true;
}

function validateappointmentdate(fld) {
    var error = "";
 
    if (fld.value == "0000-00-00" || fld.value == "") {
        fld.style.color = '#0f0802'; 
        error = "- Please select the date.\n"
    } else {
        fld.style.color = '#0f0802';
    }
    return error;  
}

function validateQuestion(fld){
	var error = "";
	
    if (fld.value == "") {
        fld.style.color = '#0f0802'; 
        error = "- Answer the simple question.\n";
    } else {
        fld.style.color = '#0f0802'; 
    }
    return error; 

}

function validateName(fld) {
    var error = "";
 
    if (fld.value == "* Your Name"  ||  fld.value == "") {
        fld.style.color = '#0f0802'; 
        error = "- Enter your name.\n"
    } else {
        fld.style.color = '#0f0802';
    }
    return error;  
}



function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
}

function validateEmail(fld) {
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
   
    if (fld.value == "" || fld.value == "* Your Email") {
        fld.style.color = '#0f0802';
        error = "- Enter your email address.\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.color = '#0f0802';
        error = "- Please enter a valid email address.\n";
    } else {
        fld.style.color = '#0f0802';
    }
    return error;
}

function isNumeric(v) {
	return v.length > 0 && !isNaN(v) && v.search(/[A-Z]|[#]/ig) == -1;
};
		
function validatePhone(fld) {
    var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');    

   if (fld.value == "" || fld.value == "* Your Phone") {
        error = "- Please enter your telephone number.\n";
        fld.style.background = '#efe6cc';
    } else {
		if (isNumeric(fld.value) == false) {
			error = "- Please enter only numeric values for the telephone number.\n";
		}
		else{
			fld.style.background = '#efe6cc';
		}
    }
    return error;
}


