/*
 * Ajax72 v1
 */

/*
	AJAX72 OBJECT ---------------------------------------------------------------------------------------------------------------
 */

function Ajax72(){
	this.conf={
		'debug':false,
		'debugMethod':'div', /* div|window */
		'messageStatusBar':false,
		'messageUser':true
	};
	this.method='GET';
	this.requestUri='';
	this.loaded=false;
	this.initOk=false;
	this.debugWindow=null;
	this.queue=new Array();
	this.queueInterval=null;
}

Ajax72.prototype.debugOpenWindow=function(){
	var h='<html><head><title>Ajax72 debug window</title>';
	h+='<style type="text/css"><!-- pre{border:1px solid #aaa;margin:2px;} --></style>';
	h+='</head><body><h1>Ajax72 debug</h1><div id="ajax72Debug"></div></body></html>';
	this.debugWindow=window.open('about:blank','Ajax72-debug');
	this.debugWindow.document.write(h);
}

Ajax72.prototype.debug=function(func,str,clear){
	if(this.conf.debug){
		var dd=false,dw=false,d=null;
		if(this.conf.debugMethod=='div') dd=true;
		else if(this.conf.debugMethod=='window') dd=true;
		else return(false);
		var t=str;
		if(str!=''){
			t='<strong>'+(new Date()).toString()+'</strong><br />\n<strong>'+func+'</strong><br />'+str+'<hr />\n';
		}
		if(dw){
			var r=3;
			do{
				try{
					d=this.debugWindow.document.getElementById('ajax72Debug');
				}catch(e){
					this.debugOpenWindow();
				}
			}while((!d)&&(r-->0));
			if((!d)&&(!this.debugWindow)) this.message('Can\'t open debug window. Please check your browser settings.');
		}else if(dd){
			d=document.getElementById('ajax72Debug');
		}
		if(!d) return(false);
		var c=(typeof(clear)!='undefined');
		var b=(c)?'':d.innerHTML;
		var t=str;
		if(str!=''){
			t='<strong>'+(new Date()).toString()+'</strong><br />\n<strong>'+func+'</strong><br />'+str+'<hr />\n'+b;
		}
		d.innerHTML=t;
	}
}

Ajax72.prototype.htmlentities=function(str){
	str=str.replace(/&/g,'&amp;');
	str=str.replace(/</g,'&lt;');
	str=str.replace(/>/g,'&gt;');
	return(str);
}

Ajax72.prototype.message=function(str){
	this.messageUser(str);
	this.messageStatusBar(str);
}

Ajax72.prototype.messageUser=function(str){
	if(this.conf.messageUser){
		alert('Ajax72:\n'+str);
	}
}

Ajax72.prototype.messageStatusBar=function(str){
	if(this.conf.messageStatusBar){
		window.status=((str!='')?'Ajax72: ':'')+str;
	}
}

Ajax72.prototype.call=function(funcName,funcArgs){
	if(this.initOk){
		var i,s=new Array();
		for(i=0;i<funcArgs.length;i++) s[i]=funcArgs[i];
		this.debug('call','Remote function called:<pre>'+funcName+'('+s.join(', ')+')</pre>');
		var r=this.createRequestObject();
		if(r){
			var uri=this.requestUri;
			var params='';
			var postData=null;
			uri+=(this.requestUri.indexOf('?')==-1)?'?':'&';
			params+='ajax72func='+funcName;
			params+='&ajax72ts='+(new Date()).getTime();
			for(i=0;i<funcArgs.length;i++) params+='&ajax72argv[]='+funcArgs[i];
			this.debug('call','Method=<pre>'+this.method+'</pre>URL=<pre>'+uri+'</pre>Params=<pre>'+params+'</pre>');
			if(this.method=='GET') uri+=params;
			r.open(this.method,uri,true);
			if(this.method=='POST'){
				postData=params;
				try{
					r.setRequestHeader('Method','POST '+uri+' HTTP/1.1');
					r.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
				}catch(e){
					this.debug('call','Can\'t create POST request.');
					var t='Your browser does not appear to support asynchronous requests using POST.';
					this.message(t);
					return(false);
				}
			}
			r.onreadystatechange=function(){
				if(r.readyState!=4) return;
				if(r.status==200){
					ajax72.debug('call','Received:<pre>'+ajax72.htmlentities(r.responseText)+'</pre>');
					if(r.responseXML&&r.responseXML.documentElement) ajax72.processResponse(r.responseXML);
				}else{
					ajax72.debug('call','Status: '+r.status);
					if(r.status==404) ajax72.messageUser('"'+ajax72.requestUri+'" doesn\'t exist.\nRequest status is 404.');
					else ajax72.messageUser('An unexpected error occurred.\nRequest status is '+r.status+'.');
				}
			}
			r.send(postData);
			this.waitingCursor(true);
			this.debug('call','Send done.');
			this.messageStatusBar('Waiting for data...');
		}
	}else{
		if(this.queueInterval==null) this.queueInterval=setInterval('ajax72.processQueue();',50);
		this.debug('Queue: Add '+funcName+'().');
		this.messageStatusBar('Add '+funcName+'() to queue.');
		this.queue[this.queue.length]={'func':funcName,'args':funcArgs};
	}
}

