﻿/************************************
 Ajax common xmlhttprequest
*************************************/
/**************************************
XMLHTTP helper class
ROOT_PATH : WebServer's root process path
**************************************/

var _temp_xml_http = null;
var _temp_invoke_function = null;

function RequestHandler(isXml) 
{
	this.IsMultiProcess = isXml;
	this.XmlDoc	= null;
	this.XML_BASE_LAYOUT = "<Action><Type></Type></Action>";
	this.WEB_SERVER_URL = null;
	this.XmlHttp		= null;
	this.Root			= null;
	this.ParamName		= "menu_id";
	this.IsEnabled		= true;
	this.IsIE			= null;
	this.processing		= null;
	this.ResponseXML	= null;
	this.invokeFunction	= null;
	
	this.initialize = function()
	{
		
		// Document
		if(this.XmlDoc == null)
		{
			if (window.ActiveXObject)
			{
				this.XmlDoc = new ActiveXObject("Microsoft.XMLDOM");
				this.IsIE = true; 
			}
			else if (document.implementation &&
				document.implementation.createDocument)
			{
				
				 this.XmlDoc=document.implementation.createDocument("","",null);
				 this.IsIE = false;
			}
		}
		
		
		// URL
		if(this.IsMultiProcess) this.WEB_SERVER_URL = ROOT_PATH + "Handler/XmlHttpHandler.ashx";
		else this.WEB_SERVER_URL = SITE_NAME + "Handler/XmlHttpHandler.ashx"; // (SingleInput/OutPut)
		
		// XML HTTP
		if(this.XmlHttp == null)
		{
			if(window.XMLHttpRequest)
			{
				this.XmlHttp = new XMLHttpRequest();
			}
			else if (window.ActiveXObject)
			{
				this.XmlHttp = new ActiveXObject("Microsoft.XMLHTTP")
			}

		}
		
		
		
		if(this.XmlDoc == null)
		{
			alert('Your browser cannot handle xml parser');
			this.IsEnabled = false;
			return;
		}
		
		if(this.XmlHttp == null)
		{
			alert('Your browser cannot handle xml http protocol');
			this.IsEnabled = false;
			return;
		}
			
		// Root 
		this.Root = this.createRootNode();
		
	}
	this.createRootNode = function()
	{
		//this.XmlDoc.load("<XML></XML>");
		var rootNode = this.createNode("XML");
		//alert(this.XmlDoc.documentElement);
		this.XmlDoc.appendChild(rootNode);
		return this.XmlDoc.documentElement;
	}
	
	this.createNode = function(nodeName)
	{
		return this.XmlDoc.createElement(nodeName);
	}
	
	this.createText = function(text)
	{
		return this.XmlDoc.createTextNode(text);
	}
	
	this.createMessage = function(actionName)
	{
		var actionNode = this.createNode("Action");	// Action Node 
		var typeNode = this.createNode("Type");		// Type Node 
		this.Root.appendChild(actionNode);			// RootActionNode 
		actionNode.appendChild(typeNode);			// Action
		typeNode.setAttributeNode(GetXmlTypeAttribute(this.XmlDoc,actionName)); // Type
		
		return typeNode;
	}
	
	this.addMessageParam = function(messageNode,paramValue)
	{
		if(this.XmlDoc == null) this.initialize();
		
		var paramNode = this.createNode("Param");
		var textNode = null;
		if(paramValue == null)
		{
			textNode = this.createText('null');
			//paramNode.text = 'null';
		}
		else
		{
			textNode = this.createText(paramValue);
			//paramNode.text = paramValue;
		}
		paramNode.appendChild(textNode);
		
		messageNode.appendChild(paramNode);
	}
	
	this.setData = function(proc,arr)
	{
		var tmpMsg = this.createMessage("$Tran_Set$" + proc);
	
		for(var i =0; i < arr.length ; i++)
		{
			this.addMessageParam(tmpMsg,arr[i]);
		}
	}
	

	this.requestXML = function()
	{
		//if(this.XmlDoc == null) this.initialize();
		if(this.IsIE == true)
			return this.XmlDoc.xml;
		else
			return (new XMLSerializer()).serializeToString(this.XmlDoc);
	}
	
	this.beginSend = function()
	{
		var url = this.WEB_SERVER_URL + "?" + this.ParamName + "=" + RequestValue(this.ParamName);

		this.XmlHttp.open("POST", url, true);
		this.XmlHttp.setRequestHeader("Content-Type", "text/xml");
		var sXmlData = this.requestXML();
		this.XmlHttp.send(sXmlData);
	}
	
	this.invokeSend = function()
	{
		if(this.invokeFunction == null) 
		{
			alert('invoke function not exist.');
			return;
		}
		
		_temp_xml_http = this.XmlHttp;
		_temp_invoke_function = this.invokeFunction;
		
		this.processing		= "sending";
		
		var url = this.WEB_SERVER_URL + "?" + this.ParamName + "=" + RequestValue(this.ParamName);

		//this.XmlHttp.onreadystatechange=this.onResponse;
		this.XmlHttp.onreadystatechange=this.onResponse;
		this.ResponseXML = null;

		this.XmlHttp.open("POST", url, true);
		this.XmlHttp.setRequestHeader("Content-Type", "text/xml");
		
		var sXmlData = this.requestXML();
		this.XmlHttp.send(sXmlData);
		
		
	}
	
	this.send = function()
	{
		//if(this.XmlHttp == null) this.initialize();
		
		this.processing		= "sending";
		
		var url = this.WEB_SERVER_URL + "?" + this.ParamName + "=" + RequestValue(this.ParamName);

		//this.XmlHttp.onreadystatechange=this.onResponse;
		this.ResponseXML = null;

		this.XmlHttp.open("POST", url, false);
		this.XmlHttp.setRequestHeader("Content-Type", "text/xml");
		
		var sXmlData = this.requestXML();
		
		this.XmlHttp.send(sXmlData);
		
		
		var sResult = this.XmlHttp.responseText;
		this.XmlHttp.abort();
		return sResult;
		
	}
	
	this.onResponse = function()
	{
		if (_temp_xml_http.readyState==4)
		{
		    //alert(_temp_xml_http.responseText);
			if (_temp_xml_http.status==200)
			{

				// message start
				var msg = new MessageHandler();
				msg.initialize(_temp_xml_http.responseText);
//				
//				_temp_xml_http.responseText
//				
//				if(msg.ErrorMessage != null)
//				{
//					alert(msg.ErrorMessage);
//				}
//				else
//				{
//					var oDs = msg.DataSet;
//					//document.getElementById("result").innerHTML = oDs.Tables[0].Rows.length;
//					
//				}
				// message end
				_temp_invoke_function(msg.ErrorMessage,msg.DataSet);

			}
			else
			{
				alert("Problem retrieving XML data")
			}
			this.processing		= "finished";
			
			_temp_xml_http = null;
			_temp_invoke_function = null;
		}
	}
	
	this.dispose = function()
	{
		if(this.XmlDoc != null)		this.XmlDoc = null;
		if(this.XmlHttp != null)	{this.XmlHttp.abort();this.XmlHttp = null;}
	}
}


