function validateFormOnSubmit(theForm) {
var reason = "";

  reason += validateName(theForm.Name);
  reason += validateEmail(theForm.Email);
  reason += validateVerification(theForm.Verification);
      
  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 validateName(fld) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = '#afd1c0'; 
        error = "- Enter your name.\n"
    } else {
        fld.style.background = 'White';
    }
    return error;  
}

function validateVerification(fld) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = '#afd1c0'; 
        error = "- Enter the verification code.\n"
    } else {
        fld.style.background = 'White';
    }
    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.style.background = '#afd1c0';
        error = "- Enter your email address.\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = '#afd1c0';
        error = "- Please enter a valid email address.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}
