// PostBackPrototype
//gDebug = true;
function PostBackPrototype()
{
	this.async = false;
	this.callback = null;
	this.asyncHttp();

	this.currentStatus = false;
	this.oMessage = null;
	this.querystring = [];

	this.rule = null;

	this._visible = [];
	this._hide = [];

	this.bProc = false;
}

PostBackPrototype.prototype = {
	setValue:function(name, value)
	{
		this.querystring.push(name + "=" + escape(value));
	},
	getValue:function()
	{
		data = this.querystring.join("&");
		this.querystring.length = 0;
		return data;
	},
	setValueFromObject:function(obj)
	{
		for (var key in obj)
		{
			this.setValue(key, obj[key]);
		}
		this.setValue('_rnd', Math.random());
	},
	initRule:function(oRuleData)
	{
		this.rule = oRuleData;
	},
	asyncHttp:function(bAsync)
	{
		var _this = this.asyncHttp;
		var _obj = this;

		if (bAsync != undefined)
			_obj.async = bAsync;

		_this.xmlhttp = null;

		try {
			_this.xmlhttp = new XMLHttpRequest();
		} catch (trymicrosoft) {
			try {
				_this.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (othermicrosoft) {
				try {
					_this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (failed) {
					_this.xmlhttp = false;
				}  
			}
		}

		_this.post = function(url, query)
		{
			if (_obj.async)
			{ // 비동기 방식
				_this.xmlhttp.open('POST', url, true); 
				_this.xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
				_this.xmlhttp.send(query); 

				_this.xmlhttp.onreadystatechange = function()
				{
					if (_this.xmlhttp.readyState != 4 || _this.xmlhttp.status != 200)
						return false;

					_obj.bProc = false;

					var objWaitInfoDiv = null;

					if ((objWaitInfoDiv = $("wait-info")) != undefined)
					{
//						$("overlay").style.display = "none";
						objWaitInfoDiv.style.display = "none";
					}

					if(_obj.callBack)
					{
						if (gDebug != undefined && gDebug == true)
						{
							alert(_this.xmlhttp.responseText);
						}
						_obj.callBack(_this.xmlhttp.responseXML);
					}
				}

				return true;
			}
			else
			{ // 동기 방식
				_this.xmlhttp.open('POST', url, false); 
				_this.xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
				_this.xmlhttp.send(query); 
				
				if (gDebug != undefined && gDebug == true)
				{
					alert(_this.xmlhttp.responseText);
				}

				return _this.xmlhttp.responseXML;
			}
		}

		return _this;
	},
	doSubmit:function(targetURL, callBack)
	{
		if (targetURL == undefined)
			var targetURL = (gSelf == undefined) ? '/' : gSelf;

		if (this.bProc == true)
		{
			alert(getMsg("ALREDY_CONNECTION"));
			return false;
		}

		this.bProc = true;

		if (callBack != undefined)
		{
			this.callBack = callBack;

			this.async = true;

			var objWaitInfoDiv = null;

			if ((objWaitInfoDiv = $("wait-info")) != undefined)
			{
//				$("overlay").style.display = "block";
				objWaitInfoDiv.style.display = "block";
			}
		}

		var obj = this.asyncHttp(this.async);
		var oXML = obj.post(targetURL, this.getValue());


		if (oXML == undefined)
		{
			alert("서버와의 통신 상태가 고르지 않습니다.");
			return false;
		}

		if (this.async == true)
		{
			return true;
		}

		this.bProc = false;

		var oError = oXML.selectSingleNode("//response/error");
		this.oMessage = oXML.selectSingleNode("//response/message");


		if (oError == undefined)
		{
			alert("서버로 부터 정상적인 데이터를 수신하지 못하였습니다. [ERR-1]");
			return false;
		}

		if (parseInt(oError.text) == 1)
		{
			if (this.oMessage != undefined)
			{
				alert("서버로 부터 정상적인 데이터를 수신하지 못하였습니다.\n\n오류 내용 : " + this.oMessage.text);
			}
			else
			{
				alert("서버로 부터 정상적인 데이터를 수신하지 못하였습니다. [ERR-2]");
			}
			return false;
		}

		return true;
	}
}
