//Requires include default.js
try { var _test_ajax = __include_default; } 
catch(e) { alert("ajax.js : Include '_system/js/default.js' is required"); }
var __include_ajax = true;

//class Ajax
function Ajax() {	
	var unique = true;
	var response_type = "html";
	var debug = false;
	
	var html;
	var xml;
	
	var onLoad = function () { };
	
	var _obj;
	
	this.get = function (tmp_url) {
		if (tmp_url == '') {
			alert("Ajax.get() : Empty url");
			return;
		}
		
		if (this._isFree()) {
			this._createObject();			
			
			_obj.open('GET', tmp_url);
			_obj.setRequestHeader("Content-type", "text/html;charset=iso-8859-1");
			_obj.setRequestHeader("Connection", "close");
			_obj.send(null);
		}
	}
	
	this.post = function (tmp_url, tmp_parameters, tmp_content_type) {
		if (tmp_url == '') {
			alert("Error - Ajax.post() : empty url");
			return;
		}
		
		if (tmp_content_type == undefined) {
			tmp_content_type = "application/x-www-form-urlencoded";
		}
		
		if (this._isFree()) {
			this._createObject();
			
			_obj.open('POST', tmp_url);
			_obj.setRequestHeader("Content-type", tmp_content_type);
			_obj.setRequestHeader("Content-length", tmp_parameters.length);
			_obj.setRequestHeader("Connection", "close");
			_obj.send(tmp_parameters);
		}
	}
	
	this.sendForm = function(tmp_id) {
		var p = "";
		var field;
		var form = $(tmp_id);
		
		for (i = 0; i < form.length; i++) {
			field = form.elements[i];
			
			if (field.name != "") {
				if (field.type == 'checkbox' || field.type == 'radio') {
					if (field.checked) {
						p += '&' + field.name + '=' + field.value.formatVars();
					}
				} else {
					p += '&' + field.name + '=' + field.value.formatVars();
				}
			}
		}
		
		this.post(form.action, p);
	}
	
	this.runJS = function (tmp_html) {
		var __el = document.createElement('div');
		__el.innerHTML = '<div style="display: none">content</div>' + tmp_html; //Ie sucx!
		
		var __scripts = __el.getElementsByTagName('script');
		var __js_exec = "";
		var __total_eval = __scripts.length;
		var __i_eval = 0;
		
		for (; __i_eval < __total_eval; __i_eval++) {
			if (Browse.isIE) {
					__js_exec = __scripts[__i_eval].text;
			} else if (Browse.isMoz) {
					__js_exec = __scripts[__i_eval].textContent;
			} else {
					__js_exec = __scripts[__i_eval].innerHTML;
			}
			
			try {
				eval(__js_exec);
			} catch(e) {
				if (this.debug) {
					alert("Error - Ajax.runJS('" + __js_exec.substr(0, 2048) + "...')\n\nMessage: " + e.message + "\nLine: " + e.lineNumber);
				}
			}
		}
	}
	
	//Private functions
	this._createObject = function () {
		var parent = this;
		
		_obj = this._getAjaxObj();
		_obj.onreadystatechange = function() {
			if(_obj.readyState == 4) {
				var arr;
				
				//xml
				if (response_type == "xml") {
					//.need test
					if (_obj.responseXml != undefined) {
						try {
							parent.xml = _obj.responseXml;
						} catch (e) { }
					} else {
						try {
							parent.xml = parent._parseXML(_obj.responseText);	
						} catch (e) { }
					}
				} else {
					//html
					parent.html = _obj.responseText;
				}
				
				_obj = null;
				
				parent._dispatchEvent('onLoad');
				
				Ajax._unique = false;
			}
		}
	}
	
	this._isFree = function () {		
		var r = (!(this.unique && Ajax._unique)) || !this.unique;
		
		if (r) {
			Ajax._unique = true;	
		}
		
		return r;
	}
	
	this._getAjaxObj = function () {
		var xmlhttp;
		try {
			xmlhttp = new XMLHttpRequest();
		} catch (e) {
			try {
				xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (ex) {
				try {
					xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
				} catch(exc) {
					alert("Error - Ajax._getAjaxObj() : " + exc.error);
				}
			}
		}
		return xmlhttp ? xmlhttp : false;
	}
	
	this._parseXML = function (tmp_string) {
		var xml;
		
		try {
			xml = new ActiveXObject("Microsoft.XMLDOM");
			xml.async = "false";
			xml.loadXML(tmp_string);
		} catch(e) {
			try {
				Parser = new DOMParser();
				xml = Parser.parseFromString(tmp_string,"text/xml");
			} catch(ex) {
				if (this.debug) {
					alert("Error - Ajax._parseXML('" + tmp_string.substr(0, 256) + "...') : " + ex.message);
				}
			}
		}
		
		return xml;
	}
	
	this._dispatchEvent = function (tmp_event) {
		if (typeof(this[tmp_event]) == "function") {
			this[tmp_event]();
		}
	}
}
Class.extend(Ajax, {
	_unique: false
});