function GetXmlTypeAttribute(xmlDoc,actionName)
{
	var xmlAttribute = xmlDoc.createAttribute("action");
	xmlAttribute.value	= actionName;
	return xmlAttribute;
}

function GetXmlElement(xmlDoc,paramValue)
{
	var xmlElement = xmlDoc.createElement("Param");
	xmlElement.text	= paramValue;
	return xmlElement;
}
function CvtDoc2XML(doc)
{
	if (window.ActiveXObject)
	{
		return doc.xml;
	}
	else if (document.implementation &&
		document.implementation.createDocument)
	{
		 return (new XMLSerializer()).serializeToString(doc);
	}
	 
}
/***************************************
 Result Message Handler
****************************************/
function MessageHandler()
{

	this.ErrorMessage = null;
	this.DataSet = null;

	this.initialize = function(resultXML)
	{
		this.ErrorMessage = null;
		this.DataSet = null;
		
		if((resultXML == null ) || (resultXML == "undefined"))
		{return;}
		
		var msghelper = new XmlMessageCollection(resultXML);
		msghelper.initialize();
		
		var oDs = new DataSet();
			
			
			
		for(var nCount = 0;nCount <  msghelper.Messages.length ; nCount++)
		{
			var unitNode = msghelper.Messages.item(nCount);
			var msg = new XmlMessage(unitNode);
			msg.initialize();//alert('a');
			
			if(msg.Success)
			{
				var dataTable = new DataTable();
				dataTable.Rows		= msg.Rows;
				dataTable.Columns	= msg.getColumnNames();
				oDs.Tables.push(dataTable);
			}
			else
			{
				this.ErrorMessage = msg.Message;
			}
		}
		this.DataSet = oDs;
		msghelper.dispose();
	}
}

