/* Ajax functions library.
Developed by Ignasi Lirio for Platcom */

function cHttpObj() { //creates Http Object for any compatible browser
	var xmlhttp = null;
// Testing compatibility
	if (window.XMLHttpRequest) // create an instance for Mozilla-based browsers
  		{
  			xmlhttp=new XMLHttpRequest();
  		}
	else if (window.ActiveXObject) // create the object for IE
  		{
  			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  		}
// If browser does not support Http Request object, the value returned by the function will remain to 'null'
	return xmlhttp;
}


/* syncGet: sends the contents of data to the url specified in Synchronous mode.
Data must be URL-encoded in the way of pairs of variable names and value 
Return response text from feedback based on PHP page action. Otherwise in case
of HttpRequest failure sends back custom parameter "error_msg" as output.
*/
function syncGet(url,data,error_msg) {  // uses GET method

	//alert("syncGet - data: "+data);
	//alert("syncGet - URL: "+url+"?"+data);
	var ajaxObject = cHttpObj();
	ajaxObject.open("GET",url+"?"+data,false);
	ajaxObject.send(null);
	if(ajaxObject.status==200) {
		resultado = unescape(ajaxObject.responseText);
		resultado = resultado.replace(/\+/gi," ");
	} else {
		 //alert('Status: '+ajaxObject.status);
		resultado = error_msg;
	}
		 //alert('Status: '+ajaxObject.status);
		 //alert('Resultado: '+resultado);
	return resultado;
	
	
}






function syncPost(url,data,error_msg) {  // same as sendS, uses POST method for Form data
	var ajaxObject = cHttpObj();
	ajaxObject.open("POST",url,false);
	ajaxObject.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	ajaxObject.send(data);
	if(ajaxObject.status==200) {
		resultado = unescape(ajaxObject.responseText);
		resultado = resultado.replace(/\+/gi," ");
	}
	else {
		 //alert('Status: '+ajaxObject.status);
		resultado = error_msg;
	}
		 //alert('Status: '+ajaxObject.status);
		 //alert('Resultado: '+resultado);
	return resultado;
}

/* asyncGet: sends a request via GET in ASYNCHRONOUS mode.
Data must be URL-encoded in the way of pairs of variable names and value.
Return response text from feedback based on PHP page action. Otherwise in case
of HttpRequest failure sends back custom parameter "error_msg" as output.
Parameter destination is a string with the name of the ID of HTML element
to be rendered. Parameter type is a string with the name of the type of
that HTML element (i.e.: "text","select","hidden", etc.)
*/

var targetElement;








function asyncGet(url,data,error_msg,destination,type) {  // uses GET method
	//alert("asyncGet - data: "+data);
	//alert("URL: "+url+"?"+data);
	targetElement = destination;
	ajaxObject = cHttpObj();
	//alert("targetElement: "+targetElement);
	
	switch (type) {
		case "text": 
			ajaxObject.onreadystatechange = textChanged; // calls stateChanged() function at every change in connection status. 
			break;
		case "select":
			ajaxObject.onreadystatechange = selectChanged;
			break;
	}
 
	ajaxObject.open("GET",url+"?"+data,true);
	ajaxObject.send(null);
}








//For textfields, feedback of PHP fileis placed straight to the textfield, as is.
function textChanged() {
	if(ajaxObject.readyState==4) {
		s = eval("document.getElementById('"+targetElement+"')");
		result = unescape(ajaxObject.responseText);
		result = result.replace(/\+/gi," ");
		//alert("result: "+result);
		s.innerHTML = result;	
	}
}

/*For SELECT as output, feedback needs to be formatted in the shape of
'option name'$'option value' separated by '|' in order function to
populate properly the SELECT provided in the parameters*/
function selectChanged() {
	if(ajaxObject.readyState==4) {
		s = eval("document.getElementById('"+targetElement+"')");
		//Cleaning the current list
		nl = (s.options.length);
		while(nl>1){
			s.remove(1);
			nl = (s.options.length);
		}
		elements = unescape(ajaxObject.responseText);
		elements = elements.replace(/\+/gi," ");
		elArray = elements.split("|");
		for(k=0;k<elArray.length;k++) {
			singleElement = elArray[k];
			portions = singleElement.split("$");
			s.options[k]=new Option(portions[0],portions[1]);
			}s
	}
}


	

