/**
 * @author bhochstetler
 *
 * Reusable object to validate forms
 *
 * Usage:
 * fv = new formValidator();
 * if (fv.isEmpty(document.forms[0].elements[0].value))
 * fv.raiseError("Please enter a value")
 *
 **/
//function that calls a new instance of formValidator to check fields
function checkForm(){
    //create instance of validator
    fv = new formValidator();
    
    //call variables to store field values
    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;
    
    //validate fields
    if (!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");
    }
    if (!fv.isEmail(email)) {
        fv.raiseError("Please enter a valid email address");
    }
	if(!fv.isPhone(phone)){
		fv.raiseError("Please enter a valid 10 digit phone number");
	}
    
    //display all errors with displayError or only one error with displayFirstError
    if (fv.numErrors() > 0) {
        fv.displayFirstError();
        return false;
    }
    else {
        return true;
    }
}



function formValidator(){
    //set up array to hold error messages
    this.errorList = new Array;
    
    //object methods
    
    //checks to see if value is empty
    this.isEmpty = isEmpty;
    //checks to see if value is a number
    this.isNumber = isNumber;
    //checks to see if value is within a range
    this.isWithinRange = isWithinRange;
    //checks a checkbox to see if it is checked
    this.isChecked = isChecked;
    //checks a value to see if it is alphabetic
    this.isAlphabetic = isAlphabetic;
    //checks a value to see if it is alphanumeric
    this.isAlphaNumeric = isAlphaNumeric;
    //checks a value to see if it is a valid email address
    this.isEmail = isEmail;
    //checks a value for proper length (val,min,max)
    this.isLength = isLength;
    //checks a value to see if it is a valid phone number
    this.isPhone = isPhone;
    //strips white space from a string
    this.wsStripped = wsStripped;
    //compiles errors into an array
    this.raiseError = raiseError;
    //counts the number of errors compiled by raiseError
    this.numErrors = numErrors;
    //displays the first error in the array
    this.displayFirstError = displayFirstError;
    //displays all errors in the array
    this.displayErrors = displayErrors;
}

function isEmpty(val){
    if (this.wsStripped(val).match(/^s+$/) || this.wsStripped(val) == "") {
        return true;
    }
    else {
        return false;
    }
}

function isNumber(val){
    if (isNAN(val)) {
        return false;
    }
    else {
        return true;
    }
}

function isWithinRange(val, min, max){
    if (val >= min && val <= max) {
        return true;
    }
    else {
        return false;
    }
}

function isChecked(obj){
    if (obj.checked) {
        return true;
    }
    else {
        return false;
    }
}

function isAlphabetic(val){
    if (val.match(/^[a-zA-z]+$/)) {
        return true;
    }
    else {
        return false;
    }
}

function isAlphaNumeric(val){
    if (val.match(/^[a-zA-Z0-9]+$/)) {
        return true;
    }
    else {
        return false;
    }
}

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

function isLength(val, min, max){
    if (val.length >= min && val.length <= max) {
        return true;
    }
    else {
        return false;
    }
}

function wsStripped(val){
    val = val.replace(/\s+/g, '');
    return val;
}

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

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

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]);
}
