if (!Array.indexOf) {
    Array.prototype.indexOf = function (obj, start) {
        for (var i = (start || 0); i < this.length; i++) {
            if (this[i] == obj) {
                return i;
            }
        }
        return null;
    }
}

if (!String.pad) {
    String.prototype.pad = function(l, s, t){
        return s || (s = " "), (l -= this.length) > 0 ? (s = new Array(Math.ceil(l / s.length)
            + 1).join(s)).substr(0, t = !t ? l : t == 1 ? 0 : Math.ceil(l / 2))
        + this + s.substr(0, l - t) : this;
    };
}


function el(){
    var elements = new Array();
    for (var i = 0; i < arguments.length; i++) {
        var element = arguments[i];
        if (typeof element == 'string') element = document.getElementById(element);
        if (arguments.length == 1) return element;
        elements.push(element);
    }
    return elements;
}
function setHTML(id, html){
    el(id).innerHTML = html;
}

function setDisplay(id, display){
    el(id).style.display = display;
}

function showForm(id){
    setDisplay(id, "");
}

function hideForm(id){
    setDisplay(id, "none");
}

function focusThis(obj){
    el(obj).css('border','1px solid #6F8AA0'); //
    el(obj).css('background-color','#FFFFFF');
}

function unfocusThis(obj){
    el(obj).css('border','1px solid #CCCCCC');
    el(obj).css('background-color','#FFFFFF');
}

function setFriendlyUrlAction(frm, url, args){
    for (i=0; i<args.length ;i++){
        url += "/" + el(args[i]).value;
    }
    frm.action = url;
}

function function_exists( function_name ) {
    if (typeof function_name == 'string'){
        return (typeof window[function_name] == 'function');
    } else{
        return (function_name instanceof Function);
    }
}

function onLoad() {
    if (function_exists("init")){
        init();
    }
    if (function_exists("IEHoverPseudo")){
        IEHoverPseudo();
    }
	
}

function getKey(e){
    if(document.all){
        return(event.keyCode);
    }else{
        return(e.which);
    }
}

function only_numbers(e){
    var key = getKey(e);
    if(key > 57){
        return false;
    }
    return true;
}

function get_field_array(container_id){
    container_obj = document.getElementById(container_id);
    inputArray = container_obj.getElementsByTagName("input");
    var field_array = [];
    var index = 0;
    for (var i = 0; i < inputArray.length; i++){
        if (inputArray[i].type == 'text'){
            field_array[index] = inputArray[i].id;
            index += 1;
        }
    }
    return field_array;
}

function multi_field_pad(obj){
    if ((obj.value.length > 0) && (obj.value.length < obj.size)){
        obj.value = obj.value.pad(obj.size,'0');
    }
}

function multi_field_update(container_id){
    fields = get_field_array(container_id);
    var value = "";
    for (var i = 0; i < fields.length; i++){
        value += document.getElementById(fields[i]).value;
    }
    dest_obj = document.getElementById('field_'+container_id);
    dest_obj.value = value;
}

function multi_field_make(container_id){
    container_obj = document.getElementById(container_id);
    container_obj.innerHTML =
    container_obj.innerHTML
    + '<input type="hidden" id="field_'+container_id+'" name="'+container_id+'" value="" />';

    fields = get_field_array(container_id);

    for (var i = 0; i < fields.length; i++){
        document.getElementById(fields[i]).onkeyup = function(event){
            multi_field_proc(this, container_id, event);
            multi_field_update(container_id);
        };

        document.getElementById(fields[i]).onkeypress = function(event){
        //return only_numbers(event);
        };
        document.getElementById(fields[i]).onblur = function(){
            multi_field_pad(this);
        };
    }
}

function multi_field_proc(obj, container_id, e){
    id = obj.id;

    fields = get_field_array(container_id);
    index = fields.indexOf(id);

    prev_obj = document.getElementById(fields[index-1]); // captura o campo anterior
    next_obj = document.getElementById(fields[index+1]); // captura o campo seguinte

    var key = getKey(e);  // captura a tecla pressionada

    str = obj.value.replace(/\D/g,"");  // remove caracteres não numéricos
    len = str.length - obj.size; // calcula a quantidade de caracteres excedidos

    if((key == 8) && (str.length ==0) && (fields[index-1])){ // se a tecla for backspace
        prev_obj.focus(); // foca no campo anterior
        //prev_obj.select(); // seleciona todo o texto
        prev_obj.value = prev_obj.value; // posiciona o cursor no final do texto
    }
    if(key >= 48){
        if(obj.value != str){
            obj.value = str;
        }
        if(len >= 0){
            obj.value = str.substr(0, obj.size);  // elimina o texto excedido
            if(fields[index+1]){
                next_obj.focus(); // foca no campo seguinte
                if(len > 0){
                    next_obj.value = str.substr(obj.size, len); // copia o texto excedido do campo anterior
                }
                multi_field_proc(next_obj, container_id, e); // executa rotina no campo seguinte
            }
        }
    }
}
