﻿/* Variable para saber si forma parte de una empresa
Si es parte de una empresa valdra 1, esto lo controlamos con e
el metodo toggle ( Si oculta la capa es que no es empresa) */
empresa = false;
/*var PostalCodeInit = new Array()
PostalCodeInit[0] = '01' //Primeros digitos del codigo postal de alava
PostalCodeInit[1] = '02' //Primeros digitos del codigo postal de Albacete
PostalCodeInit[2] = '03' //Primeros digitos del codigo postal de Alicante
PostalCodeInit[3] = '04' //Primeros digitos del codigo postal de Almeria
PostalCodeInit[4] = '33' //Primeros digitos del codigo postal de Asturias
PostalCodeInit[5] = '05' //Primeros digitos del codigo postal de avila
PostalCodeInit[6] = '06' //Primeros digitos del codigo postal de Badajoz
PostalCodeInit[7] = '08' //Primeros digitos del codigo postal de Barcelona
PostalCodeInit[8] = '09' //Primeros digitos del codigo postal de Burgos
PostalCodeInit[9] = '10' //Primeros digitos del codigo postal de Caceres
PostalCodeInit[10] = '11' //Primeros digitos del codigo postal de Cadiz
PostalCodeInit[11] = '39' //Primeros digitos del codigo postal de Cantabria
PostalCodeInit[12] = '12' //Primeros digitos del codigo postal de Castellon de la Plana
PostalCodeInit[13] = '51' //Primeros digitos del codigo postal de Ceuta
PostalCodeInit[14] = '13' //Primeros digitos del codigo postal de Ciudad Real
PostalCodeInit[15] = '14' //Primeros digitos del codigo postal de Cordoba
PostalCodeInit[16] = '15' //Primeros digitos del codigo postal de Coruña, A
PostalCodeInit[17] = '16' //Primeros digitos del codigo postal de Cuenca
PostalCodeInit[18] = '17' //Primeros digitos del codigo postal de Girona
PostalCodeInit[19] = '18' //Primeros digitos del codigo postal de Granada
PostalCodeInit[20] = '19' //Primeros digitos del codigo postal de Guadalajara
PostalCodeInit[21] = '20' //Primeros digitos del codigo postal de Guipuzcoa
PostalCodeInit[22] = '21' //Primeros digitos del codigo postal de Huelva
PostalCodeInit[23] = '22' //Primeros digitos del codigo postal de Huesca
PostalCodeInit[24] = '07' //Primeros digitos del codigo postal de Illes Balears
PostalCodeInit[25] = '23' //Primeros digitos del codigo postal de Jaen
PostalCodeInit[26] = '24' //Primeros digitos del codigo postal de Leon
PostalCodeInit[27] = '25' //Primeros digitos del codigo postal de Lleida
PostalCodeInit[28] = '27' //Primeros digitos del codigo postal de Lugo
PostalCodeInit[29] = '28' //Primeros digitos del codigo postal de Madrid
PostalCodeInit[30] = '29' //Primeros digitos del codigo postal de Malaga
PostalCodeInit[31] = '52' //Primeros digitos del codigo postal de Melilla
PostalCodeInit[32] = '30' //Primeros digitos del codigo postal de Murcia
PostalCodeInit[33] = '31' //Primeros digitos del codigo postal de Navarra
PostalCodeInit[34] = '32' //Primeros digitos del codigo postal de Ourense
PostalCodeInit[35] = '34' //Primeros digitos del codigo postal de Palencia
PostalCodeInit[36] = '35' //Primeros digitos del codigo postal de Palmas, Las
PostalCodeInit[37] = '36' //Primeros digitos del codigo postal de Pontevedra
PostalCodeInit[38] = '26' //Primeros digitos del codigo postal de Rioja, La
PostalCodeInit[39] = '37' //Primeros digitos del codigo postal de Salamanca
PostalCodeInit[40] = '38' //Primeros digitos del codigo postal de Santa Cruz de Tenerife
PostalCodeInit[41] = '40' //Primeros digitos del codigo postal de Segovia
PostalCodeInit[42] = '41' //Primeros digitos del codigo postal de Sevilla
PostalCodeInit[43] = '42' //Primeros digitos del codigo postal de Soria
PostalCodeInit[44] = '43' //Primeros digitos del codigo postal de Tarragona
PostalCodeInit[45] = '44' //Primeros digitos del codigo postal de Teruel
PostalCodeInit[46] = '45' //Primeros digitos del codigo postal de Toledo
PostalCodeInit[47] = '46' //Primeros digitos del codigo postal de Valencia
PostalCodeInit[48] = '47' //Primeros digitos del codigo postal de Valladolid
PostalCodeInit[49] = '48' //Primeros digitos del codigo postal de Vizcaya
PostalCodeInit[50] = '49' //Primeros digitos del codigo postal de Zamora
PostalCodeInit[51] = '50' //Primeros digitos del codigo postal de Zaragoza*/

