// GLOBAL
var searchString;
var p_timer;

var xhrObj;

function XHRObj() {
	var xhr = null;
	try {
		xhr = new XMLHttpRequest();
	} catch(ms) {
		try {
			xhr = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (nonms) {
			try {
				xhr = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (failed) {
				xhr = null;
			}
		}			
	}
	return xhr;
}
/************************************************/
function handleXHRResponse() {
	if (xhrObj.readyState == 4 && xhrObj.status == 200) {
		xhrJsonResult = json_parse(xhrObj.responseText);
	
		for(i in xhrJsonResult) {
			switch(i) {
				case 'doSearchResp': doSearchResp(xhrJsonResult[i]); break;
			}
		}
	}
}
/************************************************/
function doSearchReq(s) {
	if(s != '') {
		searchString = s;
		xhrObj = XHRObj();
		
		xhrObj.multipart = true;
		
		xhrObj.open("POST","doSearch/");
		xhrObj.setRequestHeader("If-Modified-Since", true);
		xhrObj.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		
		xhrObj.onreadystatechange = handleXHRResponse;
		
		// send
		xhrObj.send("s="+s);
	} else {
		clearTimeout(p_timer);
		p_timer = setTimeout('hideElement(\'searchSuggestBox\')', 500);
	}
}
function doSearchResp(data) {
	var p_ident = '';
	var p = '';
	var c = 0;
	var suggestBox = document.getElementById('searchSuggestBox');
	suggestBox.innerHTML = '';

	for(i in data) {
		if(i != p_ident) {
			switch(i) {
				case "dsl": p += '<br/><strong>DSL-Provider:</strong><br/>'; break;
				case "mobil": p += '<br/><strong>Mobile-Provider:</strong><br/>'; break;
			}
			p_ident = i;
		}
		
		for(j in data[i]) {
			p += '<a href="/'+i+'/'+j+'/" >'+data[i][j]+'</a><br/>';
		}
		c++;
	}

	if(c == 0) {
		suggestBox.innerHTML += 'die Suche nach \'<strong>'+searchString+'</strong>\' ergabe keine Treffer';
		suggestBox.setAttribute("style", "visibility: visible;");
		if(checkBrowserName('MSIE'))
			suggestBox.style.setAttribute("cssText", "visibility: visible;");
		clearTimeout(p_timer);
		p_timer = setTimeout('hideElement(\'searchSuggestBox\')', 7500);
	} else {
		suggestBox.innerHTML += 'die Suche nach \'<strong>'+searchString+'</strong>\' ergabe folgende Treffer:<br/>';
		suggestBox.innerHTML += p;
		suggestBox.setAttribute("style", "visibility: visible;");
		if(checkBrowserName('MSIE'))
			suggestBox.style.setAttribute("cssText", "visibility: visible;");
		clearTimeout(p_timer);
		p_timer = setTimeout('hideElement(\'searchSuggestBox\')', 7500);
	}
	
}
/************************************************/
function hideElement(e) {
	var element = document.getElementById(e);
	element.setAttribute("style", "visibility: hidden;");
	if(checkBrowserName('MSIE'))
		element.style.setAttribute("cssText", "visibility: hidden;");
}
/************************************************/
function checkBrowserName(name){  
	var agent = navigator.userAgent.toLowerCase();  
	if (agent.indexOf(name.toLowerCase())>-1) {  
	  return true;  
	}  
	return false;
}
/************************************************/
function loadExtJS(url) {
	var s = document.createElement('script');
	s.setAttribute('type','text/javascript');
	s.setAttribute('src', url);
	document.getElementsByTagName('head')[0].appendChild(s);
}
/************************************************/
function loadJSContent(content) {
	var s = document.createElement('script');
	s.setAttribute('type','text/javascript'); 
	s.text = content;
	document.getElementsByTagName('head')[0].appendChild(s);
}



ttDiv = null;
_r = 0;

// Temporary variables to hold mouse x-y pos.s
var _left = 0;
var _top = 0;

function updateTooltip(e) {
	ttDiv = document.getElementById("tableTooltip");	// get Tooltip-DIV
	x = (document.all) ? window.event.x + ttDiv.offsetParent.scrollLeft : e.pageX;
	y = (document.all) ? window.event.y + ttDiv.offsetParent.scrollTop  : e.pageY;
	_left = (x + 20);
	_top =  (y - (50 + (10*_r)));
	ttDiv.style.left = _left + "px";
	ttDiv.style.top   = _top + "px";
}
function showTooltip(r, c) {
	ttDiv = document.getElementById("tableTooltip");	// get Tooltip-DIV
	ttDiv.innerHTML = '';
	ttDiv.innerHTML = unescape(c);
	_r = r;
	ttDiv.style.display = "block";
}
function hideTooltip() {
	ttDiv.innerHTML = '';
	ttDiv.style.display = "none";
}
