﻿function $() {
    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 FormularioBeneficio()
{
    $('opcion').value = "beneficio";
    document.formulario.submit();
}

function FormularioBeneficioXCuil(sCuil)
{
    $('numero').value = sCuil;
    $('opcion').value = "beneficio";
    document.formulario.submit();
}

function FormularioCuil()
{
    var sMsg = VerificarCuil($('numero').value);
    if (sMsg != "OK") 
    {
        alert(sMsg);
        return;
    }
    $('opcion').value = "cuil";
    document.formulario.submit();
}

function Periodo(sNumero) 
{
    $('numero').value = sNumero;
    $('opcion').value = "periodo";
    document.formulario.submit();
}

function Volver() 
{
    window.location.href = 'default.aspx';
}

function VerificarCuil(cuil) 
{
    var sCuil = new String(cuil);
    var sCab = sCuil.substr(0, 2);
    var sNum = sCuil.substr(2, 8);
    var sDv = sCuil.substr(10, 1);
    var DVE, msg;

    if (sCuil.length != 11) {
        msg = " El CUIL debe ser de 11(once) digitos.";
        return msg;
    }

    if (sCab.length != 2) {
        //msg = " El cabezal no tiene dos (2) posiciones. ";
        msg = " Verifique el CUIL ";
        return msg;
        //return false;
    }

    if (sNum.length != 8) {
        //msg = " El identificador no tiene ocho (8) posiciones. ";
        msg = " Verifique el CUIL ";
        return msg;
        //return false;
    }

    if (sDv.length != 1) {
        //msg = " El digito verificador no tiene una (1) posicion. ";
        msg = " Verifique el CUIL ";
        return msg;
        //return false;
    }
    //- Compruebo cabezal valido
    var j = /20|23|24|27|30|33|34/;
    var valor = sCab.match(j);
    if (valor == null) {
        //msg = " El cabezal no es un número válido. ";
        msg = " Verifique el CUIL ";
        return msg;
        //return false;
    }
    //- Cargo los pesos
    var pesos = new Array();
    pesos[0] = 5;
    pesos[1] = 4;
    pesos[2] = 3;
    pesos[3] = 2;
    pesos[4] = 7;
    pesos[5] = 6;
    pesos[6] = 5;
    pesos[7] = 4;
    pesos[8] = 3;
    pesos[9] = 2;
    //- Genero el array
    var s = sCab + sNum;
    var ss = s.split("");
    var total = 0;
    //- Multiplico y sumo
    for (i = 0; i <= 9; i++) {
        total = total + ss[i] * pesos[i];
    }
    //- Saco el mod
    var resto = total % 11;
    if ((resto == 0) || (resto == 1)) {
        DVE = 0;
    }

    if (resto > 1) {
        DVE = 11 - resto;
    }

    if (sDv == DVE) {
        msg = "OK";
        //return true;		
        return msg;
    }
    else {
        msg = " No es un CUIL válido. ";
        //msg = msg + " DV:" + DVE;	
        return msg;
        //return false;
    }
}
