// v.1

var xAjax = function ()
{
	this.vDebugHeight = 100;
	this.vDebugHeightTitle = 16;
	this.vDebugClassName = 'ajax_';

	this.vDebugWnd = null;
	this.vDebugWndContent = null;
	this.vDebugSeparator = '<hr>';

	this.vDebugWndElementName = 'ajax_debug_wnd';
	this.vDebugContentElementName = 'ajax_debug_wnd_content';

	this.vCache = [];
}

xAjax.prototype = new xCommon;

xAjax.prototype.AJAXcreateRequestObject = function ()
{
	var xmlHttp = false;
	try
	{
		xmlHttp = new ActiveXObject('Msxml2.XMLHTTP');
	}
	catch (e)
	{
		try
		{
			xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
		}
		catch (e2)
		{
			xmlHttp = false;
		}
	}

	if (!xmlHttp && typeof XMLHttpRequest != 'undefined')
	{
		xmlHttp = new XMLHttpRequest();
	}

	return xmlHttp;
}

xAjax.prototype.AJAXPostData = function ()
{
	var p = this;
	var tFound = false;
	this.vLastArguments = arguments[0] + this.AJAXurlEncodeData(arguments[1]);
	if (tFound = this.fSearchCache(this.vLastArguments))
	{
		p.vAJAXReceivedData = tFound;
		p.AJAXprocessVars();
		p.AJAXprocessArrays(p.vAJAXReceivedData.firstChild);
		p.AJAXdebug();
		p.AJAXprocessExec();
	}
	else
	{
		var AJAXhttp = this.AJAXcreateRequestObject();
		p.lastAJAXObject = AJAXhttp;
		if (!AJAXhttp) return false;
		AJAXhttp.onreadystatechange = function ()
			{
				if (AJAXhttp.readyState == 4)
				{
					p.AJAXresponseText = AJAXhttp.responseText;
					if (AJAXhttp.responseXML)
					{
						if (p.vWaitForValue != '' && (p.vWaitForValue == p.vLastArguments))
						{
							p.vCache[p.vWaitForValue] = AJAXhttp.responseXML;
						}
						p.vWaitForValue = '';
						p.vAJAXReceivedData = AJAXhttp.responseXML;
	//					alert(p.vAJAXReceivedData);

						p.AJAXprocessVars();
						p.AJAXprocessArrays(p.vAJAXReceivedData.firstChild);
						p.AJAXdebug();
						p.AJAXprocessExec();
					}

					if (p.vAutoShow == true)
					{
						p.fShow();
					}
				}
			}
		
		AJAXhttp.open('POST', arguments[0], true);
		AJAXhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		AJAXhttp.send(this.AJAXurlEncodeData(arguments[1]));
		return true;
	}
}

xAjax.prototype.AJAXurlEncodeData = function (data)
{
	var query = [];
	if (data instanceof Object)
	{
		for (var k in data)
		{
			if (!(data[k] instanceof Function))
			{
				if (data[k] instanceof Object)
				{
					for (var j in data[k])
					{
						if (data[k][j] instanceof Object)
						{
							for (var i in data[k][j])
								if (data[k][j][i] != 'undefined')
									query.push(encodeURIComponent(k) +"["+j+"]["+i+"]=" + encodeURIComponent(data[k][j][i]));
								
						}
						else
						{
							if (data[k][j])
								query.push(encodeURIComponent(k) +"["+j+"]=" + encodeURIComponent(data[k][j]));
						}
					}
				}
				else
				{
					if (data[k])
						query.push(encodeURIComponent(k) + "=" + encodeURIComponent(data[k]));
				}
			}
		}

		return query.join('&');
	}
	else
	{
		return encodeURIComponent(data);
	}
}

