//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Este software fué realizado por Ricardo Naranjo Faccini 
// para Skina IT Solutions E.U.
// 
// Esta librería podrá ser incluída dentro de los desarrollos
// realizados por Skina IT Solutions E.U. como complemento a
// software desarrollado para terceros sin perder los derechos
// de propiedad intelectual de esta empresa sobre esta librería.
// 
// Todos los derechos reservados.
// 
// PROHIBIDO SU USO, MODIFICACION O COPIA total o parcial
// sin autorización expresa y por escrito.
// 
// Skina IT Solutions E.U.
// Calle 95 No. 48-33 INT 8
// (+571) 257 98932
// info@skinait.com
// 
// Copyright 2008
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    function apagar_divs(prefijo) {
    //----------------------------------------------------------------------
    // PROPOSITO:
    // Oculta todas las divisiones con identificación similar al prefijo.
    // Es decir que pone display: none; como su estilo de despliegue;
    //----------------------------------------------------------------------
        divisiones = document.getElementsByTagName('div');
        for (i = 0; i < divisiones.length; i++)
            if (divisiones[i].id.substr(0, prefijo.length) == prefijo) {
                divisiones[i].style.display = 'none';
            }
    }

    function encender_divs(prefijo) {
    //----------------------------------------------------------------------
    // PROPOSITO:
    // Oculta todas las divisiones con identificación similar al prefijo.
    // Es decir que pone display: none; como su estilo de despliegue;
    //----------------------------------------------------------------------
        divisiones = document.getElementsByTagName('div');
        for (i = 0; i < divisiones.length; i++)
            if (divisiones[i].id.substr(0, prefijo.length) == prefijo) {
                divisiones[i].style.display = 'inline';
            }
    }

    function cambiar_clase(nombre, clase) {
    //----------------------------------------------------------------------
    // PROPOSITO:
    // Modifica el estilo de visualización de una división.
    //----------------------------------------------------------------------
            cuerpo=document.getElementById(nombre);
            if (cuerpo == null)
                alert('Error('+nombre+'): Fallo en la comunicaci&oacute;n\\n'+
                      'Favor informar a soporte@skinait.com');
            else
                cuerpo.className=clase;
    }

    function cambiar_dia(numero_anho, numero_mes, numero_dia, numero_hora, numero_minuto,
                         div_despliegue, variable, tabla, campo, reg_id, opciones, acc_retorno) 
    //----------------------------------------------------------------------
    // PROPOSITO:
    // Cambia el día de una fecah específica.
    //----------------------------------------------------------------------
    {
        llamarProceso('divisionAjax.php',
                      div_despliegue,
                      '&modulo=ingreso_variable'+
                      '&modulo_retorno='+acc_retorno+
                      '&division='+div_despliegue+
                      '&tabla='+tabla+
                      '&campo='+campo+
                      '&registro_id='+reg_id+
                      '&valor='+encodeURIComponent(numero_anho+'-'+
                                                   numero_mes+'-'+
                                                   numero_dia+' '+
                                                   numero_hora+':'+
                                                   numero_minuto)+
                      '&tipo_cambio=dia'+
                      '&opciones='+opciones+
                      '&variable='+variable,
                      null,
                      '');
    }

    function cambiar_imagen(nombre, fuente)
    //----------------------------------------------------------------------
    // PROPOSITO:
    // Cambiar alguna propiedad de un objeto
    //----------------------------------------------------------------------
    {
        imagen = document.getElementById(nombre);
        imagen.src = fuente;
    }

    function cambiar_propiedad(nombre, propiedad, valor)
    //----------------------------------------------------------------------
    // PROPOSITO:
    // Cambiar alguna propiedad de un objeto
    //----------------------------------------------------------------------
    {
        seccion = document.getElementById(nombre);
        if (seccion != null)
            switch (propiedad) {
                case 'display':
                    seccion.style.display = valor;
                    break;
                case 'visibility':
                    seccion.style.visibility = valor;
                    break;
                case 'background':
                    seccion.style.background = valor;
                    break;
                case 'top':
                    seccion.style.top = valor;
                    break;
                case 'bottom':
                    seccion.style.bottom = valor;
                    break;
                case 'left':
                    seccion.style.left = valor;
                    break;
                case 'right':
                    seccion.style.right = valor;
                    break;
                case 'width':
                    seccion.style.width = valor;
                    break;
                case 'height':
                    seccion.style.height = valor;
                    break;
                case 'margin':
                    seccion.style.margin = valor;
                    break;
                case 'overflow':
                    seccion.style.overflow = valor;
                    break;
                default:
                    alert('js_error: No implementado para '+propiedad);
            }
        else
            alert('Error ('+nombre+'.'+propiedad+') Fallo en la comunicacion\nRevise su conexion de red.');
    }

    function cambiar_texto(nombre, valor)
    //----------------------------------------------------------------------
    // PROPOSITO:
    // Modificar un texto en el interior de una división.
    //----------------------------------------------------------------------
    {
        seccion = document.getElementById(nombre);
        if (seccion != null)
            seccion.innerHTML = valor;
        else
            alert('PLEASE WAIT...\n['+nombre+']');
    }

    function cambiar_seleccion(nombre, indice)
    //----------------------------------------------------------------------
    // PROPOSITO:
    // Modificar el seleccionado en una cortina de selección.
    //----------------------------------------------------------------------
    {
        objeto = document.getElementById(nombre);
        if (objeto != null)
            objeto.selectedIndex = indice;
        else
            alert('PLEASE WAIT...\n['+nombre+']');
    }

    function cambiar_valor(nombre, valor)
    //----------------------------------------------------------------------
    // PROPOSITO:
    // Modificar el valor de un objeto.
    //----------------------------------------------------------------------
    {
        objeto = document.getElementById(nombre);
        if (objeto != null)
            objeto.value = valor;
        else
            alert('PLEASE WAIT...\n['+nombre+']');
    }

    function es_numero(valor)
    //----------------------------------------------------------------------
    // PROPOSITO:
    // Identificar si un texto contiene solo números.
    //----------------------------------------------------------------------
    {
        var numeros_validos = "0123456789.-";
        var actual;
        var retorno = true;

        if (valor.length == 0)
            return false;

        for (i = 0; i < valor.length && retorno == true; i++) {
            actual = valor.charAt(i);
            if (numeros_validos.indexOf(actual) == -1)
                retorno = false;
        }

        return retorno;
    }

    function div_esc_most(nombre) {
    //----------------------------------------------------------------------
    // PROPOSITO:
    // cambia la visualización de una division llamada 'nombre'
    //----------------------------------------------------------------------
        var division = document.getElementById(nombre);

        if (division.style.display == '')
            division.style.display = 'none';
        else if (division.style.display == 'none')
            division.style.display = '';
    }

    function div_esc_most(nombre) {
    //----------------------------------------------------------------------
    // PROPOSITO:
    // cambia la visualización de una division llamada 'nombre'
    //----------------------------------------------------------------------
        var division = document.getElementById(nombre);

        if (division.style.display == '')
            division.style.display = 'none';
        else if (division.style.display == 'none')
            division.style.display = '';
    }

    function esc_most(nombre, clase, imagen, urlbase) {
    //----------------------------------------------------------------------
    // PROPOSITO:
    // Intercambia el estado de una sección entre ESCONDIDO y la clase indicada.
    //----------------------------------------------------------------------
            var re=new RegExp('\\b'+'ESCONDIDO'+'\\b');
    
            cuerpo=document.getElementById(nombre);
            if (cuerpo == null)
                alert('Error en esc_mos, no hay: '+nombre+'\\nFavor informar a soporte@skina.com.co');
            else
                if (re.test(cuerpo.className)) {
                    cuerpo.className=clase;
                    img_src = urlbase+"/img/menos_pq.png";
                    cambiar_imagen(imagen, img_src);
                } else {
                    cuerpo.className='ESCONDIDO';
                    img_src = urlbase+"/img/mas_pq.png";
                    cambiar_imagen(imagen, img_src);
                }
    }

    function esc_most_cal(nombre, clase, imagen, urlbase) {
    //----------------------------------------------------------------------
    // PROPOSITO:
    // Intercambia el estado de un calendario entre ESCONDIDO y la clase indicada.
    //----------------------------------------------------------------------
            var re=new RegExp('\\b'+'ESCONDIDO'+'\\b');
    
            img_mas = new Image;
            img_mas.src = urlbase+"/img/calendario.png";
    
            img_menos = new Image;
            img_menos.src = urlbase+"/img/encoger.png";
    
            cuerpo=document.getElementById(nombre);
            if (cuerpo == null)
                alert('Error en esc_mos, no hay: '+nombre+'\\nFavor informar a soporte@skina.com.co');
            else
                if (re.test(cuerpo.className)) {
                    cuerpo.className=clase;
                    imagen.src=img_menos.src;
                } else {
                    cuerpo.className='ESCONDIDO';
                    imagen.src=img_mas.src;
                }
    }

    function opciones2URI(seleccion, prefijo) {
    //----------------------------------------------------------------------
    // PROPOSITO:
    // De acuerdo a las opciones seleccionadas en un select multiple
    // construye un URI con los valores seleccionados.
    //----------------------------------------------------------------------
        sel = document.getElementById(seleccion);
        retorno = '';
        var puestos = 0;
        for (var i = 0; i < sel.length; i++)
            if (sel.options[i].selected) {
                retorno += '&'+prefijo+'['+puestos+']='+sel.options[i].value;
                puestos ++;
            }

        return retorno;
    }

    function tope_pagina(event) {
    //----------------------------------------------------------------------
    // PROPOSITO:
    // Posiciona la página al principio.
    //----------------------------------------------------------------------
        window.scrollTo(0,0)
    }

    //------------------------------------------------------------
    // AJAX
    //------------------------------------------------------------

    function nuevoAjax(){
    //----------------------------------------------------------------------
    // PROPOSITO:
    // Crea un nuevo objeto ajax.
    //----------------------------------------------------------------------
        var xmlhttp=false;
        try {
            xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (E) {
                xmlhttp = false;
            }
        }

        if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
            xmlhttp = new XMLHttpRequest();
        }
        return xmlhttp;
    }

    function llamarProceso(script, division, datos, divmensaje, mensaje) {
    //----------------------------------------------------------------------
    // PROPOSITO:
    // Carga el resultado del script indicado en la division correspondiente
    // presentando el mensaje en la posición indicada.
    //----------------------------------------------------------------------

        if (divmensaje != null && mensaje != '')
            cambiar_texto(divmensaje, mensaje);

        contenedor = document.getElementById(division);
        if (contenedor == null)
            alert('ERROR: null container - llamarProceso ['+division+']');

        if (mensaje != '')
            contenedor.innerHTML = mensaje;

        ajax=nuevoAjax();
        ajax.open("POST", script, true);
        ajax.onreadystatechange=function() {
            if (ajax.readyState==4) {
                contenedor.innerHTML = ajax.responseText;
            }
        }
        ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        ajax.send("datos="+datos)
    }

