/* 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
	}
}

var ns4 = (document.layers)? true:false;
var ie4 = (document.all)? true:false;
var moz = (document.getElementById)? 1:0

var http = getXmlHttpObject();

var urlCountryDivision = "createAddress.php?countryDivision="; // Behind the Scenes
var urlStates = "createAddress.php?state="; // Behind the Scenes
var urlTowns = "createAddress.php?town="; // Behind the Scenes

	
function sendQuerystringCountryDivision(nCountryDivision){
    http.open("GET", urlCountryDivision + nCountryDivision, true);  
	http.onreadystatechange = handleHttpResponseCountryDivision;
    http.send(null);
}


function sendQuerystringState(nState){
    http.open("GET", urlStates + nState, true);
    http.onreadystatechange = handleHttpResponseState;
    http.send(null);
}

function sendQuerystringTown(nTown){
    http.open("GET", urlTowns + nTown, true);
    http.onreadystatechange = handleHttpResponseTown;
    http.send(null);
}


function handleHttpResponseCountryDivision(){
    if (http.readyState == 4){
        results = http.responseText;
        document.getElementById('selectStates').innerHTML = results;
    }
}


function handleHttpResponseState(){
    if (http.readyState == 4){
        results = http.responseText;
        document.getElementById('selectTowns').innerHTML = results;
    }
}

function handleHttpResponseTown(){
    if (http.readyState == 4){
        results = http.responseText;
        document.getElementById('selectPCs').innerHTML = results;
    }
}


function getXmlHttpObject(){
    var xmlhttp;

    /*@cc_on
    @if (@_jscript_version >= 5)
    try{
    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e){
    try{
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    catch (e){
    xmlhttp = false;
    }
    }
    @else
    xmlhttp = false;
    @end @*/

    if (!xmlhttp && typeof XMLHttpRequest != 'undefined'){
        try{
            xmlhttp = new XMLHttpRequest();
        }
        catch (e){
            xmlhttp = false;
        }
    }
    return xmlhttp;
}

function showStates(nCountryDivision) {
    if (nCountryDivision != 0) {
	    sendQuerystringCountryDivision(nCountryDivision);
	}	
}

function showTowns(nState) {
    if (nState != 0) {
	    sendQuerystringState(nState);
	}	
}

function showPCs(nTown) {
    if (nTown != 0) {
	    sendQuerystringTown(nTown);
	}	
}