/***************************************
 XML HTTP's message collection
****************************************/
function XmlMessageCollection(resultXmlData)
{
	this.Source			= resultXmlData;
	this.XmlDoc			= null;
	this.Messages		= null;
	this.Count			= null;
	this.IsEnabled		= true;
	this.IsIE			= null;
	
	this.initialize		= function()
	{
	
		// Document
		if(this.XmlDoc == null)
		{
			if (window.ActiveXObject)
			{
				this.XmlDoc = new ActiveXObject("Microsoft.XMLDOM");
				this.XmlDoc.loadXML(this.Source);
				this.IsIE	= true;
			}
			else if (document.implementation &&
				document.implementation.createDocument)
			{
				 var parser=new DOMParser();
				var doc=parser.parseFromString(this.Source,"text/xml");
				this.XmlDoc = doc;
				 this.IsIE	= false;
			}
			else
			{
				this.IsEnabled	= false;
				alert('Your browser cannot handle xml parser');
				return;
			}
		}

		
		var rootNode	= this.XmlDoc.documentElement;	//<results>
		this.Messages	= rootNode.childNodes;			// <Result>NodeList
		this.Count		= this.Messages.length;

	}
	
	this.getMessage = function(nIndex)
	{		
		return this.Messages.item(nIndex);
	}
	this.getCount = function()
	{
		return this.Count;
	}
	
	this.dispose = function() {if(this.XmlDoc != null) {this.XmlDoc = null;}}
}



