var idxSel = -1;
var visibleSel = false;

var prepareForm = function(){
	var oInput = document.getElementById(_sIdInput);
	var oSubmit = document.getElementById(_sIdSubmit);
	_sPrevAction = document.getElementById(_sFormName).action;
	if (oInput) {
		oInput.setAttribute("autocomplete", "off");
		if (oInput.value == '' && _sValueInput != '') {
			oInput.value=_sValueInput;
			oInput.style.fontStyle='italic';
			oInput.style.color="#D1D1D1";
			oInput.style.fontWeight='normal';
		}else if (oInput.value != '') {
			oInput.style.color="#888";
			oInput.style.fontWeight='bold';
		}
		oInput.onfocus = oInput.onblur = function(){cleanInput(oInput);}
		if (_hasXhr) {
			//oInput.onkeydown = function(ev){checkValue(ev);}
			oInput.onkeydown = function(ev){scrollSelect(ev);}
			oInput.onkeyup = function(ev){checkValue(ev);}
			oInput.onkeypress = function(){setPrevValue(oInput);}
		}
	}
	if (oSubmit) {
		oSubmit.onclick = function(){return checkInputValue();}
	}

}

var cleanInput = function(oInput){
	if (oInput.value == _sValueInput){
		oInput.value = "";
		oInput.style.color="#888";
		oInput.style.fontStyle='';
		oInput.style.fontWeight='bold';
	}else if (oInput.value == "") {
		oInput.value = _sValueInput;
		oInput.style.color="#D1D1D1";
		oInput.style.fontStyle='italic';
		oInput.style.fontWeight='normal';
	}
	hideSelect();
}

var checkInputValue = function(){
	var oInput = document.getElementById(_sIdInput);
	if (oInput.value == _sValueInput){
		alert('Attenzione: '+_sValueInput);
		return false;
	}
	if (oForm = document.getElementById(_sFormName)){
		setCookieSearchType();
		oForm.submit();
	}
	return false;
}

var setPrevValue = function(oInput){
	document.getElementById(_sFormName).action = _sPrevAction;
	if (oInput.value == _sValueInput){
		//alert('Attenzione: '+_sValueInput);
		return false;
	}
	_sPrevValue = oInput.value;
}

var checkValue = function (ev) {
	var oInput = document.getElementById(_sIdInput);
	if(oInput && oInput.value && oInput.value != _sPrevValue && oInput.value.length >= _nMinChar) {
		setPrevValue(oInput);
		changeButton('Cerca');
		dojo.xhrPost({
			url: "/requests/searchName.php",
			handleAs: "text",
			postData: createQS(oInput),
			handle: function(response,args){
				if(typeof response == "error"){
					console.warn("error!");
					console.log(args);
				}
				createSelect(response);
			}
		});
	}else if (visibleSel && oInput && oInput.value.length >= _nMinChar) {
		navigateSelect(ev);
	}else {
		hideSelect();
	}
	return false;
};

var createSelect = function(response) {
	if (typeof _sIdSelect != "undefined") {
		var oDivCont = document.getElementById(_sIdSelect);
		oDivCont.innerHTML = response;
		oDivCont.style.display = 'block';
		visibleSel = true;
		idxSel = -1;
	}
}

var selectChoise = function(sIdSelected) {
	var oInput = document.getElementById(_sIdInput);
	var oValue = document.getElementById(sIdSelected);
	if (oInput && oValue) {
		oChilds = oValue.childNodes;
		nItems = oChilds.length
		for (i = 0; i < nItems; i++) {
			if (oChilds[i] && typeof oChilds[i].id != 'undefined') {
				if (oChilds[i].id.indexOf('url_') >= 0 && oChilds[i].id != ''){
					setCookieSearchType();
					window.location = oChilds[i].value;
					break;
				}
			}
		}
		hideSelect();
		oInput.focus();
	}
}

var hideSelect = function() {
	var oDivCont = document.getElementById(_sIdSelect);
	if (oDivCont) {
		setTimeout(function(){oDivCont.style.display = 'none';}, 500);
		visibleSel = false;
		idxSel = -1;
	}
}

var navigateSelect_old = function(id) {
	if (idxSel == '') {
		idxSel = -1;
	}else {
		idxSel++;
	}
	selectRowItem(idxSel);
}

var selectNextRowItem = function () {
	selectRowItem(idxSel+1);
}

var selectPrevRowItem = function () {
	selectRowItem(idxSel-1);
}

var selectRowItem = function (idxNew) {
	var oUl = document.getElementById('ul_suggest');
	if (oUl){
		var nElement = oUl.childNodes.length;
		if (idxSel >= 0 && oUl.childNodes[idxSel])
			oUl.childNodes[idxSel].className = '';
		if (idxNew <= 0) {
			idxSel = 0;
		}else if (idxNew >= nElement-1) {
			idxSel = nElement-1;
		}else {
			idxSel = idxNew;
		}
		oUl.childNodes[idxSel].className = 'selected';
	}
}

var navigateSelect = function(ev) {
	var key = (window.event) ? window.event.keyCode : ev.keyCode;
	var RETURN = 13;
	var TAB = 9;
	var ESC = 27;
	switch(key) {
		case RETURN:
			setHighlightedValue();
			break;
		case ESC:
			hideSelect();
			break;
	}
}

var scrollSelect = function (ev) {
	var key = (window.event) ? window.event.keyCode : ev.keyCode;
	var ARRUP = 38;
	var ARRDN = 40;
	var TAB = 9;
	switch(key) {
		case ARRUP:
			selectPrevRowItem();
			break;
		case ARRDN:
			selectNextRowItem();
			break;
		case TAB:
			setHighlightedValue();
			break;
	}
}

var setCookieSearchType = function() {
	if (_sCat) {
		document.cookie="last_search_"+_sCat+"=name";
	}
	
}

var changeButton = function(sValue) {
	var oSubmit = document.getElementById(_sIdSubmit);
	if(oSubmit){
		oSubmit.innerHTML = '<div class="text_button">' + sValue +'</div>';
	}
}

var createQS = function (oInput){
	var qsa = '';
	if (document.getElementById('provincia_n')) {
		oProv = document.getElementById('provincia_n')
		qsa += '&pr=' + oProv.options[oProv.selectedIndex].value;
	}
	if (document.getElementById('comune_n')) {
		oCom = document.getElementById('comune_n')
		qsa += '&cy=' + oCom.options[oCom.selectedIndex].value;
	}
	return "c="+_sCat+"&t=h&input="+oInput.value+qsa;
}

var setHighlightedValue = function () {
	var oUl = document.getElementById('ul_suggest');
	if (oUl){
		oLiSel = oUl.childNodes[idxSel].childNodes;
		nItems = oLiSel.length
		for (i = 0; i < nItems; i++) {
			if (oLiSel[i].id && oLiSel[i].id != '' && oLiSel[i].id.indexOf('s_i_') >= 0){
				selectChoise(oLiSel[i].id);
			}
		}
	}
}











