// Ajax Class
// Writer: I Sung, 2008-10-08

AjaxDiv = function(PostURL, TargetDivID, PreLoading, Sync)
{
	var xmlHttp;
	var PostURL;
	var DivID;
	var callfunction;

	this.PostURL = PostURL;
	this.DivID = TargetDivID;
	this.PreLoading = PreLoading;
	this.Async = (typeof Sync != 'undefined') ? ( (Sync === false) ? true : false ) : true;
	this.brs = ""; 
	this.loadingtime = 0;
	this.beforeload = 0;
}

AjaxDiv.prototype = {
	Init:function()
	{
		var	ret;
		if ( typeof XMLHttpRequest != 'undefined' ) {
			ret = new XMLHttpRequest();
		// check the dom to see if this is IE or not
		} else {
			if (typeof window.XMLHttpRequest != 'undefined') {
				// Not IE
				ret = new XMLHttpRequest();
			} else {
				if (window.ActiveXObject) {
					// Hello IE!
					// Instantiate the latest MS ActiveX Objects
					if ( typeof dm_xmlhttprequest_type != 'undefined' ) {
						ret = new ActiveXObject(dm_xmlhttprequest_type);
					} else {
						// loops through the various versions of XMLHTTP to ensure we're using the latest
						var versions = ["Msxml2.XMLHTTP.7.0", "Msxml2.XMLHTTP.6.0", "Msxml2.XMLHTTP.5.0", "Msxml2.XMLHTTP.4.0", "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP", "Microsoft.XMLHTTP"];
						for (var i = 0; i < versions.length ; i++) {
							try {
								// try to create the object
								// if it doesn't work, we'll try again
								// if it does work, we'll save a reference to the proper one to speed up future instantiations
								ret = new ActiveXObject(versions[i]);
								if (ret) {
									dm_xmlhttprequest_type = versions[i];
									break;
								}
							}
							catch (objException) {
								// trap; try next one
							};
						};
					}
				}
			}
		}
		this.xmlHttp = ret;
		return ret;
	},

	setData:function(param)
	{
		var me = this;
		var Target = this.DivID;
		me.Init();
		
		me.xmlHttp.onreadystatechange = function()
		{
			if(me.xmlHttp.readyState == 1){
				if(me.PreLoading == 'None') {
					//
				} else if(me.PreLoading == 'Text') {
					document.getElementById(Target).innerHTML = "loading";
				} else {
					document.getElementById(Target).innerHTML = "&nbsp;<br /><center><img align='middle' alt='loading' src='//imagex.navi.com/simage/common/loading.gif' /></center>&nbsp;<br />";
				}

			} else if(me.xmlHttp.readyState == 4) {
		
				switch(me.xmlHttp.status){
		
					case 404:
						alert('Not Found');
						break;
					case 500:
						alert('Internal Server Error'+me.xmlHttp.responseText);
						break;
					default:
						var retStr = me.xmlHttp.responseText;
						//alert(retStr);
						document.getElementById(Target).innerHTML = retStr;
						if(me.callfunction != undefined) me.callfunction();
						break;
				}
			}
		
		};
		me.xmlHttp.open("POST", this.PostURL, true);
		me.xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		me.xmlHttp.send("param="+param);
	},

	setInnerHTML:function(Type, obj, innerhtml)
	{
		if(Type == 'id' || Type == 'obj') {
			obj.innerHTML = innerhtml;

		} else {
			if(obj.length > 0) {
				for(var j=0; j<obj.length; j++) {
					obj[j].innerHTML = innerhtml;
				}
			}
		}
	},

	setDataX:function(URL, Div, param, ClassName, DivObj)
	{
		var me = this;
		//var Target = Div;

		if(typeof Div != 'undefined' && Div != '') {
			if(document.getElementById(Div)) {
				var Type = 'id';
				var Target = document.getElementById(Div);
			} else {
				return;
			}

		} else if(typeof ClassName != 'undefined' && ClassName != '') {
			var Type = 'class';
			var Target = document.getElementsByClassName(ClassName);

		} else if(typeof DivObj != 'undefined' && DivObj != '') {
			var Type = 'obj';
			var Target = DivObj;

		} else {
			return;
		}

		me.Init();

		me.xmlHttp.onreadystatechange = function()
		{
			if(me.xmlHttp.readyState == 1){
				if(me.PreLoading == 'None') {
					//
				} else if(me.PreLoading == 'N') {
					me.setInnerHTML(Type,Target,"loading");
				} else {
					me.setInnerHTML(Type,Target,"&nbsp;<br /><center><img align='middle' alt='loading' src='//imagex.navi.com/simage/common/loading.gif' /></center>&nbsp;<br />");
				}

			} else if(me.xmlHttp.readyState == 4) {
				switch(me.xmlHttp.status){
		
					case 404:
						alert('Not Found');
						break;
					case 500:
						alert('Internal Server Error'+me.xmlHttp.responseText);
						break;
					default:
						var retStr = me.xmlHttp.responseText;
						me.setInnerHTML(Type,Target,retStr);
						if(me.callfunction != undefined && me.Async === true) me.callfunction();
						break;
				}
			}
		};
		me.xmlHttp.open("POST", URL, me.Async);
		me.xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		me.xmlHttp.send("param="+param);

		if (me.Async === false) {// 동기
			var retStr = me.xmlHttp.responseText;
			me.setInnerHTML(Type,Target,retStr);
			if(me.callfunction != undefined) me.callfunction();
			return;
		}
	}
}

