var xmlHttp = createXmlHttpRequestObject();
var session_name = '';
var session_id = '';
var livesearch = false;
var lastsearch = '';
var buttonsearch = false;

function createXmlHttpRequestObject()
{
	var xmlHttp;

	// IE
	if(window.ActiveXObject)
	{
		try
		{
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e) {
			xmlHttp = false;
		}

	} else {
		// other
		try
		{
			xmlHttp = new XMLHttpRequest();
		} catch (e) {
			xmlHttp = false;
		}
	}

	if(!xmlHttp)
		alert("Error creating the XMLHttpRequest object.");
	else
		return xmlHttp;
}

function LoginUserByEnter (e, sessname, sessid)
{
	var characterCode;

	if(e && e.which)
	{
		// NN4
		e = e;
		characterCode = e.which;
	} else {
		// IE
		try
		{
			e = event;
			characterCode = e.keyCode //character code is contained in IE's keyCode property
		}

		catch(e) { }
	}

	if(characterCode == 13)
	{
		LoginUser(sessname, sessid);
	}
}

function LoginUser (sessname, sessid)
{
	document.getElementById('status_wait').style.display = 'block';
	document.getElementById('status_ok').style.display = 'none';
	document.getElementById('status_err').style.display = 'none';

	if(xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
	{
		user_login = encodeURIComponent(document.getElementById("login").value);
		user_password = encodeURIComponent(document.getElementById("passwort").value);

		params = 'login='+user_login+'&passwort='+user_password+'&action=login';

		xmlHttp.open("POST", "admin/login.php?"+sessname+'='+sessid, true);
		xmlHttp.onreadystatechange = handleLoginResponse;

		xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlHttp.setRequestHeader("Content-length", params.length);
		xmlHttp.setRequestHeader("Connection", "close");
		xmlHttp.send(params);
	}
}

function handleLoginResponse ()
{
	if(xmlHttp.readyState == 4)
	{
		document.getElementById('status_wait').style.display = 'none';

		if(xmlHttp.status == 200)
		{
			xmlResponse = xmlHttp.responseXML;
			xmlDocumentElement = xmlResponse.documentElement;
			Antwort = xmlDocumentElement.getElementsByTagName("status");
			URL = xmlDocumentElement.getElementsByTagName("url");

			if(Antwort.item(0).firstChild.data == 'ok')
			{
				document.getElementById('status_ok').style.display = 'block';
				window.location = decodeURIComponent(URL.item(0).firstChild.data);
			} else {
				document.getElementById('status_err').style.display = 'block';
			}
		}
	}
}

function SearchByEnter (e, sessname, sessid)
{
	var characterCode;

	if(e && e.which)
	{
		e = e;
		characterCode = e.which;
	} else {
		// IE
		try
		{
			e = event;
			characterCode = e.keyCode //character code is contained in IE's keyCode property
		}

		catch(e) { }
	}

	if(characterCode == 13)
	{
		livesearch = false;
		SearchWaypoint(sessname, sessid, 'search');
	} else {

		if(document.getElementById("search").value.length>2)
		{
			if(!livesearch)
			{
				livesearch = true;
				buttonsearch = false;
				SearchWaypoint(sessname, sessid, 'livesearch');
			}
		}
	}
}

function SearchWaypoint(sessname, sessid, action)
{
	if(!action)
	{
		var action = 'search';
		livesearch = false;
		lastsearch = document.getElementById("search").value;
		buttonsearch = true;
	}

	if(!livesearch)
	{
		document.getElementById("searchbutton").innerHTML = '&nbsp;&nbsp;<img src="'+document.getElementsByName("tpl_path")[0].value+'images/icons/search.gif" alt="search" style="position:relative;top:3px;" />';
		document.getElementById('livesearchresult').style.display = 'none';
	}

	session_name = sessname;
	session_id = sessid;

	if((xmlHttp.readyState == 4 || xmlHttp.readyState == 0) && (!livesearch || (livesearch && lastsearch != document.getElementById("search").value && document.getElementById("search").value.length>2)))
	{
		lastsearch = document.getElementById("search").value;
		searchfor = encodeURIComponent(document.getElementById("search").value);

		params = 'search='+searchfor+'&action='+action;

		xmlHttp.open("POST", "admin/start.php?"+sessname+'='+sessid, true);
		xmlHttp.onreadystatechange = SearchWaypointResponse;

		xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlHttp.setRequestHeader("Content-length", params.length);
		xmlHttp.setRequestHeader("Connection", "close");
		xmlHttp.send(params);
	} else {

		if(livesearch)
		{
			setTimeout('SearchWaypoint(\''+sessname+'\', \''+sessid+'\', \''+action+'\')', 3500);
		}
	}
}

function SearchWaypointResponse ()
{
	if(xmlHttp.readyState == 4)
	{
		document.getElementById("searchbutton").innerHTML = '&nbsp;<input type="submit" name="submit" value="suchen" onclick="SearchWaypoint(\''+session_name+'\',\''+session_id+'\');" style="border:1px solid #666666;" />';

		if(xmlHttp.status == 200)
		{
			xmlResponse = xmlHttp.responseXML;
			xmlDocumentElement = xmlResponse.documentElement;

			if(xmlDocumentElement.nodeName == 'livesearchresults' && buttonsearch==false && xmlDocumentElement.firstChild.nodeValue.length>1)
			{
				/*
				* live search Ergebnis auswerten und anzeigen
				*/
				document.getElementById('livesearchresult').style.display = 'block';
				document.getElementById('livesearchdata').innerHTML = xmlDocumentElement.firstChild.nodeValue;

			} else if (xmlDocumentElement.nodeName == 'searchresults') {

				/*
				* Standardsuche Ergebnis anzeigen
				*/
				if(xmlDocumentElement.firstChild.nodeValue.length>1)
				{
					document.getElementById('livesearchresult').style.display = 'none';
					document.getElementById('contentarea').style.display = 'block';
					document.getElementById('contentarea').innerHTML = xmlDocumentElement.firstChild.nodeValue;
				} else {
					document.getElementById('contentarea').style.display = 'none';
					document.getElementById('contentarea').innerHTML = '';
				}
			}

			if(xmlDocumentElement.nodeName == 'livesearchresults' && xmlDocumentElement.firstChild.nodeValue.length<=1)
			{
				document.getElementById('livesearchresult').style.display = 'none';
			}

			// restart
			if(livesearch)
			{
				setTimeout('SearchWaypoint(\''+session_name+'\', \''+session_id+'\', \'livesearch\')', 3500);
			}
		}
	}
}

function startEditor(waypoint)
{
	lastsearch = '';
	livesearch = false;

	document.getElementById("livesearchresult").style.display = 'none';
	document.getElementById("contentarea").innerHTML = document.getElementById("loading").innerHTML;
	document.getElementById("contentarea").style.display = 'block';

	if(xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
	{
		lastsearch = document.getElementById("search").value;

		params = 'waypoint='+encodeURIComponent(waypoint)+'&action=edit';

		xmlHttp.open("POST", "admin/editor.php?"+session_name+'='+session_id, true);
		xmlHttp.onreadystatechange = editorResponse;

		xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlHttp.setRequestHeader("Content-length", params.length);
		xmlHttp.setRequestHeader("Connection", "close");
		xmlHttp.send(params);
	}
}

function editorResponse()
{
	if(xmlHttp.readyState == 4)
	{
		document.getElementById("contentarea").style.display = 'block';

		if(xmlHttp.status == 200)
		{
			document.getElementById("contentarea").innerHTML = xmlHttp.responseText;

			switchToPage('start');
		}
	}
}

function switchToPage(page)
{
	document.getElementById("start").style.display = 'none';
	document.getElementById("description").style.display = 'none';
	document.getElementById("solution").style.display = 'none';

	if(page == 'start' || page == 'description' || page == 'solution')
	{

		document.getElementById(page).style.display = 'block';
	}
}

