// 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,bHideLightBox,endCallFunction){
	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 = null;
		if( !bHideLightBox )
			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 if(timeID){
					hideLightbox();	
					clearTimeout(timeID);
				}
				
				if( endCallFunction )
					eval(endCallFunction);
			}
		}
		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_lastArrivalsProducts");
		ajxElements.push("request_products_filter_categories_box_lastArrivalsProducts2");
		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(){
											clearTimeout( window["solvingProd_time_interval"] );
											// ^^^^^ fermo l'animazione se esiste
											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("&"));
	}
}

function ajaxCallSolvingProduct(){
	solvingBoxProducts(null,0,"+");
}
function solvingBoxProducts(params,opacIndex,opacType){
	if( params!=undefined )
		window["solvingProd_params"] = params;
	if( opacIndex!=undefined )
		window["solvingProd_opacIndex"] = opacIndex;
	if( opacType!=undefined )
		window["solvingProd_opacType"] = opacType;
	
	eval('window["solvingProd_opacIndex"] = window["solvingProd_opacIndex"] ' + window["solvingProd_opacType"] +  ' 10;');
	opacType = window["solvingProd_opacType"];
	opacIndex = window["solvingProd_opacIndex"];
	params = window["solvingProd_params"];
	
	window["solvingProd_time_interval"] = setTimeout("solvingBoxProducts();",10);
	changeOpac( opacIndex,null,document.getElementById('products_multi_box_content') );
	if( opacIndex<=0 ){
		clearTimeout( window["solvingProd_time_interval"] );
		ajaxRequest(params,null,true,"ajaxCallSolvingProduct();");
	}else if(opacIndex>=100){
		clearTimeout( window["solvingProd_time_interval"] );
		window["solvingProd_time_interval"] = setTimeout("displayBoxPresentation();",5000);
	}
}
function displayBoxPresentation(){
	var oConteiner = document.getElementById("request_products_multi_box");
	if(oConteiner){
		var aList = oConteiner.getElementsByTagName("li");
		// la prima volta recupero l'indice del link selezionato di default
		if( window["solvingProd_linkIndex"]==undefined ){
			for(var j=0;j<aList.length;j++){
				if( aList[j].className=="current" ){
					window["solvingProd_linkIndex"] = j;
					break;
				}
			}
		}
		var index = window["solvingProd_linkIndex"];
		oLI = aList[index];
		oLI.className = "";
		if( (index+1) >= aList.length )
			index = -1;
		index = index+1;
		window["solvingProd_linkIndex"] = index;
		oLI = aList[index];
		if(oLI){
			oLI.className = "current";
			solvingBoxProducts( oLI.getElementsByTagName("a")[0].href,100,"-" );
		}
	}
}

// run initLightbox onLoad
addLoadEvent(initAjaxMenus);





