/* ajax simple */
var AjaxReplace = true;
var AjaxAfterLoadCall = null;
var AjaxRequestType = 'html';
var AjaxRequestResult = null;
var AjaxRequestInProc = false;
function AjaxRequestCall(action, request, containerid, tp, aalc)
{
	if (AjaxRequestInProc)
	{
		var t = '\'' + action + '\', \'' + request + '\', \'' + containerid + '\', \'' + tp + '\', \'' + aalc + '\'';
		setTimeout("AjaxRequestCall("+t+")", 100);
		return;
	}
	else
	{
		//AjaxAfterLoadCall = aalc;
		if (tp == 'get')
			AjaxRequestGet(action, request, containerid, aalc);
		else
			AjaxRequestPost(action, request, containerid, aalc);
	}
}

function AjaxRequestPost(action, request, containerid, aalc)
{
	aalc = (aalc) ? aalc : null;
	AjaxRequestInProc = true;
	setTimeout("AjaxRequestInProc = false", 10000);
	action = action.replace(/^https?\:\/\/[^\/]+/, '');
	var page_request = false
	if (window.XMLHttpRequest)
		page_request = new XMLHttpRequest()
	else if (window.ActiveXObject){ 
		try { page_request = new ActiveXObject("Msxml2.XMLHTTP") } 
		catch (e){
			try { page_request = new ActiveXObject("Microsoft.XMLHTTP") }
			catch (e){}
		}
	}
	else
		return false;

	page_request.onreadystatechange=function() { AjaxUpdateContent(page_request, containerid, aalc); }
	page_request.open('POST', (action), true);
	page_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	page_request.send(request);
}

function AjaxRequestGet(action, request, containerid, aalc)
{
	action = action.replace(/^https?\:\/\/[^\/]+/, '');
	if (action != "")
		request = action + "?" + request;

	var page_request = false
	
	if (window.XMLHttpRequest)
		page_request = new XMLHttpRequest()
	else if (window.ActiveXObject){ 
		try { page_request = new ActiveXObject("Msxml2.XMLHTTP") } 
		catch (e){
			try { page_request = new ActiveXObject("Microsoft.XMLHTTP") }
			catch (e){}
		}
	}
	else
		return false;

	page_request.onreadystatechange=function() 
	{ 
		AjaxUpdateContent(page_request, containerid, aalc, request); 
	}
	page_request.open('GET', request, true);
	page_request.send(null);
}

function AjaxUpdateContent(page_request, containerid, aalc, call_request)
{
	aalc = (!aalc || aalc == '') ? null : aalc;
	if (page_request.readyState == 4 && (page_request.status==200 || window.location.href.indexOf("http")==-1))
	{
		if (gEl(containerid + '-loading') != null)
			gEl(containerid + '-loading').style.display="none";
		
		var getContent = '';
		if (AjaxRequestType == 'xml')
			getContent = page_request.responseXML;
		else
			getContent = page_request.responseText;
		
		AjaxRequestType = 'html';
		
		if (typeof getContent != 'object' && getContent == 'out')
		{
			document.location = '/logout';
			return;
		}
		
		if (AjaxReplace)
		{
			if (containerid == 'alert')
				alert(getContent);
			else if (containerid != 'null')
			{
				if (!gEl(containerid)) 
					AxSetContainer(containerid);
				gEl(containerid).innerHTML = getContent;
			}
		}
		else
		{
			if (!gEl(containerid)) 
				AxSetContainer(containerid);
			gEl(containerid).innerHTML = document.getElementById(containerid).innerHTML + getContent;
		}
		if (aalc != null)
		{
			var _tc = (containerid != 'null') ? "'" + containerid + "'" : "null";
			AjaxRequestResult = getContent;
			if (call_request) { setTimeout("CatchPrint('" + call_request + "');", 300); }
			
			if (typeof(aalc) == 'function') aalc(_tc.replace(/\'/g,''))
			else window.setTimeout(aalc + "("+_tc+")", 50);
			
			AjaxAfterLoadCall = null;
		}
		AjaxRequestInProc = false;
	}
}

function AxSetContainer(id)
{
	var inBody = document.getElementsByTagName("body").item(0);
	var ne = document.createElement("div");
	ne.setAttribute('id', id);
	ne.style.position = 'absolute';
	ne.style.display = 'none';
	inBody.insertBefore(ne, inBody.firstChild);
}

function AxGetClean(_tc)
{
	if (gEl(_tc))
	{
		var _t = gEl(_tc).innerHTML;
		gEl(_tc).innerHTML = '';
		return _t;
	}
	return '';
}
