/* vim: ts=4 sw=4 et smartindent syntax=javascript fdm=marker
 * $Id: /local/applause/WebUI/branches/production/js/applause.js 1106 2009-11-10T15:44:18.451461Z mats  $
 */
// addLoadEvent(func) {{{
function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            if (oldonload) {
                oldonload();
            }
            func();
        }
    }
} // }}}
// filterInput(fieldid, type) {{{
/* filters text input into a form field at typing time to prevent invalid
 * data being entered.
 */
function filterInput(fieldid, type) {
    // attributes {{{
    this.field      = null;
    this.validchars = null;
    this.debug      = true;
    // }}}

    // keypress(ev) {{{
    this.keypress = function(ev) {
        var e;
        var key;
        var keychar;

        if (window.event) {
            e = window.event;
        } else if (ev) {
            e = ev;
        } else {
            if (this.debug) {
                alert("Can't get key code!");
            }
            return true;
        }
        key = e.charCode || e.keyCode;

        // control keys
        if ((key==null) || (key==0) || (key==8) || 
            (key==9) || (key==13) || (key==27)) {
           return key;
        }

        keychar = String.fromCharCode(key);
        if (this.debug) {
            alert("Testing char '" + keychar +
                  "' against:\n" + this.validchars);
        }

        if (this.validchars.indexOf(keychar) <= -1) {
            // DOM Level 2
            e.preventDefault();
            return false;
        } else {
            return true;
        }
    } // }}}
    // getHandler(func, arg) {{{
    
    /* wrap an object method in a closure
     * so we can use it as an event handler
     * without breaking the "this" reference.
     */
    this.getHandler = function(func) {
        var t = this;
        return function(event) { t[func](event); }
    } // }}}
    // init(fieldid, type) {{{
    this.init = function(fieldid, type) {
        field = document.getElementById(fieldid);
        if (field == null) {
            alert("Can't find input field '" + fieldid + "' !");
            return false;
        }

        this.validchars = "";
        switch (type) {
            case "ufloat":
                this.validchars += ".";
            case "uint":
                this.validchars += "0123456789";
                break;
            case "email":
                this.validchars += "@.";
            case "alnum":
                this.validchars += "abcdefghijklmnopqrstuvwxyz";
                this.validchars += "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
                this.validchars += "0123456789";
                break;
            default:
                alert("Unknown type '" + type + "' !");
                return false;
        }

        if (this.debug) {
            alert("Field '" + fieldid + "' valid input:\n" +
                  this.validchars);
        }

        field.onkeypress = this.getHandler('keypress');
        return true;
    } // }}}

    return this.init(fieldid, type);
} // }}}

