/*
   File: XmlHttpRequest
   提供JavaScript版的XmlHttpRequest相關函式。   
*/

/*
   Function: cex_XmlHttpRequestGet
   允許在 Web 要求期間讀取用戶端送出的 HTTP 值，並使用GET為 HTTP 資料傳輸方法。
      
   Parameters:
      url - 要求的 URL 的資訊。

   Returns:
      傳回要求作業的 HTTP 回應資訊。
   
   Example:   
   (start example)
   var str = cex_XmlHttpRequestGet( "http://www.google.com");
   writeln( str);
   (end)
   
   Notes: 
   原作者 - Jeffrey Lee
   最後更新 - Alexander Tang (2005/2/16)
*/
var ajax_on = 1;
function cex_XmlHttpRequestGet( url) 
{
	var constFunctionName = 'cex_XmlHttpRequestGet';
		
	var xmlHttp=null;

/*
	try 
	{
		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	} 
	catch (CalE_xmlHttpErr) 
	{
		xmlHttp=null;
	}
	
	if (xmlHttp==null) 
	{
		try 
		{
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		} catch (CalE_xmlHttpErr2) 
		{
			xmlHttp=null;
		}
	}
*/

if (window.XMLHttpRequest) 
{ // Mozilla, Safari,...
  xmlHttp = new XMLHttpRequest();
} 
else if (window.ActiveXObject) 
{ // IE
      try {
        // 新版的 IE
        xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
      } catch (e) {
        try {
          // 舊版的 IE
          xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e) {}
      }
}
	
	if (xmlHttp==null) 
	{
		cex_RaiseMessage( constFunctionName, cex_clsMsgType.MsgTypeError, 0, 'Failed to create XMLHTTP object!');
		return "";
	}
			
	try 
	{
		xmlHttp.open("GET",url,false);
		xmlHttp.send(null);
		
		return xmlHttp.responseText;
	} 
	catch (calE_xmlHttpErr3) 
	{
		cex_RaiseMessage( constFunctionName, cex_clsMsgType.MsgTypeError, 0, 'Failed to complete the GET request!');
		return "";
	}
}

/*
   Function: cex_XmlHttpRequestGet_HashTable
   允許在 Web 要求期間讀取用戶端送出的 HTTP 值，並使用GET為 HTTP 資料傳輸方法。
      
   Parameters:
      url - 要求的 URL 的資訊。
      bItemIcnRow - 儲存在Scripting.Dictionary中的Item值

   Returns:
      傳回要求作業的 HTTP 回應資訊, 並依照fjaoirjvaf。
   
   Example:   
   (start example)
   var str = cex_XmlHttpRequestGet( "http://www.google.com");
   writeln( str);
   (end)
   
   Notes: 
   原作者 - Alexander Tang (2005/4/1)
   最後更新 - Alexander Tang (2005/4/1)
*/
function cex_XmlHttpRequestGet_HashTable( url, bItemIcnRow)
{
	var HashTable = new ActiveXObject("Scripting.Dictionary");
	var rc = cex_XmlHttpRequestGet( url);
	var rowArray = rc.split( ";");
	var strCol = "";
	
	for ( var i=0; i<rowArray.length; i++)
	{
		var colArray = rowArray[i].split( "|")
		
		if ( bItemIcnRow)
		{
			strCol = ""
			for ( var j=1; j<colArray.length; j++)
			{
				if ( j<=colArray.length-1)
					strCol = strCol + colArray[j] + "|";
				else
					strCol = strCol + colArray[j];
			}
			HashTable.Add( colArray[0], strCol);
		}
		else
		{
			HashTable.Add( colArray[0], colArray[1]);
		} 
		//alert( rowArray[i]);
	}	
	return HashTable;
}

function cex_XmlHttpRequestGet_AdoHelper( url, cmdType, connStr, sqlCmd, maxRow)
{
	var reqUrl = url + "?";
	
	if ( cmdType != "" && cmdType != undefined)
		reqUrl = reqUrl + "cmdtype=" + escape( cmdType);
	
	if ( connStr != "" && connStr != undefined)
		reqUrl = reqUrl + "&connStr=" + escape( connStr);
		
	if ( sqlCmd != "" && sqlCmd != undefined)
		reqUrl = reqUrl + "&sqlCmd=" + escape( sqlCmd);
	
	if ( maxRow != "" && maxRow != undefined)
		reqUrl = reqUrl + "&maxRow=" + escape( maxRow);
	 
	//document.writeln( reqUrl);

	var returnString = cex_XmlHttpRequestGet( reqUrl);

	return returnString;
}

function cex_clsMsgType()
{
	this.MsgTypeError = 0;
	this.MsgTraceInfoError = 1;
}

function cex_RaiseMessage( funcName, msgType, msgNumber, msgDesc)
{
	// msgType = 0 : Error Message.
	//           1 : Trace Information
	alert( '[ACTIF-CEX Error] ' + msgDesc);
}


