function getXMLHttp(){
	var xmlhttp = null;
	if (window.ActiveXObject) {
  		if (navigator.userAgent.toLowerCase().indexOf("msie 5") != -1) {
  			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
 		} else {
  			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
 		}
	}
	if (!xmlhttp && typeof(XMLHttpRequest) != 'undefined') {
 		xmlhttp = new XMLHttpRequest();
	}
	return xmlhttp;
}

function cmPHP(cosa,dove,opec,tipo,clink){
	//chiamata alla pagina php che inserisce i dati nelle tabelle click, tabella_visite e visitatori
	var id_query = document.getElementById("id_query").innerHTML;
	cosa = encodeURIComponent(cosa);
	var xmlhttp = getXMLHttp();
	var p = "_clickMonitor.php?cosa="+cosa+"&dove="+dove+"&clickMonitor=si&codiceOPEC="+opec+"&type="+tipo.split("_").join(" ")+"&clink="+clink+"&id_query="+id_query;
	xmlhttp.open("GET",p);
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4) {
			var rispostaHTTP=xmlhttp.responseText;
		}
	}  
	xmlhttp.send(null);	
}

function insertDB(ref) {
	var cosa = document.getElementById("cosa").value;
	if(cosa == "") cosa = document.getElementById("cosa0").innerHTML;
	var dove = document.getElementById("dove").value;
	if(dove == "") dove = document.getElementById("dove0").innerHTML;
	var post = "cosa=" + encodeURI( cosa ) +
	            "&dove=" + encodeURI( dove ) +
		     "&elapsedtime="     + document.getElementById("elapsedtime").innerHTML +                     
	             "&what=" + encodeURI( cosa ) +
	             "&nresults=" + document.getElementById("nresults").innerHTML +
	             "&categoryrow=" + document.getElementById("categoryrow").innerHTML +
	             "&row=" + document.getElementById("row").innerHTML +
	             "&codPUnum=" + document.getElementById("codPUnum").innerHTML +
	             "&nrescategory=" + document.getElementById("nrescategory").innerHTML +
	             "&rank=" + document.getElementById("rank").innerHTML +
	             "&codiceOPEC=" + document.getElementById("codiceOPEC").innerHTML +
	             "&referer=" + ref ;
	
	var xmlhttp = newXMLHttpRequest();
	
	if (xmlhttp!=null) {        
	    xmlhttp.open("POST","_extractIdQuery.php",true);
	    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	    xmlhttp.send(post);
	    xmlhttp.onreadystatechange = handleResponse(xmlhttp, manageIdQuery, ResponseType.html);      
	} else {
	    alert("Il tuo browser non supporta XMLHttpRequest");
	}
}
    
    function manageIdQuery(XMLResult){
    	document.getElementById("id_query").innerHTML = XMLResult;
    }