/*
AJAX Request
© 2009 BENRIC Mediengestaltung
*/

var searchTimeout;
var lastQuery;
var minSearchStringLength = 2;

String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g, "");
};

function sendLiveRequest(lang) {
	document.getElementById('searchresult').style.display = "none";
	window.clearTimeout(searchTimeout);
	searchTimeout = window.setTimeout("clearSearchString(document.getElementById('searchoption').value, '"+lang+"')", 1);
}

function clearSearchString( q, lang ) {
	document.getElementById('searchresult').style.display = "none";
	var q = q.trim();  // entfernt irrelevante Leerzeichen am Anfang und Ende
	if( q.length >= minSearchStringLength && lastQuery != q ) {
		requestLivesearch(q, lang);
	}	
	lastQuery = q;
}

function requestLivesearch(value, lang){
 var xmlHttp = null;
 try {
  // Mozilla, Opera, Safari sowie Internet Explorer (ab v7)
  xmlHttp = new XMLHttpRequest();
 }catch(e){
  try{
   // MS Internet Explorer (ab v6)
   xmlHttp  = new ActiveXObject("Microsoft.XMLHTTP");
  }catch(e){
   try{
    // MS Internet Explorer (ab v5)
    xmlHttp  = new ActiveXObject("Msxml2.XMLHTTP");
   }catch(e){
    xmlHttp  = null;
   }
  }
 }
 
 if(xmlHttp){
  xmlHttp.open('POST', '/module/mod_search/livesearch.script.php');
  xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  
  document.getElementById('searchresult').style.display = "none";
  
  xmlHttp.onreadystatechange = function() {
   if(xmlHttp.readyState != 4){
	document.getElementById('searchresult').innerHTML = '<img src="/style/images/layout/ajax-loader.gif" align="center" style="margin-top:15px;" alt="ladebalken" />';
   }else if ((xmlHttp.readyState == 4) && (xmlHttp.status == 200)) {
	document.getElementById('searchresult').style.display = "block";
	document.getElementById('searchresult').innerHTML = xmlHttp.responseText;
   }else{
    document.getElementById('searchresult').innerHTML = '<img src="/style/images/layout/ajax-loader.gif" align="center" style="margin-top:15px;" alt="ladebalken" />';
   }
  };
  xmlHttp.send('value='+value +'&lang='+lang);
 }
}

function showResults(id){
 var img = 'img_' + id;
 if(document.getElementById('search_'+id).style.display == 'none'){
  document.getElementById('search_'+id).style.display = 'block';
  document.getElementById(img).src = '/style/images/layout/pfeil_und.jpg'; // Grafik referenzieren
 }
 else
 {
  document.getElementById('search_'+id).style.display = 'none';
  document.getElementById(img).src = '/style/images/layout/pfeil_right.jpg'; // Grafik referenzieren
 }


}


