function getXmlDoc() {
    var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
    //var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.4.0");
    return xmlDoc;
}

function getXmlHttp() {
    var xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    //var xmlHttp = new ActiveXObject("Msxml2.XMLHTTP.4.0");
    return xmlHttp;    
}

function getDefaultXmlObj() {

	var strDefaultXml = "<?xml version='1.0' encoding='Big5'?><Root></Root>";
	
	return strToXmlObj(strDefaultXml);

}

function strToXmlObj( strXml ) {
    var xmlDoc = getXmlDoc();
    
    xmlDoc.async = false;
    xmlDoc.loadXML( strXml );
    
    return xmlDoc;
    
}

function sendXmlStr(strTargetURL, strXml) {
    
    var xmlHttp = getXmlHttp();
    var xmlDoc;
    
    xmlDoc = strToXmlObj( strXml );
    
    xmlHttp.Open("POST", strTargetURL, false);
    xmlHttp.setRequestHeader("Content-Type","text/xml;charset=big5");
    xmlHttp.send(xmlDoc);
    
    alert("responseText: \n" + xmlHttp.responseText);
    
    xmlDoc = xmlHttp.responseXML;
    xmlDoc.createProcessingInstruction("xml", "version='1.0' encoding='Big5'"); 
    
    return xmlDoc;
    //return xmlHttp.responseXML;
    
    
}

function sendXmlObj(strTargetURL, xmlObj) {
    
    var xmlHttp = getXmlHttp();
    var xmlDoc;
    
    xmlHttp.Open("POST", strTargetURL, false);
    xmlHttp.send(xmlObj);
    //xmlDoc = xmlHttp.responseXML;
    
    return xmlHttp.responseXML;

    
}

// 取得指定 URL 的 XML 檔案內容
function loadXml(strXmlURL){

	var xmlDoc = getXmlDoc();
	xmlDoc.async = false;
	xmlDoc.load(strXmlURL);
	
	return xmlDoc;

}

// 取得執行後的回傳資訊
function getRtnMsg(rtnXmlObj) {

	var rtnMsgObj = new Object();
	rtnMsgObj.StatusCode = "";
	rtnMsgObj.Msg = "";
	rtnMsgObj.RecordCount = "";

    if ( rtnXmlObj.selectSingleNode("//Root/ReturnInfo/StatusCode") ) {
        rtnMsgObj.StatusCode = rtnXmlObj.selectSingleNode("//Root/ReturnInfo/StatusCode").text;
    }
    
    if ( rtnXmlObj.selectSingleNode("//Root/ReturnInfo/Message") ) {
        rtnMsgObj.Msg = rtnXmlObj.selectSingleNode("//Root/ReturnInfo/Message").text;
    }    

    if ( rtnXmlObj.selectSingleNode("//Root/ReturnInfo/RecordCount") ) {
        rtnMsgObj.RecordCount = rtnXmlObj.selectSingleNode("//Root/ReturnInfo/RecordCount").text;
    }    

	return rtnMsgObj;
    
}

// 顯示回傳資訊
function showXmlRtnMsg(xmlObj) {

	var strStatusCode = "";
	var strRtnMsg = "";
	var iRecordCount = 0;
	
	if ( xmlObj.selectSingleNode("//Root/ReturnInfo/StatusCode") ) {
		strStatusCode = xmlObj.selectSingleNode("//Root/ReturnInfo/StatusCode").text;
	}
	
	if ( xmlObj.selectSingleNode("//Root/ReturnInfo/Message") ) {
		strRtnMsg = xmlObj.selectSingleNode("//Root/ReturnInfo/Message").text;
	}	
	
	if ( xmlObj.selectSingleNode("//Root/ReturnInfo/RecordCount") ) {
		iRecordCount = parseInt(xmlObj.selectSingleNode("//Root/ReturnInfo/RecordCount").text, 10);
	}
	

	if ( strRtnMsg != "" ) {
		alert(strRtnMsg);
	}


}

// 取得執行後的有效 record 數
function getRecordCount(xmlObj) {
	
	var iRecordCount = 0;
	
	if ( xmlObj.selectSingleNode("//Root/ReturnInfo/RecordCount") ) {
		iRecordCount = xmlObj.selectSingleNode("//Root/ReturnInfo/RecordCount").text;
	}
	
	return iRecordCount;
	
}

// 取得執行後的回傳狀態代碼
function getStatusCode(xmlObj) {

	var strStatusCode = "";
	
	if ( xmlObj.selectSingleNode("//Root/ReturnInfo/StatusCode") ) {
		strStatusCode = xmlObj.selectSingleNode("//Root/ReturnInfo/StatusCode").text;
	}
	
	return strStatusCode;
	
}

