var http_request = false;
var last_query = '';

var pid = '1'; 
var res = '-1'; 
var tab = ''; 
var pru = 'www.studero.de/'; 
var uid = '0'; 
var paid = '1'; 
var spi = '3'; 


//trimming function
function ajax_trim(_string) {
	while(_string.substring(0, 1) == ' ') {
		_string = _string.substring(1, _string.length);
	}
	
	while(_string.substring(_string.length - 1, _string.length) == ' ')	{
		_string = _string.substring(0, _string.length - 1);
	}
	
	return _string;
}



//main function to call out of html
function ajax_search(_query) {
  if (document.getElementById){
    document.getElementById("ajax_result").style.display = 'inline';
    document.getElementById('ajax_result').style.visibility = 'visible';
}

	_query = ajax_trim(_query);
	if(_query != last_query && _query != '') {
		//show the spinner
		if(spi != 1) document.getElementById('ajax_spinner').style.display = 'inline';
		
		//set last query
		last_query = _query;
		
		//prepare url
		//var prujs = (document.location.href.indexOf('index.php') > 0) ? '' : (document.location.protocol+'//'+pru);
		//var queryurl = 'http://www.studero.de/ajax_search.php?be='+_query+'&pid='+pid+'&res='+res+'&uid='+uid+'&tab='+tab+'&paid='+paid;
		var queryurl = 'http://www.studero.de/ajax_search.php?be='+_query;
		
		//start request
		ajax_request(queryurl);
	}
}

function ajax_search_mail(_query, _user) {
  if (document.getElementById){
    document.getElementById("ajax_result").style.display = 'inline';
    document.getElementById('ajax_result').style.visibility = 'visible';
}

	_query = ajax_trim(_query);
	if(_query != last_query && _query != '') {
		//show the spinner
		if(spi != 1) document.getElementById('ajax_spinner').style.display = 'inline';
		
		//set last query
		last_query = _query;
		
		//prepare url
		//var prujs = (document.location.href.indexOf('index.php') > 0) ? '' : (document.location.protocol+'//'+pru);
		//var queryurl = 'http://www.studero.de/ajax_search.php?be='+_query+'&pid='+pid+'&res='+res+'&uid='+uid+'&tab='+tab+'&paid='+paid;
		var queryurl = 'http://www.studero.de/ajax_search_mail.php?be='+_query+'&uid='+_user;
		
		//start request
		ajax_request(queryurl);
	}
}



//check return of the request
function ajax_return() {
	if (http_request.readyState == 4) {
		if(http_request.status == 200) {
			ajax_output(http_request.responseText);
		}
	}
}


//output something to the user
function ajax_output(_data) {
	//output the whole thing
	document.getElementById('ajax_result').innerHTML = _data;
	
	//hide the spinner
	if(spi != 1) document.getElementById('ajax_spinner').style.display = 'none';
}



//the request in an extra function
function ajax_recopen(_url) {
	//open requester
	http_request.open('GET', _url, true);
	
	//no caching please
	http_request.setRequestHeader('If-Modified-Since', 'Wed, 15 Nov 1995 00:00:00 GMT');
	http_request.setRequestHeader('Cache-Control', 'no-cache');
	
	//what to do on return
	http_request.onreadystatechange = ajax_return;
	
	//needed but what for?
	http_request.send(null);
}




//do the request
function ajax_request(_url) {
	if (window.XMLHttpRequest) { // Mozilla, Safari,...
        http_request = new XMLHttpRequest();
    } else if (window.ActiveXObject) { // IE
        try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP.4.0");
        } catch (e) {
            try {
                http_request = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    http_request = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {}
            }
        }
    }

    if (!http_request) {
        alert('Aborted :( Cannot generate XMLHTTP-Instance');
        return false;
    }
    
	try {
		ajax_recopen(_url);
	} catch(e) {
		try {
			//no rights? lets try it that way ...
			netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
			ajax_recopen(_url);
		} catch (e) {
			alert(e);
		}
	}
}


//--------------------------------------------
//main function to call out of html
function set(_query) {
  if (document.getElementById){
    document.getElementById("ajax_result").style.display = 'none';
    document.getElementById('ajax_result').style.visibility = 'hidden';
    document.businessdata.be.value = _query;
}
}
