/* Comprobar campos del formulario */
function esNumerico(strString)
{
   var strValidChars = "0123456789.-";
   var strChar;
   var blnResult = true;

   if (strString.length == 0) return true;

   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)
      {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         {
         blnResult = false;
         }
      }
   return blnResult;
}

function esNif(control) {
    if (control.value == "")
        return false;

    var dni = control.value;
    var numero = dni.substr(0, dni.length - 1);
    var let = dni.substr(dni.length - 1, 1);
    let = let.toUpperCase();
    numero = numero % 23;
    var letra = 'TRWAGMYFPDXBNJZSQVHLCKET';
    letra = letra.substring(numero, numero + 1);
    //alert(letra);
    if (letra != let) {
        return false;
    }

    return true;
}


function esNie(control) {
    if (control.value == "")
        return false;

    var a = control;
    var temp = a.value.toUpperCase();
    var cadenadni = "TRWAGMYFPDXBNJZSQVHLCKET";
    var v1 = new Array(0, 2, 4, 6, 8, 1, 3, 5, 7, 9);
    var posicion = 0;
    var letra = " ";

    //Residente en España	
    if (a.value.length == 9) {
        if (temp.substr(0, 1) == "X") {
            var temp1 = temp.substr(1, 7);

            posicion = temp1 % 23; /*Resto de la division entre 23 es la posicion en la cadena*/
            letra = cadenadni.substring(posicion, posicion + 1);
            if (!/^[A-Za-z0-9]{9}$/.test(temp)) {
                return false;
            }
            else {
                //Tiene los 9 dígitos, comprobamos si la letra esta bien
                var temp1 = temp.substr(1, 7);
                posicion = temp1 % 23; /*Resto de la division entre 23 es la posicion en la cadena*/
                letra = cadenadni.charAt(posicion);
                var letranie = temp.charAt(8);
                if (letra != letranie) {
                    return false;
                }
            }
        }
        else {
            return false;
        }
    } else if (a.value.length == 14) {//14 caracteres, los 2 primeros letras
        var temp1 = temp.substr(0, 2);
        if (isAlphabetic(temp1) != true) {
            return false;
        }
    }
    else {
        return false;
    }

    return true;

}

function esEmail(campo) 
{
    var strMail = campo;
    var patMail = /^(.+)@(.+)$/; 
    var patUser = /^[a-zA-Z\d_-]+(\.[a-zA-Z\d_-]+)*$/;
    var patDomainIP = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/;
    var patDomain = /^[a-zA-Z\d_-]+(\.[a-zA-Z\d_-]+)+$/;
    var matchArray = strMail.match(patMail);
    if (matchArray == null) { return false; }
    var user = new String(matchArray[1]);
    var domain = new String(matchArray[2]);
    if (user.match(patUser) == null) { return false; }
    var IPArray = domain.match(patDomainIP);
    if (IPArray != null) {
        for (var i = 1; i <= 4; i++) {
            if (IPArray[i] > 255)
            { return false; } 
        }
        return true;
    }
    var domainArray = domain.match(patDomain);
    if (domainArray == null) { return false; }
    if (domainArray[domainArray.length - 1].length < 3 || domainArray[domainArray.length - 1].length > 4) { return false; }
    return true;
}




function ValidarContacteBolsaTrabajo() {

    var Nombre = document.getElementById('ctl00_ContentPlaceHolder1_' + 'tb_Nombre')
    if (Nombre.value == "") {
        alert('Nombre es un dato obligatorio!!');
        Nombre.focus();
        return false;
    }


    var Email = document.getElementById('ctl00_ContentPlaceHolder1_' + 'tb_Email')
    if (Email.value == "") {
        alert('Email es un dato obligatorio!!');
        Email.focus();
        return false;
    }


    if ((Email.value)) {
        if (!esEmail(Email.value)) {
            alert('El campo "E-mail" debe ser un email válido.');
            Email.focus(); return false;
        }
    }

    var Doc = document.getElementById('ctl00_ContentPlaceHolder1_' + 'fileVitae')
    if (Doc.value == "") {
        alert('Por favor, haz clic en Examinar para incluir tu CViate!!');
        Email.focus();
        return false;
    }
    
    var Acepto = document.getElementById('ctl00_ContentPlaceHolder1_' + 'chbx_Acepto')
    if (!Acepto.checked) {
        alert('Para enviar el formulario es necesario marcar la casilla conforme ha leido la Politica de proteccion de datos personales. !!');
        return false;
    }


    return true;
}