xAjax.prototype.AJAXprocessVars = function ()
{
	if (this.vAJAXReceivedData)
	{
		var tt = this.vAJAXReceivedData.getElementsByTagName('VarName');
		if (!tt || tt.length == 0)
			tt = this.vAJAXReceivedData.getElementsByTagName('VN');
		if (tt)
		{
			for (var i=0; i<tt.length; i++)
			{
				for (var j=0; j<tt.item(i).attributes.length; j++)
				{
					if (tt.item(i).attributes[j].name == 'name' || tt.item(i).attributes[j].name == 'n')
					{
						var qq = tt.item(i).getElementsByTagName('data');
						if (!qq || qq.length == 0)
							var qq = tt.item(i).getElementsByTagName('d');

						if ((qq.item(0).attributes[0].name == 'type' && qq.item(0).attributes[0].value == 'int') ||
							(qq.item(0).attributes[0].name == 't' && qq.item(0).attributes[0].value == 'i'))
							eval(tt.item(i).attributes[j].value + '=' + (qq.item(0).childNodes.length?qq.item(0).childNodes[0].nodeValue:''));
						else if ((qq.item(0).attributes[0].name == 'type' && qq.item(0).attributes[0].value == 'string') ||
							(qq.item(0).attributes[0].name == 't' && qq.item(0).attributes[0].value == 's'))
						{
							var zz = qq.item(0).childNodes.length?qq.item(0).childNodes[0].nodeValue:'';
							eval(tt.item(i).attributes[j].value + '= zz;');
						}
					}
				}
			}
		}
	}
}

