var DOCIRCLE = {};
DOCIRCLE.util = {};

DOCIRCLE.util.AsyncRequest = function(url, callbackMethod, params, div, method, callbackParams) {
	this.url = url;
	this.callbackMethod = callbackMethod;
	this.params = (params == null) ? null : params;
	this.div = (div == null) ? null : div;
	this.method = (method == null) ? 'POST' : method;
	this.callbackParams = (callbackParams == null) ? null : callbackParams;
	
	this.send();
}

DOCIRCLE.util.AsyncRequest.prototype = {
	getXMLHttpRequest: function() {
		if (window.ActiveXObject) {
			try {
				return new ActiveXObject("Msxml2.XMLHTTP");
			} catch(e) {
				try {
					return new ActiveXObject("Microsoft.XMLHTTP");
				} catch(e1) { return null; }
			}
		} else if (window.XMLHttpRequest) {
			return new XMLHttpRequest();
		} else {
			return null;
		}		
	},
	send: function() {

		this.req = this.getXMLHttpRequest();


		var httpMethod = this.method ? this.method : 'GET';
		if (httpMethod != 'GET' && httpMethod != 'POST') {
			httpMethod = 'POST';
		}
		var httpParams = (this.params == null || this.params == '') ? 
		                 null : this.params;
		var httpUrl = this.url;
		if (httpMethod == 'GET' && httpParams != null) {
			httpUrl = httpUrl + "?" + httpParams;
		}
		this.req.open(httpMethod, httpUrl, true);
		this.req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		this.req.setRequestHeader('Connection', 'close');

		var request = this;
		this.req.onreadystatechange = function() {
			request.onStateChange.call(request);
		}

		this.req.send(httpMethod == 'POST' ? httpParams : null);
	},
	onStateChange: function() {
		if(this.callbackMethod) this.callback(this.req,this.callbackMethod,this.div,this.callbackParams);
		return;
	},
	callback : function (req,callbackMethod,div,callbackParams){
		if(req.readyState == 4){
			if(req.status == 200){
				var t=req.responseText;

				if(t.match(/Parse error/)) {
					alert_layer("PHP error\n\n"+t);
					return;
				}

				try
				{
					var t=req.responseXML;
 
					var err=t.selectSingleNode("//ERRORMESSAGE").firstChild.nodeValue;
					if(err != "success" && err !="") { 
						alert_layer(err);
						return;
					}
				}
				catch (e){}
//				if(t.getElementsByTagName("ERRORMESSAGE").item(0).nodeValue) {
//					alert_layer("DB Qeury ERROR\n\n"+t.getElementsByTagName("ERRORMESSAGE").item(0).nodeValue);
//					return;
//				}

				if(div)		callbackMethod(req,div,callbackParams);
				else		callbackMethod(req,callbackParams);
			} else {
				if(req.status=="12029" || req.status=="12030") alert_layer("Process failed temporarily due to poor network connection.  Please try again...");
				else alert_layer('HTTP ERROR:'+req.status+'<br><br>callback Failed.');
			}
		}
	} 
		
}


//simple Send Request : 간단하게 POST로 전송할때 쓰임..
var simpleHttpRequest=null;

function simpleSendRequest(url,str,callback) {
	if (window.ActiveXObject) {
		try {
			simpleHttpRequest=new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
			try {
				simpleHttpRequest=new ActiveXObject("Microsoft.XMLHTTP");
			} catch(e1) { return null; }
		}
	} else if (window.XMLHttpRequest) {
		simpleHttpRequest=new XMLHttpRequest();
	} else {
		return null;
	}		

	simpleHttpRequest.open("POST",url,true);
	simpleHttpRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
	simpleHttpRequest.onreadystatechange=callback;
	simpleHttpRequest.send(str);

}

