// v.1

var xAjax = function ()
{
	var p = this;
	p.vDebugHeight = 100;
	p.vDebugHeightTitle = 16;
	p.vDebugClassName = 'ajax_';

	p.vDebugWnd = null;
	p.vDebugWndContent = null;
	p.vDebugSeparator = '<hr>';

	p.vDebugWndElementName = 'ajax_debug_wnd';
	p.vDebugContentElementName = 'ajax_debug_wnd_content';

	p.vCache = [];

	for (var i in arguments[0])
		p[i] = arguments[0][i];
}

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;
	p.vLastArguments = arguments[0] + p.AJAXurlEncodeData(arguments[1]);
	if (tFound = p.fSearchCache(p.vLastArguments))
	{
		p.vAJAXReceivedData = tFound;
		p.AJAXprocessVars();
		p.AJAXprocessArrays(p.vAJAXReceivedData.firstChild);
		p.AJAXdebug();
		p.AJAXprocessExec();
	}
	else
	{
		var AJAXhttp = p.AJAXcreateRequestObject();
		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;

					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(p.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] instanceof Object)
								{
									for (i1 in data[k][j][i])
									{
										if (data[k][j][i][i1] != 'undefined')
											query.push(encodeURIComponent(k) +"["+j+"]["+i+"]["+i1+"]=" + encodeURIComponent(data[k][j][i][i1]));
									}
								}
								else
								{
									if (data[k][j][i])
										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];
//	console.dir(x);
	if (x)
	{
		for (var i=0; i<x.childNodes.length; i++)
		{
			var tt = x.childNodes[i];
//			console.debug(tt.tagName);
			if (tt.tagName == 'VarArrayName' || tt.tagName == 'VAN' || tt.tagName == 'arrays')
			{
				var zz = tt.childNodes[0].nodeValue;
				eval(zz);
			}
		}
	}
//				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 + '\'':j) + ']');
//						}
//						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, "\\'"):j) + "']" +  '= zz'  + ';');
//							}
//						}
//					}
//				}
//			}
//		}
//	}
}

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] == 'this.fOk()')
		this.fOk();
	else if (arguments[0] == 'error')
		this.fError(this.vError[this.vCurrentError]);
	else if (arguments[0] == 'notify')
		this.fNotify(this.vNotify[this.vCurrentNotify]);
}

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 (!p.vDebugWnd)
		{
			p.vDebugWnd = document.getElementById(p.vDebugWndElementName);
			p.vDebugWndContent = document.getElementById(p.vDebugContentElementName);
		}
		if (!p.vDebugWnd)
		{
			tTable = document.createElement('TABLE');
			tTable.border = 1;
			tTable.className = p.vDebugClassName + 'main_tab';
			tTable.height = p.vDebugHeight;
			tTable.id = p.vDebugWndElementName;
			tTbody = document.createElement('TBODY');
			tTr = document.createElement('TR');
			tTr.className = p.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 = p.vPicWidth;
			tImg = document.createElement('IMG');
			tImg.src = p.vPicPath + 'trash.gif';
			tImg.className = 'hand';
			tImg.onclick = function ()
			{
				p.vDebugWndContent.innerHTML = '';
			}
			tTd.appendChild(tImg);
			tTr.appendChild(tTd);

			tTd = document.createElement('TD');
			tTd.width = p.vPicWidth;
			tTd.height = p.vDebugHeightTitle;
			tImg = document.createElement('IMG');
			tImg.src = p.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 = p.vPicWidth;
			tImg = document.createElement('IMG');
			tImg.src = p.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 = p.vDebugClassName + 'tr_content';
			tTd = document.createElement('TD');
			tTd.className = p.vDebugClassName + 'td_content';
			tTd.colSpan = 4;
			tDiv = document.createElement('DIV');
			tDiv.id = p.vDebugContentElementName;
			tDiv.height = p.vDebugHeight - p.vDebugHeightTitle;
			tDiv.style.height = p.vDebugHeight - p.vDebugHeightTitle;
			tDiv.className = p.vDebugClassName + 'div_content';
			p.vDebugWndContent = tDiv;
			tTd.appendChild(tDiv);

			tTr.appendChild(tTd);
			tTbody.appendChild(tTr);
			tTable.appendChild(tTbody);

			document.body.appendChild(tTable);

			p.vDebugWnd = tTable;
		}
		p.vDebugWndContent.innerHTML = '<pre>' + tt[0].childNodes[0].nodeValue + p.vDebugSeparator + p.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) || (cache_string.indexOf('set-') != -1))
		{
			this.vCache = [];
		}
		else
		{
			if (this.vCache[cache_string])
			{
				return this.vCache[cache_string];
			}
			this.vWaitForValue = cache_string;
		}
	}
	return false;
}

xAjax.prototype.fCleanCache = function ()
{
	this.vCache = [];
}
