// JavaScript Document

// holds an instance of XMLHttpRequest 
var xmlHttp = createXmlHttpRequestObject(); 
 
// creates an XMLHttpRequest instance 
function createXmlHttpRequestObject(){
        var xmlhttp=false;
        try{
                xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
        }catch(e){
                try{
                        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                }catch(E){
                        xmlhttp = false;
                }
        }

        if(!xmlhttp && typeof XMLHttpRequest!='undefined'){
                xmlhttp = new XMLHttpRequest();
        }
        return xmlhttp;
}
 
// make asynchronous HTTP request using the XMLHttpRequest object  
function process() 
{ 
  // proceed only if the xmlHttp object isn't busy 
  if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0) 
  { 

 
    // define the method to handle server responses 
    xmlHttp.onreadystatechange = handleServerResponse; 
    // make the server request 
    xmlHttp.send(null); 
  }else {
    // if the connection is busy, try again after one second   
    setTimeout('process()', 30);
	}
} 
 
// executed automatically when a message is received from the server 
function handleServerResponse()  
{ 
  // move forward only if the transaction has completed 
  if (xmlHttp.readyState == 4)  
  { 
    // status of 200 indicates the transaction completed successfully 
    if (xmlHttp.status == 200)  
    { 
      resultQuery = xmlHttp.responseText;
      // update the client display using the data received from the server 
      document.getElementById("divConsulta").innerHTML = resultQuery ; 
      // restart sequence 
      /*setTimeout('process()', 1000); */
    }else{
	   // a HTTP status different than 200 signals an error 		
      alert("There was a problem accessing the server: " + xmlHttp.statusText); 
    } 
  } 
} 

function bProfeDpt(){
if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0) 
  	{ 
   var dept;
    	// se obtiene la cadena digitada por el usuario.
    dept = encodeURIComponent(document.getElementById("profe_dept").value);
		var divgeneral=document.getElementById("divConsulta");
	
			// execute the quickstart.php page from the server 
			xmlHttp.open("POST", "consulta/src/ajax_departamentos.php", true);
		
		 	// define the method to handle server responses 
			xmlHttp.onreadystatechange = handleSearch; 
			xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			divgeneral.innerHTML = "";
			xmlHttp.send("dpt_acd="+dept);
		
  	}else{
	  setTimeout('process()', 30); 
  	}	
}

function handleSearch(){
	if (xmlHttp.readyState == 4)  
 	 { 
    	// status of 200 indicates the transaction completed successfully 
    	if (xmlHttp.status == 200)  
    	{ 
			resultQuery = xmlHttp.responseText;
      		// update the client display using the data received from the server 
      		document.getElementById("divConsulta").innerHTML = resultQuery ; 
      		// restart sequence 
      		/*setTimeout('process()', 1000);*/
		}else{
			alert("There was a problem accessing the server: " + xmlHttp.statusText);
		}
  	}
}

