//documento navegador
String.prototype.trim = function() 
{
    return this.replace(/^\s+|\s+$/g,"");
}

String.prototype.ltrim = function() 
{
    return this.replace(/^\s+/g,"");
}

String.prototype.rtrim = function() 
{
    return this.replace(/\s+$/g,"");
} 

function gE(idElemento)
{
    return document.getElementById(idElemento);
}

function oE(idElemento)
{
    return gE(idElemento).style.display='none';
}

function mE(idElemento)
{
    return gE(idElemento).style.display='';
}

function hE(idElemento)
{
    gE(idElemento).disabled=false;
}

function dE(idElemento)
{
    gE(idElemento).disabled=true;
}

//cadenas
function trim(s)
{
    return rtrim(ltrim(s));
}

function ltrim(s)
{
    var l=0;
    while((l < s.length) && (s.charCodeAt(l) == 32))
    {   
        l++; 
    }
    return s.substring(l, s.length);
}

function rtrim(s)
{
    var r=s.length -1;
    while((r > 0) && (s.charCodeAt(r) == 32))
    {   
        r--;    
    }
    return s.substring(0, r+1);
}

//Validadores

function esListaNoVacia(obj,lista,msg,minItems)
{
    var res=obj.childNodes.length;
    if(res<minItems)
    {
        msgError(msg);
        marcarCampo(lista);
        return false;
    }
    desmarcarCampo(lista);
    return true;
}

function esNoVacio(obj)
{
    var res=trim(obj.value);
    if(res.length==0)
    {
        msgError("El campo es obligatorio");
        marcarCampo(obj);
        return false;
    }
    desmarcarCampo(obj);
    return true;
}
function esNumero(obj)
{
    entero=parseInt(obj.value);
    if(isNaN(entero))
    {
        msgError('El valor ingresado no es numérico');
        marcarCampo(obj);
        return false;
    }
    desmarcarCampo(obj);

    return true;
}

function esNumeroNoVacio(obj)
{
    if(esNoVacio(obj))
    {
        return esNumero(obj);
    }
}

//mensajes

//funciones para teclas presionadas

function esNumero(evt)
{
    var nav4 = window.Event ? true : false;
    var key = nav4 ? evt.which : evt.keyCode;
    return (key <= 13 || (key >= 48 && key <= 57));
}                          
//
function msgInformacion(mensaje)
{
    Ext.MessageBox.show(
                            {
                                title: 'Super Fasti',
                                msg: mensaje,
                                buttons: Ext.MessageBox.OK,
                                icon: Ext.MessageBox.INFO
                            }
                        );
}

function msgAlerta(mensaje)
{
    Ext.MessageBox.show(
                            {
                                title: 'Super Fasti',
                                msg: mensaje,
                                buttons: Ext.MessageBox.OK,
                                icon: Ext.MessageBox.WARNING
                            }
                        );
}

function msgError(mensaje)
{
    Ext.MessageBox.show(
                            {
                                title: 'Super Fasti',
                                msg: mensaje,
                                buttons: Ext.MessageBox.OK,
                                icon: Ext.MessageBox.ERROR
                            }
                        );
}

//formato a controles

function marcarCampo(obj)
{
    obj.style.background='#FFd2dC';
    obj.style.backgroundColor='#FFd2dC';
}

function desmarcarCampo(obj)
{
    obj.style.background='#FFFFFF';
    obj.style.backgroundColor='#FFFFFF';
}

//funciones de arbol

function limpiarNodo(nodo)
{
    
    while(nodo.hasChildNodes())
    {
        nodo.removeChild(nodo.item(0));
    }
}

function obtenerNodoSel(raiz)
{
    var z=0;
    var enc=false;
    var nodoSel=null;
    
    while((z<raiz.childNodes.length) &&(!enc))
    {
        enc=raiz.childNodes[z].isSelected();
        if(enc)
            nodoSel=raiz.childNodes[z];
        else
        {
            nodoSel=obtenerNodoSel(raiz.childNodes[z]);
            if(nodoSel!=null)
                enc=true;
            
        }
        z++;
    }
    return nodoSel;
}
//RevistaE
function rellenarValoresVacios(dSet,columna,valor)
{
    for(x=0;x<dSet.getCount();x++)
    {
        fila=dSet.getAt(x);
        if(trim(fila.get(columna))=='')
            fila.set(columna,valor);
    }
}

function rellenarValoresVaciosColumna(dSet,columna,columnaCopia)
{
    for(x=0;x<dSet.getCount();x++)
    {
        fila=dSet.getAt(x);
        if(trim(fila.get(columna))=='')
            fila.set(columna,'['+fila.get(columnaCopia)+']');
    }
}

function obtenerFilaIdioma(dSet,idIdioma)
{
    for(x=0;x<dSet.getCount();x++)
    {
        fila=dSet.getAt(x);
        if(fila.get('idIdioma')==idIdioma)
            return fila;
    }
}

function obtenerPosFila(dSet,columna,valor)
{
    for(x=0;x<dSet.getCount();x++)
    {
        fila=dSet.getAt(x);
        if(fila.get(columna)==valor)
            return x;
    }
    return -1;
}

function validar(dSet,columna,idIdioma)
{
    var fila;
    var nomDefault=false;
    var ct=0;
    for(x=0;x<dSet.getCount();x++)
    {
        fila=dSet.getAt(x);
        if(trim(fila.get(columna))!='')
        {
            if(fila.get('idIdioma')==idIdioma)
                nomDefault=true;
            ct++;
        }
    }
    if(dSet.getCount()==ct)
        return 0; //Sin problemas
    
    if(!nomDefault)
        return 1; //El nombre en idioma original no fue especificado
    
    return 2;
}

function validarCampoNoVacio(dSet,columna)
{
    for(x=0;x<dSet.getCount();x++)
    {
        fila=dSet.getAt(x);
        if(trim(fila.get(columna))=='')
            return x+1;
    }
    return -1;
}


function limpiarCombo(combo)
{
    while (combo.length > 0) 
        combo.remove(0);
}