/***********************************************
// xml parse  
// success : 1 or 0
// message : message content
// results : array type
a XmlMessage in XmlMessageCollection
************************************************/
function XmlMessage(resultNode)
{
	this.Success	= null;	// Success
	this.Message	= null;
	this.Items		= null;
	this.ResultNode	= resultNode;
	
	this.Columns	= null;
	this.ColumnCount= null;
	this.Rows		= new Array;
	this.RowsCount	= null;
	this.fullText	= null;
	
	
	
	this.initialize	= function()
	{//alert(resultNode.xml);
	
//	    document.write(resultNode.xml);
//	    return;
		// Success 
		if(this.Success == null)
		{
			//alert(this.ResultNode.getElementsByTagName("Success").length);
			//alert(CvtDoc2XML(this.ResultNode.firstChild));
			//if(this.ResultNode.selectSingleNode("Success").text == "1" )
			if(this.ResultNode.getElementsByTagName("Success")[0].childNodes[0].nodeValue == "1" )
				this.Success = true;
			else
				this.Success = false;
		}
		
		// Message init
		if(this.Message == null)
		{
			//this.Message = this.ResultNode.selectSingleNode("Message").text;
			this.Message = this.ResultNode.getElementsByTagName("Message")[0].childNodes[0].nodeValue;
		}
		
		if(!this.Success) return;
		
		// Items 
		if(this.Items == null)
		{
			try
			{
				this.Items = this.ResultNode.getElementsByTagName("Infos")[0].childNodes;
				this.RowsCount = this.Items.length;
			}
			catch(e)
			{
				this.Items = new Array();
				this.RowsCount = 0;
			}
		}
		
		
		
		// Columns init
		if(this.Items.length > 0)
		{
			var firstRows = this.Items.item(0).childNodes;
			this.ColumnCount = firstRows.length;
			
			this.Columns = new Array(this.ColumnCount);
			
			
			// 
			for(i =0 ; i < this.ColumnCount ; i++)
			{
				this.Columns[i] = firstRows.item(i).nodeName;
			}
			
			// 
			this.Rows	= new Array(this.RowsCount);
			this.fullText = "";
			for(nRow = 0 ; nRow < this.RowsCount ; nRow ++)
			{
				var searchRow = this.Items.item(nRow).childNodes;
				var result = new Array(this.ColumnCount);
				for(nCol =0 ; nCol < this.ColumnCount ; nCol++)
				{
					//result[nCol] = searchRow.item(nCol).xml;
					//result[nCol] = searchRow.item(nCol).childNodes.legnth;
					result[nCol] = searchRow.item(nCol).childNodes[0].nodeValue;
					//alert(result[nCol]);
				}
				
				this.Rows[nRow] = result;
			}
		}
		else
		{
			this.ColumnCount = 0;
			this.Columns = new Array();
			this.Rows	= new Array();
		}
	}
	
	
	
	this.getSuccessValue	= function()
	{
		if(this.Success == null) this.initialize();
		return this.Success;
	}
	
	this.getInfos = function()
	{
		if(this.Items == null) this.initialize();
		
		return this.Items;
	}
	
	this.getMessage	= function()
	{
		if(this.Message == null) this.initialize();

		return this.Message;
	}
	
	this.getColumnNames = function()
	{
		if(this.Columns == null ) this.initialize();
		return this.Columns;
	}
	
	this.getColumnCount = function()
	{
		if(this.ColumnCount == null ) this.initialize();
		return this.ColumnCount;
	}
	
	this.getRowCount = function()
	{
		if(this.RowsCount == null ) this.initialize();
		return this.RowsCount;
	}
	
	this.getRow = function(nRow)
	{
		if(this.Rows == null ) this.initialize();
		
		if(nRow >= this.RowsCount) { alert('max index must be less than ' + this.RowsCount);return;}
		
		return this.Rows[nRow];
	}
	
	this.getValue = function(nCol,nRow)
	{
		if(this.Columns == null ) this.initialize();
		var iCol;
		
		if(typeof(nCol) == "string")	
		{
			iCol = this.getColumnIndexByName(nCol);
			
			if(iCol == -1) {alert(nCol + " is not exists!");return;}
		}
		else {iCol	= nCol;}
		selectedRow = this.getRow(nRow);
		return selectedRow[iCol];
	}
	
	this.getColumnIndexByName = function(colName)
	{
		if(this.Columns == null ) this.initialize();
		for(i = 0; i < this.ColumnCount ; i++)
		{
			if(this.Columns[i] == colName)
			{
				return i;
			}
		}
		return -1;
	}
	
}
/********************
DataSet
*********************/
function DataSet()
{
	this.Source	= null;
	this.Tables = new Array;
}

/*******************
DataTable
*******************/
function DataTable()
{
	this.Rows = new Array;
	this.Columns = new Array;
	
	this.getValue = function(rowIndex, columnName)
	{
		var colIndex = this.getColIndex(columnName);
		return this.Rows[rowIndex][colIndex];
	}

	this.getColIndex = function(columnName)
	{
		var nRet = -1;
		
		if(this.Columns)
		{
			for(var i=0; i < this.Columns.length ; i++)
			{
				if(this.Columns[i].toUpperCase() == columnName.toUpperCase())
				{
					nRet = i;
					break;
				}
			}
		}
		return nRet;
	}
}


