var xmlhttp_fe_suche = null;

var objsearch = new Object();
objsearch['action'] = '';
objsearch['idartikel'] = new Array();
objsearch['currentpage'] = 1;
objsearch['currentstring'] = '';
objsearch['maxoutput'] = 0;
objsearch['matchcode'] = '';
objsearch['idsprache'] = 1;
objsearch['search_ele'] = false;
objsearch['search_form'] = null;
objsearch['divproposallist'] = null;
objsearch['lastsearchhistory'] = new Array();

function lastsearch(ele)
{
	var emergencyexit = 0;
	var tmpele = ele;
	while (tmpele.tagName != 'FORM' && tmpele.tagName != 'BODY')
	{
		tmpele = tmpele.parentNode;
		
		if (tmpele.tagName == 'FORM')
		{
			var allinputs = tmpele.getElementsByTagName('INPUT');
			for (var i = 0; i < allinputs.length; i++)
			{
				if (allinputs[i].name == 'lastsearchresult')
				{
					allinputs[i].value = true;
					tmpele.submit();
				}
			}
			break;
		}
		
		if (emergencyexit == 20)
		{
			return;
		}
	}
}

function requestproposallist(local_search_ele, local_lngid, immediately)
{
	if (local_search_ele.value.length <= 1)
	{
		return;
	}
	
	objsearch['currentstring'] = local_search_ele.value;
	objsearch['search_ele'] = local_search_ele;
	objsearch['idsprache'] = local_lngid;
		
	// Vorschlagsliste ausfuehren
	if (objsearch['timeout'] != null)
	{
		window.clearTimeout(objsearch['timeout']);
	}
	if (immediately != null && immediately == true)
	{
		objsearch['timeout'] = window.setTimeout('requestproposallistdo();', 100);
	}
	else
	{
		objsearch['timeout'] = window.setTimeout('requestproposallistdo();', 1000);
	}
}

function requestproposallistdo()
{
	objsearch['timeout'] = null;
	objsearch['action'] = 'PROPOSALLIST';
	
	// Vorschlagsliste finden
	if (objsearch['search_form'] == null)
	{
		objsearch['search_form'] = objsearch['search_ele'].parentNode;
		var emergencyexit = 0;
		while (objsearch['search_form'].tagName != 'FORM' && objsearch['search_form'].tagName != 'BODY')
		{
			objsearch['search_form'] = objsearch['search_form'].parentNode;
			
			if (emergencyexit == 20)
			{
				break;
			}
			emergencyexit++;
		}
	}
	
	if (objsearch['search_form'].tagName != 'FORM')
	{
		return;
	}
	
	objsearch['divproposallist'] = null;
	
	var alldivs = objsearch['search_form'].getElementsByTagName('DIV');
	for (var i = 0; i < alldivs.length; i++)
	{
		if (alldivs[i].className.indexOf('divproposallist') > -1)
		{
			objsearch['divproposallist'] = alldivs[i];
			break;
		}
	}
	
	if (objsearch['divproposallist'] == null)
	{
		return;
	}
	
	// Pruefen ob Text gesucht werden muss
	for (var i = 0; i < objsearch['lastsearchhistory'].length; i++)
	{
		if (objsearch['search_ele'].value.length == objsearch['lastsearchhistory'][i]['text'].length)
		{
			if (objsearch['lastsearchhistory'][i]['text'] == objsearch['search_ele'].value && objsearch['lastsearchhistory'][i]['found'] == 0)
			{
				return;
			}
		}
		else if (objsearch['search_ele'].value.length - 1 == objsearch['lastsearchhistory'][i]['text'].length)
		{
			if (objsearch['search_ele'].value.substr(0, objsearch['search_ele'].value.length - 1) == objsearch['lastsearchhistory'][i]['text'] && objsearch['lastsearchhistory'][i]['found'] == 0)
			{
				return;
			}
		}
	}
	if (objsearch['lastsearchhistory'].length > 0)
	{
		var lastsearchhistoryitem = objsearch['lastsearchhistory'][objsearch['lastsearchhistory'].length - 1];
		if (lastsearchhistoryitem['text'].length <= objsearch['search_ele'].value.length)
		{
			if (objsearch['search_ele'].value.substr(0, lastsearchhistoryitem['text'].length) == lastsearchhistoryitem['text'] && lastsearchhistoryitem['found'] == 0)
			{
				return;
			}
		}
	}
	
	request_search('type=proposallist&wortstart=' + encodetext(objsearch['search_ele'].value, false, true) + '&lngid=' + objsearch['idsprache'] + '&kid=' + objsearch['idkategorie']);
}

