js.module('ajax');

var ajax = {

		ajaj : function(url)
		{
			var scr = document.createElement('script');

			scr.type='text/javascript';
			scr.src=url;

			document.getElementsByTagName('head')[0].appendChild(scr);
		}
		,

		tools : {

		swapButtons : function(idon, max, prefix)
		{
			if(prefix == undefined)
				prefix = '';
			else
				prefix += '_';
				
			for(var i=0; i<max; i++)
			{
				if(!$('button_'+prefix+i)) continue;
				
				if(i==idon)
				{
					ajb.setActive('button_'+prefix+i);
				}
				else
				{
					ajb.setNotActive('button_'+prefix+i);
				}
			}
		}
	}
};

var AJAX = function(url, postdata, async, listener, nowait, errlistener, noautoexec)
{
	if(!nowait)
		visual.showWait();

	var loader = js.getXHTTPTransport();
	
	if (loader.overrideMimeType)
	{
		loader.overrideMimeType('text/xml');
	}

	//PRIVATE IMPLEMENTATION
	var onChanged=function(context)
	{
		if(!async && listener)
			throw('Cannot create non-async AJAX with a listener function');
		
		if(context.isComplete() && !nowait)
			visual.hideWait();
		
		if(context.isComplete())
		{
			if(context.isSuccess())
			{
				if(listener)
				{
					if(typeof listener == 'function')
					{
						if(!noautoexec)	context.evalExec();
						listener(context);
						if(!noautoexec)	context.evalPostExec();

					}
					else //if(typeof listener == 'string')
					{
						context.showContent(listener);
						if(!noautoexec)	context.evalExec();
						if(!noautoexec)	context.evalPostExec();
					}
				}
				else
				{
					if(!noautoexec)	context.evalExec();
					if(!noautoexec)	context.evalPostExec();
				}
			}
			else if(errlistener)
			{
				if(!noautoexec)	context.evalExec();
				errlistener(context);
				if(!noautoexec)	context.evalPostExec();
			}
			else
			{
				if(!noautoexec)	context.evalExec();
				if(!noautoexec)	context.evalPostExec();
			}
	
			if(context.getMsg() && !context.suppressMsg)
			{
				if(context.isSuccess())
					visual.showMsg(context.getMsg());
				else
					visual.showErr(context.getMsg());
			}
		}
	}
	
	var doRequest=function(context)
	{
		if(async) loader.onreadystatechange = function () { onChanged(context); };
	
		loader.open(postdata?'POST':'GET', url, async);
		if(postdata) loader.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		loader.send(postdata);
		
		if(!async) 
		{
			return context;
		}
	}

	this.isSuccess = function()
	{
		if(!this.isComplete()) return false;
		
		if(loader.status!=200) return false;
		
		
		if(common.checkBrowser('ie')) //IE
		{
			var xml = new ActiveXObject("Microsoft.XMLDOM");
			xml.loadXML(loader.responseText);					
			var it = xml.getElementsByTagName('response');
			if(it.length==0) return false;		
			return $A(it[0], 'success') == '1';
		}
		else
		{
			if(loader.responseXML == null) return null;

		 	var it = loader.responseXML.getElementsByTagName('response');
		    if(it.length==0) return false;
		
		    if(it[0].hasAttributes)
		    {
		      return $A(it[0], 'success') == '1';
		    }
		}

 		return false;
	}
	
	//PUBLIC FUNCTIONS
	this.getPath = function() { return url; }
	this.isComplete = function() { return loader.readyState==4; }
	this.getNodeContent = function(node) 
	{
		if(!this.isComplete()) return null;
		
		if(common.checkBrowser('ie') || common.checkBrowser('ie7')) //IE
		{
			var xml = new ActiveXObject("Microsoft.XMLDOM");
			xml.loadXML(loader.responseText);					
			var it = xml.getElementsByTagName(node);
			if(it.length==0) return false;
			return it[0].text;
		}
		else
		{
			if(loader.responseXML == null) return null;
			var it = loader.responseXML.getElementsByTagName(node);
			if(it.length==0) return false;
			return it[0].textContent;
		}
	}
	
	this.getContent = function() { return this.getNodeContent('content'); }
	this.getMsg = function() { return this.getNodeContent('msg'); }
	this.getExtra = function() { return this.getNodeContent('extra'); }
	this.getExec = function() { return this.getNodeContent('exec'); }
	this.getPostExec = function() { return this.getNodeContent('postexec'); }
	
	this.showContent = function(target)
	{
		visual.setContent(target, this.getContent());
		visual.show(target);
	}
	this.evalExec = function() { if(this.getExec())	{ eval(this.getExec()); } }
	this.evalPostExec = function() { if(this.getPostExec())	{ eval(this.getPostExec()); } }
	this.suppressMsg = false;
	
	doRequest(this);
};