/*-----------------------------------------------------------------------------------------
* XML http Request
* ÀÛ¼ºÀÏ : 2005.08.11.
* ¼öÁ¤ÀÏ : 2006.11.30.
* ÀÛ¼ºÀÚ : À¯¿µÀç
-----------------------------------------------------------------------------------------*/

//-----------------------------------------------------------------------------------
// XML http Request

XMLhttpReq = function(method, action, formName) 
{
	try 
	{
		this.formName = formName;
		this.method = method;
		this.action = action;
		this.type = 0;					// 1 : Mozilla/Safari, 2 : IE
		this.run = false;				// ÇöÀç µ¿ÀÛ ÁßÀÎÁö È®ÀÎ
		this.showLoadingMsg = true;
		this.xmlHttpReq = false;
		this.cache = false;
		this.hideMessage = false;
		
		if (window.XMLHttpRequest) // Mozilla/Safari
		{
			this.type = 1;
			this.xmlHttpReq = new XMLHttpRequest();
			this.xmlHttpReq.overrideMimeType('text/xml');
		}
		else if (window.ActiveXObject) // IE
		{
			this.type = 2;
			this.xmlHttpReq = new ActiveXObject(getXmlHttpPrefix() + ".XmlHttp");
		}
	}
	catch (e) {};
};

XMLhttpReq.prototype.fireHttp = function(strSubmit, strResultFunc, dataTtype)
{
	var type = this.type;	// 1 : Mozilla/Safari, 2 : IE
	var thisObj = this;

	if (!this.run)
	{
		this.xmlHttpReq.open(this.method, this.action, true);	// true : ºñµ¿±â¸ðµå(ÀÌº¥Æ®·Î »óÅÂ ¹ÝÈ¯), false : µ¿±â ¸ðµå(ÀÀ´ä È®ÀÎÈÄ °è¼ÓÁøÇà)
//		this.xmlHttpReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");
		this.xmlHttpReq.setRequestHeader("Content-type", (this.method.toUpperCase() == "POST")?"application/x-www-form-urlencoded":"text/xml");
		if (!this.cache)
		{
			this.xmlHttpReq.setRequestHeader("Cache-Control", "no-cache"); 
			this.xmlHttpReq.setRequestHeader("Pragma", "no-cache"); 
		}
		this.xmlHttpReq.setRequestHeader("Referer", this.action); 

		this.xmlHttpReq.onreadystatechange = function() 
		{
/*
		(0)Uninitialized °³Ã¼°¡ »ý¼ºµÇ¾úÁö¸¸ ÃÊ±âÈ­µÇÁö´Â ¸øÇß´Ù.(open ¸Þ¼­µå¸¦ È£ÃâÇÏÁö ¾ÊÀº »óÅÂ) 
		(1)Loading °³Ã¼°¡ »ý¼ºµÇ¾ú°í ÃÊ±âÈ­µÇ¾úÁö¸¸ send ¸Þ¼­µå¸¦ È£ÃâÇÏÁö´Â ¾ÊÀº »óÅÂ 
		(2)Loaded send ¸Þ¼­µå°¡ È£ÃâµÇ¾úÀ¸³ª ¾ÆÁ÷ ÀÀ´ä´Ü°è´Â ¾Æ´Ô 
		(3)Interactive ºÎºÐ µ¥ÀÌÅÍ°¡ Àü¼Û. responseBody³ª responseText¸¦ »ç¿ëÇÏ¿© ÀÌ ºÎºÐ µ¥ÀÌÅÍ¸¦ °¡Á®¿Ã ¼ö ÀÖ´Ù. 
		(4)Completed ¸ðµç µ¥ÀÌÅÍ°¡ Àü¼Û. responseBody³ª responseText¸¦ »ç¿ëÇÑ ¸ðµç µ¥ÀÌÅÍ Àü¼Û ¿Ï·á 
*/			
			if (thisObj.xmlHttpReq.readyState == 4) // ¿Ï·á
			{
				thisObj.run = false;
				
				if (thisObj.xmlHttpReq.status == 200) // Á¤»óÀûÀ¸·Î µ¥ÀÌÅÍ ¼ö½Å
				{
					switch(String(dataTtype).toUpperCase())
					{
						case "JSON" :
							try 
							{	
								eval("var data = " + thisObj.xmlHttpReq.responseText + ";");
//								var data = eval(thisObj.xmlHttpReq.responseText);
							} 
							catch(e) 
							{
//								alert("code:" + e.number +",\n¼³¸í:" + e.description + ",\n" + thisObj.xmlHttpReq.responseText); 
								var data = false; 
							};
							break;
							
						case "XML" :
							var data = (type == 1)?thisObj.xmlHttpReq.responseXML:thisObj.xmlHttpReq.responseXML.documentElement;
							break;
							
						case "TEXT" :
						default :
							var data = thisObj.xmlHttpReq.responseText;
							break;
					}

					if (typeof(strResultFunc) == "function")
						strResultFunc(data);
					else
						eval(strResultFunc + "(data);");
				}
				else
				{
					if (!thisObj.hideMessage)
						alert("AJAX Error : Á¤»óÀûÀ¸·Î µ¥ÀÌÅÍ¸¦ ¼ö½ÅÇÏÁö ¸øÇß½À´Ï´Ù.");

//					eval(strResultFunc + "(false);");// ºñÁ¤»óÀûÀ¸·Î µ¥ÀÌÅÍ ¼ö½Å
				}
			}
		};

		this.xmlHttpReq.send(strSubmit);
		this.run = true;
		return true;
	}
	else
	{
		if (this.showLoadingMsg == true)
			alert("µ¥ÀÌÅÍ¸¦ °¡Á®¿À´Â ÁßÀÔ´Ï´Ù.");
		
		return false;
	}
};

