function validateFormOnSubmit(theForm,option) {
	var reason = "";

	if (option == 1)
	{
  		reason = validateEmpty(theForm.login, "Login");
  		reason += validateEmpty(theForm.first_name, "First Name");
  		reason += validateEmpty(theForm.last_name, "Last Name");
  		reason += validateEmpty(theForm.cust_password1, "Password");
  		reason += validateEmpty(theForm.cust_password2, "Password Confirmation");
  		reason += validateEmail(theForm.email);
  		reason += validateEmpty(theForm.address, "Address");
  		reason += validateEmpty(theForm.city, "city");
  		reason += validateEmpty(theForm.zip, "Zip Code");


	}

  	if (reason != "") {
    	alert("Some fields were entered incorrectly\n" + reason);
    	return false;
 	}

  	return true;
}

function validateEmpty(fld, label) {
    var error = "";

    if (fld.value.length == 0) {
        fld.style.background = 'Yellow';
        if (label != "") error = "Please enter the " + label + "\n";
        else error = "Please enter the field " + fld.name + " \n"
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validatePhone(fld) {
    var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');

   if (fld.value == "") {
        error = "Phone number is empty!!";
        fld.style.background = 'Yellow';
    } else if (isNaN(parseInt(stripped))) {
        error = "Phone number incorrect\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 = "Please enter the e-mail address!!!\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = 'Yellow';
        error = "Email Address is invalid!!!\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = 'Yellow';
        error = "Email address have incorrects characters\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}


