// funzione per assegnare l'oggetto XMLHttpRequest
// compatibile con i browsers pių recenti e diffusi
function setXMLHttpRequest() {
	// lista delle variabili locali
	// variabile di ritorno, nulla di default
	
	var XHR = window.ajax;
	if(!XHR){
		// informazioni sul nome del browser
		var browserUtente = navigator.userAgent.toUpperCase();
		
		
		// browser standard con supporto nativo
		// non importa il tipo di browser
		if(typeof(XMLHttpRequest) === "function" || typeof(XMLHttpRequest) === "object")
			XHR = new XMLHttpRequest();
		
		// browser Internet Explorer
		// č necessario filtrare la versione 4
		else if(
			window.ActiveXObject &&
			browserUtente.indexOf("MSIE 4") < 0
		){
			// la versione 6 di IE ha un nome differente
			// per il tipo di oggetto ActiveX
			if(browserUtente.indexOf("MSIE 5") < 0)
				XHR = new ActiveXObject("Msxml2.XMLHTTP");
			
			// le versioni 5 e 5.5 invece sfruttano lo stesso nome
			else
				XHR = new ActiveXObject("Microsoft.XMLHTTP");
		}
		window.ajax = XHR;
	}
	return XHR;
}

function ajaxRequest(sHref,sPostData){
	sHref = sHref||"";
	sPostData = sPostData||"";
	var sQsData = (sHref||"").split("?");
		sQsData = (sQsData[1]||"").split("#"); // se ci sono anchors le tolgo..
	sUrl = "xmlResponse.asp?"+sQsData[0];
	var ajax = setXMLHttpRequest();
	if(ajax){
		var timeID = setTimeout("showLightbox();",200);
		ajax.open("post",sUrl,true);
		ajax.onreadystatechange = function(){
			if(ajax.readyState==4){
				var showResponseHTML;
				var oResponse = document.createElement("div");
					oResponse.innerHTML = (ajax.responseText||"").replace(/<responseRoot>/,"").replace(/<\/responseRoot>/,"");
				
				if(oResponse.firstChild && oResponse.firstChild.getAttribute("id")=="response_ajax" )
					showResponseHTML = oResponse.firstChild.innerHTML;
				
				// Sostituisce gli elementi in pagina
				writeAjaxResponse(oResponse);
				
				if(showResponseHTML)
					showLightbox(null,'<div id="response_ajax">'+oResponse.firstChild.innerHTML+'</div>');
				else
					hideLightbox();	
				
				clearTimeout(timeID);
			}
		}
		ajax.setRequestHeader("content-type", "application/x-www-form-urlencoded");
		ajax.setRequestHeader("charset", "iso-8859-1");
		ajax.setRequestHeader("connection", "close");
		ajax.send(sPostData);
	}
	return false;
}

function writeAjaxResponse(oResponse){
	if(oResponse){
		var nodesList = oResponse.childNodes;
		for(var i=0,o,id;i<nodesList.length;i++){
			if(nodesList[i] && nodesList[i].nodeType!=3){
				id = nodesList[i].getAttribute("id");
				if(id){
					o = document.getElementById(id);
					if(o)
						o.parentNode.replaceChild(nodesList[i],o);
				}
			}
		}
	}
}


function initAjaxMenus(){
	var ajxElements = ["request_products_multi_box"];
		ajxElements.push("request_sheet_product_multi_box");
		ajxElements.push("request_products_filter_categories_box_recentProducts");
		ajxElements.push("request_products_filter_categories_box_recentProducts2");
		ajxElements.push("request_products_filter_categories_box_preorderProducts");
		ajxElements.push("request_products_filter_categories_box_preorderProducts2");
	for(var i=0,j=0,conteiner,aList;i<ajxElements.length;i++){
		conteiner = document.getElementById(ajxElements[i]);
		if(conteiner){
			aList = conteiner.getElementsByTagName("a");
			if(aList && aList.length){
				for(j=0;j<aList.length;j++)
					aList[j].onclick = function(){
											var conteiner = this.parentNode.parentNode;
											var tempList = conteiner.childNodes;
											for(var i=0;i<tempList.length;i++)
												tempList[i].className = "";
											this.parentNode.className = "current";
											return ajaxRequest(this.href);
										}
			}
		}
	}
}

function sendData(frm){
	if(frm){
		var a = [];
		for(var i=0;i<frm.elements.length;i++)
			a.push(frm.elements[i].name+"="+frm.elements[i].value);
		return ajaxRequest(frm.action,a.join("&"));
	}
}

// run initLightbox onLoad
addLoadEvent(initAjaxMenus);	