﻿//array de control global    
var municipios;
var emails;
 

//NOMBRE
function nombre_onchange()
{   
    var nombre = document.getElementById('nombre').value;      
    if (nombre != "")
    {
        document.getElementById('nombre_ok').style.visibility = "visible";
        document.getElementById('nombre_ko').style.visibility = "hidden";
    }else
    {
        document.getElementById('nombre_ok').style.visibility = "hidden";
        document.getElementById('nombre_ko').style.visibility = "visible";
    }
    
}

//APELLIDOS
function apellidos_onchange()
{   
    var apellidos = document.getElementById('apellidos').value;      
    if (apellidos != "")
    {
        document.getElementById('apellidos_ok').style.visibility = "visible";
        document.getElementById('apellidos_ko').style.visibility = "hidden";
    }else
    {
        document.getElementById('apellidos_ok').style.visibility = "hidden";
        document.getElementById('apellidos_ko').style.visibility = "visible";
    }
}

//EMAIL
//funcion que valida el formato del campo correo
function mail(texto){    
    var mailres = true;            
    var cadena = "abcdefghijklmnñopqrstuvwxyzABCDEFGHIJKLMNÑOPQRSTUVWXYZ1234567890@._-";
    
    var arroba = texto.indexOf("@",0);
    if ((texto.lastIndexOf("@")) != arroba) arroba = -1;
    
    var punto = texto.lastIndexOf(".");
                
     for (var contador = 0 ; contador < texto.length ; contador++){
        if (cadena.indexOf(texto.substr(contador, 1),0) == -1){
            mailres = false;
            break;
     }
    }

    if ((arroba > 1) && (arroba + 1 < punto) && (punto + 1 < (texto.length)) && (mailres == true) && (texto.indexOf("..",0) == -1))
     mailres = true;
    else
     mailres = false;
                
    return mailres;
}

function comprobarEmail()
{    
    var m = document.getElementById('email').value;    
    if (m!="")
    {
        for(i=0;i<emails.length;i++)
        {
           if(emails[i]==m)
           {
                return false;            
           }            
        }
    }
    return true;
}

function email_onchange()
{    
    pedirEmails();        
}



//TELÉFONO
function comprobarTelefono()
{
    //comprobar teléfono
     var patron = /^([0-9\s\+\-])+$/;
     var arrTelefono = document.getElementById('telefono').value.split(" ");
     var telefonoSinEsp="";
     for(var i=0;i<arrTelefono.length;i++)
     {
        telefonoSinEsp += arrTelefono[i];
     }
     document.getElementById('telefono').value = telefonoSinEsp;
     return ( telefonoSinEsp.length == 9 && patron.test(document.getElementById('telefono').value));  
}

function telefono_onchange()
{    
     
    if (comprobarTelefono())
    {
        document.getElementById('telefono_ok').style.visibility="visible";
        document.getElementById('telefono_ko').style.visibility="hidden";
    }else
    {
        document.getElementById('telefono_ok').style.visibility="hidden";
        document.getElementById('telefono_ko').style.visibility="visible";
    }
}

//CIUDAD
function comprobarCiudad()
{
    var ok=false;
    var m = document.getElementById('ciudad').value;    
    if (m!="")
    {
        for(i=0;i<municipios.length;i++)
        {
           if(municipios[i]==m)
           {
                ok = true;            
           }            
        }
    }
    return ok;
}

function ciudad_onchange()
{   
    var ok=comprobarCiudad();
    if(ok)
    {
        document.getElementById('ciudad_ok').style.visibility="visible";
        document.getElementById('ciudad_ko').style.visibility="hidden";        
    }
    else
    {
        document.getElementById('ciudad_ok').style.visibility="hidden";
        document.getElementById('ciudad_ko').style.visibility="visible";
    }
    
}
    
function GetXmlHttpObject()
{
    if (window.XMLHttpRequest)
      {
      // code for IE7+, Firefox, Chrome, Opera, Safari
      return new XMLHttpRequest();
      }
    if (window.ActiveXObject)
      {
      // code for IE6, IE5
      return new ActiveXObject("Microsoft.XMLHTTP");
      }
    return null;
}

function stateChanged()
{
    if (xmlhttp.readyState==4)
    {
        
        var a = xmlhttp.responseText;
        municipios = a.split("<br>");
        
        $("#ciudad").autocomplete(municipios);       
    }
}

function pedirMunicipios(str)
{
    xmlhttp=GetXmlHttpObject();
    if (xmlhttp==null)
      {
      alert ("Your browser does not support AJAX!");
      return;
      }
    //url de la pagina que devuelve los datos de la bd            
    var url="municipios.aspx";
    url=url+"?q="+str;        
    xmlhttp.onreadystatechange=stateChanged;
    xmlhttp.open("GET",url,true);
    xmlhttp.send(null);
}

function stateChangedEmails()
{
    if (xmlhttpEmail.readyState==4)
    {
        //document.getElementById('seleccion').innerHTML = xmlhttp.responseText;
        var a = xmlhttpEmail.responseText;        
        emails = a.split("<br>"); 
        
        if (mail(document.getElementById('email').value) && comprobarEmail())
        {
            document.getElementById('email_ok').style.visibility="visible";
            document.getElementById('email_ko').style.visibility="hidden";
            document.getElementById('repe').style.display="none";
        }else
        {        
            document.getElementById('email_ok').style.visibility="hidden";
            document.getElementById('email_ko').style.visibility="visible";
            if (!comprobarEmail())
            {
                document.getElementById('repe').style.display="block";
            }
        }                  
    }
}

function pedirEmails()
{
    xmlhttpEmail=GetXmlHttpObject();
    if (xmlhttpEmail==null)
      {
      alert ("Your browser does not support AJAX!");
      return;
      }
    //url de la pagina que devuelve los datos de la bd            
    var url="emails.aspx";           
    xmlhttpEmail.onreadystatechange=stateChangedEmails;
    xmlhttpEmail.open("GET",url,true);
    xmlhttpEmail.send(null);
}

 
//cargar autocompletar de municipios y poner foco en el campo nombre      
$(document).ready(function()
{   
    //pedirEmails();
    pedirMunicipios(" ");    
    document.getElementById('nombre').focus();
});

function valida()
{
    if (document.getElementById('nombre').value=="" )
    {
        alert('Anda, dinos cómo te llamas, nos va a hacer falta saberlo');
        return false;
    }
    if (document.getElementById('apellidos').value=="" )
    {
        alert('Los apellidos son la salsa de la vida, no los olvides.');
        return false;
    }
    if (!mail(document.getElementById('email').value))
    {
        alert('Nuestros informaticos dicen que lo que has puesto no es un email...');
        return false;       
    }
    if (!comprobarEmail())
    {
        alert('Nos gusta tu insistencia, pero creo que ya estás dado de alta...');
        return false;       
    }     
    if (!comprobarCiudad())
    {
        alert('Je je, en esa ciudad no tenemos hoteles...');
        return false;
    }    
    if (!comprobarTelefono())
    {
        alert('Si alguien te llama a ese numero y te localiza, será un milagro!');
        return false;
    }
    if(document.getElementById('fecha').value=="")
    {
        alert('En qué año dices que has nacido????');
        return false;
    }
    document.getElementById('alta').submit;
    return true;
}

function cargarFechas()
{       
    var sel = document.getElementById('fecha');
    //var arr = new Array();
    var i = 0;
    for(var anho=1940; anho<=1992; anho++)
    {  
        var Op=new Option(anho,i); 
        sel.options[i]=Op;
        i++;
    }
}

