/**
 *	Ajax v2.0
 *
 *	@author		HanSeungho landboy@gmail.com
 *	@copyright	Copyright (c) HanSeungho
 *
 *	작성일 :			2008-12-01
 *	최종수정일 :	2008-12-01
 **/

function Ajax(method, url, params, callBack, async) {
	this.XmlHttpReq = null;
	this.server = "php"; // asp, php, jsp

	this.method = (method.toUpperCase()!="POST"?"GET":"POST"); // post, get
	this.url = url;
	this.params = params; // object : { key1: value1, key2: value2 } || string : key1=value1&key2=value2
	this.callBack = callBack; // function
	this.async = ((async!=true && async!=false)?true:async);
	this.result = null;

	this.init();
	this.execute();
}

Ajax.prototype = {
	init: function () {
		if (window.XMLHttpRequest) {
			// Create XMLHttpRequest object in non-Microsoft browsers
			this.XmlHttpReq = new XMLHttpRequest();
		}
		else if (window.ActiveXObject) {
			// Create XMLHttpRequest via MS ActiveX
			try {
				// Try to create XMLHttpRequest in later versions of Internet Explorer
				this.XmlHttpReq = new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch (e1) {
				// Failed to create required ActiveXObject
				try {
					// Try version supported by older versions of Internet Explorer
					this.XmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
				}
				catch (e2) {
					// Unable to create an XMLHttpRequest with ActiveX
				}
			}
		}
	},

	execute: function () {
		var self = this;

		var params = this.getParams();
		this.url += (this.method=="GET" && params) ? (this.url.indexOf("?")>-1 ? "&" : "?")+params : "";
		params = (this.method=="POST" ? params : null);

		this.url += (this.url.indexOf("?")>-1 ? "&" : "?")+"rnd="+Math.random();

		this.XmlHttpReq.onreadystatechange = function() {
			if (self.XmlHttpReq.readyState == 4) {
				if (self.XmlHttpReq.status == 200) {
					switch (typeof (self.callBack)) {
						case "function":
							self.callBack(self.XmlHttpReq);
						break;
						case "string":
							switch (self.callBack.toUpperCase()) {
								case "TEXT":
									self.result = self.XmlHttpReq.responseText;
								break;
								case "XML":
									self.result = self.XmlHttpReq.responseXML;
								break;
							}
						break;
					}
				}
				else self.result = self.XmlHttpReq.statusText;
			}
			else self.result = "loading ...";
		}

		try {
			this.XmlHttpReq.open(this.method, this.url, this.async);
			if (this.method == "POST") this.XmlHttpReq.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
			this.XmlHttpReq.send(params);
		}
		catch (e){}
	},

	getParams: function () {
		var result = "";
		if (this.params) {
			switch (typeof (this.params)) {
				case "string":
					result = this.params;
				break;
				default:
					for (key in this.params) {
						if (typeof (this.params[key]) != 'function') {
							result += (result ? "&" : "")+key+"="+this.encode(this.params[key]);
						}
					}
				break;
			}
		}
		return result;
	},

	innerHTML: function (obj, content) {
		obj = (typeof obj=='string' ? document.getElementById(obj) : obj);

		// avoid IE innerHTML bug
		content = '<body>' + content.replace(/<\/?head>/gi, '')
					.replace(/<\/?html>/gi, '')
					.replace(/<body/gi, '<div')
					.replace(/<\/body/gi, '</div') + '</body>';
		obj.innerHTML = content;

		var scripts = obj.getElementsByTagName('script');
		if (scripts == false) return true;

		for (var i=0; i<scripts.length; i++) {
			var script = document.createElement('script');
			if (scripts[i].attributes.length > 0) {
				for (var j in scripts[i].attributes) {
					if (
						typeof(scripts[i].attributes[j]) != 'undefined' &&
						typeof(scripts[i].attributes[j].nodeName) != 'undefined' /* IE needs it */ &&
						scripts[i].attributes[j].nodeValue != null &&
						scripts[i].attributes[j].nodeValue != '' /* IE needs it ou il copie des nodes vides */
					) {
						script.setAttribute(scripts[i].attributes[j].nodeName, scripts[i].attributes[j].nodeValue);
					}
				}
			}
			script.text = scripts[i].text;

			if (navigator.userAgent.indexOf("Opera")>0) { return false; }

			scripts[i].parentNode.replaceChild(script, scripts[i]);
		}
		return true;
	},

	encode: function (value) {
		return (this.server=="asp") ? escape(value).replace(/\+/g, '%2B') : encodeURIComponent(value);
	}
}