xAjax.prototype.AJAXprocessArrays = function (x)
{
	var x = arguments[0];

	if (x)
	{
		for (var i=0; i<x.childNodes.length; i++)
		{
			var tt = x.childNodes[i];
			if (tt.tagName == 'VarArrayName' || tt.tagName == 'VAN')
			{
				var nodeName = tt.getAttribute('name');
				if (!nodeName)
					nodeName = tt.getAttribute('n');

				if (arguments[1])
				{
					eval(arguments[1] + ' = new Array');
				}
				else
				{
					eval(nodeName + ' = new Array');
				}

				for (var j=0; j<tt.childNodes.length; j++)
				{
					if (tt.childNodes[j].tagName == 'data' || tt.childNodes[j].tagName == 'd')
					{
						var aType = tt.childNodes[j].getAttribute('type')
						if (!aType)
							aType = tt.childNodes[j].getAttribute('t')

						var akey = tt.childNodes[j].getAttribute('key')
						if (!akey)
							akey = tt.childNodes[j].getAttribute('k')

						if (aType == 'array' || aType == 'a')
						{
							this.AJAXprocessArrays(tt.childNodes[j], (arguments[1]?arguments[1]:nodeName) + '[' + (akey?'\'' + akey + '\'':k) + ']');
						}
						else
						{
							if (tt.childNodes[j].childNodes.length > 0)
							{
								var zz = tt.childNodes[j].childNodes[0].nodeValue;
								eval((arguments[1]?arguments[1]:nodeName) + "['" + (akey?akey.replace(/\'/g, "\\'"):k) + "']" +  '= zz'  + ';');
							}
						}
					}
				}
			}
		}
	}
}

xAjax.prototype.__call = function ()
{
	alert(arguments[0]);
}

xAjax.prototype.AJAXprocessExec = function ()
{
	var tt = this.vAJAXReceivedData.getElementsByTagName('Exec');
	if (!tt || tt.length == 0)
		tt = this.vAJAXReceivedData.getElementsByTagName('E');
	
	for (var i=0; i<tt.length; i++)
	{
		var zz = tt[i].childNodes[0].nodeValue;
		if (zz)
		{
			this.aOnExec(zz);
		}
	}
}

xAjax.prototype.aOnExec = function ()
{
	if (arguments[0] == 'this.fShow()')
		this.fShow();
	else if (arguments[0] == 'this.fGetData()')
		this.fGetData();
	else if (arguments[0] == 'this.fUnLockControl()')
		this.fUnLockControl();
	else if (arguments[0] == 'error')
		this.fError(this.vError[this.vCurrentError]);
	else if (arguments[0] == 'notify')
		this.fNotify(this.vNotify[this.vCurrentNotify]);
	else
		try{eval(arguments[0]);}catch(e){};
	
	
}

xAjax.prototype.AJAXdebug = function()
{
	var tt = this.vAJAXReceivedData.getElementsByTagName('Ajax');
	var tTable, tTbody, tTr, tTd, tText, tImg, tDiv;

	var p = this;

	if (tt.length > 0)
	{
		if (!this.vDebugWnd)
		{
			this.vDebugWnd = document.getElementById(this.vDebugWndElementName);
			this.vDebugWndContent = document.getElementById(this.vDebugContentElementName);
		}
		if (!this.vDebugWnd)
		{
			tTable = document.createElement('TABLE');
			tTable.border = 1;
			tTable.className = this.vDebugClassName + 'main_tab';
			tTable.height = this.vDebugHeight;
			tTable.id = this.vDebugWndElementName;
			tTbody = document.createElement('TBODY');
			tTr = document.createElement('TR');
			tTr.className = this.vDebugClassName + 'tr_header';

			tTd = document.createElement('TD');
			tTd.innerHTML = '&nbsp;Debug Window.&nbsp;&nbsp;Click to hide';

			tTr.appendChild(tTd);

			tTd = document.createElement('TD');
			tTd.width = this.vPicWidth;
			tImg = document.createElement('IMG');
			tImg.src = this.vPicPath + 'trash.gif';
			tImg.className = 'hand';
			tImg.onclick = function ()
			{
				p.vDebugWndContent.innerHTML = '';
			}
			tTd.appendChild(tImg);
			tTr.appendChild(tTd);

			tTd = document.createElement('TD');
			tTd.width = this.vPicWidth;
			tTd.height = this.vDebugHeightTitle;
			tImg = document.createElement('IMG');
			tImg.src = this.vPicPath + 'export.gif';
			tImg.className = 'hand';
			tImg.onclick = function ()
			{
				if (this.getAttribute('full') != 1)
				{
					p.vDebugWnd.style.position = 'absolute';
					p.vDebugWnd.style.top = document.body.scrollTop;
					p.vDebugWnd.style.height = document.body.clientHeight;
					p.vDebugWndContent.style.height = document.body.clientHeight - p.vDebugHeightTitle - 4;
					this.src = p.vPicPath + 'import.gif';
					this.setAttribute('full', 1);
					p.vDebugFull = 1;
				}
				else
				{
					this.setAttribute('full', 0);
					this.src = p.vPicPath + 'export.gif';

					p.vDebugWnd.style.position = '';
					p.vDebugWnd.height = p.vDebugHeight;

					p.vDebugFull = 0;
				}
			}
			tTd.appendChild(tImg);
			tTr.appendChild(tTd);

			tTd = document.createElement('TD');
			tTd.width = this.vPicWidth;
			tImg = document.createElement('IMG');
			tImg.src = this.vPicPath + 'close.gif';
			tImg.className = 'hand';
			tImg.onclick = function ()
			{
				p.vDebugWnd.parentNode.removeChild(p.vDebugWnd);
				p.vDebugWnd = null;
				p.vDebugWndContent = null;
			}
			tTd.appendChild(tImg);
			tTr.appendChild(tTd);

			tTbody.appendChild(tTr);

			tTr = document.createElement('TR');
			tTr.className = this.vDebugClassName + 'tr_content';
			tTd = document.createElement('TD');
			tTd.className = this.vDebugClassName + 'td_content';
			tTd.colSpan = 4;
			tDiv = document.createElement('DIV');
			tDiv.id = this.vDebugContentElementName;
			tDiv.height = this.vDebugHeight - this.vDebugHeightTitle;
			tDiv.style.height = this.vDebugHeight - this.vDebugHeightTitle;
			tDiv.className = this.vDebugClassName + 'div_content';
			this.vDebugWndContent = tDiv;
			tTd.appendChild(tDiv);

			tTr.appendChild(tTd);
			tTbody.appendChild(tTr);
			tTable.appendChild(tTbody);

			document.body.appendChild(tTable);

			this.vDebugWnd = tTable;
		}
		this.vDebugWndContent.innerHTML = '<pre>' + tt[0].childNodes[0].nodeValue + this.vDebugSeparator + this.vDebugWndContent.innerHTML;
	}
}

xAjax.prototype.fSearchCache = function ()
{
	if (this.vUseCache)
	{
		var cache_string = arguments[0];
		
		if ((cache_string.indexOf('edit') != -1) || (cache_string.indexOf('delete') != -1) || (cache_string.indexOf('add') != -1)  || (cache_string.indexOf('save') != -1)  || (cache_string.indexOf('update') != -1))
		{
			this.vCache = [];
		}
		else
		{
			if (this.vCache[cache_string])
			{
				return this.vCache[cache_string];
			}
			this.vWaitForValue = cache_string;
		}
	}
	return false;
}