//////////////////////////////////////////////////////////////////
//
// programed by kim dong man
//
//////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////
// AJAX
//////////////////////////////////////////////////////////////////

var g_kGlobalAjaxSelf = null;
var g_kGlobalAjaxXmlCodeErrorFunction = null;
function kGlobalAjaxXmlCodeErrorFunction(func)
{
	g_kGlobalAjaxXmlCodeErrorFunction = func;
}

function kXmlCode() {
	var code = [];

	this._getnode = function(node) {
		if(node != null && node.nodeName == '#text') node = node.nextSibling;
		return node;
	}

	this.xml = function(xml) {
	    try {
		var kcode= xml.getElementsByTagName("kcode")[0];
		if(typeof kcode == "undefined") return null;

		delete code;

		var curNode = this._getnode(kcode.firstChild);

		while(curNode != null) {
			var itemNode = this._getnode(curNode.firstChild);
			var data = {};
			while(itemNode != null) {
				var name = itemNode.nodeName;
				var value = itemNode.firstChild.nodeValue;
				data[name] = value.replace(/&lt;/g, "<").replace(/&gt;/g, ">");
				if(data[name] == "#empty#") data[name] = "";
				itemNode = this._getnode(itemNode.nextSibling);
			}

			code.push(data);
			curNode = this._getnode(curNode.nextSibling);
		}
	    } catch(e) {
		if(g_kGlobalAjaxXmlCodeErrorFunction == null) alert('kXmlCode::xml parse error');
		else g_kGlobalAjaxXmlCodeErrorFunction();
	    }

		//code.reverse();
		//for(i=0; i<code.length; i++) alert(code[i]['name']);
	}

	this.get = function(no, name) {
		try {
			if(typeof code[no][name] == "undefined") return "";
			return code[no][name];
		} catch(e) {
			return "";
		}
	}

	this.get_count = function() {
		try {
			return code.length;
		} catch(e) {
			return 0;
		}
	}
}

function kAjaxCB() {
	this.cb_xml = function(ajax, cb_func, param, param2, issilent) {
		return function() {
			if (ajax.xmlreq.readyState == 4) {
				if (ajax.xmlreq.status == 200) {
					cb_func(ajax.xmlreq.responseXML, param, param2);
				}
				else if(ajax.xmlreq.status == 404) {
					// Requested URL is not found
				}
				else if(ajax.xmlreq.status == 403) {
					// Access denied
				}
				ajax._Done(issilent);
			}
		}
	}
	this.cb_text = function(ajax, cb_func, param, param2, issilent) {
		return function() {
			if (ajax.xmlreq.readyState == 4) {
				if (ajax.xmlreq.status == 200) {
					cb_func(ajax.xmlreq.responseText, param, param2);
				}
				else if(ajax.xmlreq.status == 404) {
					// Requested URL is not found
				}
				else if(ajax.xmlreq.status == 403) {
					// Access denied
				}
				ajax._Done(issilent);
			}
			//alert(ajax.xmlreq.readyState + " , " + ajax.xmlreq.status);
		}
	}
}

