var req;
var suggest_field_focus = 0;
var suggest_out_focus = 0;
var suggest_blur_delayed = 0;
var timeout_input = 0;
var timeout_blur = 0;

function loadXMLDoc(url) 
{
   // branch for native XMLHttpRequest object
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = processReqChange;
        req.open("get", url, true);
        req.send(null);
    // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = processReqChange;
            req.open("get", url, true);
            req.send();
        }
    }
	xml_loading = 1;
}

function processReqChange() 
{

	// only if req shows "complete"
	if (req.readyState == 4) {
	// only if "OK"
		if (req.status == 200) {
			var response  = req.responseXML;
			if (document.all) {
				var out_div=document.all[suggest_output_name];
				var in_form=document.forms[suggest_form_index];
			} else {
				var out_div=document.getElementById(suggest_output_name);
				var in_form=document.forms[suggest_form_index];
			}
			text = document.createElement("div");
			text.setAttribute("style","margin: 5px;");
 			text.appendChild(document.createTextNode("Treffer: "+response.getElementsByTagName('keyword').length));
			text.appendChild(document.createElement("br"));
			for (i=0;i<response.getElementsByTagName('keyword').length;i++) {
				link_text  = "/suggest/show_"+in_form.session.value+"_"+in_form.user.value+"_";
				link_text += in_form.entity.value+"_"+in_form.lang.value+"_74_"
				link_text += response.getElementsByTagName('key_id')[i].firstChild.data+"_22.html";
				link = document.createElement("a");
				link.onclick = function() {suggest_out_focus=1;return 1;};
				link.setAttribute("href",link_text);
				link.setAttribute("target","_top");
				link.appendChild(document.createTextNode(" "+response.getElementsByTagName('keyword')[i].firstChild.data));
				link.appendChild(document.createTextNode("  ("));
				link.appendChild(document.createTextNode(response.getElementsByTagName('count')[i].firstChild.data+" Produkte) "));
				text.appendChild(link);
				text.appendChild(document.createElement("br"));
			}
			if (response.getElementsByTagName('keyword').length > 0) {
				out_div.style.color = "black";
				out_div.style.visibility = "visible";
			} else {
				out_div.style.visibility = "hidden";
			}
			if (out_div.hasChildNodes()) out_div.removeChild(out_div.firstChild);
			out_div.appendChild(text);
		}
	} else {
		if (document.all) {
			var out_div=document.all[suggest_output_name];
		} else {
			var out_div=document.getElementById(suggest_output_name);
		}
		text = document.createElement("div");
		text.appendChild(document.createTextNode("Moment bitte ..."));
		text.appendChild(document.createElement("br"));
		out_div.style.visibility = "visible";
		out_div.style.color = "red";
		if (out_div.hasChildNodes()) out_div.removeChild(out_div.firstChild);
		out_div.appendChild(text);
	}
}

function start_suggest(input) {
		url  = suggest_script_url + escape(input);
		loadXMLDoc(url);
}

function suggest(input) {
	if (((input.length > 2) && (input.toLowerCase() != 'sch')) || (input.length > 3)) {
		if (timeout_input) window.clearTimeout(timeout_input);
		timeout_input = window.setTimeout("start_suggest('"+input+"')",300);
	}
}

function suggest_delay_blur(output) {
	if (suggest_blur_delayed != 1) {
		window.setTimeout("suggest_delay_blur('"+output+"')",1000);
		suggest_blur_delayed=1;
	} else {
		if (suggest_out_focus != 1) {
			suggest_field_focus=0;
			document.all[output].style.visibility='hidden';
		};
		suggest_blur_delayed=0;
	}
}

