
var currentDiv = "";
var loadingdiv = "";

function showTopLoading()
{
	createShade();
	var el = document.createElement("div");
	el.style.position = "absolute";
	el.style.top = "0px";
	el.style.left = "0px";
	el.style.width = "200px";
	el.style.height = "75px";
	el.style.textAlign = "center";
	el.style.backgroundColor = "#000000";
	window.scrollTo(0, 0);

	el.innerHTML = '<br /><img src="images/Spinning_wheel_throbber.gif" alt="" />&nbsp;&nbsp;<br /><b><font color="white">' + TR('Loading') + '...</font></b>';
	el.id = "DivLoading";
	el.style.visibility = "visible";
	document.body.appendChild(el);
}

function showDivLoading(id)
{
	var el = document.getElementById(id);
	if (typeof el == 'undefined' || el == null)
		return;
	el.innerHTML = '<div align="center"><br /><br /><img src="images/loading.gif" alt="" /><br />' + TR('Loading') + '... </div>' +
				'<div id="loadingStatus"></div>';
}

function showSmallLoading(id, _text)
{
	var text;
	if (typeof _text == 'undefined' || _text == null)
		text = TR('Loading');
	else
		text = _text;

	var el = document.getElementById(id);
	if (typeof el == 'undefined' || el == null)
		return;
	el.innerHTML = '<img src="images/loading_small.gif" alt="" />   ' + text + '... ';
}

function showLoading(id)
{
	if (typeof id == "undefined" || id == "")
		showTopLoading();
	else if (id == 'divContent')
		showDivLoading('divContent');
	else
		showSmallLoading(id);
		
}

function hideLoading()
{
	var el = document.getElementById("DivLoading");
	if (typeof el != 'undefined' && el != null)
		document.body.removeChild(el);
	
	removeShade();
}

function ajaxStateChanged()
{
	if (typeof xmlHttp == 'undefined')
		return;

 	//this is not working in IE anyways yet
	/*
	if (xmlHttp.readyState==3)
        {	
                if (xmlHttp.getResponseHeader("Content-Length") != null && parseInt(xmlHttp.getResponseHeader("Content-Length")) > 0)
                {
                                var totallength = parseInt(xmlHttp.getResponseHeader("Content-Length"));
                                var currentlength = xmlHttp.responseText.length;
                                if (currentlength < totallength)
                                {
                                        var el = document.getElementById("loadingStatus");
                                        el.innerHTML = '<b>' + parseInt(currentlength/totallength*100) + '%</b>';
                                }
                }
        }
	*/


	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{
		hideLoading();
		var response = xmlHttp.responseText;
		currentDiv = "";
		var content = "";
		var contenttype = "";
		var lines = response.split("\n");
		for (i=0; i < lines.length; i++)
		{
			//trim
			lines[i] = lines[i].replace(/^\s+/g, "");
			lines[i] = lines[i].replace(/\s+$/g, "");

			if (lines[i].substring(0, 3) == "DIV") //div
			{
				content = "";
				if (lines[i].substring(4) != "")
					currentDiv = lines[i].substring(4);
				contenttype = "DIV";
			}
			else if (lines[i].substring(0, 3) == "REM") //comment
			{
				//nothing
			}
			else if (lines[i].substring(0, 3) == "EIV") //end div content
			{
				var el = document.getElementById(currentDiv);
				if (typeof el != 'undefined' && el != null)
				{
					if (contenttype == "AIV")
						el.innerHTML = el.innerHTML + content;
					else if (contenttype == "BIV")
						el.innerHTML = content + el.innerHTML;
					else
						el.innerHTML = content;
				}
				else
				{
					var newreq = lines[i].substring(4);
					eval(newreq);
				}

				content = "";
				currentDiv = "";
				contenttype = "";
			}
			else if (lines[i].substring(0, 3) == "AIV") //append div
			{
				content = "";
				currentDiv = lines[i].substring(4);
				contenttype = "AIV";
			}
			else if (lines[i].substring(0, 3) == "BIV") //before div
			{
				content = ""
				currentDiv = lines[i].substring(4)
				contenttype = "BIV"
			}
			else if (lines[i].substring(0, 3) == "REQ") //request
			{
				var req = lines[i].substring(4);
				ajaxRequest(req);
			}
			else if (lines[i].substring(0, 3) == "SJS") //start javascript
			{
				contenttype = "JS";
				content = "";
			}
			else if (lines[i].substring(0, 3) == "EJS") //end javascript
			{
				eval(content);
				contenttype = "";
				content = "";
			}
			else if (lines[i].substring(0, 3) == "CLH") //clear history
			{
				HistoryEvent = null;
				History.length = 0;
			}
			else if (contenttype == "DIV" || contenttype == "JS" || contenttype == "AIV" || contenttype == "BIV")
			{
				content = content + lines[i] + "\n";
			}
			else if (lines[i] != "")
			{
			}
			else
			{
			}
		}
		hideLoading();
		Waiting = false;
		checkBacklog();
	}
	else
	{
	}
}


