var xmlHttp;
var json = {
	query_dat : [],
	is_active : false,
	go : function ( r_url, r_cb, r_mth ){
		if ( typeof(r_mth) == 'undefined') r_mth = false;
		json.query_dat.unshift({ url : r_url, is_data : r_mth, call_back : r_cb });
		if ( !json.is_active ){
			json.is_active = true;
			return json.connect();
		}
		return true;
	},
	current_mth : false,
	current_cb : null,
	connect : function(){
		if ( json.query_dat.length == 0 ) return;
		xmlHttp = null;
		try{ xmlHttp = new XMLHttpRequest(); } 
		catch (e) { 
			try { xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); } 
			catch (e) {	xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");}
		}
		if ( xmlHttp == null ) { alert ('Browser does not support HTTP Request.'); return false; }
		var tmp = json.query_dat.pop();
		json.current_mth = tmp.is_data;
		json.current_cb = tmp.call_back;
		if (tmp.url.search(/\?/) == -1 ) { tmp.url += '?RANDOMISE=' + Math.random(9999); }
		else { tmp.url += '&RANDOMISE=' + Math.random(9999); }
		xmlHttp.onreadystatechange = json.status;
		xmlHttp.open("GET",tmp.url,true);
		xmlHttp.send(null);
		return;
	},
	status : function(){
		if (xmlHttp.readyState == 4 || xmlHttp.readyState == 'complete' ){
			if (typeof (json.current_cb) == 'function' ) {
				if ( xmlHttp.responseText == '' ) {  json.current_cb ( null ); }
				if ( json.current_mth == false ) { json.current_cb ( xmlHttp.responseText ); }
				else {
					try{ 
						var tmp = eval( ("(" + xmlHttp.responseText+ ");") );
						json.current_cb ( tmp );
					} catch( err ){
						json.current_cb ( null );
					}
				}
			}
			if ( json.query_dat.length == 0 ) { json.is_active = false; }
			else { json.connect(); }
		}
	},
	js_include : function ( sf ) {
		var html_doc = document.getElementsByTagName('head').item(0);
		var js = document.createElement('script');
		js.setAttribute('language', 'javascript');
		js.setAttribute('type', 'text/javascript');
		js.setAttribute('src', sf);
		html_doc.appendChild(js);
		return false;
	}
}

var AIM = {
	frame : function(c) {
		var n = 'f' + Math.floor(Math.random() * 99999);
		var d = document.createElement('DIV');
		d.id = 'd'+n;
		d.innerHTML = '<iframe style="display:none" src="about:blank" id="'+n+'" name="'+n+'" onload="AIM.loaded(\''+n+'\')"></iframe>';
		document.body.appendChild(d);
		var i = document.getElementById(n);
		if (c && typeof(c.after) == 'function') {
			i.after = c.after;
		}
		return n;
	},
	form : function(f, name) {	
		f.setAttribute('target', name); },
	send : function(f, c) {
		if (c && typeof(c.before) == 'function') {
			var d = c.before();
			if ( d === false ) return false;
			AIM.form(f, AIM.frame(c));
			return true;
		} else {
			AIM.form(f, AIM.frame(c));
			return true;
		}
	},
	loaded : function(id) {
		var i = document.getElementById(id);
		if (i.contentDocument) {
			var d = i.contentDocument;
		} else if (i.contentWindow) {
			var d = i.contentWindow.document;
		} else {
			var d = window.frames[id].document;
		}
		if (d.location.href == "about:blank") {	return; }
		if (typeof(i.after) == 'function') {
			if ( typeof(i.is_data) != undefined && i.is_data ){
				try{ 
					var tmp = eval( ("(" + d.body.innerHTML + ");") );
					i.after ( tmp );
				} catch( err ){
					i.after ( null );
				}
			}
			else i.after(d.body.innerHTML);
		}
		document.body.removeChild(document.getElementById('d'+id));
	}
}

var tpl = {
	obj : [],
	add : function ( tobj ){
		if ( tobj.name == null ) { return -1;}
		if ( tobj.source != null ) {
			if ( typeof(tobj.source) == 'object' ) { tobj.tpl = tobj.source.innerHTML; tobj.is_inner = true;}
			if ( typeof(tobj.source) == 'string' ) {
				flag = tobj.source.substr(0,1);
				if ( flag == '$' ||  flag == '#' ) tobj.source = tobj.source.substr(1);
				tobj.source = document.getElementById(tobj.source);
				if ( flag == '$' ) { 
					var str = tobj.source.value;
					tobj.tpl = str.replace(/TxA/g,'textarea');
					tobj.is_inner = false;
				}
				else { tobj.tpl = tobj.source.innerHTML; tobj.is_inner = true; }
			}
		}
		if ( tobj.target != null && typeof(tobj.target) == 'string'){ 
			flag = tobj.target.substr(0,1);
			if ( flag == '$' ||  flag == '#' ) tobj.target = tobj.target.substr(1);
			tobj.target = document.getElementById(tobj.target);
		}
		for ( var i=0; i<tpl.obj.length;i++) if (tpl.obj[i].name == tobj.name) { tpl.obj[i] = tobj; return; }
		tpl.obj[tpl.obj.length] = tobj;
	},
	get : function ( par1, par2 ){
		if ( typeof(par1) == 'string' ) { sid = par1; rpv = par2; }
		else { sid= par1.name; rpv = par1; }
		if ( sid == 'undefined' ) { return false; }
		for (var i=0; i<tpl.obj.length; i++) if (tpl.obj[i].name == sid) break;
		if ( i == tpl.obj.length ) { return false;}
		t = tpl.obj[i];
		if ( t.source != null ){
			if (t.is_inner) t.tpl = t.source.innerHTML;
			else t.tpl = t.source.value;
			t.tpl = t.tpl.replace(/TxA/g,'textarea');
		}
		ret_str = '';
		if ( t.pre != null ) ret_str = t.pre;
		if ( rpv != null){
			if ( rpv.length == null ) { rpv = [rpv]; }
			for ( var j=0; j<rpv.length; j++ ){
				obj_def = rpv[j];
				tmp_str = t.tpl;
				for ( var ac_key in obj_def ){
					var txt_reg =new RegExp('{'+ac_key+'}','g');
					tmp_str = tmp_str.replace( txt_reg, obj_def[ac_key]);
				}
				var txt_reg =new RegExp('{index}','g');
				tmp_str = tmp_str.replace( txt_reg, j);
				ret_str += tmp_str;
			}
		} else { ret_str = tpl.obj[i].tpl; }
		if ( t.post != null ) ret_str += t.post;
		if ( t.target == null ) return ret_str;
		t.target.innerHTML = ret_str;
		return;
	}
}