function ValidarContacte() {

    var Nombre = document.getElementById('ctl00_ContentPlaceHolder1_' + 'tb_Nombre')
    if (Nombre.value == "") {
        alert('Nombre es un dato obligatorio!!');
        Nombre.focus();
        return false;
    }


    var Email = document.getElementById('ctl00_ContentPlaceHolder1_' + 'tb_Email')
    if (Email.value == "") {
        alert('Email es un dato obligatorio!!');
        Email.focus();
        return false;
    }
    
    
    if ((Email.value)) {
        if (!esEmail(Email.value))
		{
		    alert('El campo "E-mail" debe ser un email válido.');
		    Email.focus(); return false;
		}
	}

	var Acepto = document.getElementById('ctl00_ContentPlaceHolder1_' + 'chbx_Acepto')
	if (!Acepto.checked) {
	    alert('Para enviar el formulario es necesario marcar la casilla conforme ha leido la Politica de proteccion de datos personales. !!');
	    return false;
	}
	

	return true;
}


function setActiveStyleSheet(title) {
    var i, a, main;
    for (i = 0; (a = document.getElementsByTagName("link")[i]); i++) {
        if (a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
            a.disabled = true;
            if (a.getAttribute("title") == title) a.disabled = false;
        }
    }
}

function getActiveStyleSheet() {
    var i, a;
    for (i = 0; (a = document.getElementsByTagName("link")[i]); i++) {
        if (a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
    }
    return null;
}

function getPreferredStyleSheet() {
    var i, a;
    for (i = 0; (a = document.getElementsByTagName("link")[i]); i++) {
        if (a.getAttribute("rel").indexOf("style") != -1
       && a.getAttribute("rel").indexOf("alt") == -1
       && a.getAttribute("title")
       ) return a.getAttribute("title");
    }
    return null;
}

function createCookie(name, value, days) {
    document.cookie = name + "=" + value + "; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return null;
}

window.onload = function(e) {

    var cookie = readCookie("style");
    var title = cookie ? cookie : getPreferredStyleSheet();
    setActiveStyleSheet(title);
}

window.onunload = function(e) {
    var title = getActiveStyleSheet();
    createCookie("style", title, 365);
}

var cookie = readCookie("style");
var title = cookie ? cookie : getPreferredStyleSheet();
setActiveStyleSheet(title);

var isCtrl = false;
document.onkeydown = function(e) {

    var keycode;
    if (window.event) keycode = window.event.keyCode;
    else if (e) keycode = e.which;

    if (keycode == 18)
        isCtrl = true;
    /*    
    
    if (keycode == 84 && isCtrl == true) { //t
    location.href = "Integra_QuienesSomos.aspx";
    return false;
    }

    if (keycode == 78 && isCtrl == true) { //n
    location.href = "Noticias_Ultimas.aspx";
    return false;
    }

    if (keycode == 66 && isCtrl == true) { //b
    location.href = "BolsaTrabajo_Ventajas.aspx";
    return false;
    }

    if (keycode == 82 && isCtrl == true) { //r
    location.href = "Integra_Referencias.aspx";
    return false;
    }


    if (keycode == 67 && isCtrl == true) { //c
    location.href = "Contacto_Integra.aspx";
    return false;
    }
    

    if (keycode == 73 && isCtrl == true) { //i
        location.href = "Inicio.aspx";
        return false;
    }  
    
    if (keycode == 77 && isCtrl == true) { //m
        location.href = "Integra_MedidasAlternativas.aspx";
        return false;
    }  
    
    if (keycode == 80 && isCtrl == true) { //p
        location.href = "Integra_CentralCompras.aspx";
        return false;
    }  
    
    if (keycode == 69 && isCtrl == true) { //e
        location.href = "Integra_Delegaciones.aspx";
        return false;
    }     

    if (keycode == 81 && isCtrl == true) { //q
        location.href = "Integra_QueHacemos.aspx";
        return false;
    }
    */

}

function ValidarDatosPersonales_IntegraEriksson() {

    var Navegador = navigator.appName;
    var EsIE = false;
    if (Navegador == 'Microsoft Internet Explorer')
        EsIE = true;

    //Nombre -----------------------------
    
    if (EsIE) {
        Nombre = document.getElementById('ctl00$ContentPlaceHolder1$' + 'tb_Nombre');
    }
    else {
        Nombre = document.getElementById('ctl00_ContentPlaceHolder1_' + 'tb_Nombre');
    }

    Nombre.className = "NoFocus";

    if (Nombre.value == "") {
        alert('Nombre es un dato necesario');
        Nombre.className = "Focus";
        Nombre.focus();
        return false;
    }


    //Email -----------------------------

    if (EsIE) {
        Email = document.getElementById('ctl00$ContentPlaceHolder1$' + 'tb_Email');
    }
    else {
        Email = document.getElementById('ctl00_ContentPlaceHolder1_' + 'tb_Email');
    }
    Nombre.className = "NoFocus";

    if (Email.value == "") {
        alert('Email es un dato necesario');
        Email.className = "Focus";
        Email.focus();
        return false;
    }

    if ((Email.value)) {
        if (!esEmail(Email.value)) {
            alert('El Email introducido no es correcto');
            Email.className = "Focus";
            Email.focus(); return false;
        }
    }

    //Nif -----------------------------

    if (EsIE) {
        Nif = document.getElementById('ctl00$ContentPlaceHolder1$' + 'tb_Nif');
    }
    else {
        Nif = document.getElementById('ctl00_ContentPlaceHolder1_' + 'tb_Nif');
    }

    Nif.className = "NoFocus";

    if (Nif.value == "") {
        alert('Por favor, introduce tu Nif / Nie');
        Nif.focus();
        Nif.className = "Focus";
        return false;
    }

    if (Nif.value.substr(0, 1) == "X") {
        if (!esNie(Nif)) {
            alert('Tu Nie es incorrecto');
            Nif.focus();
            Nif.className = "Focus";
            return false;
        }
    }
    else {
        if (!esNif(Nif)) {
            alert('Tu Nif es incorrecto');
            Nif.focus();
            Nif.className = "Focus";
            return false;
        }
    }      

    return true;
}

function ValidarDatosPostales_IntegraEriksson() {

    var Navegador = navigator.appName;
    var EsIE = false;
    if (Navegador == 'Microsoft Internet Explorer')
        EsIE = true;

    //Direccion -----------------------------

    if (EsIE) {
        Direccion = document.getElementById('ctl00$ContentPlaceHolder1$' + 'tb_Direccion');
    }
    else {
        Direccion = document.getElementById('ctl00_ContentPlaceHolder1_' + 'tb_Direccion');
    }

    Direccion.className = "NoFocus";

    if (Direccion.value == "") {
        alert('Dirección es un dato necesario');
        Direccion.className = "Focus";
        Direccion.focus();
        return false;
    }


    //tb_CP -----------------------------

    if (EsIE) {
        tb_CP = document.getElementById('ctl00$ContentPlaceHolder1$' + 'tb_CP');
    }
    else {
        tb_CP = document.getElementById('ctl00_ContentPlaceHolder1_' + 'tb_CP');
    }
    tb_CP.className = "NoFocus";

    if (tb_CP.value == "") {
        alert('Codigo postal es un dato necesario');
        tb_CP.className = "Focus";
        tb_CP.focus();
        return false;
    }


    //tb_Poblacion -----------------------------

    if (EsIE) {
        tb_Poblacion = document.getElementById('ctl00$ContentPlaceHolder1$' + 'tb_Poblacion');
    }
    else {
        tb_Poblacion = document.getElementById('ctl00_ContentPlaceHolder1_' + 'tb_Poblacion');
    }

    tb_Poblacion.className = "NoFocus";

    if (tb_Poblacion.value == "") {
        alert('Poblacion es un dato necesario');
        tb_Poblacion.focus();
        tb_Poblacion.className = "Focus";
        return false;
    }

    //tb_Provincia -----------------------------

    if (EsIE) {
        tb_Provincia = document.getElementById('ctl00$ContentPlaceHolder1$' + 'tb_Provincia');
    }
    else {
        tb_Provincia = document.getElementById('ctl00_ContentPlaceHolder1_' + 'tb_Provincia');
    }

    tb_Provincia.className = "NoFocus";

    if (tb_Provincia.value == "") {
        alert('Provincia es un dato necesario');
        tb_Provincia.focus();
        tb_Provincia.className = "Focus";
        return false;
    }



    return true;
}
