﻿var peticion = null;

function inicializa_xhr() {
  if (window.XMLHttpRequest) {
    return new XMLHttpRequest(); 
  } else if (window.ActiveXObject) {
    return new ActiveXObject("Microsoft.XMLHTTP"); 
  } 
}


function cargaMunicipios() {

    var valores=document.getElementById("provinciasvalidas").value;
    var palabra=document.getElementById("provincia").value;
    

  if (document.getElementById("municipio"))
  {  
    
  if (document.getElementById("municipio2"))
  {
  var lista = document.getElementById("provincia");
  var provincia = lista.options[lista.selectedIndex].value;
  if(!isNaN(provincia)) {
    peticion = inicializa_xhr();
    if (peticion) {
      peticion.onreadystatechange = muestraMunicipios;
      peticion.open("GET", "/dameprovinciasxml.asp?tipo=m&provincia=" + provincia + "&nocache=" + Math.random(), true);
      peticion.send(null);
    }
  }
  }
  }
}

function cargaPaises() {
   
 peticion = inicializa_xhr();
  if(peticion) {
    peticion.onreadystatechange = muestraPaises;
    peticion.open("GET", "/dameprovinciasxml.asp?tipo=pa&nocache="+Math.random(), true);
    peticion.send(null);
  }
}

function cargaProvincias()
{

  
    var valores=document.getElementById("paisesvalidas").value;
    var palabra=document.getElementById("paises").value;
    var error=false; 

           
     
    
  var lista = document.getElementById("paises");
  var provincia = lista.options[lista.selectedIndex].value;
  if(!isNaN(provincia))
  { 
  if (provincia==23)
  {

    if (document.getElementById("Divprov"))
    {  
    document.getElementById("Divprov2").className='dato';
    document.getElementById("Divprov").className='dato1';
    }
    if (document.getElementById("Divmuni"))
    {
    document.getElementById("Divmuni2").className='dato';
    document.getElementById("Divmuni").className='dato1';
    } 
   if (document.getElementById("provincia"))
    {  
    document.getElementById("provincia2").style.display='none';
    document.getElementById("provincia").style.display='inline';
    
    if (document.getElementById("municipio2"))
        document.getElementById("municipio2").style.display='none';
   
    if (document.getElementById("municipio"))
        document.getElementById("municipio").style.display='inline';

/*   document.getElementById("provincia2").style.visibility='hidden';
   document.getElementById("provincia").style.visibility='visible';
   
   if (document.getElementById("municipio2"))
        document.getElementById("municipio2").style.visibility='hidden';
   
   if (document.getElementById("municipio"))
        document.getElementById("municipio").style.visibility='visible';*/
        
        

    peticion = inicializa_xhr();
      if(peticion) {
        peticion.onreadystatechange = muestraProvincias;
        peticion.open("GET", "/dameprovinciasxml.asp?tipo=p&nocache="+Math.random(), true);
        peticion.send(null);
        }
        
     
     document.getElementById("provincia").onchange = cargaMunicipios;
    }
  }
  else
  {
    if (document.getElementById("Divprov"))
    { 
    document.getElementById("Divprov2").className='dato1';
    document.getElementById("Divprov").className='dato';
    }
    if (document.getElementById("Divmuni"))
    {
    document.getElementById("Divmuni2").className='dato1';
    document.getElementById("Divmuni").className='dato';
    }
      
    
    if (document.getElementById("provincia"))
    { 
     document.getElementById("provincia").style.display='none';
     document.getElementById("provincia2").style.display='inline';
     if (document.getElementById("municipio"))
        document.getElementById("municipio").style.display='none';
     if (document.getElementById("municipio2"))
        document.getElementById("municipio2").style.display='inline';
        
    /* 
     document.getElementById("provincia").style.visibility='hidden';
     document.getElementById("provincia2").style.visibility='visible';
     if (document.getElementById("municipio"))
        document.getElementById("municipio").style.visibility='hidden';
     if (document.getElementById("municipio2"))
        document.getElementById("municipio2").style.visibility='visible';
        */
    }
    
       
  }
 }
 

     
     
}


function muestraProvincias() {
  if (peticion.readyState == 4) {
    if (peticion.status == 200) {
      var lista = document.getElementById("provincia");
      var documento_xml = peticion.responseXML;

      var provincias = documento_xml.getElementsByTagName("provincias")[0];
      var lasProvincias = provincias.getElementsByTagName("provincia");
      lista.options[0] = new Option("Elige");
      
      // Mtodo 1: Crear elementos Option() y a�adirlos a la lista
      for(i=0; i<lasProvincias.length; i++) {
        var codigo = lasProvincias[i].getElementsByTagName("codigo")[0].firstChild.nodeValue;
        var nombre = lasProvincias[i].getElementsByTagName("nombre")[0].firstChild.nodeValue;
        lista.options[i+1] = new Option(nombre, codigo);
        if (nombre.toLowerCase()==document.getElementById("usuprov").value.toLowerCase())
           { 
           lista.options[i+1].selected=1;
           cargaMunicipios();
           }
      }

     
    }
  }
  
  
}

