
/**
 * @author chris
 */
// Array functies missend in IE / FF

// indexOf
if (!Array.prototype.indexOf) {
    Array.prototype.indexOf = function(elt /*, from*/){
        var len = this.length;
        
        var from = Number(arguments[1]) || 0;
        from = (from < 0) ? Math.ceil(from) : Math.floor(from);
        if (from < 0) 
            from += len;
        
        for (; from < len; from++) {
            if (from in this &&
            this[from] === elt) 
                return from;
        }
        return -1;
    };
}

//lastIndexOf
if (!Array.prototype.lastIndexOf) {
    Array.prototype.lastIndexOf = function(elt /*, from*/){
        var len = this.length;
        
        var from = Number(arguments[1]);
        if (isNaN(from)) {
            from = len - 1;
        }
        else {
            from = (from < 0) ? Math.ceil(from) : Math.floor(from);
            if (from < 0) 
                from += len;
            else 
                if (from >= len) 
                    from = len - 1;
        }
        
        for (; from > -1; from--) {
            if (from in this &&
            this[from] === elt) 
                return from;
        }
        return -1;
    };
}

// map
if (!Array.prototype.map) {
    Array.prototype.map = function(fun /*, thisp*/){
        var len = this.length;
        if (typeof fun != "function") 
            throw new TypeError();
        
        var res = new Array(len);
        var thisp = arguments[1];
        for (var i = 0; i < len; i++) {
            if (i in this) 
                res[i] = fun.call(thisp, this[i], i, this);
        }
        
        return res;
    };
}

//find
Array.prototype.find = function(searchStr){
    var returnArray = false;
    for (i = 0; i < this.length; i++) {
        if (typeof(searchStr) == 'function') {
            if (searchStr.test(this[i])) {
                if (!returnArray) {
                    returnArray = []
                }
                returnArray.push(i);
            }
        }
        else {
            if (this[i] === searchStr) {
                if (!returnArray) {
                    returnArray = []
                }
                returnArray.push(i);
            }
        }
    }
    return returnArray;
}

function WireAutoTab(CurrentElementID, NextElementID, FieldLength) {
    //Get a reference to the two elements in the tab sequence.
    var CurrentElement = $('#' + CurrentElementID);
    var NextElement = $('#' + NextElementID);
 
    CurrentElement.keyup(function(e) {
        //Retrieve which key was pressed.
        var KeyID = (window.event) ? event.keyCode : e.keyCode;
 
        //If the user has filled the textbox to the given length and
        //the user just pressed a number or letter, then move the
        //cursor to the next element in the tab sequence.   
        if (CurrentElement.val().length >= FieldLength
            && ((KeyID >= 48 && KeyID <= 90) ||
            (KeyID >= 96 && KeyID <= 105)))
            NextElement.focus();
    });
}

