﻿/*!
 * DJFL v1.0.0
 * Deiser Javascript Functions Library
 * 
 * http://www.deiser.com
 *
 * Carlos Fernández Moreno
 * DSF
 *
 * Copyright 2011, Deiser
 * Date: 15/04/2011
 */
 
 
 //Valida email
 String.prototype.testEmail=function(){    
    return /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(this); 
 }
 
  
 //Valida CIF
 String.prototype.testCif = function(){
    var pares = 0;
    var impares = 0;
    var suma;
    var ultima;
    var unumero;
    var uletra = new Array("J", "A", "B", "C", "D", "E", "F", "G", "H", "I");
    var xxx;

    texto = this.toUpperCase();

    var regular = new RegExp(/^[ABCDEFGHKLMNPQS]\d{7}[0-9,A-J]$/g);
    if (!regular.exec(texto)) return false;

    ultima = texto.substr(8,1);

    for (var cont = 1 ; cont < 7 ; cont ++){
        xxx = (2 * parseInt(texto.substr(cont++,1))).toString() + "0";
        impares += parseInt(xxx.substr(0,1)) + parseInt(xxx.substr(1,1));
        pares += parseInt(texto.substr(cont,1));
    }
    xxx = (2 * parseInt(texto.substr(cont,1))).toString() + "0";
    impares += parseInt(xxx.substr(0,1)) + parseInt(xxx.substr(1,1));

    suma = (pares + impares).toString();
    unumero = parseInt(suma.substr(suma.length - 1, 1));
    unumero = (10 - unumero).toString();
    if(unumero == 10) unumero = 0;

    if ((ultima == unumero) || (ultima == uletra[unumero]))
        return true;
    else
        return false;
}
 
//Valida NIF
String.prototype.testDni = function() {
    dni = this.toUpperCase();
    numero = dni.substr(0,dni.length-1);
    let = dni.substr(dni.length-1,1);
    let = let.toUpperCase();
    numero = numero % 23;
    letra = 'TRWAGMYFPDXBNJZSQVHLCKET';
    lletra = letra.charAt(numero);

    return (lletra == let)
}
 
//Valida NIE
String.prototype.testNie = function() {
    var dni = this.toUpperCase();
    var pre = dni.substr(0, 1);
    var prev = '0';
    if (pre == 'X')
       prev = '0';
    else if (pre == 'Y')
       prev = '1';
    else if (pre == 'Z')
       prev = '2';
    numero = prev + dni.substr(1,dni.length-1);
    return numero.testDni();
} 

//Valida todos los documentos
String.prototype.testDocuments=function(){ 
    if(this.testCif()  || this.testDni()  || this.testNie()){
        return true;   
    }else{
        return false;
    }
}

/**
 * Funcion para validar si el texto introducido en un campo de texto
 * es un valor alfanumÃ©rico
 * @param {event} e	Evento
 * @param {String} v Valor de la tecla pulsada
 * @returns	True si el valor es alfanumÃ©rico / False si no lo es
 */
function validarAlfanumerico(e,v){	
    t=(document.all)?e.keyCode:e.which;
    if(t==13|t==8|t==0) return;
    patron=(v.length>-1)?/[\w-\)\(\s]/:/[\w-\)\(]/;
    return patron.test(String.fromCharCode(t));
}

/**
 * Funcion para validar si el texto introducido en un campo de texto
 * es un número
 * @param {event} e	Evento
 * @param {String} v Valor de la tecla pulsada
 * @returns	True si el valor es numÃ©rico / False si no lo es
 */
function validarNumerico(e,v){
	t=(document.all)?e.keyCode:e.which;
	if(t==13|t==8|t==0) return;
	patron = (v.length>-1)?/[0-9]/:/[\w-\)\(]/;
	return patron.test(String.fromCharCode(t));
}



