
///////////////////////////////////////////////////////////////////////
// an extremely basic data qualification library
///////////////////////////////////////////////////////////////////////

var BROWSER_IE = (document.all) ? true : false;
var BROWSER_NET = (document.layers) ? true : false;
var BROWSER_NET_SIX =
	((document.getElementById) && (! BROWSER_IE)) ? true : false;
var BROWSER_VER_4_PLUS =
	(BROWSER_NET_SIX || BROWSER_NET || BROWSER_IE) ? true : false;

//////////////////////////////////////////////////
// override these on the calling page
//////////////////////////////////////////////////

// required text fields
var REQUIRED_ALL_FIELDS = Array();

// required email fields
var REQUIRED_EMAIL_FIELDS = Array();

// radio fields
var REQUIRED_RADIO_FIELDS = Array();

// checkbox group
var CHECKBOX_GROUP = Array();
var CHECKBOX_ALERT = "";

//////////////////////////////////////////////////
// should not need to edit below this point
//////////////////////////////////////////////////

// data qualification reg exs
var RE_INT = /^\d*$|^[\-]\d*$/;
var RE_PHONE = /^\(?\d{3}[\-\)\.\s]?\s?\d{3}[\-\)\.\s]?\d{4}$/;
var RE_STR = /[a-zA-Z]/;
var RE_ALL = /[a-zA-Z0-9]/;
var RE_EMAIL = /^([\da-zA-Z])\S+(\@)+[\da-zA-Z+]\S+(\.[a-zA-Z]{2,4}$)/;

// adds/removes the default field value for the specified field
function toggleFieldDefValue (field, defVal) {
    if (field.value == defVal) {
        field.value = "";
    } else if (field.value == "") {
        field.value = defVal;
    } 
}

// checks the reg form upon submission
function checkForm (button) {

    var rhett = true;
    
    // disable form buttons
    this.disableFormButtons(button, 60000);

    // required fields (re-assign rhett each time)
    rhett = (rhett) ? this.checkReqFieldGroup(REQUIRED_ALL_FIELDS, RE_ALL, "") : rhett;
    rhett = (rhett) ? this.checkReqFieldGroup(REQUIRED_EMAIL_FIELDS, RE_EMAIL, "user@domain.com") : rhett;
    rhett = (rhett) ? this.checkReqRadioFieldGroup(REQUIRED_RADIO_FIELDS, "") : rhett;

    // checkbox grouping
    rhett = ((CHECKBOX_GROUP.length > 0) && (rhett)) ? this.checkFieldGrouping(CHECKBOX_GROUP, CHECKBOX_ALERT) : rhett;

    return rhett;
}

// check the passed array of fields against the passed regex
function checkReqFieldGroup (formFields, regEx, defFormat) {


    for (var i=0; i < formFields.length; i++) {
        var field = formFields[i][0];
        var msgStr = formFields[i][1];
        var defVal = formFields[i][2];
        var elVal = (document.bodyForm.elements[field].type == "text") ? document.bodyForm.elements[field].value : document.bodyForm.elements[field][document.bodyForm.elements[field].selectedIndex].value;

        if ((elVal.length == 0) || (! regEx.test(elVal)) || (elVal == defVal)) {
            // bad format
            var msg = (defFormat == "") ?
                "You did not provide a value for the " + msgStr + " field.\n\nThis is a required field.\n" :
                "You did not provide a value for the " + msgStr + " field or it is in the incorrect format.\n\nPlease use a '" + defFormat + "' format.\n";

            // let them know
            return this.processBadElement(field, "text", msg);
        }
    }
    
    // all good
    return true;
}


// check the passed array of fields against the passed regex
function checkReqRadioFieldGroup (formFields, defFormat) {
    
    for (var i=0; i < formFields.length; i++) {
        var rhett = false;
        var field = formFields[i][0];
        var msgStr = formFields[i][1];
        var defVal = formFields[i][2];

        for (var f=0; f < document.bodyForm.elements[field].length; f++) {
            if (document.bodyForm.elements[field][f].checked) {
                rhett = true;
                break;
            }
        }
        if (! rhett) {
            var msg = (defFormat == "") ?
                "You did not provide a value for the " + msgStr + " field.\n\nThis is a required field.\n" :
                "You did not provide a value for the " + msgStr + " field or it is in the incorrect format.\n\nPlease use a '" + defFormat + "' format.\n";
            // let them know
            return this.processBadElement(field, "radio", msg);
        }
    }

    // all good
    return true;

}

// check the passed array of fields has at least one with a value
function checkReqFieldGroupMatchOne (formFields, regEx, defFormat, msg) {

    var matchCount = formFields.length;
    
    for (var i=0; i < formFields.length; i++) {
        var field = formFields[i][0];
        var msgStr = formFields[i][1];
        var defVal = formFields[i][2];	
        if ((document.bodyForm.elements[field].value.length == 0) || (! regEx.test(document.bodyForm.elements[field].value)) || (document.bodyForm.elements[field].value == defVal)) {
            // bad format
            matchCount--;
        }
    }
    
    // all good
    return (matchCount > 0) ? true : this.processBadElement(field, "radio", msg);
}

// check the passed group of checkboxes for at least one being checked
function checkFieldGrouping (checkGroup, msg) {

    var rhett = 0;
    var msg = (msg == "") ? "One or more of the checkboxes must be selected" : msg;
    
    for (var i=0; i < checkGroup.length; i++) {
        if (document.bodyForm.elements[checkGroup[i]].checked) {
            rhett++;
        }
    }

    if (rhett == 0) {
        // MAY EXIT THIS BLOCK
        return this.processBadElement(null, "", msg);
    }
    
    // all good
    return true;
}

// bad/missing data alert
function processBadElement (el, type, msg) {

    // let them know...
    alert(msg);

    // re-enable buttons
    enableFormButtons();

    // focus on the field
    if ((el != null) && (type == "radio")) {
        document.bodyForm.elements[el][0].focus();
    } else if (el != null) {
        document.bodyForm.elements[el].focus();
    }

    // return bad
    return false;
}

// disables all form buttons for a specified amount of times, starting 
// with the passed one (helps ensure that a double click is stopped)
function disableFormButtons (button, time) {

    if ((BROWSER_NET) || (BROWSER_NET_SIX)) {
        window.captureEvents(Event.CLICK | Event.MOUSEDOWN | Event.MOUSEUP);
    } else {
        // do this one right away
        if (button != "null") {
            button.disabled = true;
        }

        if (document.bodyForm.elements != null) {
            for (var i=0; i < document.bodyForm.elements.length; i++) {
                var el = document.bodyForm.elements[i];
                if ((el.type == "submit") || (el.type == "reset")
                        || (el.type == "button")) {
                    el.disabled = true;
                }
            }
        }
    }
    
    // see 'ugh' below
    window.setTimeout('enableFormButtons()', time);
}

// netscape can't pass arguments in setTimeout() calls, so we must
//        create an enable method to call - ugh...
function enableFormButtons () {

    if ((BROWSER_NET) || (BROWSER_NET_SIX)) {
        window.releaseEvents(Event.CLICK | Event.MOUSEDOWN | Event.MOUSEUP);
    } else {
           for (var i=0; i < document.bodyForm.elements.length; i++) {
            var el = document.bodyForm.elements[i];
            if ((el.type == "submit") || (el.type == "reset")
                        || (el.type == "button")) {
                el.disabled = false;
            }
        }
    }
}