function trim(cadena){ 
    return cadena.replace(/^\s+|\s+$/g,''); 
}
/* Oculta o muestra las capas de los datos de empresa */
function toggle(obj, marc){
    if (empresa){
        obj.style.visibility = "hidden";
        marc.checked = false;
    }else{
    obj.style.visibility = "visible";
    marc.checked = true;
}
empresa = !empresa; 
}
function polit(){
    alert("Data Protection Law\n" +
        "Following the stipulations of the Personal Data Protection Law, " +
        "we inform you that the data presented in this form, will be incorporated in the" +
        "automatic files of Edinn Global, S.L. These data will be treated confidentially " +
        "and following all security measures established in the Data Protection Law, in order to avoid " +
        "non authorized access and treatments. The provided data will not be transfered to third parties nor used " +
        "for other purposes that are not strictly related to software download or license obtaining. " +
        "You will be able to exersise your rights to access, rectify and revoke the consent to" +
        "use these data, in the accorded terms of the Data Protection Law.");
}
function comprobar(formulario){
    var error = false;
    if (formulario.politica.checked == true){
        if (camposVaciosCheck(formulario.visitorname,"You should specify a valid name.") == false)
            error = true;
        if (emailCheck(formulario.visitormail) == false)
            error = true;
        if (telCheck(formulario.visitortel, "You should specify a valid telephone number.") == false)
            error = true;
        if (radioCheck(formulario.conocer, formulario.conocer_t, "with_others", "You should mark a box in the section\"How did you get to know us\".") == false)
            error = true;
        if (radioCheck(formulario.uso, formulario.otros_uso, "use_others", "You should mark a box in the section \"For what use\".") == false)
            error = true;
        if (adqiCheck(formulario.adq,"You should mark a box in the section \"When do you expect to buy edinn M2\"") == false)
            error = true;
        // Si esta activada la seccion "Formo parte de una empresa", comprobamos los datos
        if (empresa){
            if (camposVaciosCheck(formulario.companyname,"You should specify a company name.") == false)
                error = true;
            if (camposVaciosCheck(formulario.companycif,"You should specify a fiscal code.") == false)
                error = true;
            if (emailCheck(formulario.companymail) == false)
                error = true;
            if (telCheck(formulario.companytel, "You should specify a valid company telephone number.") == false)
                error = true;
            if (camposVaciosCheck(formulario.companydir,"You should specify a valid company address.") == false)
                error = true;
            if (camposVaciosCheck(formulario.companypro,"You should specify a valid country.") == false)
                error = true;
            if (camposVaciosCheck(formulario.companyloc,"You should specify a valid city.") == false)
                error = true;
            if (camposVaciosCheck(formulario.companyCP,"You should specify a valid postal code.") == false)
                error = true;
            if (actividadCheck(formulario.company_info, "You should indicate the type of company.") == false)
                error = true;
        }
        if (error == true)
            return false;
        return true;
    } else{
    alert("You should accept the data policy.")
    return false;
}
}
function actividadCheck(tarea, error){
    if (trim(tarea.value).length > 1){
        tarea.style.backgroundColor = "";
        return true;
    }
    alert(error);
    tarea.focus();
    tarea.style.backgroundColor = "#feb0b0";
    return false;
}
/* Comprueba el Codigo postal de una provincia española */
function cpCheck(nycp, YourProvinceIndex, error){
    var Template = /[0-9]{5}$/;
    if (nycp.value.length == 5 && Template.test(nycp.value)){
        if (nycp.value.substring(0, 2) == PostalCodeInit[YourProvinceIndex]){
            nycp.style.backgroundColor = "";
            return true;
        }
    }
    alert(error);
    nycp.style.backgroundColor = "#feb0b0";
    nycp.focus();
    return false;
}
/* Comprobacion de los radio buttons con campo Otros 
formulin -> grupo de radio buttons
formulte -> el campo otros a comprobar
ix -> ID del radio de otros a comprobar
error -> el error que se le dara al usuario*/
function radioCheck(formulin, formulte, ix, error){ // Comprobacion para ¿Como nos conocio?
    for (i=0 ; i<formulin.length; i++){
        if (formulin[i].checked){
            if(formulin[i].id == ix){ // Si se trata del campo Otros
                if (trim(formulte.value).length != 0){  // Comprobamos que el texto en otros exista
                    formulte.style.backgroundColor = "";
                    return true;
                }else { // Si no existe damos el error
                alert(error);
                formulte.focus();
                formulte.style.backgroundColor = "#feb0b0";
                return false;
            }
        }else{ // Aqui llega siempre que no se haya marcado el campo "Otros"
        formulte.style.backgroundColor = "";
        return true;
    }
}
}
alert(error);
return false;
}
function adqiCheck(formulin, error){
    for (i=0 ; i<formulin.length ; i++){
        if (formulin[i].checked)
            return true;
    }
    alert(error);
    return false;
}
function emailCheck (emailStr) {
    var checkTLD=1;
    var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
    var emailPat=/^(.+)@(.+)$/;
    var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
    var validChars="\[^\\s" + specialChars + "\]";
    var quotedUser="(\"[^\"]*\")";
    var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
    var atom=validChars + '+';
    var word="(" + atom + "|" + quotedUser + ")";
    var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
    var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
    var emailStrr = emailStr.value;
    var matchArray=emailStrr.match(emailPat);
    if (matchArray==null) {
        alert("Your email address is incorrect (please check @ and .)");
        emailStr.style.backgroundColor = "#feb0b0";
        return false;
    }
    var user=matchArray[1];
    var domain=matchArray[2];
    for (i=0; i<user.length; i++) {
        if (user.charCodeAt(i)>127) {
            alert("Your email address contains invalid characters.");
            emailStr.style.backgroundColor = "#feb0b0";
            return false;
        }
    }
    for (i=0; i<domain.length; i++) {
        if (domain.charCodeAt(i)>127) {
            alert("The domain of the introduced address contains invalid characters.");
            emailStr.style.backgroundColor = "#feb0b0";
            return false;            
        }        
    }  
    if (user.match(userPat)==null) {     
        alert("The email address is incorrect, please check it again.");
        emailStr.style.backgroundColor = "#feb0b0";
        return false;       
    }  
    var IPArray=domain.match(ipDomainPat);  
    if (IPArray!=null) {     
        for (var i=1;i<=4;i++) {          
            if (IPArray[i]>255) {                
                alert("The IPInternet Protocol is incorrect!");
                emailStr.style.backgroundColor = "#feb0b0";
                return false;              
            }          
        }      
        return true;      
    }
    var atomPat=new RegExp("^" + atom + "$");
    var domArr=domain.split(".");
    var len=domArr.length;
    for (i=0;i<len;i++) { 
        if (domArr[i].search(atomPat)==-1) {
            alert("The email address is incorrect, please check it, including the incorrect use of points, commas at the end of the address.");
            emailStr.style.backgroundColor = "#feb0b0";
            return false; 
        }
    }
    if (checkTLD && domArr[domArr.length-1].length!=2 && domArr[domArr.length-1].search(knownDomsPat)==-1) {
        alert("Your email address should end with a domain or with two letters " + "country.");
        emailStr.style.backgroundColor = "#feb0b0";
        return false;
    }
    if (len<2) { 
        alert("Lack of host in your email address - please check. Or you might have added a white space at the end of the address - correct it and try again.");
        emailStr.style.backgroundColor = "#feb0b0";
        return false;
    }
    emailStr.style.backgroundColor = "";
    return true;
} // Comprobacion para los emails
function camposVaciosCheck (nameStr, mensajeError) { // Comprueba campos vacios 
    cad = trim(nameStr.value);
    if (cad.length != 0 ){
        nameStr.style.backgroundColor = "";
        return true;
    }
    else{
        alert(mensajeError);
        nameStr.focus();
        nameStr.style.backgroundColor = "#feb0b0";
        return false;
    }
}
function telCheck (telStr, error) { // Comprueba el campo del telefono
    cad = trim(telStr.value);
    if (cad.length > 5){
        telStr.style.backgroundColor = "";
        return true;
    }else{
    alert(error);
    telStr.focus();
    telStr.style.backgroundColor = "#feb0b0";
    return false;
}
}
function deshabilita(ff){
    if (ff)
        document.forms[0].conocer_t.disabled = true;
    else
        document.forms[0].conocer_t.disabled = false;
}
function deshabilita2(ff){
    if (ff)
        document.forms[0].otros_uso.disabled = true;
    else
        document.forms[0].otros_uso.disabled = false;
}
function validarCIF(camp_cif,error){
    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 = camp_cif.value;
    texto = texto.toUpperCase();
    
    var regular = new RegExp(/^[ABCDEFGHKLMNPQS]\d\d\d\d\d\d\d[0-9,A-J]$/g);
    if (!regular.exec(texto)) {
        camp_cif.style.backgroundColor = "#feb0b0";
        alert(error);
        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])){
        camp_cif.style.backgroundColor = "";
        return true;
    }
    else{
        alert(error);
        camp_cif.style.backgroundColor = "#feb0b0";
        return false;
    }
} 