function muestraPaises() {
  if (peticion.readyState == 4) {
    if (peticion.status == 200) {
      var lista = document.getElementById("paises");
      var documento_xml = peticion.responseXML;

      var municipios = documento_xml.getElementsByTagName("paises")[0];
      var losMunicipios = municipios.getElementsByTagName("pais");

 
       lista.options[0] = new Option("Elige");
       lista.options[1] = new Option("");
       lista.options[2] = new Option("España",23);
       lista.options[3] = new Option("");
      
      // Se utiliza el m�todo de crear elementos Option() y a�adirlos a la lista
      for(i=0; i<losMunicipios.length; i++) {
        var codigo = losMunicipios[i].getElementsByTagName("codigo")[0].firstChild.nodeValue;
        var nombre = losMunicipios[i].getElementsByTagName("nombre")[0].firstChild.nodeValue;
        lista.options[i+4] = new Option(nombre, codigo);
        if (nombre.toLowerCase()==document.getElementById("usupai").value.toLowerCase())
           { 
           lista.options[i+4].selected=1;
           cargaProvincias();
           }
      }
    }
  }
}

function muestraMunicipios() {
  if (peticion.readyState == 4) {
    if (peticion.status == 200) {
      var lista = document.getElementById("municipio");
      var documento_xml = peticion.responseXML;

      var municipios = documento_xml.getElementsByTagName("municipios")[0];
      var losMunicipios = municipios.getElementsByTagName("municipio");

      // Borrar elementos anteriores
      lista.options.length = 0;
      lista.options[0] = new Option("Elige");
      
      // Se utiliza el m�todo de crear elementos Option() y a�adirlos a la lista
      for(i=0; i<losMunicipios.length; i++) {
        var codigo = losMunicipios[i].getElementsByTagName("codigo")[0].firstChild.nodeValue;
        var nombre = losMunicipios[i].getElementsByTagName("nombre")[0].firstChild.nodeValue;
        lista.options[i+1] = new Option(nombre, codigo);
        if (nombre.toLowerCase()==document.getElementById("usupob").value.toLowerCase())
           { 
           lista.options[i+1].selected=1;
           }
      }
    }
  }
}



window.onload = function() {
document.getElementById("busqueda").focus();
if (document.getElementById("paises"))
{ 
    var valores=document.getElementById("paisesvalidas").value;
    var palabra=document.getElementById("paises").value;
    var error=false; 
   
    

 if (document.getElementById("provincia"))
 { 
    if (document.getElementById("usupai").value!='España')
    {
    if (document.getElementById("Divprov"))
    { 
    document.getElementById("Divprov2").className='dato1';
    document.getElementById("Divprov").className='dato';
    }
    if (document.getElementById("Divmuni"))
    {
    document.getElementById("Divmuni2").className='dato1';
    document.getElementById("Divmuni").className='dato';
    }
    if (document.getElementById("provincia"))
    {  
     
     document.getElementById("provincia").style.display='none';
     document.getElementById("provincia2").style.display='inline';
     if (document.getElementById("municipio"))
      {  document.getElementById("municipio").style.display='none';
        document.getElementById("municipio2").style.display='inline';}
     }
     
    /*
     document.getElementById("provincia").style.visibility='hidden';
     document.getElementById("provincia2").style.visibility='visible';
     if (document.getElementById("municipio"))
      {  document.getElementById("municipio").style.visibility='hidden';
        document.getElementById("municipio2").style.visibility='visible';}
     }*/
         
        document.getElementById("provincia2").value=document.getElementById("usuprov").value;
        if (document.getElementById("municipio2"))
            document.getElementById("municipio2").value=document.getElementById("usupob").value;;
    }
    else
       {
        cargaProvincias()
        
        document.getElementById("provincia2").style.display='none';
        if (document.getElementById("municipio2"))
            document.getElementById("municipio2").style.display='none';
            
       /* document.getElementById("provincia2").style.visibility='hidden';
        if (document.getElementById("municipio2"))
            document.getElementById("municipio2").style.visibility='hidden';*/
            
        }
  }
 
 document.getElementById("paises").onchange = cargaProvincias;
 }
 
}



 
 function expandCollapse(elem, changeImage) {
if (document.getElementById) {
ecBlock = document.getElementById(elem);
if (ecBlock != undefined && ecBlock != null) {
if (changeImage) {
//gets the image associated
elemImage = document.getElementById(elem + "Image");
elemLabel = document.getElementById(elem + "Label");
}
//make sure elemImage is good
if (!changeImage || (elemImage != undefined && elemImage != null)) {
if (ecBlock.style.display == "none" || ecBlock.style.display == null || ecBlock.style.display == "") {
//shows the info.
ecBlock.style.display = "block";
if (changeImage) {
//Just got in expanded mode. Thus, change image to "collapse"
elemImage.src = "images2/up.png";
elemLabel.innerHTML=" menos "
}
}
else if (ecBlock.style.display == "block") {
//hide info
ecBlock.style.display = "none";
if (changeImage) {
//Just got in collapsed mode. Thus, change image to "expand"
elemImage.src = "images2/down.png";
elemLabel.innerHTML=" más "
}
}
else {
//catch any weird circumstances.
ecBlock.style.display = "block";
if (changeImage) {
elemImage.src = "images2/up.png";
elemLabel.innerHTML=" menos "
}
}
}
//end check elemImage
}
//end check ecBlock
}
//end getElemById
}
//end expandCollapse function