function checkError(req) {
	var xml=req.responseXML;
	try
	{
		var errorcode=xml.getElementsByTagName("ERRORCODE").item(0).nodeValue;	
	}
	catch (e)
	{
		var errorcode=0;
	}
	try
	{
		var errormessage=xml.getElementsByTagName("ERRORMESSAGE").item(0).nodeValue;	
	}
	catch (e)
	{
		var errormessage="";
	}

	if(errorcode == 1) {
		alert_layer("Error!! \n\n"+errormessage);
	}

}


function cbDisplay(req,div){
	if(req.responseText!='') document.getElementById(div).innerHTML = req.responseText;	//Display Html
}
function cbalert_layer(req){
	alert_layer(req.responseText);
}
function cbRedirect(req){
	top.location.href = req.responseText;
}


////////////////////////////
// function: parseJSON
// coded by hanulis@docircle
// create date : 06-11-2006
// description:
//
// parse XML to JSON object
//
// usage : 
//          data=parseJSON(xml);
////////////////////////////

function parseAttributes(o) {
	var len=o.length;
//	if(len==0) return "";
	var str="att:{";

	for(al=0;al<len;al++) {
		str+=o[al].name+":\""+o[al].value+"\", ";
	}
	str=str.substring(0,str.length-2);
	str+="}";
	return str;

}

function makeJSON(obj) {
	var node=obj.childNodes;

	var str="{";

	if(node[0].nodeName!="#text") {
		for(i=0;i<node.length;i++) {
			str+=node[i].nodeName+":";

			if(node[i].attributes.length>0) str+="{"+parseAttributes(node[i].attributes);
			if(node[i].childNodes.length>0) {
				for(j=0;j<node[i].childNodes.length;j++) {
//					str+="{";
					var tc=node[i].getElementsByTagName(node[i].childNodes[j].nodeName);

					if(tc.length>0) {
						str+=",";
						str+=node[i].childNodes[j].nodeName+":[";
						for(l=j;l<j+tc.length;l++) {
							str+=makeJSON(node[i].childNodes[l]);
						}
						str=str.substring(0,str.length-1);
						str+="], ";
					}
					str=str.substring(0,str.length-2);
					j+=tc.length-1;
//					str+="}";
				}
			}
			if(node[i].attributes.length>0) str+="}";
				str+=",";
//			str+="\n";
		}

		str=str.substring(0,str.length-1);

	} else {
		if(obj.attributes.length>0) str+=parseAttributes(obj.attributes)+",";
		var val=obj.firstChild.nodeValue.replace(/\"/g,"\\\"");
		str+="value:\""+val+"\""; 
	}
	str+="}";

	if(node.length<2) str+=",";

	return str;

}

function parseJSON(obj,req) {

	var r=makeJSON(obj);

	return eval('('+r+')');
}


// xpath for FireFox.
if( document.implementation.hasFeature("XPath", "3.0") )
{
	XMLDocument.prototype.selectNodes = function(cXPathString, xNode)
	{
		if( !xNode ) { xNode = this; } 

		var oNSResolver = this.createNSResolver(this.documentElement)
		var aItems = this.evaluate(cXPathString, xNode, oNSResolver, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null)
		var aResult = [];
		for( var i = 0; i < aItems.snapshotLength; i++)
		{
			aResult[i] =  aItems.snapshotItem(i);
		}
		
		return aResult;
	}
	XMLDocument.prototype.selectSingleNode = function(cXPathString, xNode)
	{
		if( !xNode ) { xNode = this; } 

		var xItems = this.selectNodes(cXPathString, xNode);
		if( xItems.length > 0 )
		{
			return xItems[0];
		}
		else
		{
			return null;
		}
	}

	Element.prototype.selectNodes = function(cXPathString)
	{
		if(this.ownerDocument.selectNodes)
		{
			return this.ownerDocument.selectNodes(cXPathString, this);
		}
		else{throw "For XML Elements Only";}
	}

	Element.prototype.selectSingleNode = function(cXPathString)
	{	
		if(this.ownerDocument.selectSingleNode)
		{
			return this.ownerDocument.selectSingleNode(cXPathString, this);
		}
		else{throw "For XML Elements Only";}
	}

}
