﻿//-----------------------------------------------------------------------------
// Validation.js
//
// General purpose form submission validation routines


//-----------------------------------------------------------------------------
// constants
var WARNING_IMAGE = "url(images/fieldform-yellow.png)";
var WARNING_IMAGE_SMALL = "url(images/zipbox-yellow.png)";
var WARNING_IMAGE_ADDCABLEINFO = "url(images/adinfo-yellow.png)";
var DEFAULT_IMAGE = "url(images/fieldform.png)";
var DEFAULT_IMAGE_SMALL = "url(images/zipbox.png)";
var DEFAULT_IMAGE_ADDCABLEINFO = "url(images/adinfo.png)";


//-----------------------------------------------------------------------------
// validate submitted form
function ValidateSubmit(f) {
    // the error message - as array so that we may pass by value
    var msg = new Array();
    msg[0] = getBaseErrorMsg();
    // assume no errors until we find one
    var error = false;

    // reset background images
    f.contactnamefield.style.backgroundImage = DEFAULT_IMAGE
    f.companynamefield.style.backgroundImage = DEFAULT_IMAGE
    f.emailfield.style.backgroundImage = DEFAULT_IMAGE
    f.phonenumberfield.style.backgroundImage = DEFAULT_IMAGE
    f.zipfield.style.backgroundImage = DEFAULT_IMAGE_SMALL


    // has a Contact Name been provided?
    if (isEmpty(f.contactnamefield.value)) {
        msg[0] = appendError(msg[0], "Contact Name must be entered.");
        f.contactnamefield.style.backgroundImage = WARNING_IMAGE
        error = true;
    }
    
    // has a Company Name been provided?
    if (isEmpty(f.companynamefield.value)) {
        msg[0] = appendError(msg[0], "Company Name must be entered.");
        f.companynamefield.style.backgroundImage = WARNING_IMAGE
        error = true;
    }

    // has an Email been provided?
    if (isEmpty(f.emailfield.value)) {
        msg[0] = appendError(msg[0], "Email must be entered.");
        f.emailfield.style.backgroundImage = WARNING_IMAGE
        error = true;
    }
    
    // has a Phone Number been provided?
    if (isEmpty(f.phonenumberfield.value)) {
        msg[0] = appendError(msg[0], "Phone Number must be entered.");
        f.phonenumberfield.style.backgroundImage = WARNING_IMAGE
        error = true;
    }

    // Is ZIP 5 digits and all numeric?
    if (!isEmpty(f.zipfield.value) && (f.zipfield.value.length != 5 || isNaN(f.zipfield.value))) {
        msg[0] = appendError(msg[0], "Installation ZIP must be 5 digits.");
        f.zipfield.style.backgroundImage = WARNING_IMAGE_SMALL
        error = true;
    }



    // how'd we do? 
    if (error == true) {
        // not well
        alert(msg);
        return false;
    }
    // looks good
    return true;
}





//-----------------------------------------------------------------------------
// getBaseErrorMsg()
//
// Get the starting error message
//
function getBaseErrorMsg() {
    var msg = "";
    msg += "_______________________________________________________________\n\n";
    msg += "The form was not submitted because of the following error(s).\n";
    msg += "Please correct these error(s) and re-submit.\n";
    msg += "_______________________________________________________________\n\n";

    return msg;
}


//-----------------------------------------------------------------------------
// appendError()
//
// Append error messages destined for dialog box using this function
//
function appendError(msg, strErr) {
    return msg + "* " + strErr + "\n";

} // end of appendError()


//-----------------------------------------------------------------------------
// isEmpty()
//
function isEmpty(str) {
    // is it empty?
    if (str == null || str == "")
        return true;
    else
        return false;

} // end of isValidEmailAddress()


//-----------------------------------------------------------------------------
// isValidEmailAddress()
//
function isValidEmailAddress(strAdr) {
    // this is magic
    var regexp = /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/;
    if (strAdr.match(regexp))
        return true;
    else
        return false;

} // end of isValidEmailAddress()