function writeproposallist(response)
{
	response = decodetext(response, true);
	var tmpresponse = new Array();
	var searchtext = response.substr(0, response.indexOf('|'));
	var code = '';
	response = response.substr(response.indexOf('|') + 1);
	
	if (response != '' > 0)
	{
		if (response.indexOf('|') > -1)
		{
			tmpresponse = response.split('|');
		}
		else
		{
			tmpresponse.push(response);
		}
		
		code += '<UL>';
		for (var i = 0; i < tmpresponse.length; i++)
		{
			if (tmpresponse[i] != '' && typeof(tmpresponse[i]) != 'undefined')
			{
				code += '<LI><a href="javascript:void(0);" class="proposallistitem" onclick="setproposallist(this)">'+tmpresponse[i]+'</a></LI>';
			}
		}
		code += '</UL>';
	}
	else
	{
		var objlastsearchhistory = new Object();
		objlastsearchhistory['text'] = searchtext;
		objlastsearchhistory['found'] = 0;
		objsearch['lastsearchhistory'].push(objlastsearchhistory);
	}
	
	objsearch['divproposallist'].innerHTML = code;
	objsearch['divproposallist'].style.display = 'block';
}

function setproposallist(vorsch_anker)
{
	var vorsch_value = vorsch_anker.innerHTML.replace(/\<strong\>/g, '').replace(/\<\/strong\>/g, '');
	objsearch['search_ele'].value = vorsch_value.replace(/\<STRONG\>/g, '').replace(/\<\/STRONG\>/g, '');
	send_form();
}

function hidedivproposallist(ele)
{
	if (objsearch['timeout'] != null)
	{
		window.clearTimeout(objsearch['timeout']);
	}
	objsearch['timeout'] = window.setTimeout("hidedivproposallistdo()", 200);
}
function hidedivproposallistdo()
{
	if (objsearch['divproposallist'] != null)
	{
		objsearch['divproposallist'].style.display = 'none';
	}
}

function send_form()
{
	if (objsearch['search_form'] != null)
	{
		objsearch['search_form'].submit();
	}
}

function searchsend(maxhitsearch, maxoutput, matchcode, container, idsprache, idkategorie)
{
	disableallsearchinputs(true);
	
	searchchangestatus('searchstatus_counting');
	
	objsearch['idsprache'] = idsprache;
	objsearch['idkategorie'] = idkategorie;
	objsearch['matchcode'] = matchcode;
	objsearch['container'] = container;
	objsearch['maxoutput'] = maxoutput;
	objsearch['maxhitsearch'] = maxhitsearch;
	objsearch['action'] = 'COUNTING';
	
	request_search('type=counting&matchcode=' + matchcode + '&maxhitsearch=' + maxhitsearch + '&lngid=' + objsearch['idsprache'] + '&kid=' + objsearch['idkategorie']);
}

function disableallsearchinputs(disableflag)
{
	var allinputs = document.getElementsByName('searchTxt');
	for (var i = 0; i < allinputs.length; i++)
	{
		allinputs[i].disabled = disableflag;
	}
}

