﻿// JScript File
function CompruebaNIF(source,arguments)
{
    debugger;
      var ok = true;
    var sLetraTRE = "";
    var sTipoDocumento = document.getElementById("ddTipoDocumento").value;
    if (document.getElementById("txtDocumento").value != "") {
        if (sTipoDocumento != "PAS" && 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 CompruebaFechaNacimiento(source,arguments)
	        {
	        sDia = document.getElementById("wdFechaNacimiento").value;
						
			if ((sDia) == "01/01/1900" || (sDia) == "")
				arguments.IsValid = false;
			else
				arguments.IsValid = true;
			
			return;
		    }
		    
function validateCombo(source,arguments)
{    
    debugger;
    var sValue;
    sValue = trim(arguments.Value);
    arguments.IsValid = ( sValue != "0" ) ;
}


function trim (string) {
	return string.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}
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; }
}
		    