Ajax72.prototype.processQueue=function(){
	if(this.initOk){
		clearInterval(this.queueInterval);
		this.queueInterval=null;
		var i,o;
		for(i in this.queue){
			o=this.queue[i];
			this.debug('Queue: Delayed launch of '+o.func+'().');
			this.messageStatusBar('Launch '+o.func+'() from queue.');
			this.call(o.func,o.args);
			delete o;
		}
	}
}

Ajax72.prototype.processResponse=function(response){
	this.debug('processResponse','Processing data...');
	this.messageStatusBar('Processing data...');
	var xml=response.documentElement;
	var a,c,o,i,j,n,t,v;
	var attr=new Array('a','t');
	var r=xml.childNodes[0];
	for(i=0;i<xml.childNodes.length;i++){
		o=xml.childNodes[i];
		if(o.nodeType!=1) continue;
		if(o.nodeName=='cmd'){
			c=o.getAttribute('c');
			v=(o.firstChild)?o.firstChild.nodeValue:'';
			a=new Array();
			for(j in attr){
				if(t=o.getAttribute(attr[j])) a[attr[j]]=t;
			}
			try{
				if(c=='js'){
					n='JS Eval';
					v=v.replace(/[\n]/g,'\\'+'n');
					eval(v);
				}else if(c=='al'){
					n='Alert';
					this.cmd_alert(v);
				}else if(c=='as'){
					n='Assign';
					this.cmd_assign(a,v);
				}else if(c=='inc'){
					n='Include';
					this.cmd_include(v);
				}
			}catch(e){
				this.debug('processResponse','Can\'t do "'+n+'" operation.\nValue: "'+v+'").\nEvent: '+e);
				this.messageUser('An error occured while processing received data.');
			}
		}
	}
	this.messageStatusBar('');
	this.waitingCursor(false);
}

Ajax72.prototype.createRequestObject=function(){
	var rv=null;
	if(typeof XMLHttpRequest!='undefined') rv=new XMLHttpRequest();
	if(!rv){
		if(typeof(ActiveXObject)!='undefined'){
			try{rv=new ActiveXObject('Msxml2.XMLHTTP');}
			catch(e){
				try{rv=new ActiveXObject('Microsoft.XMLHTTP');}
				catch(e2){
					try{rv=new ActiveXObject('Msxml2.XMLHTTP.4.0');}
					catch(e3){
						rv=null;
					}
				}
			}
		}
	}
	if(!rv&&window.createRequest) req=window.createRequest();
	if(!rv) this.debug('createRequestObject','Request object instantiation failed.');
	return(rv);
}

Ajax72.prototype.cmd_include=function(str){
	var h=document.getElementsByTagName('head');
	var o=document.createElement('script');
	o.type='text/javascript';
	o.src=str;
	h[0].appendChild(o);
}

Ajax72.prototype.cmd_alert=function(v){
	alert(v);
}

Ajax72.prototype.cmd_assign=function(attr,v){
	var t=(typeof(attr.t)=='string')?document.getElementById(attr.t):null;
	var a=(typeof(attr.a)=='string')?attr.a:'';
	if(t&&a&&typeof(t[a])=='string'){
		t[a]=v;
	}
}

Ajax72.prototype.cmd_js=function(str){
	eval(str);
}

Ajax72.prototype.waitingCursor=function(on){
	var cursor=(on)?'wait':'default';
	document.body.style.cursor=cursor;
}

/*
	CODE ------------------------------------------------------------------------------------------------------------------------
 */
var ajax72=new Ajax72();
ajax72.loaded=true;
