// JavaScript Document
function validateFormOnSubmit(theForm) {
var reason = "";

  reason += validateemail(theForm.email);//listo
   reason += validatename(theForm.name);//listo
    reason += validatesubject(theForm.subject);//listo 
	    reason += validatemessage(theForm.message);//listo 
	    if (reason != "") {
   alert("Some fields need corrections:\n" + reason);
   document.getElementById('receiver').style.display = "none";
   document.getElementById('receiver2').style.display = "block";
     
    return false;
  }
var valuec=document.getElementById('contact').value;
if (valuec == "sales") {
      alert("Your message has been successfully send to Lucy Velez[Sales].");
    } else if (valuec == "accounting") {              //test email for illegal characters
       alert("Your message has been successfully send to Nancy Negron[Accounting].");
    } else if (valuec == "customer") {
        alert("Your message has been successfully send to Milma Gonzalez[Customer Service].");
    } 
	else if (valuec == "inside") {
        alert("Your message has been successfully send to Lizzie Gonzalez[Inside Sales].");
    } 
	else if (valuec == "salesrep") {
        alert("Your message has been successfully send to Julio C. Gonzalez[Sales Executive].");
    } 
	else if (valuec == "webmaster"){
         alert("Your message has been successfully send to Julio G. Alvarado [WebMaster].");
    }
	else{
		alert("Your message has been successfully send to Julio C. Gonzalez[Sales Executive].");
	}
 document.getElementById('receiver2').style.display = "none";
  document.getElementById('receiver').style.display = "block";
  return true;
}

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$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
   
    if (fld.value == "") {
        fld.style.background = 'Yellow';
		
        error = "You must provide a valid Email address.\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = 'Yellow';
        error = "Please, provide a valid Email Address.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = 'Yellow';
        error = "Your Email Address contain illegal characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}
  
function validatename(fld) {
    var error = "";
    var illegalChars =/[\"\'\;]/; // allow letters, numbers, and underscores
 
    if (fld.value == "") {
        fld.style.background = 'Yellow'; 
        error = "You must provide a valid Name.\n";
    } else if ((fld.value.length < 5) || (fld.value.length > 40)) {
        fld.style.background = 'Yellow'; 
        error = "The Name must have at least 5 to 40 characters.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = 'Yellow'; 
        error = "The Name contain illegal characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validatesubject(fld) {
    var error = "";
    var illegalChars =/[\"\'\;]/; // allow letters, numbers, and underscores
 
    if (fld.value == "") {
        fld.style.background = 'Yellow'; 
        error = "You must provide a valid Subject.\n";
    } else if ((fld.value.length > 50)) {
        fld.style.background = 'Yellow'; 
        error = "Subject length exceeded. Max length 50 characters long.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = 'Yellow'; 
        error = "DO NOT USE dots in the Subject.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}
function validatemessage(fld) {
    var error = "";
 
    if (fld.value == "") {
        fld.style.background = 'Yellow'; 
        error = "You must provide a brief Message.\n";
    }else {
        fld.style.background = 'White';
    }
    return error;
}


