/**
 *  New England Nurses, Inc.
 *  Copyright (c) 2011 All rights reserved
 *  js/validation.js
 */

// Set the filters needed
var filterEmail = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
var filterSpecial = /[(\*\(\)\[\]\+\/\?\@\!\_\|\:\;\"\`\~\\#\$\%\^\&\<\>)+]/;
//var filterPostal = /^([0-9]{5})(?:[-\s]*([0-9]{4}))?$/;
var filterPostal = /^[0-9]+$/;
var filterNumbers = /^[0-9]+$/;
var filterPhone = /^[0-9-()]+$/;

var FORMVALIDATION = {
    textinput : function (entry) {
        // Get the value from the field
        var inputvalue = $("#" + entry).val();
        // Validate that it isn't blank
        if (!inputvalue) {
            FORMMESSAGING.failed(entry, "This field is required!");
        } else {
            // The field was filled in so check against invalid characters
            if (filterSpecial.test(inputvalue)) {
                FORMMESSAGING.failed(entry, "No special characters please!");
            } else {
                FORMMESSAGING.success(entry);
            }
        }
    },
    postalcode : function (entry) {
        // Get the value from the field
        var inputvalue = $("#" + entry).val();
        // Validate that it isn't blank
        if (!inputvalue) {
            FORMMESSAGING.failed(entry, "This field is required!");
        } else {
            // The field was filled in so check against proper format
            if (!filterPostal.test(inputvalue)) {
                FORMMESSAGING.failed(entry, "Numbers only in the zip code please!");
            } else {
                FORMMESSAGING.success(entry);
            }
        }
    },
    phonenumberrequired : function (entry) {
        // Get the value from the field
        var inputvalue = $("#" + entry).val();
        // Validate that it isn't blank
        if (!inputvalue) {
            FORMMESSAGING.failed(entry, "This field is required!");
        } else {
            // The field was filled in so check against proper format
            if (!filterPhone.test(inputvalue)) {
                FORMMESSAGING.failed(entry, "Please format properly (###-###-####)!");
            } else {
                FORMMESSAGING.success(entry);
            }
        }
    },
    phonenumber : function (entry) {
        // Get the value from the field
        var inputvalue = $("#" + entry).val();
        // Only test if they have entered text
        if (inputvalue) {
            // Validate that it is a proper phone format
            if (!filterPhone.test(inputvalue)) {
                FORMMESSAGING.failed(entry, "Please format properly (###-###-####)!");
            } else {
                FORMMESSAGING.success(entry);
            }
        } else {
            FORMMESSAGING.success(entry);
        }
    },
    emailaddress : function (entry) {
        // Get the value from the field
        var inputvalue = $("#" + entry).val();
        // Validate that it isn't blank
        if (!inputvalue) {
            FORMMESSAGING.failed(entry, "Please enter your email address!");
        } else {
            // The field was filled in so check against proper format
            if (!filterEmail.test(inputvalue)) {
                FORMMESSAGING.failed(entry, "Please format properly (name@url.com)!");
            } else {
                FORMMESSAGING.success(entry);
            }
        }
    },
    nospecial : function (entry) {
        // Get the value from the field
        var inputvalue = $("#" + entry).val();
        // Only test if they have entered text
        if (inputvalue) {
            // Check the filter for special characters
            if (filterSpecial.test(inputvalue)) {
                FORMMESSAGING.failed(entry, "No special characters please!");
            } else {
                FORMMESSAGING.success(entry);
            }
        } else {
            FORMMESSAGING.success(entry);
        }
    },
    recaptcha : function (entry) {
        // Get the value from the field
        var inputvalue = $("#" + entry).val();
        // Check that they filled in the recaptcha
        if (!inputvalue) {
            FORMMESSAGING.failed(entry, "Please enter the reCAPTCHA!");
        } else {
            FORMMESSAGING.success(entry);
        }
    },
    numbersonly : function (entry) {
        // Get the value from the field
        var inputvalue = $("#" + entry).val();
        // Only test if they have entered text
        if (inputvalue) {
            // The field was filled in so check against proper format
            if (!filterNumbers.test(inputvalue)) {
                FORMMESSAGING.failed(entry, "Numbers only please!");
            } else {
                FORMMESSAGING.success(entry);
            }
        } else {
            FORMMESSAGING.success(entry);
        }
    }
};

var FORMMESSAGING = {
    success : function (entry) {
        // Remove the errors classes as it now validates
        $("#" + entry).removeClass("formInputError");
        $("#error_" + entry).html("");
        $("#error_" + entry).removeClass("formDisplayError");
    },
    failed : function (entry, message) {
        // Add error classes as it failed validation
        $("#" + entry).addClass("formInputError");
        $("#error_" + entry).html(message);
        $("#error_" + entry).addClass("formDisplayError");
        foundError = true;
    }
};
