function validateFormOnSubmit(theForm) {
var reason = "";
  
  reason += validatePostal(theForm.postal_code);
  reason += validateCity(theForm.city);
  reason += validateAddress(theForm.address);
  reason += validatePhone(theForm.phone);
  reason += validateEmail(theForm.email);
  reason += validatePassword(theForm.user_passwd);
  reason += validateUsername(theForm.user_name);
  reason += validateLastName(theForm.last_name);
  reason += validateFirstName(theForm.first_name);
  reason += validateProvince(theForm.province);
        
  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }
  return true;
}

function validatePostal(fld) {
    var error = "";
 
    if ((fld.value.length < 5) || (fld.value.length >7)) {
        fld.style.background = 'yellow';
		fld.focus();
        error = "Please check your postal code(zip code).\n"
    } else {
        fld.style.background = 'White';
    }
    return error;  
}

function validateFirstName(fld) {
    var error = "";
    var illegalChars = /[\W_]/;
 
    if (fld.value == "") {
        fld.style.background = 'yellow';
		fld.focus();
        error = "You didn't enter your first name.\n";
    } else if (fld.value.length < 2) {
        fld.style.background = 'yellow';
		fld.focus();
        error = "your first name is too short.\n";
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = 'yellow';
		fld.focus(); 
        error = "Please check your first name.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validateLastName(fld) {
    var error = "";
    var illegalChars = /[\W_]/;
 
    if (fld.value == "") {
        fld.style.background = 'yellow';
		fld.focus();
        error = "You didn't enter your last name.\n";
    } else if (fld.value.length < 2) {
        fld.style.background = 'yellow';
		fld.focus();
        error = "Your last name is too short.\n";
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = 'yellow';
		fld.focus(); 
        error = "Please check your last name.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validateUsername(fld) {
    var error = "";
    var illegalChars = /[\W_]/; // allow letters and numbers
 
    if (fld.value == "") {
        fld.style.background = 'yellow';
		fld.focus();
        error = "You didn't enter your username.\n";
    } else if ((fld.value.length < 4) || (fld.value.length > 14)) {
        fld.style.background = 'yellow';
		fld.focus();
        error = "The username should be more than 3 letters and less than 15 letters.\n";
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = 'yellow';
		fld.focus();
        error = "The username contains illegal characters. Only letters and numbers are allowed.\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';
		fld.focus();
        error = "You didn't enter your password.\n";
    } else if ((fld.value.length < 5) || (fld.value.length > 14)) {
        error = "The password should be more than 5 letters and less than 15 letters.";
        fld.style.background = 'yellow';
		fld.focus();
    } else if (illegalChars.test(fld.value)) {
        error = "The password contains illegal characters. Only letters and numbers are allowed.\n";
        fld.style.background = 'yellow';
		fld.focus();
      } 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';
		fld.focus();
        error = "You didn't enter an email address.\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = 'yellow';
		fld.focus();
        error = "Please enter a valid email address.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = 'yellow';
		fld.focus();
        error = "Please enter a valid email address.\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';
		fld.focus();
    } else if (isNaN(parseInt(stripped))) {
        error = "The phone number contains illegal characters.\n";
        fld.style.background = 'yellow';
		fld.focus();
    } else if ((stripped.length <10) || (stripped.length >20)) {
        error = "Please check your phone Number. Make sure you included an area code.\n";
        fld.style.background = 'yellow';
		fld.focus();
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validateAddress(fld) {
    var error = "";
     
    if (fld.value == "") {
        fld.style.background = 'yellow';
		fld.focus();
        error = "You didn't enter your address.\n";
    } else if (fld.value.length < 6) {
        error = "Please check your address. Your address is too short.\n";
        fld.style.background = 'yellow';
		fld.focus();
    }  else if (!((fld.value.search(/(a-z)+/)) && (fld.value.search(/(0-9)+/)))) {
        error = "Please check your address. Make sure your house number are included.\n";
        fld.style.background = 'yellow';
		fld.focus();
    } else {
        fld.style.background = 'White';
    }
   return error;
} 

function validateCity(fld) {
    var error = "";
    var illegalChars = /[\W_]/; // allow only letters and numbers 
 
    if (fld.value == "") {
        fld.style.background = 'yellow';
		fld.focus();
        error = "Please enter your city.\n";
    } else if ((fld.value.length < 4) || (fld.value.length > 15)) {
        error = "Please check your city.\n";
        fld.style.background = 'yellow';
		fld.focus();
    } else {
        fld.style.background = 'White';
    }
   return error;
}
function validateProvince(fld) {
    var error = "";
    if (fld.value == "") {
        fld.style.background = 'yellow';
		fld.focus();
        error = "Please select a province or state.\n";
    } else {
        fld.style.background = 'White';
    }
   return error;
}
