/* Variabili globali **/
var warningAjax = true;
var warningMsg = "OPPS Ajax is not aviable on this computer!";

/* getDataReturnText(url, callback)
** Uses the GET method to get text from the server. **
Gets text from url, calls function named callback with that text.
Use when you just want to get data from an URL, or can easily
encode the data you want to pass to the server in an URL, such as
"http://localhost/script.php?a=1&b=2&c=hello+there".
Example: getDataReturnText("http://localhost/data.txt", doWork);
Here, the URL is a string, and doWork is a function in your own
script.

evalFlag = true  -> fa un eval di 'callback' passata come stringa (es OUT dal server: 0, dato1, dato2 ...dove dato1/2 possono essere numeri, stringhe o array)
evalFlag = false -> chiama 'callback' passato come oggetto

*/

function getDataReturnText(url, callback, evalFlag){
    var XMLHttpRequestObject = false;
    if (window.XMLHttpRequest) {
        XMLHttpRequestObject = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
    }
    if(XMLHttpRequestObject) {
        XMLHttpRequestObject.open("GET", url);
        XMLHttpRequestObject.onreadystatechange = function(){
            if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200) {            	
            	if (evalFlag){
            		if (typeof (callback) == "string") {
	            		eval(callback + "(" + XMLHttpRequestObject.responseText + ")");  // se OK con evalFalg = true						
					} else {
	            		alert ("ERROR 'callback' must be a string if you set 'evalFlag'=true");
					}
            	} else {
	                callback(XMLHttpRequestObject.responseText);
	            }
                delete XMLHttpRequestObject;
                XMLHttpRequestObject = null;				
            } else {            	
            	if (XMLHttpRequestObject.readyState == 4){            		            	
	            	if (evalFlag){
	            		if (typeof (callback) == "string")
		            		eval(callback + "(1, " + XMLHttpRequestObject.readyState + ", " + XMLHttpRequestObject.status + ", '', '')");
		            	else
		            		alert ("ERROR 'callback' must be a string if you set 'evalFlag'=true");
	            	} else {
		                callback("1," + XMLHttpRequestObject.readyState + "," + XMLHttpRequestObject.status);
		            }     	            
				}	       
            }
        }
        XMLHttpRequestObject.send(null);		
    } else {
    	if (warningAjax){
	    	alert(warningMsg);
	    	warningAjax = false;
    	}
    }
}

/* getDataReturnXml(url, callback)
** Uses the GET method to get XML from the server. **
Gets XML from URL, calls function named callback with that XML.
Use when you just want to get data from an URL, or can easily
encode the data you want to pass to the server in an URL, such as
"http://localhost/script.php?a=1&b=2&c=hello+there".
Example: getDataReturnXml("http://localhost/data.txt", doWork);
Here, the URL is a string, and doWork is a function in your
own script.
*/
function getDataReturnXml(url, callback){
    var XMLHttpRequestObject = false;
    if (window.XMLHttpRequest) {
        XMLHttpRequestObject = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
    }
    if(XMLHttpRequestObject) {
        XMLHttpRequestObject.open("GET", url);
        XMLHttpRequestObject.onreadystatechange = function(){
            if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200) {
                callback(XMLHttpRequestObject.responseXML);
                delete XMLHttpRequestObject;
                XMLHttpRequestObject = null;
            }
        }
        XMLHttpRequestObject.send(null);
    } else {
    	if (warningAjax){
	    	alert(warningMsg);
	    	warningAjax = false;
    	}
    }
}

/* postDataReturnText(url, data, callback)
** Uses the POST method to send data to server, gets text back. **
Posts data to url, calls function callback with the returned text.
Uses the POST method, use this when you have more text data to send
to the server than can be easily encoded into an URL.
Example: postDataReturnText("http://localhost/data.php",
"parameter=5", doWork);
Here, the URL is a string; the data sent to the server
("parameter=5") is a string;and doWork is a function in
your own script.
*/
function postDataReturnText(url, data, callback){
    var XMLHttpRequestObject = false;
    if (window.XMLHttpRequest) {
        XMLHttpRequestObject = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
    }
    if(XMLHttpRequestObject) {
        XMLHttpRequestObject.open("POST", url);
        XMLHttpRequestObject.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
        XMLHttpRequestObject.onreadystatechange = function(){
            if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200) {
                callback(XMLHttpRequestObject.responseText);
                delete XMLHttpRequestObject;
                XMLHttpRequestObject = null;
            }
        }   
        XMLHttpRequestObject.send(data);       
    } else {
    	if (warningAjax){
	    	alert(warningMsg);
	    	warningAjax = false;
    	}
    }       
}


/* postDataReturnXml(url, data, callback)
** Uses the POST method to send data to server, gets XML back. **
Posts data to url, calls function callback with the returned XML.
Uses the POST method, use this when you have more text data to send
to the server than can be easily encoded into an URL.
Example: postDataReturnXml("http://localhost/data.php",
"parameter=5", doWork);
Here, the URL is a string; the data sent to the server
("parameter=5") is a string; and doWork is a function in
your own script.
*/
function postDataReturnXml(url, data, callback){
    var XMLHttpRequestObject = false;
    if (window.XMLHttpRequest) {
        XMLHttpRequestObject = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
    }
    if(XMLHttpRequestObject) {
        XMLHttpRequestObject.open("POST", url);
        XMLHttpRequestObject.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        XMLHttpRequestObject.onreadystatechange = function() {
            if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200) {
                callback(XMLHttpRequestObject.responseXML);
                delete XMLHttpRequestObject;
                XMLHttpRequestObject = null;
            }
        }
        XMLHttpRequestObject.send(data);
    } else {
    	if (warningAjax){
	    	alert(warningMsg);
	    	warningAjax = false;
    	}
    }
}
