﻿/*!* 
* Deiser Javascript Functions Library v1.0.0
* 
* http://www.deiser.com
* 
* DSF
*
* Copyright 2011, Deiser
* Date: 27/09/2011
*/

String.prototype.testEmail = function () {
    /// <summary>
    /// Comprueba la validez de un email
    /// </summary>
    /// <return>
    /// true si es correcto y false si no lo es
    /// </return>
    return /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(this);
}

String.prototype.fileExtension = function () {
    /// <summary>
    /// Devuelve la extension de un archivo
    /// </summary>
    /// <return>
    /// La extensión si es posible obtenerla y undefined si no es posible
    /// </return>
    return /(?:\.([^.]+))?$/.exec(this)[1];
}

String.prototype.decode = function () {

    var str = this.replace("[#at#]", "@");
    var map = new Array();
    var s = "abcdefghijklmnopqrstuvwxyz";

    for (i = 0; i < s.length; i++)
        map[s.charAt(i)] = s.charAt((i + 13) % 26);
    for (i = 0; i < s.length; i++)
        map[s.charAt(i).toUpperCase()] = s.charAt((i + 13) % 26).toUpperCase();

    s = "";
    for (i = 0; i < str.length; i++) {
        var b = str.charAt(i);

        s += (b >= 'A' && b <= 'Z' || b >= 'a' && b <= 'z' ? map[b] : b);
    }   
    
    return s;
}

String.prototype.reverse = function () {
    splitext = this.split("");
    revertext = splitext.reverse();
    reversed = revertext.join("");
    return reversed;
}

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;
}

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)
}
/**
* @class Hola
*/
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();
}

/**
* Funcion para validar si el texto introducido en un campo de texto
* es un valor alfanumÃ©rico
* Ejemplo return validarAlfanumerico(event,this.value)
* @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
* Ejemplo return validarNumerico(event,this.value)
* @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));
}

function upperCase(e) {
    /// <summary>
    /// Pone en mayusculas
    /// </summary>

    // Firefox e Internet Explorer.
    var evt = arguments[0] || event;
    var char = String.fromCharCode(evt.which || evt.keyCode);

    // Está en minusculas?
    if (/[a-z]/.test(char)) {
        // añade las mayusculas
        this.value += char.toUpperCase();

        // Cancela el evento original
        evt.cancelBubble = true;
        return false;
    }

}


