addEvent(window,'load',inicializarEventos,false);

function inicializarEventos()
{
  var select1=document.getElementById('categoria');
  addEvent(select1,'change',mostrarMaterias,false);
}

var conexion1;
function mostrarMaterias(e) 
{
  var codigo=document.getElementById('categoria').value;
//  if (codigo!=-1)
//  {
    conexion1=crearXMLHttpRequest();
    conexion1.onreadystatechange = procesarEventos;
    conexion1.open('GET','http://www.almeriaempleo.es/funciones/obtener-subcategorias.php?categoria='+codigo, true);
    conexion1.send(null);
/*  }
  else
  {
    var select2=document.getElementById('subcategoria');
    select2.options.length=0;
      var op=document.createElement('option');
	  op.value = "-1";
	  op.text = "[Todas]";
      select2.appendChild(op);
  }*/
}

function procesarEventos()
{
  if(conexion1.readyState == 4)
  {
    var d=document.getElementById('espera');
    d.innerHTML = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';

    var xml = conexion1.responseXML;
    var pals=xml.getElementsByTagName('identificacion');
    var noms=xml.getElementsByTagName('nombre');
    var select2=document.getElementById('subcategoria');
    select2.options.length=0;

    var op=document.createElement('option');
    var texto=document.createTextNode("[Todas]");
    op.appendChild(texto);
	op.value = "";
    select2.appendChild(op);
	
	for(f=0;f<pals.length;f++)
    {
  
	  var op=document.createElement('option');
      var texto=document.createTextNode(noms[f].firstChild.nodeValue);
	  op.appendChild(texto);
	  op.value = pals[f].firstChild.nodeValue;
      select2.appendChild(op);
    } 
  } 
  else 
  {
    var d=document.getElementById('espera');
    d.innerHTML = '<img height="15" src="imagenes/cargando.gif">';
  }
}

//***************************************
//Funciones comunes a todos los problemas
//***************************************
function addEvent(elemento,nomevento,funcion,captura)
{
  if (elemento.attachEvent)
  {
    elemento.attachEvent('on'+nomevento,funcion);
    return true;
  }
  else  
    if (elemento.addEventListener)
    {
      elemento.addEventListener(nomevento,funcion,captura);
      return true;
    }
    else
      return false;
}

function crearXMLHttpRequest() 
{
  var xmlHttp=null;
  if (window.ActiveXObject)  {
    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");

}
  else 
    if (window.XMLHttpRequest) 
      xmlHttp = new XMLHttpRequest();
  return xmlHttp;
}
