//Devuelve: 1 = NIF ok, 2 = CIF ok, 3 = NIE ok, -1 = NIF error, -2 = CIF error, -3 = NIE error, 0 = ??? error
function valida_nif_cif_nie(a) 
{
	var temp=a.toUpperCase();
	var cadenadni="TRWAGMYFPDXBNJZSQVHLCKE";
 
	if (temp!==''){

		if ((!/^[A-Z]{1}[0-9]{7}[A-Z0-9]{1}$/.test(temp) && !/^[T]{1}[A-Z0-9]{8}$/.test(temp)) && !/^[0-9]{8}[A-Z]{1}$/.test(temp))
		{
			return 0;
		}

		if (/^[0-9]{8}[A-Z]{1}$/.test(temp))
		{
			posicion = a.substring(8,0) % 23;
			letra = cadenadni.charAt(posicion);
			var letradni=temp.charAt(8);
			if (letra == letradni)
			{
			   	return 1;
			}
			else
			{
				return -1;
			}
		}

		suma = parseInt(a[2])+parseInt(a[4])+parseInt(a[6]);
		for (i = 1; i < 8; i += 2)
		{
			temp1 = 2 * parseInt(a[i]);
			temp1 += '';
			temp1 = temp1.substring(0,1);
			temp2 = 2 * parseInt(a[i]);
			temp2 += '';
			temp2 = temp2.substring(1,2);
			if (temp2 == '')
			{
				temp2 = '0';
			}
 
			suma += (parseInt(temp1) + parseInt(temp2));
		}
		suma += '';
		n = 10 - parseInt(suma.substring(suma.length-1, suma.length));
 
		if (/^[KLM]{1}/.test(temp))
		{
			if (a[8] == String.fromCharCode(64 + n))
			{
				return 1;
			}
			else
			{
				return -1;
			}
		}
 
		if (/^[ABCDEFGHJNPQRSUVW]{1}/.test(temp))
		{
			temp = n + '';
			if (a[8] == String.fromCharCode(64 + n) || a[8] == parseInt(temp.substring(temp.length-1, temp.length)))
			{
				return 2;
			}
			else
			{
				return -2;
			}
		}

		if (/^[T]{1}/.test(temp))
		{
			if (a[8] == /^[T]{1}[A-Z0-9]{8}$/.test(temp))
			{
				return 3;
			}
			else
			{
				return -3;
			}
		}

		if (/^[XYZ]{1}/.test(temp))
		{
			pos = str_replace(['X', 'Y', 'Z'], ['0','1','2'], temp).substring(0, 8) % 23;
			if (a[8] == cadenadni.substring(pos, pos + 1))
			{
				return 3;
			}
			else
			{
				return -3;
			}
		}
	}
 
	return 0;
}

function str_replace(search, replace, subject) {
    
    var f = search, r = replace, s = subject;
    var ra = r instanceof Array, sa = s instanceof Array, f = [].concat(f), r = [].concat(r), i = (s = [].concat(s)).length;
 
    while (j = 0, i--) {
        if (s[i]) {
            while (s[i] = s[i].split(f[j]).join(ra ? r[j] || "" : r[0]), ++j in f){};
        }
    };
 
    return sa ? s : s[0];
}


function CIFCorrecto(cif)
{
      
    if (!/^([A-Z]\d{8})|([PX]\d{7}[A-J])$/.test(cif.toUpperCase()))
    {
		return false;
    }

    if (!/^[ABCDEFGHKLMPQSX]/.test(cif.toUpperCase()))
    {
		return false;
	}

	return ComprobarDigitoControlCIF(cif);
}


function ComprobarDigitoControlCIF(cif)
{
    var arrayCIF
    var sumaDigitosPares = 0;
    var sumaDigitosImpares = 0;
    var numero;
    var arrayNumero;
    var digitoControl;
    

	arrayCIF = cif.split("");
	

	if (arrayCIF.length != 9)
	{
		return false;
	}
	
	for(i = 2; i < arrayCIF.length - 1; i = i + 2)
	{
		sumaDigitosPares = sumaDigitosPares + parseInt(arrayCIF[i]);
	}

	for(i = 1; i < arrayCIF.length - 1; i = i + 2)
	{
		numero = arrayCIF[i] * 2;
		if (numero > 9)
		{
			numero = (numero % 10) + 1
		}
		
		sumaDigitosImpares = sumaDigitosImpares + numero;
	}

	numero = sumaDigitosPares + sumaDigitosImpares	
	for (i = numero; i >= 0; i = i - 10)
	{
		numero = i;
	}
	digitoControl = 10 - numero;

	if (arrayCIF[0].toUpperCase() == "P" || arrayCIF[0].toUpperCase() == "X")
	{
		digitoControl = String.fromCharCode(digitoControl + 64);
	} 
	
	if (digitoControl.toString() == arrayCIF[8])
	{
		return true;
	}
	else
	{
		return false;
	}
}


function ValidaDNI(Valor){
	
	var Res = false;
	
	var regex = /[^a-zA-Z0-9/]/gi;
	var ValorFiltered = Valor.replace(regex,'');

	if((CIFCorrecto(ValorFiltered) == true)||(valida_nif_cif_nie(ValorFiltered) > 0)){
		Res = true
	}

	return Res;
}