// 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 

  }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 busq_area(){
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0) 
  	{ 

    	// se obtiene la cadena digitada por el usuario.
    	area= encodeURIComponent(document.getElementById("area_buscar").value);
	var preloader=document.getElementById("divConsulta");
	 preloader.innerHTML = "";
	 if(area==null || area=="" || area=="buscar_a"){
			alert("Digita el área para buscar los profesores asociados");	
		}else{
			// execute the quickstart.php page from the server 
			xmlHttp.open("POST", "consulta/src/ajax_areas.php", true);
		 	// define the method to handle server responses 
	 
			xmlHttp.onreadystatechange = handleSearch; 
			xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			xmlHttp.send("area="+area);
		}
  	}else{
	  setTimeout('process()', 30); 
  	}	
}


function handleSearch(){
	if (xmlHttp.readyState == 4)  
 	 { 
    	// status of 200 indicates the transaction completed successfully 
    	if (xmlHttp.status == 200)  
    	{ 
		   document.getElementById("divConsulta").innerHTML='';
			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);
		}
  	}
}
