var d = document;

function contactSubmit(frm, ad) {
  var respSuccess = /SUCCESS/;
  var respDebug = /DEBUG/;
  var respFailed = /ERROR:/m;
  var stripFailed = /^ERROR:/m;

  if (validateContactForm(frm)) {
  } else {
    return false;
  }
  var req = new XMLHttpRequest();
  var elem = frm.elements;
  var ps = '';
  for(var i = 0; i < elem.length; i++) {
    ps += "&"+elem[i].name+"="+elem[i].value;
  }
  ps = ps.replace(/^&/, "");
  req.open("POST", "/cgi-bin/contact.pl", false);
  req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  req.send(ps);
  var ty = d.getElementById("thankYou");
  if (respSuccess.test(req.responseText)) {  
    if (ad == 1) {
      var ht = document.getElementById('hideThis');
      ht.style.display = "none";
      ty.innerHTML = "<p>Thank you for your inquiry.</p><p>A sales team member will contact you soon.</p>";
    } else {
      ty.innerHTML = "<p>Thank you for your submission</p><p>You should recieve a response within 24 hours.</p><br>";
    }
    d.contactForm.style.display = "none";
    ty.style.display = "";
    d.contactForm.reset();
  } else if (respFailed.test(req.responseText) && !respDebug.test(req.responseText)) {
    alert(req.responseText.replace(stripFailed, ''));
  } else {
    alert("There was a problem with your submission.\nCheck to make sure you have filled in all required fields");
}
/* else {
    ty.innerHTML = req.responseText;
    ty.style.display = "";
  } */
//  myform.innerHTML = req.responseText;
  return false;
}

