// Ajax Class
// Writer: I Sung, 2008-10-08

AjaxDiv = function(PostURL, TargetDivID)
{
	var xmlHttp;
	var PostURL;
	var DivID;

	this.PostURL = PostURL;
	this.DivID = TargetDivID;
	this.brs = ""; 
}

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();
		
/*		if(this.brs == 'opera' || this.brs == 'applewebkit' || this.brs == 'gecko'){
		alert("3");
			xmlHttp.onload = handleUpdate;
		}
		else{
		alert("31");
		}
*/	
		me.xmlHttp.onreadystatechange = function()
		{
			if(me.xmlHttp.readyState == 1){
				document.getElementById(Target).innerHTML = "&nbsp;<br /><img align='middle' alt='loading' src='//imagex.navi.com/simage/common/loading.gif' />&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;
						break;
				}
			}
		
		};
		me.xmlHttp.open("POST", this.PostURL, true);
		me.xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		me.xmlHttp.send("param="+param);
	},

	setDataX:function(URL, Div, param)
	{
		var me = this;
		var Target = Div;
		me.Init();
		
		me.xmlHttp.onreadystatechange = function()
		{
			if(me.xmlHttp.readyState == 1){
				document.getElementById(Target).innerHTML = "&nbsp;<br /><img align='middle' alt='loading' src='//imagex.navi.com/simage/common/loading.gif' />&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;
						break;
				}
			}
		
		};
		me.xmlHttp.open("POST", URL, true);
		me.xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		me.xmlHttp.send("param="+param);
	}
}

XmlParser = function()
{
	var xmlHttp;
	var results;
	var count;
	var callfunction;

	this.results = null;
	this.count = 0;
	this.callfunction = function call(){ alert('Finished');}
}

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;
						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)
	{
		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(tag_name);
						me.count = me.results.length;
						me.callfunction();
						break;
				}
			}
		
		};
		me.xmlHttp.open("POST", path, true);
		me.xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		me.xmlHttp.send("");
	},

	GetPageValue:function(row, col_name)
	{
		if(col_name){
			//alert(col_name);
			return this.results[row].getElementsByTagName(col_name)[0].childNodes[0].nodeValue;
		}else{
			//alert("no col");
			return this.results[row].childNodes[0].nodeValue;
		}
	},
	
	GetResultValue:function(row, col)
	{
		return this.results[row].getElementsByTagName("col"+col)[0].childNodes[0].nodeValue;
	}
}

