function strictFields() {
    /* USAGE
    this function loops through every input box that has the numbersOnly class it makes the field only accept numbers from 0 - 9
    you can also use the maxLength attribute on the textbox to limit the char length allowed.
    */
    $(':input.numbersOnly').each(function () { //THIS FUNCTION ONLY ALLOWS NUMBERS FROM 0 to 9
        $(this).keyup(function () {
            var val = $(this).val();
            var regex = new RegExp("[^0-9]", "gi");
            var allowed = ($(this).attr("maxLength") != -1 ? $(this).attr("maxLength") : 255);
            val = val.replace(regex, '');
            val = val.substring(0, allowed);
            $(this).val(val);
        });
    });
    /* USAGE    
    this function is to group checkboxes like radio groups.
    the functions looks at every checkbox that has the class group 
    it then compares names so the functionality would be like you cant have to radio buttons with the same name
    advanced grouping was created so you can add the claass of group1 or group2. this is usefull if you want to make horizontal and vertical grouping of checkboxes.

    CLASS

    group || minimum requirement
    group1 || groups all of group1 so only one can be selected in that group
    group2 || groups all of group2 so only one can be selected in that group

    */
    $(':checkbox.group').each(function () { //GROUPS CHECKBOXES WITH THE SAME NAME SO THEY GET THE FUNCTIONALITY OF A RADIO GROUP. JUST ADD THE CLASS NAME group TO A CHECKBOX
        $(this).click(function () {
            if (!$(this).is(":checked")) {
                $(this).attr('checked', true);
                return;
            }
            var name = $(this).attr("name");
            var count = $('[name="' + name + '"]').length;
            if (count > 1) {
                $('[name="' + name + '"]').attr('checked', false);
                $(this).attr('checked', true);
            } else {
                $(this).attr('checked', true);
            }
            if ($(this).hasClass("group1")) {
                var count = $(':checkbox.group1').length;
                if (count > 1) {
                    $(':checkbox.group1').attr('checked', false);
                    $(this).attr('checked', true);
                } else {
                    $(this).attr('checked', true);
                }
            }
            if ($(this).hasClass("group2")) {
                var count = $(':checkbox.group2').length;
                if (count > 1) {
                    $(':checkbox.group2').attr('checked', false);
                    $(this).attr('checked', true);
                } else {
                    $(this).attr('checked', true);
                }
            }
        });
    });
};
function alerter(msg, id, event) {
    alert(msg);
    return false;
};
function check(func) {
    /*
    ATTRIBUTES ON ELEMENTS
    class='' || required
    minLength = '' || minimum string length works only on TEXT and TEXTAREA
    maxLength = '' || maximum string length works only on TEXT and TEXTAREA
    errorMessage='' || message on the element to alert to the user, if errorMessage is not defined it will report the name on the element

    CLASS ATTRIBUTES

    email || when email supplied as a class it returns false if it cannot be validated
    */
    var i = 1;
    var countedElements = $('.required').length;
    var halt = false;

    $('.required').each(function () {
        var tag = $(this).attr("tagName");
        var type = $(this).attr("type");
        var error = $(this).attr("errorMessage") != undefined ? $(this).attr("errorMessage") : $(this).attr("name") + (type == 'checkbox' || type == 'radio' ? " must be checked" : " must have a value");

        //alert("tag=" + tag + "\ntype=" + type);

        if ($(this).hasClass("email")) { //MATCH THAT IT HAS A VALID EMAIL ADDRESS
            var p = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
            if (!p.test($(this).val())) {
                halt = true;
                alerter(error, "errors");
                $(this).focus();
                return false;
            }
        }
        if (tag == 'SELECT') { //SELECT
            var firstIndex = $(this).attr("firstIndex");
            if ($(this).attr("selectedIndex") < firstIndex) {
                halt = true;
                alerter(error, "errors");
                $(this).focus();
                return false;
            }
        }
        if (tag == 'INPUT') {
            if (type == 'text') { //TEXTBOX
                var minLength = $(this).attr("minLength") ? $(this).attr("minLength") : "";
                var maxLength = $(this).attr("maxLength") ? $(this).attr("maxLength") : "";
                var strLength = $(this).val().length;
                if ((minLength != "" && strLength < minLength) || $(this).val() == "") {
                    halt = true;
                    alerter(error, "errors");
                    return false;
                }
            }
            if (type == 'checkbox' || type == 'radio') { //CHECKBOX AND RADIO BUTTONS
                /* USAGE
                if a checkbox or radio has the attribute requiredClass then it will look at the minimumRequired in a whole group. You can specify 2 is minimum out of 8 checkboxes for example.
                Attribute required class should be a class name specified on all the checkboxes/radios you want to group
                if requiredClass is not specified as an attribute then it will check if multiple checkboxes or radios has the same name. At least one of these must be checked. 
                */
                var minimumRequired = $(this).attr("minRequire") != -1 ? $(this).attr("minRequire") : 0;
                var requiredClass = $(this).attr("requiredClass") != -1 ? $(this).attr("requiredClass") : 0;
                if (requiredClass != 0) {
                    if (minimumRequired > 0) {
                        var checked = $('.' + requiredClass + ':checked').length;
                        if (checked < minimumRequired) {
                            halt = true;
                            alerter(error, "errors");
                            return false;
                        }
                    }
                } else {
                    var name = $(this).attr("name");
                    var count = $('[name="' + name + '"]').length;
                    if (count == 1 && !$(this).is(":checked")) { //ONLY ONE CHECKBOX FOUND WITH THIS NAME ASSUMES IT SHOULD BE CHECKED
                        halt = true;
                        alerter(error, "errors");
                        return false;
                    } else { //THERE IS MULTIPLE CHECKBOXES WITH THE SAME NAME
                        var checked = $('[name="' + name + '"]:checked').length;
                        if (checked == 0) {
                            halt = true;
                            alerter(error, "errors");
                            return false;
                        }
                    }
                }
            }
        }
        if (tag == 'TEXTAREA') {
            var minLength = $(this).attr("minLength") ? $(this).attr("minLength") : "";
            var maxLength = $(this).attr("maxLength") ? $(this).attr("maxLength") : "";
            var strLength = $(this).val().length;
            if ((minLength != "" && strLength < minLength) || (maxLength != "" && strLength > maxLength) || $(this).val() == "") {
                halt = true;
                alerter(error, "errors");
                return false;
            }
        }

        if (i == countedElements && !halt) { //FINISHED CHECKING ALL ELEMENTS GO TO NEXT QUESTION
            eval(func);
        }
        i++;
    });
};
function isEmail(email) {
    var p = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
    return p.test(email);
};
function filter(element, length, regex) {
    /*USAGE
    ACCEPT ONLY NUMBERS AND LENGTH IF 5
    onkeyup='filter(this,5,"[^0-9]");'
    */
    var id = element.id;
    var val = element.value;
    var regex = new RegExp(regex, "gi");
    var allowed = length;
    val = val.replace(regex, '');
    val = val.substring(0, allowed);
    document.getElementById(id).value = val;
};
