﻿// JScript File

function CompruebaNIF(source,arguments)
{
    debugger;
    var ok = true;
    var sLetraTRE = "";
    var sTipoDocumento = document.getElementById("cmbTipoDocumento").value;
    if (document.getElementById("txtDocumento").value != "")
    {
        if ((sTipoDocumento == "DNI" || sTipoDocumento == "TRE") && sTipoDocumento != "0")
        {
            var sDocumento = document.getElementById("txtDocumento").value;
            sDocumento = sDocumento.toUpperCase();

            aLetras = new Array();
            aLetras[0] = "T";
            aLetras[1] = "R";
            aLetras[2] = "W";
            aLetras[3] = "A";
            aLetras[4] = "G";
            aLetras[5] = "M";
            aLetras[6] = "Y";
            aLetras[7] = "F";
            aLetras[8] = "P";
            aLetras[9] = "D";
            aLetras[10] = "X";
            aLetras[11] = "B";
            aLetras[12] = "N";
            aLetras[13] = "J";
            aLetras[14] = "Z";
            aLetras[15] = "S";
            aLetras[16] = "Q";
            aLetras[17] = "V";
            aLetras[18] = "H";
            aLetras[19] = "L";
            aLetras[20] = "C";
            aLetras[21] = "K";
            aLetras[22] = "E";


            sLetraTRE = sDocumento.substring(0, 1);
            
            if (sLetraTRE == "X" && sTipoDocumento == "TRE") {
                sDocumento = sDocumento.substring(1);
            }
            if (sLetraTRE == "Y" && sTipoDocumento == "TRE") {
                sDocumento = "1" + sDocumento.substring(1);
            }

            sDocumento = sDocumento.replace(/[" "]/g, "");
            sDocumento = sDocumento.replace(/[-]/g, "");
            sDocumento = sDocumento.replace(/[_]/g, "");

            var sCadena = sDocumento;

            while (sCadena.substring(0, 1) == "0") {
                sCadena = sCadena.substring(1, sCadena.length);
            }

            sDocumento = sCadena;

            var iDocumento = sDocumento.substring(0, sDocumento.length - 1);             
            if (IsNumeric(iDocumento) == true) {
                iDocumento = parseInt(sDocumento);
                sLetra = sDocumento.charAt(sDocumento.length - 1);
                sLetraCorrecta = aLetras[iDocumento % 23];
                if (IsNumeric(sLetra) == true) {
                    //sLetra = sLetraCorrecta;
                    sDocumento = sDocumento + sLetra;
                }
                if (sLetraTRE == "Y" && sTipoDocumento == "TRE")
                    ok = iDocumento <= 999999999;
                else
                    ok = iDocumento <= 99999999
                if (sLetra != sLetraCorrecta) //La sLetra no es correcta.
                    ok = false;
            }
            else
                ok = false;  
            
            
            if (ok) {
                if (sTipoDocumento == "TRE") {
                    if (sLetraTRE == "Y")
                        document.getElementById("txtDocumento").value = "Y" + sDocumento.substring(1);
                    else
                        document.getElementById("txtDocumento").value = "X" + sDocumento;
                }
                else
                    document.getElementById("txtDocumento").value = sDocumento;
            }
            else {
                //                document.getElementById("txtDocumento").value = "";
            }
        }
    }
    arguments.IsValid = ok;

}


function validateCombo(source,arguments)
{
    var sValue;
    sValue = trim(arguments.Value);
    arguments.IsValid = ( sValue != "0" ) ;
}


function trim (string) {
	return string.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}

function MuestraDatosHijos() {
            debugger;
	            if (document.getElementById("txtCuantosHijos")!=null)
	            {                    
	                OcultaDatosHijos();
	                if (document.getElementById("txtCuantosHijos").value > 0)
	                {
	                    iNumeroHijos=document.getElementById("txtCuantosHijos").value;
	                    if (iNumeroHijos>4)
		                    iNumeroHijos=4;
                		
	                    if (iNumeroHijos!=0)
	                    {
		                    for (i=1;i<=iNumeroHijos;i++){		                    
		                        window.document.getElementById("divTitulo_" + i).className="";
		                        window.document.getElementById("divNombre_" + i).className="";
		                        window.document.getElementById("divNombreLabel_" + i).className="";
			                    window.document.getElementById("divFecha_" + i).className="";
			                    window.document.getElementById("divFechaLabel_" + i).className="";
		                    }
	                    }
	                }
	                else
	                {
		                document.getElementById("txtCuantosHijos").value="0";
	                }
	            }
            }
	        function OcultaDatosHijos()
	        {
	        debugger;
		        if (window.document.getElementById("divFecha_1")!=null)
		        {
		            for (i=1;i<=4;i++)
		            {
		                window.document.getElementById("divTitulo_" + i).className = "clsInvisible";
		                window.document.getElementById("divNombre_" + i).className="clsInvisible";
		                window.document.getElementById("divNombreLabel_" + i).className="clsInvisible";
			            window.document.getElementById("divFecha_" + i).className="clsInvisible";
			            window.document.getElementById("divFechaLabel_" + i).className="clsInvisible";			            
		            }
		        }
		    }


   function FechaCorrecta(day, month, year) {
    if (month.toString().substr(0, 1) == "0")
       month = month.toString().substr(1, 1);
    var elMes = parseInt(month);
    var anio = parseInt(year);
    valor = true;
    // MES FEBRERO
    if (day == "--" && month == "--" && year == "--")
        return true;
    else
        if (day == "--" || month == "--" || year == "--")
        return false;
    if (elMes == 2) {
        if (esBisiesto(anio)) {
            if (parseInt(day) > 29) {
                return false;
            }
            else
                return true;
        }
        else {
            if (parseInt(day) > 28) {
                return false;
            }
            else
                return true;
        }
    }
    //RESTO DE MESES 

    if (elMes == 4 || elMes == 6 || elMes == 9 || elMes == 11) {
        if (parseInt(day) > 30) {
            return false;
        }
    }
    return true;
}
//***************************************************************************************** 
// esBisiesto(anio) 
// 
// Determina si el año pasado com parámetro es o no bisiesto 
//***************************************************************************************** 
function esBisiesto(anio) {
    var BISIESTO;
    if (parseInt(anio) % 4 == 0) {
        if (parseInt(anio) % 100 == 0) {
            if (parseInt(anio) % 400 == 0) {
                BISIESTO = true;
            }
            else {
                BISIESTO = false;
            }
        }
        else {
            BISIESTO = true;
        }
    }
    else
        BISIESTO = false;

    return BISIESTO;
}
function IsNumeric(valor) {
    var log = valor.length; var sw = "S";
    for (x = 0; x < log; x++) {
        v1 = valor.substr(x, 1);
        v2 = parseInt(v1);
        //Compruebo si es un valor numÃ©rico 
        if (isNaN(v2)) { sw = "N"; }
    }
    if (sw == "S") { return true; } else { return false; }
}

    
	        