function validateFormOnSubmit(theForm) {
var reason = "";

  reason += validateMonth(theForm.Month);
  reason += validateDay(theForm.Day);
  reason += validateYear(theForm.Year);
  reason += validateName(theForm.Name);
  reason += validateEmail(theForm.Email);
  reason += validatePhone(theForm.Phone);
  reason += validateComments(theForm.Comments);
  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 validateMonth(fld) {
    var error = "";
 
    if (fld.options[fld.selectedIndex].value == "" || fld.options[fld.selectedIndex].value == "Month") {
        fld.style.background = 'Yellow'; 
        error = "- Select the month you prefer.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validateDay(fld) {
    var error = "";
 
    if (fld.options[fld.selectedIndex].value == "" || fld.options[fld.selectedIndex].value == "Date") {
        fld.style.background = 'Yellow'; 
        error = "- Select the date you prefer.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validateYear(fld) {
    var error = "";
 
    if (fld.options[fld.selectedIndex].value == "" || fld.options[fld.selectedIndex].value == "Year") {
        fld.style.background = 'Yellow'; 
        error = "- Select the year you prefer.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validateName(fld) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
        error = "- Enter your name.\n"
    } else {
        fld.style.background = 'White';
    }
    return error;  
}

function validateComments(fld) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
        error = "- Enter your message.\n"
    } else {
        fld.style.background = 'White';
    }
    return error;  
}

function validateVerification(fld) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
        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 = 'Yellow';
        error = "- Enter your email address.\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = 'Yellow';
        error = "- Please enter a valid email address.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validatePhone(fld) {
    var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');    

   if (fld.value == "") {
        error = "- Enter your phone number.\n";
        fld.style.background = 'Yellow';
    } else {
        fld.style.background = 'White';
    }
    return error;
}