function ajaxSubmitData(prepage, callback)
{
	var url = "";
	var page = "";
	var params = "";
	var dohistory = false;
	var hash = "";
	var showld = false;

	loadingdiv = "divContent";

	if (!prepage)
	{
		Waiting = false; 
		return;
	}



	if (!isArray(prepage))
	{
		if (typeof PrependDebug != 'undefined')
			PrependDebug("Last request: " + prepage + "\n<br />");
		
		var hsplit = prepage.split('#');
		page = hsplit[0];
		if (hsplit.length > 1)
			hash = hsplit[1];

		//retrieve GET's
		var urlsplit = page.split('?');
		if (urlsplit.length > 1)
		{
			page = urlsplit[0];
			var ars = urlsplit[1].split('&');
			var i;
			for (i = 0; i < ars.length; i++)
			{
				if (i > 0)
					params = params + "&";
				var valars = ars[i].split('=');
				if (valars.length == 2)
				{
					var name = valars[0];
					var val = valars[1];
					if (name == "loading")
					{
						loadingdiv = val;
						showld = true;
					}
					else
					{
						params = params + URLEncode(name) + '=' + URLEncode(val);
					}
				}
				else
				{
					var name = valars[0];
					if (name == "d")
						dohistory = true;
					else if (name == "nl")
						showld = false;
					else if (name == "ld")
						showld = true;
				}
			}
			if (params.charAt(params.length-1) == '&')
				params = params.substring(0, params.length - 1);
		}
		else
		{
			params = "";
		}
	}
	else //it's an array
	{
		params = "";
		var hsplit = prepage[0].split('#');
		page = hsplit[0];
		if (hsplit.length > 1)
			hash = hsplit[1];

		for (var i = 1; i < prepage.length; i++)
		{
			if (i > 1)
				params = params + "&";
			if (!isArray(prepage[i]))
			{
				var valars = prepage[i][0].split('=');
				if (valars.length == 2)
				{
					var name = valars[0];
					var val = valars[1];
					if (name == "loading")
					{
						loadingdiv = val;
						showld = true;
					}
					else
					{
						params = params + URLEncode(name) + '=' + URLEncode(val);
					}
				}
				else
				{
					var name = valars[0];
					if (name == "d")
						dohistory = true;
					else if (name == "nl")
						showld = false;
				}
			}
			else //array
			{
				params = params + URLEncode(prepage[i][0]) + '=' + URLEncode(prepage[i][1]);
			}
		}
	}

	if (showld == true)
	{
		showLoading(loadingdiv);
	}

	if (page.charAt(page.length-1) != '?' && page.charAt(page.length-1) != '&')
		page = page + '?';

	while(params.charAt(0) == '&')
		params = params.substring(1);
	while(params.charAt(params.length - 1) == '&')
		params = params.substring(0, params.length - 1);

	url = page + "ms=" + new Date().getTime(); // + "&fix=" + fix

	if (window.XMLHttpRequest)
		xmlHttp = new XMLHttpRequest();
	else
		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");

	if (xmlHttp==null)
	{
		alert ("Browser does not support HTTP Requests");
		return;
	}

	xmlHttp.open("POST", url, true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");

	xmlHttp.onreadystatechange = callback;

	xmlHttp.send(params);
}

function ajaxRequest(page)
{
	if (Waiting == false)
	{
		Waiting = true;
		ajaxSubmitData(page, ajaxStateChanged);
	}
	else
	{
		Backlog.push(page);
	}
}

function checkBacklog()
{
	if (Backlog.length > 0 && Waiting == false)
	{
		var page = Backlog[0];
		Backlog.shift();
		ajaxRequest(page);
	}
}

//Do a request on page, and the value of the array fields
function ajaxSubmitForm(page, fields)
{
	var req = new Array();
	req.push(page);

	if (typeof fields == 'undefined' || fields == null || fields.length == 0)
	{
		ajaxRequest(req);
		return;
	}

	for (var i = 0; i < fields.length; i++)
	{
		var el = document.getElementById(fields[i]);
		if (typeof el != 'undefined' && el != null)
			req.push(new Array(fields[i], el.value));
	}
	
	

	ajaxRequest(req);
}

//Attach code to title in the history and execute code
function appendHistory(js, _title)
{
	var title;

	if (typeof _title == 'undefined')
		title = js;
	else
		title = _title;
	var req64 = URLEncode(encode64(js));
	dhtmlHistory.add(URLEncode(title), req64);
	eval(js);
}

function lRequest(req, _attr)
{
	lReqPage(req, _attr);
}

//gives options etc.
function lReqPage(req, _attr)
{
	var attrs;
	if (typeof _attr == 'undefined')
		attrs = "loading".split(",");
	else
		attrs = _attr.split(",");

	var title = req.replace(/\.php/, '');
	title = title.replace(/nl/, '');

	var appendfunc = function(sreq) {
			var str = sreq;
			if (sreq.indexOf('?') == -1)
				str = str + '?ld';
			else if (sreq.charAt[sreq.length-1] == '&')
				str = str + 'ld';
			else
				str = str + '&ld';
			return str;
		}

	var loading = true;
	var dohistory = true;
	var i;
	for (i=0; i < attrs.length; i++)
	{
		if (attrs[i] == "nl")
			loading = false;
		if (attrs[i] == "nh")
			dohistory = false;
	}

	var str = req;
	if (loading == true)
		str = appendfunc(str);

	if (dohistory == true)
		appendHistory("ajaxRequest('" + str + "')", title);
	else
		ajaxRequest(str);
}

//Easy
function lReq(title)
{
	lReqPage(title + '.php');
}

//Raw
function lRawJavascript(req)
{
	appendHistory(req, req);
}