XMLhttpReq.prototype.abort = function()
{
	try { this.xmlHttpReq.abort(); } catch(e) {};
};

XMLhttpReq.prototype.formToQuery = function()
{
	var docForm = document.getElementById(this.formName);
	var strSubmitContent = "";
	var formElem;
	var strLastElemName = "";
	var elLen = docForm.elements.length;
	
	for (var i=0;i<elLen;i++) 
	{
		formElem = docForm.elements[i];
		
		switch (formElem.type) 
		{
			case "text":
			case "hidden":
			case "password":
			case "textarea":
			case "select-one":
				strSubmitContent += formElem.name + "=" + encodeURIComponent(formElem.value) + "&";
				break;
				
			case "radio":
				if (formElem.checked) 
					strSubmitContent += formElem.name + "=" + encodeURIComponent(formElem.value) + "&";
				break;
				
			case "checkbox":
				if (formElem.checked) 
				{
					if (formElem.name == strLastElemName) 
					{
						if (strSubmitContent.lastIndexOf("&") == strSubmitContent.length-1) 
							strSubmitContent = strSubmitContent.substr(0, strSubmitContent.length - 1);

						strSubmitContent += "," + encodeURIComponent(formElem.value);
					}
					else 
					{
						strSubmitContent += formElem.name + "=" + encodeURIComponent(formElem.value);
					}
					strSubmitContent += "&";
					strLastElemName = formElem.name;
				}
				break;
		}
	}
	
	strSubmitContent = strSubmitContent.substr(0, strSubmitContent.length - 1);
	return strSubmitContent;
};

//-----------------------------------------------------------------------------------
// XML Parser °Ë»ö

function getDomDocumentPrefix() 
{
	if (getDomDocumentPrefix.prefix)
		return getDomDocumentPrefix.prefix;
	
	var prefixes = ["MSXML2", "Microsoft", "MSXML", "MSXML3"];
	var o;

	for (var i=0;i<prefixes.length;i++) 
	{
		try 
		{
			o = new ActiveXObject(prefixes[i] + ".DomDocument");
			return getDomDocumentPrefix.prefix = prefixes[i];
		}
		catch (e) {};
	}

	throw new Error("Could not find an installed XML parser");
};

function getXmlHttpPrefix() 
{
	if (getXmlHttpPrefix.prefix)
		return getXmlHttpPrefix.prefix;
	
	var prefixes = ["MSXML2", "Microsoft", "MSXML", "MSXML3"];
	var o;

	for (var i = 0; i < prefixes.length; i++) 
	{
		try 
		{
			o = new ActiveXObject(prefixes[i] + ".XmlHttp");
			return getXmlHttpPrefix.prefix = prefixes[i];
		}
		catch (ex) {};
	}
	
	throw new Error("Could not find an installed XML parser");
};

//-----------------------------------------------------------------------------------
// XML Document

// XmlDocument factory
function XmlDocument() {};

XmlDocument.create = function () {
	try {
		// DOM2
		if (document.implementation && document.implementation.createDocument) {
			var doc = document.implementation.createDocument("", "", null);
			
			// some versions of Moz do not support the readyState property
			// and the onreadystate event so we patch it!
			if (doc.readyState == null) {
				doc.readyState = 1;
				doc.addEventListener("load", function () {
					doc.readyState = 4;
					if (typeof doc.onreadystatechange == "function")
						doc.onreadystatechange();
				}, false);
			}
			
			return doc;
		}
		if (window.ActiveXObject)
			return new ActiveXObject(getDomDocumentPrefix() + ".DomDocument");
	}
	catch (ex) {};
	throw new Error("Your browser does not support XmlDocument objects");
};

// Create the loadXML method and xml getter for Mozilla
if (window.DOMParser &&
	window.XMLSerializer &&
	window.Node && Node.prototype && Node.prototype.__defineGetter__) {

	// XMLDocument did not extend the Document interface in some versions
	// of Mozilla. Extend both!
	XMLDocument.prototype.loadXML = 
	Document.prototype.loadXML = function (s) {
		
		// parse the string to a new doc	
		var doc2 = (new DOMParser()).parseFromString(s, "text/xml");
		
		// remove all initial children
		while (this.hasChildNodes())
			this.removeChild(this.lastChild);
			
		// insert and import nodes
		for (var i = 0; i < doc2.childNodes.length; i++) {
			this.appendChild(this.importNode(doc2.childNodes[i], true));
		}
	};
	
	
	/*
	 * xml getter
	 *
	 * This serializes the DOM tree to an XML String
	 *
	 * Usage: var sXml = oNode.xml
	 *
	 */
	// XMLDocument did not extend the Document interface in some versions
	// of Mozilla. Extend both!
	XMLDocument.prototype.__defineGetter__("xml", function () {
		return (new XMLSerializer()).serializeToString(this);
	});
	Document.prototype.__defineGetter__("xml", function () {
		return (new XMLSerializer()).serializeToString(this);
	});
}
