
function validateFormOnSubmit(theForm) {
var reason = "";



reason += validateFName(theForm.fname);
reason += validateLName(theForm.lname);
reason += validateEmpty(theForm.address1);
reason += validateCity(theForm.city);
reason += validateState(theForm.state);
reason += validateZip(theForm.zip);

reason += validatePhone(theForm.bphone);
reason += validateEmail(theForm.email);
reason += validateEmpty(theForm.business);
reason += validateEmpty(theForm.taxid);

reason += validateUsername(theForm.newuser);
reason += validatePassword(theForm.newpswd, theForm.retype);


if (reason != "") {
alert(reason);
return false;
}

return true;
}


function validateEmpty(fld) {
var error = "";

if (fld.value.length == 0) { fld.style.background = 'Yellow';  error = fld.name+" has not been filled in or selected.\n"
} else {
fld.style.background = 'White';
}
return error; 
}

function trim(s)
{
return s.replace(/^\s+|\s+$/, '');
} 


function validateCity(fld) {
var error = "";
var stripped = fld.value.replace(/[\(\)\.\-\ \+]/g, ''); 
if (fld.value == "") {  error = "You didn't enter City.\n";  fld.style.background = 'Yellow';} 
else if ((/[0-9=!@#$%\^&*_+~`|\(\)\-\?\/\<\>\,\;\:\\\"\[\]]/.test(fld.value))) { 
    error = "The City contains illegal characters.\n"; fld.style.background = 'Yellow';
 } 
return error;
}

function validateState(fld) {
var error = "";
var stripped = fld.value.replace(/[\(\)\.\-\ \+]/g, ''); 
if (fld.value == "") {  error = "You didn't enter State.\n";  fld.style.background = 'Yellow';} 
else if ((/[0-9=!@#$%\^&*_+~`|\(\)\-\?\/\<\>\,\;\:\\\"\[\]]/.test(fld.value))) { 
    error = "The State contains illegal characters.\n"; fld.style.background = 'Yellow';
 } 
return error;
}

function validateCountry(fld) {
var error = "";
var stripped = fld.value.replace(/[\(\)\.\-\ \+]/g, ''); 
if (fld.value == "") {  error = "You didn't enter Country.\n";  fld.style.background = 'Yellow';} 
else if ((/[0-9=!@#$%\^&*_+~`|\(\)\-\?\/\<\>\,\;\:\\\"\[\]]/.test(fld.value))) { 
    error = "The Country contains illegal characters.\n"; fld.style.background = 'Yellow';
 } 
return error;
}

function validateZip(fld) {
var error = "";
var stripped = fld.value.replace(/[\(\)\.\-\ \+]/g, ''); 
if (fld.value == "") {  error = "You didn't enter Zip/Postal.\n";  fld.style.background = 'Yellow';} 
else if ((/[=!@#$%\^&*_+~`|\(\)\?\/\<\>\,\;\:\\\"\[\]]/.test(fld.value))) { 
    error = "The Zip/Postal code contains illegal characters.\n"; fld.style.background = 'Yellow';
 }
else if(fld.value.length < 5 || fld.value.length>10){
    error = "TheZip/Postal code is in wrong length.\n"; fld.style.background = 'Yellow';
 }
 
return error;
}

function validateFName(fld) {
var error = "";
var stripped = fld.value.replace(/[\(\)\.\-\ \+]/g, ''); 
if (fld.value == "") {  error = "You didn't enter First Name.\n";  fld.style.background = 'Yellow';} 
else if ((/[0-9=!@#$%\^&*_+~`|\(\)\-\?\/\<\>\,\;\:\\\"\[\]]/.test(fld.value))) { 
    error = "The First Name contains illegal characters.\n"; fld.style.background = 'Yellow';
 } 
return error;
}

function validateLName(fld) {
var error = "";
var stripped = fld.value.replace(/[\(\)\.\-\ \+]/g, ''); 

if (fld.value == "") {  error = "You didn't enter Last Name.\n";  fld.style.background = 'Yellow';} 
else if ((/[0-9=!@#$%\^&*_+~`|\(\)\-\?\/\<\>\,\;\:\\\"\[\]]/.test(fld.value))) { 
    error = "The Last Name contains illegal characters.\n"; fld.style.background = 'Yellow';
 } 
return error;
}

function validateEmail(fld) {
var error="";
var tfld = trim(fld.value); // value of field with whitespace trimmed off
var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
var illegalChars= /[`~#$%\^&*\+=\s\(\)\<\>\,\;\:\\\"\[\]]/ ;

if (fld.value == "") {
fld.style.background = 'Yellow';
error = "You didn't enter an 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 if (fld.value.match(illegalChars)) {
fld.style.background = 'Yellow';
error = "The email address contains illegal characters.\n";
} 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.\n";  fld.style.background = 'Yellow';} 
else if (isNaN(parseInt(stripped))) { 
    error = "The phone number contains illegal characters.\n"; fld.style.background = 'Yellow';
 } 

else if ((/[a-zA-Z=!@#$%\^&*_+~`|\?\/\<\>\,\;\:\\\"\[\]]/.test(fld.value))) { 
    error = "The phone number contains letters.\n"; fld.style.background = 'Yellow';
 } 

else if (stripped.length < 10 ||  stripped.length >= 15) { error = "The phone number is the wrong length. Make sure you included an area code.\n";
         fld.style.background = 'Yellow';
  } 
return error;
}

function validatePassword(fld, fld2) {
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 != fld2.value) {
error = "The retyped password does not match the password. \n";
fld.style.background = 'Yellow';
}
else if ((fld.value.length < 6) || (fld.value.length > 20)) {
error = "The password is the wrong length. \n";
fld.style.background = 'Yellow';
} 
else if (illegalChars.test(fld.value)) {
error = "The password contains illegal characters.\n";
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";
fld.style.background = 'Yellow';
} 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.\n";
} else if ((fld.value.length < 6) || (fld.value.length > 20)) {
fld.style.background = 'Yellow'; 
error = "The username is the wrong length.\n";
} else if (illegalChars.test(fld.value)) {
fld.style.background = 'Yellow'; 
error = "The username contains illegal characters.\n";
} else {
fld.style.background = 'White';
} 
return error;
}

