var Ajax = function(){
	
}
Ajax.prototype.doPost = function(url,redirectUrl,callback,params){
	if (window.XMLHttpRequest) {
		var xmlhr = new XMLHttpRequest();
	} else {
		var xmlhr = new ActiveXObject('MSXML2.XMLHTTP.3.0');
	}
	xmlhr.open('POST', url, true);
	xmlhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); 

	xmlhr.onreadystatechange = function() {
		if (xmlhr.readyState == 1) {
			
		} else if (xmlhr.readyState == 4 && xmlhr.status == 200) {
			if(callback){
				if(xmlhr.responseText == "ok"){
					location=redirectUrl;
				} else if(xmlhr.responseText == "<span style=\"color:yellow;\">e-mail inviata</span>"){
					document.forms["contattaci"].reset();
					callback(xmlhr.responseText);
				} else if(xmlhr.responseText == "<span style=\"color:yellow;\">PM inviato</span>"){
					document.forms["pm"].reset();
					callback(xmlhr.responseText);
				} else {
					callback(xmlhr.responseText);
				}
			}
		} else if (xmlhr.readyState == 4) {
			if(false && log)
				log('Invalid response received - Status: ' + xmlhr.status);
			else
				alert('Invalid response received - Status: ' + xmlhr.status);
		}
	}
	xmlhr.send(params);
}
function getTop(s){
	if(s.offsetParent){
		return s.offsetTop + getTop(s.offsetParent);
	}
	return s.offsetTop;
}
function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
	}
	return [curleft,curtop];
}
function overallOffsetLeft(ele){
	var offset = 0;
	while( ele.tagName.toUpperCase() != 'BODY'){
		ele = ele.parentNode;
		offset += ele.offsetLeft;
	}
	return offset;
}
function getLeft(s){
	return s.offsetLeft + s.offsetParent.offsetLeft;
	if(s.offsetParent){
		return s.offsetLeft + getLeft(s.offsetParent);
	}
	return s.offsetLeft;
}
Ajax.prototype.autocompleter = function(searchFieldId, suggestDivId, url){
	var s = document.getElementById(searchFieldId);
	var d = document.getElementById(suggestDivId);
	if(!d || !s){
		alert("non ci sono");
		return;
	}
	d.style.position = "absolute";
//	d.style.left = overallOffsetLeft(s);
//	d.style.top = findPos(s)[1];

	s.onblur = function(){d.style.display = "none";};
	s.onfocus = function(){d.style.display = "inline";};
	s.onkeyup = function(){
		var k = event.keyCode;
		var refresh		= true;
		var selected	= false;
		if(k==8){
			//Backspace
			refresh = true;
		}else if(k==40){
			//Freccia giu
			if(d.children[0].children){
				// Se ci sono elementi nella suggest list
				if(d.children[0].children[Ajax.prototype.selectedIndex])
					d.children[0].children[Ajax.prototype.selectedIndex].className = "";
				if(Ajax.prototype.selectedIndex+1 >= d.children[0].children.length){
					Ajax.prototype.selectedIndex = -1;
				}
				d.children[0].children[++Ajax.prototype.selectedIndex].className = "selected";
				s.value = d.children[0].children[Ajax.prototype.selectedIndex].innerText;
				refresh = false;
			}
		}else if(k==38){
			//Freccia su
			if(d.children[0].children){
				// Se ci sono elementi nella suggest list
				if(d.children[0].children[Ajax.prototype.selectedIndex])
					d.children[0].children[Ajax.prototype.selectedIndex].className = "";
				if(Ajax.prototype.selectedIndex-1 < 0){
					Ajax.prototype.selectedIndex = d.children[0].children.length;
				}
				d.children[0].children[--Ajax.prototype.selectedIndex].className = "selected";
				s.value = d.children[0].children[Ajax.prototype.selectedIndex].innerText;
				refresh = false;
			}
		}
		if(refresh){
			Ajax.prototype.selectedIndex = -1;
			Ajax.prototype.doPost(url + "?search=" + s.value,function(data){
				d.innerHTML = data;
				d.style.width = s.offsetWidth;
				for(var i=0;i<d.children[0].children.length;i++){
					d.children[0].children[i].onclick = function(){alert(s.value); s.value=event.srcElement.innerText;};
				}
			});
		}
	}
};
