function esNulo(textbox,nombre)
{
    var texto = textbox.value;
    texto = trim(texto);
    if (texto == "")
    {
        alert("Escriba un valor para el campo " + nombre);
        textbox.focus();
        return (true);
    }
    return false;
}

function esSeleccion(combobox,nombre)
{
    if (combobox[combobox.selectedIndex].value == "")
    {
        alert("Debe seleccionar algun valor en el campo " + nombre);
        combobox.focus();
        return (false);
    }
    return true;
}

function esNumero(textbox,nombre)
{
    var checkOK = "0123456789-";
    var checkStr = textbox.value;
    var allValid = true;
    var allNum = "";
    for (i = 0;  i < checkStr.length;  i++)
    {
        ch = checkStr.charAt(i);
        for (j = 0;  j < checkOK.length;  j++)
            if (ch == checkOK.charAt(j))
                break;
        if (j == checkOK.length)
        {
            allValid = false;
            break;
        }
        allNum += ch;
    }
    if (!allValid)
    {
        alert("Escriba caracteres numericos en el campo "+nombre);
        textbox.focus();
        return (false);
    }
    return true;
}

function esPorcentaje(textbox,nombre)
{
    var txt = textbox.value;
    if(txt=='') return true;
    if(isNaN(txt))
    {
        alert("Formato numerico Erroneo en el campo " + nombre);
        textbox.focus();
        return (false);
    }
    return true;
}


function imagenesC(control)
{
    var popup = window.open("../../imagenes?campo=" + control,"popupc2","scrollbars=yes,width=280,height=340,resizable=yes");
    var d = popup.document;
    d.close();
}

function imagenesCxB(control)
{
    var popup = window.open("../imagenes?botones=true&campo=" + control,"popupc2","scrollbars=yes,width=280,height=340,resizable=yes");
    var d = popup.document;
    d.close();
}
function limpiarImagen(texto)
{
    texto.value=""; 
    document.getElementById('imagen').innerHTML ="";
}
function enviaMultipleData(destino,Forma)
{
    var cadena = Forma.multidatacheck.value;
    if(cadena == '+'){
        alert("Debe hacer por lo menos una selección");
    }
    else {
        cadena = cadena.replace(/\+/g,"%2B");
        if(window.tuga_user_send_multidata){
            window.tuga_user_send_multidata(destino + cadena, Forma);
        } else {
            document.location.href=destino + cadena;
        }
    }
}
function ayuda(urlAyuda)
{
    var url = urlAyuda;
    if( url != "" )
    {
        var popupAyuda = window.open(urlAyuda,"popupAyuda","scrollbars=yes,width=250,height=200,resizable=yes");
    }
    else
    {
        alert("No hay ayuda disponible para esta página");
    }
}

function trim(texto)
{
    var posIni = 0;
    var posFin = texto.length - 1;

    if (texto == "")
    {
        return "";
    }

    for (i = 0;  i < texto.length;  i++)
    {
        if (texto.charAt(i) == ' ' && (i+1) < texto.length ) 
        {
            texto = texto.substring(i+1,texto.length);
        }
        else
        {
            break;
        }
    }

    for (i = texto.length - 1;  i > 0;  i--)
    {
        if (texto.charAt(i) == ' ') 
        {
            texto = texto.substring(0,i);
        }
        else
        {
            break;
        }
    }
    if (texto.charAt(0) == ' ') return "";
    return texto;
}

function limpiar()
{
    for ( i = 0; i < document.forms.length; i ++)
    {
        for ( j = 0; j < document.forms[i].length; j ++)
        {
            if (document.forms[i][j].type == "hidden" || document.forms[i][j].type == "text" || document.forms[i][j].type == "textarea")
            {
                document.forms[i][j].value = "";
            }
            if (document.forms[i][j].type == "select-one")
            {
                document.forms[i][j].selectedIndex = 0;
            }
        }
    }
}

function checkAll(field)
{
    if(field.length){
        for (i = 0; i < field.length; i++){
            field[i].checked = true ;
        }
    } else {
        field.checked = true ;
    }
}

function uncheckAll(field)
{
    if(field.length){
        for (i = 0; i < field.length; i++){
            field[i].checked = false ;
        }
    } else {
        field.checked = false ;
    }
}

function doCheck(control1, control2){
    if(control1.checked == true){
        control2.value = 't';
    } else if(control1.checked == false){
        control2.value = 'f';
    }
}

function testCheck(control1, control2){
    if(control1.checked == true){
        checkAll(control2);
    } else if(control1.checked == false){
        uncheckAll(control2);
    }
}

function ltrim(s) {
    return s.replace(/^\s+/, "");
}

function rtrim(s) {
    return s.replace(/\s+$/, "");
}

function xtrim(s) {
    return rtrim(ltrim(s));
}

function numberBox(e)
{
    if (e.which)
    {
        if(e.which!=8 && e.which!=9 && (e.which<48 || e.which>57))
            return false;
    }
    else if(e.keyCode)
    {
        if(e.keyCode!=8 && e.keyCode!=9 && (e.keyCode<48 || e.keyCode>57))
            return false;
    }
    return true;
}

