
///////////////////////////////////////////////////////////////////////
// Web-based Client Area
// Copyright (c) 2003 by Curtis Thompson
// http://www.sitruc.com
//
// This program is free software. You can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License.
//
// data_qualify.js
//
// a basic data qualification library for this system
//
// NOTE: this is a stripped down system, and it currently assumes
// that the client area will only need to qualify text fields for
// an existing value or an email
///////////////////////////////////////////////////////////////////////

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

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

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

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

// da form
var FORM_NAME = "document.bodyForm";
var FORM_OBJ;

// 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}$)/;

// get things started
function init () {
    FORM_OBJ = eval(FORM_NAME);
}
      
// 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;

    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 formstr = eval(FORM_NAME + "['" + field + "']");
	
        if ((formstr.value.length == 0) || (! regEx.test(formstr.value)) || (formstr.value == defVal)) {
            // bad format
            var msg = (defFormat == "") ?
                "You did not enter a " + msgStr + ".\n\nThis is a required field.\n" :
                "You did not enter a " + msgStr + " or it is in the incorrect format.\n\nPlease use a '" + defFormat + "' format.\n";

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

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

    // let them know...
    alert(msg);
    // re-enable buttons
    enableFormButtons();
    // focus on the field
    el.focus();
    if (el.type == "text") {
        el.select();
    }
    // 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 (FORM_OBJ != null) {
            for (var i=0; i < FORM_OBJ.elements.length; i++) {
                var el = FORM_OBJ.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 < FORM_OBJ.elements.length; i++) {
            var el = FORM_OBJ.elements[i];
            if ((el.type == "submit") || (el.type == "reset")
                        || (el.type == "button")) {
                el.disabled = false;
            }
        }
    }
}
