/*
* @author Brent Hochstetler
*/
function checkForm(){
    fv = new formValidator;
    lastName = document.getElementById("txtLastName").value;
    street1 = document.getElementById("txtStreet1").value;
    street2 = document.getElementById("txtStreet2").value;
    city = document.getElementById("txtCity").value;
    state = document.getElementById("txtState").value;
    zip = document.getElementById("txtZip").value;
    email = document.getElementById("txtEmail").value;
    phone = document.getElementById("txtPhone").value;
    fv.isLength(lastName, 1, 20) || fv.raiseError("Please enter a valid last name that is between 1 and 20 characters long");
    if (fv.isEmpty(fv.wsStripped(street1)) || fv.isEmpty(fv.wsStripped(city)) || fv.isEmpty(fv.wsStripped(state)) || fv.isEmpty(fv.wsStripped(zip))) 
        fv.raiseError("Please enter a valid street address");
    fv.isEmail(email) || fv.raiseError("Please enter a valid email address");
    fv.isPhone(phone) || fv.raiseError("Please enter a valid 10 digit phone number");
    if (fv.numErrors() > 0) {
        fv.displayFirstError();
        return false
    }
    else 
        return true
}

function formValidator(){
    this.errorList = [];
    this.isEmpty = isEmpty;
    this.isNumber = isNumber;
    this.isWithinRange = isWithinRange;
    this.isChecked = isChecked;
    this.isAlphabetic = isAlphabetic;
    this.isAlphaNumeric = isAlphaNumeric;
    this.isEmail = isEmail;
    this.isLength = isLength;
    this.isPhone = isPhone;
    this.wsStripped = wsStripped;
    this.raiseError = raiseError;
    this.numErrors = numErrors;
    this.displayFirstError = displayFirstError;
    this.displayErrors = displayErrors
}

function isEmpty(a){
    return this.wsStripped(a).match(/^s+$/) || this.wsStripped(a) == "" ? true : false
}

function isNumber(a){
    return isNAN(a) ? false : true
}

function isWithinRange(a, b, c){
    return a >= b && a <= c ? true : false
}

function isChecked(a){
    return a.checked ? true : false
}

function isAlphabetic(a){
    return a.match(/^[a-zA-z]+$/) ? true : false
}

function isAlphaNumeric(a){
    return a.match(/^[a-zA-Z0-9]+$/) ? true : false
}

function isEmail(a){
    return a.match(/^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/) ? true : false
}

function isLength(a, b, c){
    return a.length >= b && a.length <= c ? true : false
}

function wsStripped(a){
    return a = a.replace(/\s+/g, "")
}

function isPhone(a){
    if (a.match(/^[ ]*[(]{0,1}[ ]*[0-9]{3,3}[ ]*[)]{0,1}[-]{0,1}[ ]*[0-9]{3,3}[ ]*[-]{0,1}[ ]*[0-9]{4,4}[ ]*$/) == null && a.match(/^[ ]*[0-9]{3,3}[ ]*[-]{0,1}[ ]*[0-9]{4,4}[ ]*$/) == null) 
        return false;
    return true
}

function raiseError(a){
    this.errorList[this.errorList.length] = a
}

function numErrors(){
    return this.errorList.length
}

function displayErrors(){
    for (x = 0; x < this.errorList.length; x++) 
        alert("Error: " + this.errorList[x])
}

function displayFirstError(){
    alert("Error: " + this.errorList[0])
};
