if (!this.lightAjax) {
	lightAjax = {};
}
(function () {
	var default_action = "write";
	var default_method = "GET";
	var wait_delay = 10000;
	var wait_file = "wait.gif";
	var wait_file_top = 100;
	
	if (typeof lightAjax.create !== "function") {
		lightAjax.create = function () {
        	var xmlHttp;
			try {
				xmlHttp = new XMLHttpRequest ();
			}
			catch (e) {
				var XmlHttpVersions = new Array ("MSXML2.XMLHTTP.6.0",
												"MSXML2.XMLHTTP.5.0",
												"MSXML2.XMLHTTP.4.0",
												"MSXML2.XMLHTTP.3.0",
												"MSXML2.XMLHTTP",
												"Microsoft.XMLHTTP");
				for (var i = 0; i < XmlHttpVersions.length && !xmlHttp; i++) {
					try {
						xmlHttp = new ActiveXObject (XmlHttpVersions[i]);
					}
					catch (e) {}
				}
			}
			if (!xmlHttp) {
				alert ("Can't create xmlHttp object!");
			}
			else
				return xmlHttp;
        }
	}
	var response;
	var xmlHttp = lightAjax.create();

	if (typeof lightAjax.parse !== "function") {
		lightAjax.parse = function (text) {
			var source = text;
			var script, block;
			var pos1, pos2 = 0;
		
			while (source.indexOf("<script") > -1 || source.indexOf("</script") > -1) {
				var script = document.createElement ('script');
        		var s = source.indexOf("<script");
        		var s_e = source.indexOf(">", s);
        		var e = source.indexOf("</script", s);
        		var e_e = source.indexOf(">", e);
				pos1 = source.indexOf ("src=\"");
				pos2 = source.indexOf ("\"", pos1 + 5);
				if (pos1 < s_e && pos1 >= 0)
					script.src = source.substring (pos1 + 5, pos2);
				script.text = source.substring (s_e + 1, e);
				document.getElementsByTagName ("head").item (0).appendChild (script);
				source = source.substring (0, s) + source.substring (e_e+1);
    		}
			return source;
		}
	}

	if (typeof lightAjax.send !== "function") {
		lightAjax.send = function (url, params, ret, action, method, callback) {
			if (!url) return false;
			if (url.indexOf(".") == -1) return false;
			var qdate = new Date();
			params = "?qtime=" + qdate.getTime() + '&' + params;
			if (action != "write" || action != "append") action = default_action;
			method = method.toUpperCase ();
			if (method != "GET" && method != "POST") method = default_method;
			if (typeof callback !== "function") callback = null; else alert ("func");

			if (xmlHttp) {
				try {
					if (method == "GET") {
						xmlHttp.open (method, url + params, true)
						xmlHttp.send (null);
					}
					if (method == "POST"){
						xmlHttp.open (method, url, true);
						xmlHttp.setRequestHeader ("Content-type", "application/x-www-form-urlencoded");
						xmlHttp.setRequestHeader ("Content-length", params.length);
						xmlHttp.setRequestHeader ("Connection", "close");
						xmlHttp.send (params);
					}
					xmlHttp.onreadystatechange = function () {
						if (xmlHttp.readyState == 2) {
							while (document.getElementById('tmp_ajax_div'))
								document.getElementById('tmp_ajax_div').parentNode.removeChild(document.getElementById('tmp_ajax_div'));
							var div = document.createElement ('div');
							div.style.filter ='alpha(opacity=80)';
							div.style.MozOpacity = 0.8;
							div.style.width = 57;
							div.style.height = 12;
							div.style.marginTop = wait_file_top;
							div.id = 'tmp_ajax_div';
							div.style.backgroundImage = "url(" + wait_file + ")";
							document.getElementById (ret.substr (1, ret.length - 1)).appendChild (div);
							if (ret.indexOf("#") > -1) {
								document.getElementById (ret.substr (1, ret.length - 1)).appendChild (div);
							}
							if (ret.indexOf("@") > -1) {
								document.getElementByName (ret.substr (1, ret.length - 1))[0].appendChild (div);
							}
							setTimeout ("if (document.getElementById('tmp_ajax_div')) document.getElementById ('tmp_ajax_div').parentNode.removeChild(document.getElementById('tmp_ajax_div'))", wait_delay);
						}
						if (xmlHttp.readyState == 4) {
							if (xmlHttp.status == 200) {
								if (document.getElementById ('tmp_ajax_div'))
									document.getElementById ('tmp_ajax_div').parentNode.removeChild(document.getElementById('tmp_ajax_div'));
								try {
									if (callback) eval (callback);
									response = lightAjax.parse (xmlHttp.responseText);
									if (ret.indexOf ("#") > -1) {
										ret = ret.substr (1, ret.length - 1);
										ret = document.getElementById (ret);
									}
									else if (ret.indexOf ("@") > -1) {
										ret = ret.substr (1, ret.length - 1);
										ret = document.getElementsByName (ret)[0];
									}
									else
										return response;
									if (action.toLowerCase () == "append") ret.innerHTML += response;
									if (action.toLowerCase () == "write") ret.innerHTML	= response;
								}
								catch (e) {}
							}
						}
					}
				}
				catch (e) {}
			}
		}
	}
})();

