/****************************************************************
** File per l gestione delle richieste e risposte con Ajax
**		contiene anche tutte le variabili che servono per gestire la ricerca con Ajax
****************************************************************/


/*****************************************
variabili per la ricerca
****************************************
var currentePage="1";
var tipoRicerca="semplice";
var TxtNRicerca="";
var codAutore="";
var codCollana="0";
var codPromozione="";
var TxtNTitolo="";
var TxtNAutore="";
var SelNGenere="";
var SelNSottoGenere="";
var TxtNEditore="";
var TxtNCollana="";
var TxtNAnnoPubbDa="";
var TxtNAnnoPubbA="";
var TxtNCodiceEAN="";
var SelNPrezzo="";
var SelNRaggruppamento="";
var SelNOrdinamento=""; 
var SelNNumRisultati="";
var testoCercato="";
*/
/*****************************************
parte ajax
*****************************************/
var xmlHttp;
var xmlHttp2;
var xmlHttp3;
var xmlHttpHome;
var esecuzione;

function createXMLHttpRequest() {
	if (window.ActiveXObject) {
		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	} else if (window.XMLHttpRequest) {
		xmlHttp = new XMLHttpRequest();
	}
}


function createXMLHttpRequest2() {
	if (window.ActiveXObject) {
		xmlHttp2 = new ActiveXObject("Microsoft.XMLHTTP");
	} else if (window.XMLHttpRequest) {
		xmlHttp2 = new XMLHttpRequest();
	}
}

function createXMLHttpRequest3() {
	if (window.ActiveXObject) {
		xmlHttp3 = new ActiveXObject("Microsoft.XMLHTTP");
	} else if (window.XMLHttpRequest) {
		xmlHttp3 = new XMLHttpRequest();
	}
}

function createXMLHttpRequestHome() {
	if (window.ActiveXObject) {
		xmlHttpHome = new ActiveXObject("Microsoft.XMLHTTP");
	} else if (window.XMLHttpRequest) {
		xmlHttpHome = new XMLHttpRequest();
	}
}

//funzione che inizializza la chiamata ajax al server
//	nomePagina  	indica la pagina da chiamare
//	tipoRisposta 	indica cosa deve fare con la risposta, se nulla, se un innerHTML ecc...
//					=1  non fa niente
//					=2  esegue un innerHTML
//	idOggetto		indica l'id dell'oggetto html dove deve andare a scrivere
function startRequest(nomePagina, tipoRisposta, idOggetto) {
	createXMLHttpRequest();
	xmlHttp.onreadystatechange = handleStateChangeSenzaRisposta;
	xmlHttp.open("GET", "/script/ajax/" + nomePagina, true);
	xmlHttp.send(null);
}

function creaBoxHome(nomePagina, tipoRisposta, idOggetto) {
	switch (idOggetto) {
		case "boxTopTenLibri":
			createXMLHttpRequest();
			xmlHttp.onreadystatechange = hscBoxTopTenLibri;
			xmlHttp.open("GET", "/script/ajax/" + nomePagina, true);
			xmlHttp.send(null);
			break;
		case "boxGenereLibri":
			createXMLHttpRequest2();
			xmlHttp2.onreadystatechange = hscBoxGenereLibri;
			xmlHttp2.open("GET", "/script/ajax/" + nomePagina, true);
			xmlHttp2.send(null);
			break;
		case "boxMarchiGiochi":
			createXMLHttpRequest3();
			xmlHttp3.onreadystatechange = hscBoxMarchiGiochi;
			xmlHttp3.open("GET", "/script/ajax/" + nomePagina, true);
			xmlHttp3.send(null);
			break;
	}	
	
}

function creaVetrinaProdotti()  {
	createXMLHttpRequestHome();
	xmlHttpHome.onreadystatechange = hscVetrinaProdotti;
	xmlHttpHome.open("GET", "/script/ajax/vetrinaProdotti.asp", true);
	xmlHttpHome.send(null);
}

//Scrive dentro il box della top ten dei libri
function hscBoxTopTenLibri() {
	if (xmlHttp.readyState == 4) {
		if (xmlHttp.status == 200) {
			document.getElementById("boxTopTenLibri").innerHTML=xmlHttp.responseText;
		}
	}
}

function hscBoxGenereLibri() {
	if (xmlHttp2.readyState == 4) {
		if (xmlHttp2.status == 200) {
			document.getElementById("boxGenereLibri").innerHTML=xmlHttp2.responseText;
		}
	}
}

function hscBoxMarchiGiochi() {
	if (xmlHttp3.readyState == 4) {
		if (xmlHttp3.status == 200) {
			document.getElementById("boxMarchiGiochi").innerHTML=xmlHttp3.responseText;
		}
	}
}

function hscVetrinaProdotti() {
	if (xmlHttpHome.readyState == 4) {
		if (xmlHttpHome.status == 200) {
			document.getElementById("idVetrinaProdotti").innerHTML=xmlHttpHome.responseText;
		}
	}
}

function handleStateChange() {
	if (xmlHttp.readyState == 4) {
		if (xmlHttp.status == 200) {
			alert(xmlHttp.responseText);
		}
	}
}

function handleStateChangeSenzaRisposta() {
	if (xmlHttp.readyState == 4) {
		if (xmlHttp.status == 200) {
			esecuzione=xmlHttp.responseText;
		}
	}
}

function caricaRisultatiRicerca(queryString) {
	createXMLHttpRequestHome();
	xmlHttpHome.onreadystatechange = hscRisultatiRicerca;
	xmlHttpHome.open("GET", "/script/ajax/risultatiRicerca.asp?" + queryString, true);	
	xmlHttpHome.send(null);
}

function generaRisultatiRicerca(queryString) {
	createXMLHttpRequestHome();
	//xmlHttpHome.onreadystatechange = hscRisultatiRicerca;
	xmlHttpHome.open("GET", "/script/ajax/risultatiRicerca.asp?" + queryString, true);	
	xmlHttpHome.send(null);
}

function caricaRisultatiRicercaOld(queryString) {
	createXMLHttpRequestHome();
	xmlHttpHome.onreadystatechange = hscRisultatiRicerca;
	xmlHttpHome.open("GET", "/script/ajax/risultatiRicerca.asp?" + queryString, true);	
	xmlHttpHome.send(null);
	/*xmlHttpHome.open("POST", "/script/ajax/risultatiRicerca.asp", true);
	xmlHttpHome.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");
	xmlHttpHome.send(queryString);*/
}

function hscRisultatiRicerca() {
	if (xmlHttpHome.readyState == 4) {
		if (xmlHttpHome.status == 200) {
			document.getElementById("idVetrinaProdotti").innerHTML=xmlHttpHome.responseText;
			if (document.getElementById("titoloPagina")) {				
				document.title=document.getElementById("titoloPagina").value;
			}
		}
	}
}