function gotosite(site)
{
	objsearch['currentpage'] = site;
	
	var start = (objsearch['currentpage'] - 1) * objsearch['maxoutput'];
	var end = objsearch['currentpage'] * objsearch['maxoutput'];
	if (objsearch['idartikel'][start] != null)
	{
		var articlestring = '';
		for (var i = start; i < end; i++)
		{
			if (objsearch['idartikel'][i] != null)
			{
				articlestring = articlestring + objsearch['idartikel'][i].toString() + ';';
			}
		}
		if (articlestring != '')
		{
			articlestring = articlestring.substr(0, articlestring.length - 1);
			
			// Blaettern ausblenden
			var alldivs = document.getElementsByTagName('DIV');
			for (var i = 0; i < alldivs.length; i++)
			{
				if (alldivs[i].className.indexOf('searchpagescroll') > -1)
				{
					alldivs[i].style.display = 'none';
				}
			}
			
			// Statusmeldungen zeigen
			searchchangestatus('searchstatus_prepareoutput');
			
			// Request absetzen
			request_search('type=createoutput&matchcode=' + objsearch['matchcode'] + '&lngid=' + objsearch['idsprache'] + '&outputids=' + articlestring + '&kid=' + objsearch['idkategorie']);
		}
	}
	
	objsearch['action'] = 'CREATEOUTPUT';
}

function gotonextsite()
{
	if (objsearch['currentpage'] <= objsearch['maxpages'])
	{
		gotosite(objsearch['currentpage'] + 1);
	}
}

function gotolastsite()
{
	if (objsearch['currentpage'] >= 1)
	{
		gotosite(objsearch['currentpage'] - 1);
	}
}

function searchchangestatus(id)
{
	document.getElementById('searchstatus_counting').style.display = 'none';
	document.getElementById('searchstatus_weighting').style.display = 'none';
	document.getElementById('searchstatus_prepareoutput').style.display = 'none';
	document.getElementById('searchoutputarticlelist').style.display = 'none';
	
	if (document.getElementById(id) != null)
	{
		document.getElementById(id).style.display = 'block';
	}
}