//-----------------------------------------------------------------------------
// isNumeric()
//
function isNumeric(s) {
    var i;
    for (i = 0; i < s.length; i++) {
        // ensure each character is a digit
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    return true;

} // end of isNumeric()


//-----------------------------------------------------------------------------
// hasLength()
//
// Returns true if the string passed is exactly as long as the length passed
//   in. Useful for zip codes and phone numbers.
//
function hasLength(str, len) {
    return str.length == len

} // end of hasLength()



//-----------------------------------------------------------------------------
// isValidPhoneHard()
//
// Strict NNN-NNN-NNNN style phone number validation:
//   - no field can be empty
//   - each field must contain digits
//   - each field must contain the proper number of digits
//
// Arguments: 
//   msg - a reference to an array containing one string, the dialog error 
//   pp1 - the form element representing the first triplet of the number
//   pp2 - the form element representing the second triplet of the number
//   pp3 - the form element representing the last quadruplet of the number
//
// Returns: 
//   boolean - true if the number is valid, else false
// 
function isValidPhoneHard(msg, pp1, pp2, pp3) {
    if (isEmpty(pp1.value) || isEmpty(pp2.value) || isEmpty(pp3.value)) {
        pp1.value = "";
        pp2.value = "";
        pp3.value = "";
        pp1.style.backgroundColor = WARNING_COLOR;
        pp2.style.backgroundColor = WARNING_COLOR;
        pp3.style.backgroundColor = WARNING_COLOR;
        msg[0] = appendError(msg[0], "A valid phone number, with area code, must be provided");
        return false;
    }
    else if (!isNumeric(pp1.value) || !isNumeric(pp2.value) || !isNumeric(pp3.value)) {
        pp1.value = "";
        pp2.value = "";
        pp3.value = "";
        pp1.style.backgroundColor = WARNING_COLOR;
        pp2.style.backgroundColor = WARNING_COLOR;
        pp3.style.backgroundColor = WARNING_COLOR;
        msg[0] = appendError(msg[0], "A phone number may only contain digits");
        return false;
    }
    else if (!hasLength(pp1.value, 3) || !hasLength(pp2.value, 3) || !hasLength(pp3.value, 4)) {
        pp1.value = "";
        pp2.value = "";
        pp3.value = "";
        pp1.style.backgroundColor = WARNING_COLOR;
        pp2.style.backgroundColor = WARNING_COLOR;
        pp3.style.backgroundColor = WARNING_COLOR;
        msg[0] = appendError(msg[0], "A phone number must be of the form NNN-NNN-NNNN");
        return false;
    }

    // looks ok
    return true;

} // end of isValidPhoneHard()

//-----------------------------------------------------------------------------
// isValidPhoneSoft()
//
// Lenient NNN-NNN-NNNN style phone number validation:
//   - all fields can be empty
//   - if not all empty, each field must contain digits
//   - if not all empty, each field must contain the proper number of digits
//
// Arguments: 
//   msg - a reference to an array containing one string, the dialog error 
//   pp1 - the form element representing the first triplet of the number
//   pp2 - the form element representing the second triplet of the number
//   pp3 - the form element representing the last quadruplet of the number
//
// Returns: 
//   boolean - true if the number is valid, else false
// 
function isValidPhoneSoft(msg, pp1, pp2, pp3) {
    if (!isEmpty(pp1.value) || !isEmpty(pp2.value) || !isEmpty(pp3.value)) {
        if (!isNumeric(pp1.value) || !isNumeric(pp2.value) || !isNumeric(pp3.value)) {
            pp1.value = "";
            pp2.value = "";
            pp3.value = "";
            pp1.style.backgroundColor = WARNING_COLOR;
            pp2.style.backgroundColor = WARNING_COLOR;
            pp3.style.backgroundColor = WARNING_COLOR;
            msg[0] = appendError(msg[0], "A phone number may only contain digits");
            return false;
        }
        else if (!hasLength(pp1.value, 3) || !hasLength(pp2.value, 3) || !hasLength(pp3.value, 4)) {
            pp1.value = "";
            pp2.value = "";
            pp3.value = "";
            pp1.style.backgroundColor = WARNING_COLOR;
            pp2.style.backgroundColor = WARNING_COLOR;
            pp3.style.backgroundColor = WARNING_COLOR;
            msg[0] = appendError(msg[0], "A phone number must be of the form NNN-NNN-NNNN");
            return false;
        }
    }

    // looks ok
    return true;

} // end of isValidPhoneSoft()


//-----------------------------------------------------------------------------
// isValidZipHard()
//
// Lenient NNNNN-NNNN style zip code number validation:
//   - first field (quintet) must be filled
//   - if second field is filled (quartet), then first must be filled
//   - only digits are allowed
//   - each field must contain the proper number of digits
//
// Arguments: 
//   msg - a reference to an array containing one string, the dialog error 
//   zip1 - the form element representing the first triplet of the number
//   zip2 - the form element representing the second triplet of the number
//
// Returns: 
//   boolean - true if the number is valid, else false
// 
function isValidZipHard(msg, zip1, zip2) {
    // first field must be filled
    if (isEmpty(zip1.value)) {
        zip1.value = "";
        zip2.value = "";
        zip1.style.backgroundColor = WARNING_COLOR;
        zip2.style.backgroundColor = WARNING_COLOR;
        msg[0] = appendError(msg[0], "A zip code must be provided");
        return false;
    }
    else {
        if (!isNumeric(zip1.value) || !hasLength(zip1.value, 5)) {
            zip1.value = "";
            zip2.value = "";
            zip1.style.backgroundColor = WARNING_COLOR;
            zip2.style.backgroundColor = WARNING_COLOR;
            msg[0] = appendError(msg[0], "The zipcode must be five digits long");
            return false;
        }

        if (!isEmpty(zip2.value)) {
            if (!isNumeric(zip2.value) || !hasLength(zip2.value, 4)) {
                zip1.value = "";
                zip2.value = "";
                zip1.style.backgroundColor = WARNING_COLOR;
                zip2.style.backgroundColor = WARNING_COLOR;
                msg[0] = appendError(msg[0], "The zipcode suffix must be four digits long");
                return false;
            }
        }
    }

    // must be ok
    return true;

} // end of isValidZipHard()


//-----------------------------------------------------------------------------
// getRadioValue()
//
function getRadioValue(r) {
    var rad_val = null;
    
    for (var i = 0; i < r.length; i++) {
        if (r[i].checked) {
            rad_val = r[i].value;
        }
    }
    
    return rad_val;
}

