﻿// Función para recuperar el objeto que hace referencia al control en el formulario
function RetornarObjControl(nombre, frm) {
    for (var i = 0; i < frm.length; i++) {
        var ele = frm.elements[i];

        if (ele.id.indexOf(nombre) != -1) {
            return ele;
        }
    }
    return null;
}

/////////////////solo números con cantidad de decimales/////////////////////////////
function NumerosDecimalesExactos(e, ctlId, numDecimales) {
    var cadena = ctlId.value;
    var cantPuntos = 1;
    var d;
    var pos;
    var tecla;

    setBrowserType();

    //tecla = (document.all) ? e.keyCode : e.which;

    if (sBrowser == "ie") { //Internet Explorer
        tecla = window.event.keyCode

    } else if (sBrowser == "mo") { //Mozilla Firefox
        tecla = e.which;
        keyCode = e.keyCode;
        //Fin, Inicio, Izquierda, Derecha, Delete, BackSpace, Tab
        if (keyCode == 35 | keyCode == 36 | keyCode == 37 | keyCode == 39 | keyCode == 46 | keyCode == 8 | keyCode == 9)
            tecla = keyCode;
    }


    //if (tecla == 8) return true;
    if ((tecla < 48 || tecla > 57) & tecla != 35 & tecla != 36 & tecla != 37 & tecla != 39 & tecla != 46 & tecla != 8 & tecla != 9) {
        if (sBrowser == "ie") {
            window.event.keyCode = 0;
        }
        else {
            return false;
        }
    }

    if (tecla == 46) {
        //////valida que solo se permita un punto decimal   
        for (var i = 0; i <= cadena.length; i++) {
            if ((cadena.charAt(i) == ".")) {
                cantPuntos = cantPuntos + 1;
                if (cantPuntos > 1) {
                    return false;
                }
            }
        }
    }

    //valida la cantidad de decimales
    var decimales = cadena.split(".");

    var totalEnteros = ctlId.maxLength - (numDecimales + 1);

    //valida los números antes del punto
    if (totalEnteros > 1) {
        if (decimales.length > 1) {
            d = cadena.length - (decimales[1].length + 1);
        }
        else {
            d = totalEnteros - 1;
        }

        if (document.selection)
            pos = getPosCaret(ctlId);
        else
            pos = ctlId.selectionStart;

        if ((decimales[0].length >= totalEnteros)) {
            if (tecla != 46 & tecla != 8 & tecla != 37 & tecla != 39) {
                if ((pos = d) && (decimales.length == 1)) {
                    return false;
                }
                else if (pos < d) {
                    return false;
                }
            }
        }
    }

    //valida los números despues del punto
    if (decimales.length > 1) {
        d = cadena.length - (decimales[1].length + 1);

        if (document.selection)
            pos = getPosCaret(ctlId);
        else
            pos = ctlId.selectionStart;

        //alert(decimales[1].length + ">" + s + "y" + pos + ">" + d);

        if ((decimales[1].length > numDecimales - 1) && (pos > d)) {
            if (tecla != 46 & tecla != 8 & tecla != 37 & tecla != 39) {
                return false;
            }
        }
    }
    //return patron.test(te);
}

/////////////////solo números con cantidad de decimales/////////////////////////////
function NumerosDecimalesPosNegExactos(e, ctlId, numDecimales) {
    var cadena = ctlId.value;
    var cantPuntos = 1;
    var cantGuion = 1;
    var d;
    var pos;
    var tecla;

    setBrowserType();

    if (sBrowser == "ie") { //Internet Explorer
        tecla = window.event.keyCode;
    } else if (sBrowser == "mo") { //Mozilla Firefox
        tecla = e.which;
        keyCode = e.keyCode;

        //Fin, Inicio, Izquierda, Derecha, Delete, BackSpace, Tab
        if (keyCode == 35 | keyCode == 36 | keyCode == 37 | keyCode == 39 | keyCode == 46 | keyCode == 8 | keyCode == 9)
            tecla = keyCode;
    }


    //if (tecla == 8) return true;
    if ((tecla < 48 || tecla > 57) & tecla != 45 & tecla != 35 & tecla != 36 & tecla != 37 & tecla != 39 & tecla != 46 & tecla != 8 & tecla != 9) {
        if (sBrowser == "ie") {
            window.event.keyCode = 0;
        }
        else {
            return false;
        }
    }


    if (tecla == 46) {
        //////valida que solo se permita un punto decimal   
        for (var i = 0; i <= cadena.length; i++) {
            if ((cadena.charAt(i) == ".")) {
                cantPuntos = cantPuntos + 1;
                if (cantPuntos > 1) {
                    return false;
                }
            }
        }
    }

    if (tecla == 45) {
        //////valida que solo se permita un "-"   
        for (var i = 0; i <= cadena.length; i++) {
            if ((cadena.charAt(i) == "-")) {
                cantGuion = cantGuion + 1;
                if (cantGuion > 1) {
                    return false;
                }
            }
        }

        //Obtener la posición del cursor donde se ingresa el "-"
        if (document.selection)
            pos = getPosCaret(ctlId);
        else
            pos = ctlId.selectionStart;

        //Validar que este al inicio    
        if (pos > 0) {
            return false;
        }
    }

    //valida la cantidad de decimales
    var decimales = cadena.split(".");
    var totalEnteros = 0;

    //Elimando el .
    totalEnteros = ctlId.maxLength - (numDecimales + 1);
    //Eliminando el -
    totalEnteros = totalEnteros - 1;

    //valida los números antes del punto
    if (totalEnteros > 1) {
        if (decimales.length > 1) {
            d = cadena.length - (decimales[1].length + 1);
        }
        else {
            d = totalEnteros - 1;
        }

        if (document.selection)
            pos = getPosCaret(ctlId);
        else
            pos = ctlId.selectionStart;

        if ((decimales[0].length >= totalEnteros)) {
            if (tecla != 46 & tecla != 8 & tecla != 37 & tecla != 39) {
                if ((pos = d) && (decimales.length == 1)) {
                    return false;
                }
                else if (pos < d) {
                    return false;
                }
            }
        }
    }

    //valida los números despues del punto
    if (decimales.length > 1) {
        d = cadena.length - (decimales[1].length + 1);

        if (document.selection)
            pos = getPosCaret(ctlId);
        else
            pos = ctlId.selectionStart;

        //alert(decimales[1].length + ">" + s + "y" + pos + ">" + d);

        if ((decimales[1].length > numDecimales - 1) && (pos > d)) {
            if (tecla != 46 & tecla != 8 & tecla != 37 & tecla != 39) {
                return false;
            }
        }
    }
}

/////////////////////////Obtiene la posición del cursor en el textbox///////////////////////////////
function getPosCaret(input) {
    var range = document.selection.createRange(); //document
    var range2 = input.createTextRange();

    range2.collapse(true);

    range2.moveEnd('character', 0);
    range2.setEndPoint('EndToStart', range);

    distancia = range2.text.length;
    //alert(distancia);
    //pos_final = parseFloat(input.value.length) - parseFloat(distancia);
    //range2.move('character', pos_final);
    //alert(distancia);
    return distancia;
}

// Permite realizar la impresión del contenido de un DIV
function printDiv(id) {
    var ficha = document.getElementById(id);
    var ventimp = window.open("", "", "");
    ventimp.document.write(ficha.innerHTML);
    ventimp.document.close();
    ventimp.print();
    ventimp.close();

    return false;
}

// Permite imprimir página entera
function AllPage() {
    window.print();
    return false;
}
