/*
	AJAX.JS.PHP
	(c) inxhune | www.inxhune.com
*/

//----------------------------------------------
//Ajax v1.0 Source By Bermann
//dobermann75@gmail.com
//참고 : XMLHttpRequest.readyState 상태 정보 (4를 제외한 값은 브라우저마다 차이 있을 수 있으므로 주의)
//0 : UNINITIALIZED (open() 호출전)
//1 : LOADING (send() 호출전)
//2 : LOADED (HTTP 요청후 응답을 받은 상태)
//3 : INTERACTIVE (응답을 받고 있는 중간 상태)
//4 : COMPLETED (모든 요청 응답 완료)
//----------------------------------------------

function Ajax(Method, Url, Async, Type, Params, userFunction, statusFunction, readyStateFunction)
{
	this.Req;
	this.Method = Method;
	this.Url = Url;
	this.Async = Async;
	this.Type = Type;
	this.Params = Params;

	this.userFunction = userFunction;

	this.statusFunction = statusFunction;

	this.readyStateFunction = readyStateFunction;
	this.Result = null;
	this.initialize();
	this.execute();
}

Ajax.prototype =
{
	initialize: function () {
		if (window.ActiveXObject) {
			try {
				this.Req = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
				try {
					this.Req = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e1) {
					return null;
				}
			}
		} else if (window.XMLHttpRequest) {
			try {
				this.Req = new XMLHttpRequest();
			} catch (e) {
				return null;
			}
		}
	},
	callBack: function () {
		if (this.Req.readyState == 4) {
			if (this.Req.status == 200) {

				if (this.Type == "TEXT") {
					this.Result = this.Req.responseText;
				} else if (this.Type == "XML") {
					this.Result = this.Req.responseXML;

				}
				if (typeof this.userFunction == "function") { this.userFunction(); }
			} else {
				this.Result = this.Req.statusText;
				if (typeof this.statusFunction == "function") { this.statusFunction(); }
			}
		} else {
			if (typeof this.readyStateFunction == "function") { this.readyStateFunction(); }
		}
	},
	execute: function () {
		var This = this;
		this.Req.onreadystatechange = function () { This.callBack(); };
		this.Method = (this.Method != "POST" && this.Method != "GET") ? "GET" : this.Method;
		this.Url = (this.Method == "GET" && this.Params) ? this.Url + "?" + this.Params : this.Url;

		this.Async = (this.Async != true && this.Async != false) ? true : this.Async;

		this.Type = (this.Type != "TEXT" && this.Type != "XML") ? "TEXT" : this.Type;
		this.Params = (this.Method == "POST") ? this.Params : "''";
		try {
			this.Req.open(this.Method,this.Url,this.Async);
			if (this.Method == "POST") this.Req.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
			this.Req.send(this.Params);
		} catch (e) {}
	}
}