function kAjax()
{
	var request_type = 0;
	var is_running = false;
	var silentmode_ontime = false;

	var xmlreq = null;
	var header = new Object(); header.list = new Array();
	var postv = [];
	var stack_cb = new Array();

	var func_start = null;
	var func_end = null;


	// [] Array, {} Object

	this.isRunning = function() { return is_running; }

	this.CB_Debug = function(html) {
		alert(html);
	}

	this._startUp = function() {
		if(this.xmlreq != null) return ;

		g_kGlobalAjaxSelf = this;

		if (window.XMLHttpRequest) {
			this.xmlreq = new XMLHttpRequest();
		} else if (window.ActiveXObject) {
			try {
				this.xmlreq = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e1) {
				try {
					this.xmlreq = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e2) {
					this.xmlreq = null;
				}     
			}
		}
	}

	this.__Done = function(issilent) {
		this.xmlreq.abort();
		if(issilent == false && func_end != null) func_end();

		if(typeof stack_cb == "undefined" || stack_cb.length <= 0) {
			is_running = false;
			return ;
		}

		var a = stack_cb.pop();
		switch(a[0]) {
		case 0: this._XML(a[1], a[2], a[3], a[4], true); break;
		case 1: this._HTML(a[1], a[2], a[3], a[4], true); break;
		case 2: this._XML_POST(a[1], a[5], a[2], a[3], a[4], true); break;
		case 3: this._HTML_POST(a[1], a[5], a[2], a[3], a[4], true); break;
		}
	}
	this._Done = function(issilent) {
		if(issilent == true) setTimeout("g_kGlobalAjaxSelf.__Done(true)", 1);
		else setTimeout("g_kGlobalAjaxSelf.__Done(false)", 1);

	}

	this.ClearHeader = function() {
		header.splice(0, header.list.length);
	}

	this.SetHeader = function(name, value) {
		//if(typeof(header[name]) == 'undefined') {
		header[name] = value;
		header.list[header.list.length] = name;
		//}
	}

	this.SetFunc_Start = function(func) {
		func_start = func;
	}
	this.SetFunc_End = function(func) {
		func_end = func;
	}
	this.SetSilentMode_Onetime = function() {
		silentmode_ontime = true;
	}

	this._XML = function(url, cb_func, param, param2, isforce, issilent) {
		if(isforce == false && is_running == true) {
			stack_cb.push([0, url, cb_func, param, param2, null]);
			return ;
		}
		is_running = true;

		this._startUp();

		//this.SetHeader("Content-Type", "application/x-www-form-urlencoded;charset=euc-kr");
		//this.SetHeader("Pragma", "no-cache");
		//this.SetHeader("Cache-Control", "no-cache,must-revalidate");

		this.xmlreq.open("GET", url, true);

		var k = new kAjaxCB();
		this.xmlreq.onreadystatechange = k.cb_xml(this, cb_func, param, param2, issilent);

		for(var i=0; i<header.list.length; i++) {
			this.xmlreq.setRequestHeader(header.list[i], header[header.list[i]]);
		}
		this.xmlreq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=euc-kr");
		this.xmlreq.setRequestHeader("Pragma", "no-cache");
		this.xmlreq.setRequestHeader("Cache-Control", "no-cache,must-revalidate");

		if(issilent == false && func_start != null) func_start();
		this.xmlreq.send(null);

		delete k;
	}

	this._HTML = function(url, cb_func, param, param2, isforce, issilent) {
		if(isforce == false && is_running == true) {
			stack_cb.push([1, url, cb_func, param, param2, null]);
			return ;
		}
		is_running = true;

		this._startUp();

		//this.SetHeader("Content-Type", "text/html;charset=euc-kr");
		//this.SetHeader("Content-Type", "application/x-www-form-urlencoded;charset=euc-kr");
		//this.SetHeader("Pragma", "no-cache");
		//this.SetHeader("Cache-Control", "no-cache,must-revalidate");

		this.xmlreq.open("GET", url, true);

		var k = new kAjaxCB();
		this.xmlreq.onreadystatechange = k.cb_text(this, cb_func, param, param2, issilent);

		for(var i=0; i<header.list.length; i++) {
			this.xmlreq.setRequestHeader(header.list[i], header[header.list[i]]);
		}
		this.xmlreq.setRequestHeader("Content-Type", "text/html;charset=euc-kr");
		this.xmlreq.setRequestHeader("Pragma", "no-cache");
		this.xmlreq.setRequestHeader("Cache-Control", "no-cache,must-revalidate");

		if(issilent == false && func_start != null) func_start();
		this.xmlreq.send(null);

		delete k;
	}

	this._XML_POST = function(url, data, cb_func, param, param2, isforce, issilent) {
		if(isforce == false && is_running == true) {
			stack_cb.push([2, url, cb_func, param, param2, data]);
			return ;
		}
		is_running = true;

		this._startUp();

		//this.SetHeader("Content-Type", "application/x-www-form-urlencoded;charset=euc-kr");
		//this.SetHeader("Content-length", data.length);
		//this.SetHeader("Connection", "Close");
		//this.SetHeader("Pragma", "no-cache");
		//this.SetHeader("Cache-Control", "no-cache,must-revalidate");

		this.xmlreq.open("POST", url, true);

		var k = new kAjaxCB();
		this.xmlreq.onreadystatechange = k.cb_xml(this, cb_func, param, param2, issilent);

		for(var i=0; i<header.list.length; i++) {
			this.xmlreq.setRequestHeader(header.list[i], header[header.list[i]]);
		}
		this.xmlreq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=euc-kr");
		this.xmlreq.setRequestHeader("Content-length", data.length);
		this.xmlreq.setRequestHeader("Pragma", "no-cache");
		this.xmlreq.setRequestHeader("Cache-Control", "no-cache,must-revalidate");

		if(issilent == false && func_start != null) func_start();
		this.xmlreq.send(data);

		delete k;
	}

	this._HTML_POST = function(url, data, cb_func, param, param2, isforce, issilent) {
		if(isforce == false && is_running == true) {
			stack_cb.push([3, url, cb_func, param, param2, data]);
			return ;
		}
		is_running = true;

		this._startUp();

		//this.SetHeader("Content-Type", "text/html;charset=euc-kr");
		//this.SetHeader("Content-Type", "application/x-www-form-urlencoded;charset=euc-kr");
		//this.SetHeader("Content-length", data.length);
		//this.SetHeader("Content-length", 0);
		//this.SetHeader("Connection", "Close");
		//this.SetHeader("Pragma", "no-cache");
		//this.SetHeader("Cache-Control", "no-cache,must-revalidate");

		this.xmlreq.open("POST", url, true);

		var k = new kAjaxCB();
		this.xmlreq.onreadystatechange = k.cb_text(this, cb_func, param, param2, issilent);

		for(var i=0; i<header.list.length; i++) {
			this.xmlreq.setRequestHeader(header.list[i], header[header.list[i]]);
		}
		this.xmlreq.setRequestHeader("Content-Type", "text/html;charset=euc-kr");
		this.xmlreq.setRequestHeader("Content-length", data.length);
		this.xmlreq.setRequestHeader("Pragma", "no-cache");
		this.xmlreq.setRequestHeader("Cache-Control", "no-cache,must-revalidate");

		if(issilent == false && func_start != null) func_start();
		this.xmlreq.send(data);

		delete k;
	}

	this.XML = function(url, cb_func, param, param2) {
		var issilent = silentmode_ontime;
		silentmode_ontime = false;
		this._XML(url, cb_func, param, param2, false, issilent);
	}
	this.HTML = function(url, cb_func, param, param2) {
		var issilent = silentmode_ontime;
		silentmode_ontime = false;
		this._HTML(url, cb_func, param, param2, false, issilent);
	}
	this.XML_POST = function(url, cb_func, param, param2) {
		var data = '';
		var n = postv.length;

		for(var i=0; i<n; i++) {
			var p = postv.pop();
			if(i!=0) data += "&";
			data += p[0] + "=" + encodeURIComponent(p[1]);
		}

		var issilent = silentmode_ontime;
		silentmode_ontime = false;

		this._XML_POST(url, data, cb_func, param, param2, false, issilent);
	}
	this.HTML_POST = function(url, cb_func, param, param2) {
		var data = '';
		var n = postv.length;

		for(var i=0; i<n; i++) {
			var p = postv.pop();
			if(i!=0) data += "&";
			data += p[0] + "=" + encodeURIComponent(p[1]);
		}

		var issilent = silentmode_ontime;
		silentmode_ontime = false;

		this._HTML_POST(url, data, cb_func, param, param2, false, issilent);
	}

	this.PostV = function(name, value) {
		postv.push([name, value]);
	}
}

//////////////////////////////////////////////////////////////////
// end of AJAX
//////////////////////////////////////////////////////////////////
