function validateSignIn(theForm) {
var reason = "";

  reason += validateEmail(theForm.email);	
  reason += validatePassword(theForm.pwd);
         
  if (reason != "") {
    alert("Sorry, Please Correct Below Fields:\n" + reason);
    return false;
  }

  return true;
}

function validateSignUp(theForm) {
var reason = "";

  reason += validateUsername(theForm.name);
  reason += validateEmail(theForm.email);
       
  if (reason != "") {
    alert("Sorry, Please Correct Below Fields:\n" + reason);
    return false;
  }

  return true;
}

function validateSignUpcontactus(theForm) {
var reason = "";

  reason += validateUsername(theForm.Name);
  reason += validateEmail(theForm.Email);
  reason += validateGeneral(theForm.Phone);
  reason += validateGeneral(theForm.Phone2);
  reason += validateGeneral(theForm.Town);
  reason += validateGeneral(theForm.Zip);
  reason += validateGeneral(theForm.Fax); 
  reason += validateGeneral(theForm.Hear); 
  
  if (reason != "") {
    alert("Sorry, Please Correct Below Fields:\n" + reason);
    return false;
  }

  return true;
}

function validateSignUpfreesuits(theForm) {
var reason = "";

  reason += validateUsername(theForm.Name);
  reason += validateEmail(theForm.Email);
  reason += validateGeneral(theForm.Phone);
  reason += validateGeneral(theForm.REFName);
  reason += validateGeneral(theForm.REFPhone);
        
  if (reason != "") {
    alert("Sorry, Please Correct Below Fields:\n" + reason);
    return false;
  }

  return true;
}

function validateUnsubscribe(theForm) {
var reason = "";

  reason += validateEmail(theForm.email);
       
  if (reason != "") {
    alert(reason);
    return false;
  }

  return true;
}

function validateSearch(theForm) {
var reason = "";

  reason += validateSearch_key(theForm.search_term);
       
  if (reason != "") {
    alert(reason);
    return false;
  }

  return true;
}

function validateUsername(fld) {
    var error = "";
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\]/ ;
 
    if (fld.value == "") {
        fld.style.background = 'Yellow'; 
        error = "- You didn't enter a Name.\n";
    } else if (fld.value.length > 50) {
        fld.style.background = 'Yellow'; 
        error = "- The Name is too Long\n";
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = 'Yellow'; 
        error = "- The Name contains invalid characters.\n";
    } 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";
    } else if (fld.value.length > 25) {
        error = "- The password length is too long. \n";
        fld.style.background = 'Yellow';
    } else if (illegalChars.test(fld.value)) {
        error = "- The password contains invalid characters.\n";
        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.\n";
    }else if (fld.value.length > 50) {
        error = "- The Email length is too long. \n";
        fld.style.background = 'Yellow'; 
	}
	else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = 'Yellow';
        error = "- Please enter a valid email address.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = 'Yellow';
        error = "- The email address contains invalid characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validateSearch_key(fld) {
    var error = "";
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
 
    if (fld.value == "") {
        fld.style.background = 'Yellow'; 
        error = "- You didn't enter a Search keyword.\n";
    } else if (fld.value.length > 50) {
        fld.style.background = 'Yellow'; 
        error = "- The Search keywords are too Long\n";
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = 'Yellow'; 
        error = "- The Search Keywords contains invalid characters.\n";
    } else {
        fld.style.background = 'White';
    } 
    return error;
}


function validateGeneral(fld) {
    var fld = fld;
	var error = "";
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
 
    if (fld.value.length > 50) {
        fld.style.background = 'Yellow'; 
        error = "- The "+ fld.value +" are too Long\n";
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = 'Yellow'; 
        error = "- The "+ fld.value +" contains invalid characters.\n";
    } else {
        fld.style.background = 'White';
    } 
    return error;
}