function request_search(postparam)
{
	try 
	{
		xmlhttp_fe_suche = new XMLHttpRequest();
	}
	catch (error)
	{
		try
		{
			xmlhttp_fe_suche = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch (error)
		{
			xmlhttp_fe_suche = false;
		}
	}
		
	if (xmlhttp_fe_suche)
	{
	 	xmlhttp_fe_suche.onreadystatechange = request_search_statusveraendert;
		xmlhttp_fe_suche.open('POST', PATH_PROJECT_HTTP+'/frontend/standard/fe_search_ajax.php', true);
		xmlhttp_fe_suche.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		
		xmlhttp_fe_suche.send(postparam);
	}
}

function request_search_statusveraendert()
{
	if (xmlhttp_fe_suche.readyState == 4)
	{
		if (xmlhttp_fe_suche.status == 404)
		{
			alert("Seite nicht gefunden");
		}
		if (xmlhttp_fe_suche.status == 200)
		{
			var response = xmlhttp_fe_suche.responseText;
			//alert(response);
			//document.getElementById('searchdebug').innerHTML += response;
			if (objsearch['action'] == 'PROPOSALLIST')
			{
				writeproposallist(response);
				
				objsearch['action'] = '';
			}
			else if (objsearch['action'] == 'COUNTING')
			{
				if (response.substr(0, 6) == 'error|')
				{
					disableallsearchinputs(false);
					document.getElementById('searchoutput').innerHTML = response.substr(6);
					return;
				}
				
				var counts = response.split('|')[0].split('-');
				var allspans = document.getElementsByTagName('SPAN');
				for (var i = 0; i < allspans.length; i++)
				{
					switch (allspans[i].className)
					{
						case 'searchhitcountercomplete':
							allspans[i].innerHTML = counts[0];
							break;
						case 'searchhitcounterallparts':
							allspans[i].innerHTML = counts[1];
							break;
						case 'searchhitcounterparts':
							allspans[i].innerHTML = counts[2];
							break;
						case 'searchhitcountersum':
							allspans[i].innerHTML = counts[3];
							break;
					}
				}
				
				objsearch['foundarticle'] = counts[3];
				objsearch['maxpages'] = 0;
				
				if (counts[3] > 0)
				{
					searchchangestatus('searchstatus_weighting');
					objsearch['action'] = 'SEARCHING'
					
					objsearch['maxpages'] = Math.ceil(objsearch['foundarticle'] / objsearch['maxoutput']);
					if (objsearch['maxpages'] > 1)
					{
						var alldivs = document.getElementsByTagName('DIV');
						for (var i = 0; i < alldivs.length; i++)
						{
							if (alldivs[i].className.indexOf('searchpagescroll') > -1)
							{
								var allas = alldivs[i].getElementsByTagName('A');
								for (var n = 0; n < allas.length; n++)
								{
									if (allas[n].className.indexOf('blaetterlink') > -1)
									{
										if (parseInt(allas[n].innerHTML) <= objsearch['maxpages'])
										{
											allas[n].style.display = '';
										}
										else
										{
											allas[n].style.display = 'none';
										}
									}
								}
							}
						}
					}
					
					request_search('type=searching&matchcode=' + objsearch['matchcode'] + '&lngid=' + objsearch['idsprache'] + '&articles=' + response.split('|')[1] + '&kid=' + objsearch['idkategorie']);
				}
				else
				{
					disableallsearchinputs(false);
					searchchangestatus('searchstatus_nofound');
				}
			}
			else if (objsearch['action'] == 'SEARCHING')
			{
				objsearch['idartikel'] = new Array();
				objsearch['idartikel'] = response.split(';')
				
				var start = (objsearch['currentpage'] - 1) * objsearch['maxoutput'];
				var end = objsearch['currentpage'] * objsearch['maxoutput'];
				
				if (objsearch['idartikel'][start] != null)
				{
					var articlestring = '';
					for (var i = start; i < end; i++)
					{
						if (objsearch['idartikel'][i] != null)
						{
							articlestring = articlestring + objsearch['idartikel'][i].toString() + ';';
						}
					}
					if (articlestring != '')
					{
						articlestring = articlestring.substr(0, articlestring.length - 1);
						request_search('type=createoutput&matchcode=' + objsearch['matchcode'] + '&lngid=' + objsearch['idsprache'] + '&outputids=' + articlestring + '&kid=' + objsearch['idkategorie']);
					}
				}
				
				searchchangestatus('searchstatus_prepareoutput');
				
				objsearch['action'] = 'CREATEOUTPUT';
			}
			else if (objsearch['action'] = 'CREATEOUTPUT')
			{
				disableallsearchinputs(false);
				
				var code = '';
				
				switch (objsearch['container'].toUpperCase())
				{
					case 'UL':
						code += '<UL>' + response + '</UL>';
						break;
					case 'OL':
						code += '<OL>' + response + '</OL>';
						break;
					default:
						code += response;
						break;
				}
				
				document.getElementById('searchoutputarticlelist').innerHTML = code;
				searchchangestatus('searchoutputarticlelist');
				
				objsearch['action'] = '';
				
				if (objsearch['maxpages'] > 1)
				{
					var alldivs = document.getElementsByTagName('DIV');
					for (var i = 0; i < alldivs.length; i++)
					{
						if (alldivs[i].className.indexOf('searchpagescroll') > -1)
						{
							var allas = alldivs[i].getElementsByTagName('A');
							for (var n = 0; n < allas.length; n++)
							{
								if (allas[n].className.indexOf('listpageprevious') > -1)
								{
									if (objsearch['currentpage'] <= 1)
									{
										allas[n].style.display = 'none';
									}
									else
									{
										allas[n].style.display = '';
									}
								}
								else if (allas[n].className.indexOf('listpagenext') > -1)
								{
									if (objsearch['currentpage'] >= objsearch['maxpages'])
									{
										allas[n].style.display = 'none';
									}
									else
									{
										allas[n].style.display = '';
									}
								}
								else if (allas[n].className.indexOf('blaetterlink') > -1)
								{
									if (parseInt(allas[n].innerHTML) == objsearch['currentpage'])
									{
										allas[n].className += ' active';
									}
									else
									{
										allas[n].className = allas[n].className.replace(/ active/gi, '');
									}
									objsearch['currentpage']
								}
							}
							
							alldivs[i].style.display = '';
						}
					}
				}
			} 
		}
	}
}