XmlParser = function(Sync)
{
	var xmlHttp;
	var results;
	var count;
	var callfunction;

	this.results = null;
	this.count = 0;
	this.Async = (typeof Sync != 'undefined') ? ( (Sync === false) ? true : false ) : true;
}

XmlParser.prototype = {
	Init:function()
	{
		var	ret;
		if ( typeof XMLHttpRequest != 'undefined' ) {
			ret = new XMLHttpRequest();
		// check the dom to see if this is IE or not
		} else {
			if (typeof window.XMLHttpRequest != 'undefined') {
				// Not IE
				ret = new XMLHttpRequest();
			} else {
				if (window.ActiveXObject) {
					// Hello IE!
					// Instantiate the latest MS ActiveX Objects
					if ( typeof dm_xmlhttprequest_type != 'undefined' ) {
						ret = new ActiveXObject(dm_xmlhttprequest_type);
					} else {
						// loops through the various versions of XMLHTTP to ensure we're using the latest
						var versions = ["Msxml2.XMLHTTP.7.0", "Msxml2.XMLHTTP.6.0", "Msxml2.XMLHTTP.5.0", "Msxml2.XMLHTTP.4.0", "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP", "Microsoft.XMLHTTP"];
						for (var i = 0; i < versions.length ; i++) {
							try {
								// try to create the object
								// if it doesn't work, we'll try again
								// if it does work, we'll save a reference to the proper one to speed up future instantiations
								ret = new ActiveXObject(versions[i]);
								if (ret) {
									dm_xmlhttprequest_type = versions[i];
									break;
								}
							}
							catch (objException) {
								// trap; try next one
							};
						};
					}
				}
			}
		}
		this.xmlHttp = ret;
		return ret;
	},

	ReadXMLQuery:function(sql)
	{
		var me = this;
		me.Init();

		me.xmlHttp.onreadystatechange = function()
		{
			if(me.xmlHttp.readyState == 4)
			{
				switch(me.xmlHttp.status){
		
					case 404:
						alert('Not Found');
						break;
					case 500:
						alert('Internal Server Error'+me.xmlHttp.responseText);
						break;
					default:
						me.results = me.xmlHttp.responseXML.documentElement.getElementsByTagName("row");
						me.count = me.results.length;
						if(me.callfunction != undefined) me.callfunction();
						break;
				}
			}
		
		};
		me.xmlHttp.open("POST",  "/common/xml/xmlquery.php?sql="+sql, true);
		me.xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		me.xmlHttp.send("");
	},

	ReadXMLPage:function(path, tag_name, sync)
	{
		var me = this;
		me.Async = (sync) ? false : me.Async;

		me.Init();

		me.xmlHttp.onreadystatechange = function()
		{
			if(me.xmlHttp.readyState == 4)
			{
				switch(me.xmlHttp.status){
		
					case 404:
						alert('Not Found');
						break;
					case 500:
						alert('Internal Server Error'+me.xmlHttp.responseText);
						break;
					default:
						me.results = me.xmlHttp.responseXML.documentElement.getElementsByTagName(tag_name);
						me.count = me.results.length;
						if(me.callfunction != undefined && me.Async === true) me.callfunction();
						break;
				}
			}
		
		};
		me.beforeload = (new Date()).getTime();
		me.xmlHttp.open("POST", path, me.Async);
		me.xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		me.xmlHttp.send("");
		me.loadingtime = me.pageLoadingTime();

		if (me.Async === false) {// 동기
			me.results = me.xmlHttp.responseXML.documentElement.getElementsByTagName(tag_name);
			me.count = me.results.length;
			if(me.callfunction != undefined) me.callfunction();
			return;
		}

	},

	pageLoadingTime:function() {
		var me = this;
		var afterload = (new Date()).getTime();
		secondes = (afterload-me.beforeload)/1000;

		return secondes;
	},

	GetPageValue:function(row, col_name)
	{ // row => -1 : all

		if(row != -1) {
			if(col_name) {
				return this.results[row].getElementsByTagName(col_name)[0].childNodes[0].nodeValue;
			}else{
				return this.results[row].childNodes[0].nodeValue;
			}

		} else {
			var ret_val = new Array();

			if(col_name) {
				for(i=0; i<this.results.length; i++) {
					ret_val[i] = this.results[i].getElementsByTagName(col_name)[0].childNodes[0].nodeValue;
				}
			}else{
				for(i=0; i<this.results.length; i++) {
					ret_val[i] = this.results[i].childNodes[0].nodeValue;
				}
			}

			return ret_val;
		}
	},
	
	GetResultValue:function(row, col)
	{
		return this.results[row].getElementsByTagName("col"+col)[0].childNodes[0].nodeValue;
	}
}


