// JavaScript Document

// global var
var error_count = 0;

function validateFormOnSubmit(theForm) {
var reason = "";

  reason += validateEmpty(theForm.subject,"Subject");
  reason += validateEmpty(theForm.name,"Name");
  reason += validateEmpty(theForm.city,"City");
  reason += validateEmpty(theForm.state,"State");
  reason += validateEmail(theForm.email);
  reason += validatePhone(theForm.phone);
  reason += validateEmpty(theForm.comments,"Comments");
  reason += validateEmpty(theForm.security_code,"Message Key");
  reason += validateEmpty(theForm.how,"How did you hear about us?");
      
  if (reason != "") {
		link_loc = "../_scripts/contact_form_error.php";
		height = error_count*21+75;
		error_count = 0;
		Mediabox.open('../_scripts/contact_form_error.php?reason='+reason, '', '300 ' + height );
    return false;
  }

  return true;
}

function validate_new_login(theForm) {
var reason = "";

  reason += validateUsername(theForm.user);
  reason += validatePassword(theForm.pass);
      
  if (reason != "") {
    alert("Some fields need correction: \n\n" + reason);
    return false;
		error_count++;
  }
  return true;
}

function validate_login(theForm) {
var reason = "";

  reason += validateEmpty_alert(theForm.user, "Username");
  reason += validateEmpty_alert(theForm.pass, "Password");
      
  if (reason != "") {
    alert("Some fields need correction: \n\n" + reason);
    return false;
		error_count++;
  }
  return true;
}

function validateEmpty(fld,name) {
    var error = "";
  
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
        error = name + " is missing. ~~~~"
				error_count++;
    } else {
        fld.style.background = 'White';
    }
    return error;   
}

function validateEmpty_alert(fld,name) {
    var error = "";
  
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
        error = name + " is missing. \n"
				error_count++;
    } else {
        fld.style.background = 'White';
    }
    return error;   
}

function validateUsername(fld) {
    var error = "";
    var illegalChars = /\W/; // allow letters, numbers, and underscores
 
    if (fld.value == "") {
        fld.style.background = 'Yellow'; 
        error = "You didn't enter a username. ~~~~";
				error_count++;
    } else if ((fld.value.length < 5) || (fld.value.length > 15)) {
        fld.style.background = 'Yellow'; 
        error = "The username is the wrong length. ~~~~";
				error_count++;
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = 'Yellow'; 
        error = "The username contains illegal characters. ~~~~";
				error_count++;
    } else {
        fld.style.background = 'White';
    } 
    return error;
}

function validatePassword(fld) {
    var error = "";
    var illegalChars = /[\W_]/; // allow only letters and numbers 
 
    if (fld.value == "") {
        fld.style.background = 'Yellow';
        error = "You didn't enter a password. \n";
				error_count++;
    } else if ((fld.value.length < 7) || (fld.value.length > 15)) {
        error = "The password is the wrong length. \n";
				error_count++;
        fld.style.background = 'Yellow';
    } else if (illegalChars.test(fld.value)) {
        error = "The password contains illegal characters. \n";
				error_count++;
        fld.style.background = 'Yellow';
    } else if (!((fld.value.search(/(a-z)+/)) && (fld.value.search(/(0-9)+/)))) {
        error = "The password must contain at least one numeral. \n";
				error_count++;
        fld.style.background = 'Yellow';
    } 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$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
    
    if (fld.value == "") {
        fld.style.background = 'Yellow';
        error = "You didn't enter an email address. ~~~~";
				error_count++;
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = 'Yellow';
        error = "Please enter a valid email address. ~~~~";
				error_count++;
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = 'Yellow';
        error = "The email address contains illegal characters. ~~~~";
				error_count++;
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validatePhone(fld) {
    var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');     

   if (fld.value == "") {
        error = "You didn't enter a phone number. ~~~~";
				error_count++;
        fld.style.background = 'Yellow';
    } else if (isNaN(parseInt(stripped))) {
        error = "The phone number contains illegal characters. ~~~~";
				error_count++;
        fld.style.background = 'Yellow';
    } else if (!(stripped.length == 10)) {
        error = "The phone number is the wrong length. Make sure you included an area code. ~~~~";
				error_count++;
        fld.style.background = 'Yellow';
    } else {
        fld.style.background = 'White';
		}
    return error;
}

