/* (c) Kalamun.org - GNU/GPL 3 */

/* AJAX */

// XMLHttpRequest solo ai browser validi
function b3_createXMLHttpRequest() {
	var XHR=null,browser=navigator.userAgent.toUpperCase();

	//browser standard
	if(typeof(XMLHttpRequest)==="function"||typeof(XMLHttpRequest)==="object") XHR=new XMLHttpRequest();
	//ie4, BLOCCATO
	else if(window.ActiveXObject&&browser.indexOf("MSIE 4")<0) {
		//ie6: metodo diverso
		if(browser.indexOf("MSIE 5")<0) XHR=new ActiveXObject("Msxml2.XMLHTTP");
		//ie5.x: metodo diverso
		else XHR=new ActiveXObject("Microsoft.XMLHTTP");
		}
	return XHR;
	}

// invio di una richiesta
function b3_ajaxSend(method,uri,vars,onSuccess,onFail) {
	if(typeof(onSuccess)!=="function") onSuccess=function(txt) {};
	if(typeof(onFail)!=="function") onFail=function(txt) {};
	var ajax=b3_createXMLHttpRequest();

	// intercetto i messaggi ajax
	function k_onStateChange() {
		if(ajax.readyState===4) {
			if(ajax.status==200) onSuccess(ajax.responseText);
			else onFail(ajax.status);
			delete ajax;
			}
		}

	if(method=="get") {
		uri+="?"+vars;
		ajax.open(method,uri,true);
		ajax.setRequestHeader("connection", "close");
		ajax.onreadystatechange=k_onStateChange;
		ajax.send(null);
		}
	else if(method=="post") {
		ajax.open(method,uri,true);
		ajax.setRequestHeader("content-type","application/x-www-form-urlencoded");
		ajax.setRequestHeader("connection","close");
		ajax.onreadystatechange=k_onStateChange;
		ajax.send(vars);
		}
	if(ajax) delete ajax;
	}