function validateContactName(fld) {
    var error = "";
    var illegalChars = /['")(*&^%$#@!;_.]/;

    if (fld.value == "") {
        error = "You did not enter your name.\n";
    } else if (illegalChars.test(fld.value)) {
        error = "Your name contains invalid characters.\n";
    }
    try { 
        var turnRed = document.getElementById("lblName"); 
    }
    catch (e) {}
    if (error) {
	try { 
            turnRed.style.color = "#FF0000";
            turnRed.style.fontWeight = "bold";
        }
	catch (e) {}
    } else {
	try {
            turnRed.style.color = "";
            turnRed.style.fontWeight = "";
        }
        catch (e) {}
    }
	
    return error;
}


function validateUsername(fld) {
    var error = "";
    var illegalChars = /\W/; // allow letters, numbers, and underscores
 
    if (fld.value == "") {
        error = "You didn't enter a username.\n";
    } else if ((fld.value.length < 5) || (fld.value.length > 15)) {
        error = "The username must be at least 5 characters.\n";
    } else if (illegalChars.test(fld.value)) {
        error = "The username contains illegal characters.\n";
    } 
    try {
      var turnRed = document.getElementById("lblUsername");
      if (error) {
        turnRed.style.color = "#FF0000";
        turnRed.style.fontWeight = "bold";
      } else {
        turnRed.style.color = "";
        turnRed.style.fontWeight = "";
      }
    }
    catch (e) {}
    return error;
}

function validatePassword(fld,chkfld) {
    var error = "";
    var illegalChars = /['"\x00]/; // allow only letters and numbers 
 
    if (trim(fld.value) != trim(chkfld.value)) {
	error = "Passwords do not match.\n";
    } else if (fld.value == "") {
        error = "You didn't enter a password.\n";
    } else if ((fld.value.length < 5) || (fld.value.length > 15)) {
        error = "Password must be at least 5 characters long. \n";
    } else if (illegalChars.test(fld.value) == true) {
        error = "The password contains illegal characters.\n";
    } else if (!((fld.value.search(/(a-z)+/)) && (fld.value.search(/(0-9)+/)))) {
        error = "The password must contain at least one numeral.\n";
    }
    try {
	var turnRed = document.getElementById("lbl"+fld.name);
        var turnRedCheck = document.getElementById("lbl"+chkfld.name);
        if (error) {
            turnRed.style.color = "#FF0000";
            turnRed.style.fontWeight = "bold";
            turnRedCheck.style.color = "#FF0000";
            turnRedCheck.style.fontWeight = "bold";
        } else {
            turnRed.style.color = "";
            turnRed.style.fontWeight = "";
            turnRedCheck.style.color = "";
            turnRedCheck.style.fontWeight = "";
        }
    }
    catch (e) {}
    return error;
}  

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

function validateEmail(fld, chkfld) {
    var error="";
    var tchkfld = trim(chkfld.value);
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars = /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
    var emailreg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;

    
    if (trim(fld.value) != trim(chkfld.value)) {
        error = "Emails do not match\n";
    } else if (fld.value == "") {
        error = "You didn't enter an email address.\n";
//    } else if (emailFilter.test(tfld)) {              //test email for illegal characters
//      error = "Please enter a valid email address.\n";
    } else if (emailreg.test(trim(fld.value)) == false) {
        error = "Please enter a valid email address.\n";
    } else if (fld.value.match(illegalChars)) {
        error = "The email address contains illegal characters.\n";
    }
    try {
        var turnRed = document.getElementById("lbl"+fld.name);
        var turnRedCheck = document.getElementById("lbl"+chkfld.name);
        if (error) {
            turnRed.style.color = "#FF0000";
            turnRed.style.fontWeight = "bold";
            turnRedCheck.style.color = "#FF0000";
            turnRedCheck.style.fontWeight = "bold";
        } else {
            turnRed.style.color = "";
            turnRed.style.fontWeight = "";
            turnRedCheck.style.color = "";
            turnRedCheck.style.fontWeight = "";
        }
    }
    catch (e) {}

    return error;
}

function validateIP(fld, required) {
  var error="";
  var IPformat = /(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)/;
  if (required == 1) {
    if (!fld.value.match(IPformat)) {
      error = "Invalid IP.\n";
    }
  }
  try {
    var turnRed = document.getElementById("lbl"+fld.name);
    if (error) {
      turnRed.style.color = "#FF0000";
      turnRed.style.fontWeight = "bold";
    } else {
      turnRed.style.color = "";
      turnRed.style.fontWeight = "";
    }
  }
  catch (e) {}
  return error;
}

function validateTOS(fld) {
    var error="";
   
    if (fld.checked == false) {
	error = "You must aggree to the Terms of Service.\n";
    } else {
	fld.style.background = 'White';
    }
    try {
        var turnRed = document.getElementById("lblTOS");
        if (error) {
            turnRed.style.color = "#FF0000";
            turnRed.style.fontWeight = "bold";
        } else {
            turnRed.style.color = "";
            turnRed.style.fontWeight = "";
       }
    } 
    catch (e) {}

    return error;
}

function validateContactSubject(fld) {
    var error = "";
    if (fld.value == " ") {
        error = "You must choose a subject.\n";
    }
    try { 
        var turnRed = document.getElementById("lblSubject"); 
    }
    catch (e) {}
    if (error) {
	try { 
            turnRed.style.color = "#FF0000";
            turnRed.style.fontWeight = "bold";
        }
	catch (e) {}
    } else {
	try {
            turnRed.style.color = "";
            turnRed.style.fontWeight = "";
        }
        catch (e) {}
    }	
    return error;
}

function validateContactMessage(fld) {
    var error = "";
    if (fld.value == "") {
        error = "You must enter a message.\n";
    }
    try { 
        var turnRed = document.getElementById("lblMessage"); 
    }
    catch (e) {}
    if (error) {
	try { 
            turnRed.style.color = "#FF0000";
            turnRed.style.fontWeight = "bold";
        }
	catch (e) {}
    } else {
	try {
            turnRed.style.color = "";
            turnRed.style.fontWeight = "";
        }
        catch (e) {}
    }
    return error;
}
function validatePhone(fld) {
    var error = "";
    if (fld.value == "") {
        error = "You must enter a phone number.\n";
    }
    try { 
        var turnRed = document.getElementById("lbl"+fld.name); 
    }
    catch (e) {}
    if (error) {
	try { 
            turnRed.style.color = "#FF0000";
            turnRed.style.fontWeight = "bold";
        }
	catch (e) {}
    } else {
	try {
            turnRed.style.color = "";
            turnRed.style.fontWeight = "";
        }
        catch (e) {}
    }	
    return error;
}

function validateFormOnSubmit(theForm) {
  var reason = "";
  reason += validateUsername(theForm.uname);
  reason += validatePassword(theForm.p1, theForm.p2);
  reason += validateEmail(theForm.em1,theForm.em2);
  reason += validateTOS(theForm.tos);
  reason += validateIP(theForm.devIP, theForm.devBox.checked);
  reason += validatePhone(theForm.ph);
  if (reason != "") {
    alert("Some fields need correction:\n\n" + reason);
    return false;
  }

  return true;
}

function validateContactForm(theForm) {
  var reason = "";
  reason += validateEmail(theForm.email,theForm.email);
  reason += validateContactSubject(theForm.subject);
  reason += validateContactName(theForm.name);
  if (theForm.subject.value == 's01' || theForm.subject.value == 's02') {
    reason += validatePhone(theForm.phoneNumber);
  }
  reason += validateContactMessage(theForm.comments);
  if (reason != "") {
    alert("Some fields need correction:\n\n" + reason);
    return false;
  }

  return true;
}