function columnBox(e)
{
    if (e.which)
    {
        if(e.which!=8 && e.which!=9 && e.which!=44 && (e.which<48 || e.which>57))
            return false;
    }
    else if(e.keyCode)
    {
        if(e.keyCode!=8 && e.keyCode!=9 && e.keyCode!=44 && (e.keyCode<48 || e.keyCode>57))
            return false;
    }
    return true;
}

function variableBox(e)
{
    if (e.which)
    {
        if(e.which == 46 || e.which == 37 || e.which == 39 || e.which == 95 || e.which == 8 || e.which == 9 || (e.which>=97 && e.which<=122) || (e.which>=65 && e.which<=90) || (e.which>=48 && e.which<=57) )
            return true;
    }
    else if(e.keyCode)
    {
        if(e.keyCode == 46 || e.keyCode == 37 || e.keyCode == 39 || e.keyCode == 95 || e.keyCode == 8 || e.keyCode == 9 || (e.keyCode>=97 && e.keyCode<=122) || (e.keyCode>=65 && e.keyCode<=90) || (e.keyCode>=48 && e.keyCode<=57) )
            return true;
    }
    return false;
}

function jsBox(e)
{
    if (e.which)
    {
        if(e.which == 34)
            return false;
    }
    else if(e.keyCode)
    {
        if(e.keyCode ==34)
            return false;
    }
    return true;
}

function dateBox(e,control)
{
    if (e.which)
    {
        if(e.which != 9 && e.which != 13 && e.which != 8 && e.which != 46){
            return false;
        }
        else if(e.which == 8 || e.which == 46){
            control.value = '';
        }
    }
    else if(e.keyCode)
    {
        if(e.keyCode != 9 && e.keyCode != 13 && e.keyCode != 8 && e.keyCode != 46){
            return false;
        }
        else if(e.keyCode == 8 || e.keyCode == 46){
            control.value = '';
        }
    }
    return true;
}

function insertAtCursor(myField, myValue) {
    //IE support
    if (document.selection) {
        myField.focus();
        sel = document.selection.createRange();
        sel.text = myValue;
    }
    //MOZILLA/NETSCAPE support
    else if (myField.selectionStart || myField.selectionStart == '0') {
        var startPos = myField.selectionStart;
        var endPos = myField.selectionEnd;
        myField.value = myField.value.substring(0, startPos)
        + myValue
        + myField.value.substring(endPos, myField.value.length);
    } else {
        myField.value += myValue;
    }
}

function creaListaMultiple(variable, vector){
    var tam = 1;
    var i = 0;
    var cadena = "+";
    if(vector.length){
        tam = vector.length;
        for(i=0; i < tam; i++){
            if(vector[i].checked == true)
                cadena = cadena + vector[i].value + "+";
        }
    } else {
        cadena = cadena + vector.value + "+";
    }
    variable.value = cadena;
}

function esListaMultiple(textbox,nombre){
    var texto = textbox.value;
    if (texto == "+"){
        alert("Debe seleccionar valores en " + nombre);
        return (false);
    }
    return true;
}

function setMaxAncho(area,ancho){
    area.style.width = ancho + "px";
}

function eliminaDato(tip,codigo){
    var entrar = confirm("Desea enviar este elemento a la papelera?")
    if ( entrar ){
        new Ajax.Request("GenReport.eliminaItems", { 
            method: "post",
            parameters: {
                papelera: codigo,
                tipo: tip
            }
        });
    }
    setTimeout('document.location.reload()',1000);
}

function eliminaDatoP(tip,codigo){
    var entrar = confirm("Desea enviar este elemento de forma permanente?")
    if ( entrar ){
        new Ajax.Request("../eliminaPantallaReporte", { 
            method: "post",
            parameters: {
                codigotabla: codigo,
                tipo: tip
            }
        });
    }
    setTimeout('document.location.reload()',1000);
}

function recuperaDato(tip,codigo){
    new Ajax.Request("../eliminaPantallaReporte", { 
        method: "post",
        parameters: {
            codigotabla: codigo,
            tipo: tip
        }
    });
    setTimeout('document.location.reload()',1000);
}

function vaciaPapelera(tip){
    var entrar = confirm("Desea enviar vaciar la papelera?")
    if ( entrar ){
        new Ajax.Request("../eliminaPantallaReporte", { 
            method: "post",
            parameters: {
                codigotabla: "x",
                tipo: tip
            }
        });
    }
    setTimeout('document.location.reload()',1000);
}

function agregaOpcionMenu(dir){
    window.open("../menu/tree/addnode.jsp?url=" + dir,"agregarMenu",'scrollbars=yes,width=550,height=250,resizable=yes');
}

function esEmail(textbox) {
    var str = textbox.value;
    var at="@";
    var dot=".";
    var lat=str.indexOf(at);
    var lstr=str.length;
    var ldot=str.indexOf(dot);
    if (str.indexOf(at)==-1){
        return false;
    }
    if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
        return false;
    }
    if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
        return false;
    }
    if (str.indexOf(at,(lat+1))!=-1){
        return false;
    }
    if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
        return false;
    }
    if (str.indexOf(dot,(lat+2))==-1){
        return false;
    }		
    if (str.indexOf(" ")!=-1){
        return false;
    }
    return true;
}

function doMachine(form, codbutton){
    form.action = '../maquinaEstados';
    form._boton_presionado.value=codbutton;
    form.encoding="application/x-www-form-urlencoded";
    form.method="post";
    form.target = window.name;
    form.submit();
    return;
}