function showSugg(divID) {
	document.getElementById(divID).style.display = "block";
}

function setSearchSugg(title, divID) {
	var inputName;
	if (divID == 'searchSuggestion') {
		inputName = "search-term";
		hideSearchSugg();
	} else if (divID == 'titleSuggestion') {
		inputName = "form-title";
		hideTitleSugg();
	}
	document.getElementById(inputName).value = title;
	
	if (divID == 'titleSuggestion') {
		var str2 = document.getElementById('form-category_id').value;
		var str3 = document.getElementById('form-city_id').value;

		sndErrorReq(title, str2, str3);
	}
}

function hideSearchSugg() {
	document.getElementById('searchSuggestion').style.display = "none";
    document.getElementById('searchSuggestion').innerHTML = "";
}

function hideTitleSugg() {
	document.getElementById('titleSuggestion').style.display = "none";
    document.getElementById('titleSuggestion').innerHTML = "";
}

function hideError() {
	document.getElementById('errorCurrent').style.display = "none";
    document.getElementById('errorCurrent').innerHTML = "";
}

var xmlhttp
	/*@cc_on @*/
	/*@if (@_jscript_version >= 5)
	  try {
	  xmlhttp=new ActiveXObject("Msxml2.XMLHTTP")
	 } catch (e) {
	  try {
	    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
	  } catch (E) {
	   xmlhttp=false
	  }
	 }
	@else
	 xmlhttp=false
	@end @*/
	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
	 try {
	  xmlhttp = new XMLHttpRequest();
	 } catch (e) {
	  xmlhttp=false
	 }
	}
	function myXMLHttpRequest() {
	  var xmlhttplocal;
	  try {
	    xmlhttplocal= new ActiveXObject("Msxml2.XMLHTTP")
	 } catch (e) {
	  try {
	    xmlhttplocal= new ActiveXObject("Microsoft.XMLHTTP")
	  } catch (E) {
	    xmlhttplocal=false;
	  }
	 }

	if (!xmlhttplocal && typeof XMLHttpRequest!='undefined') {
	 try {
	  var xmlhttplocal = new XMLHttpRequest();
	 } catch (e) {
	  var xmlhttplocal=false;
	  alert('couldn\'t create xmlhttp object');
	 }
	}
	return(xmlhttplocal);
}

function sndReq(str, func, div2show) {
	str = encodeURIComponent(str);
	xmlhttp.open('get', 'http://dovolen.bg/ajax/suggestion_list/'+func+'/'+str+'/');
    xmlhttp.onreadystatechange = handleResponse;
	xmlhttp.send(null);	
}

function sndErrorReq(str1, str2, str3) {
	str1 = encodeURIComponent(str1);
	str2 = encodeURIComponent(str2);
	str3 = encodeURIComponent(str3);
	xmlhttp.open('get', 'http://dovolen.bg/ajax/object_title/'+str1+'/'+str2+'/'+str3);
    xmlhttp.onreadystatechange = handleResponse;
	xmlhttp.send(null);	
}

function handleResponse() {
  if(xmlhttp.readyState == 4){
		if (xmlhttp.status == 200){
        var response = xmlhttp.responseText;
        var update = new Array();

        if(response.indexOf('|') != -1) {
            update = response.split('|');
			update[1] = update[1].replace(/searchSuggestion$/,"");
			update[1] = update[1].replace(/titleSuggestion$/,"");
			update[1] = update[1].replace(/errorCurrent$/,"");
			update[1] = update[1].replace(/^<ul><\/ul>$/,"");
			if (update[1] != '') {
				showSugg(update[0]);
				changeText(update[0], update[1]);
			}
        }
		}
		
    }
}

function changeText( div2show, text ) {
    // Detect Browser
    var IE = (document.all) ? 1 : 0;
    var DOM = 0; 
    if (parseInt(navigator.appVersion) >=5) {DOM=1};

    // Grab the content from the requested "div" and show it in the "container"
    if (DOM) {
        var viewer = document.getElementById(div2show);
        viewer.innerHTML = text;
    }  else if(IE) {
        document.all[div2show].innerHTML = text;
    }
}

/* =============================================================== */
var suggestionListAction = {
		'input.search' : function(element){
			element.onkeyup = function(oEvent){
				if (!oEvent) {
					oEvent = window.event.keyCode;
			    }
			    hideSearchSugg();
				if(oEvent != 27) {
					var str = element.value;

					sndReq(str, 'setSearchSugg', 'searchSuggestion');
				}
				return false;
			},
			
			element.onblur = function(){
				setTimeout('hideSearchSugg()',150);
				return false;
			}
		},
		'input.title' : function(element){
			element.onkeyup = function(oEvent){
				if (!oEvent) {
					oEvent = window.event.keyCode;
				}
			    hideTitleSugg();
				if(oEvent != 27) {
					hideError();

					var str = element.value;
					
					sndReq(str, 'setTitleSugg', 'titleSuggestion');
				}
				return false;
			},

			element.onblur = function(){
				setTimeout('hideTitleSugg()',150);
				
				hideError();
				var str1 = element.value;
				var str2 = document.getElementById('form-category_id').value;
				var str3 = document.getElementById('form-city_id').value;

				sndErrorReq(str1, str2, str3);
				return false;
			}
		},
		'select.titleSelect' : function(element){
			element.onblur = function(){
				hideError();

				var str1 = document.getElementById('form-title').value;
				var str2 = document.getElementById('form-category_id').value;
				var str3 = document.getElementById('form-city_id').value;

				sndErrorReq(str1, str2, str3);
				return false;
			}
		}
	};
Behaviour.register(suggestionListAction);
