﻿// JScript 檔
///Class代號：      MessageBox
///Class名稱：      讀取MessageBox.xml文件信息
///程式說明:     　 從MessageBox.xml文件中循環讀取符合要求的信息
///xx.YYYY/MM/DD    VER     AUTHOR       COMMENTS(說明修改的內容)  
///1.2007/7/10       1.00    Lanny        CREATE 
///1.2008/4/17       1.01    Tom          Modify 適用於好客網
///1.2008/5/6        1.05    Tom          add 加入ie6,ie7,Mozilla, Safari,兼容性 
///1.2008/5/29       2.00    Tom          修改語系切換問題,upd消息檔異步取得方式.進入即異步加載. 

///進入就加載,異步處理
var strOkTitle,strErrorTitle;
var root = null;
var oDoc = null;  
Init();

//判斷瀏覽器
function getIEVersonNumber()
{
    var ua = navigator.userAgent;
    var msieOffset = ua.indexOf("MSIE");
    if(msieOffset < 0) return 0;
    
    return parseFloat(ua.substring(msieOffset + 5, ua.indexOf(";", msieOffset)));
}

//对于FireFox，navigator对象的appName属性值为"Netscape"；Opera9.02的appName属性值为"Opera"
function isIE6(){
    var boolIE6=false;
    var isIE = (navigator.appName == "Microsoft Internet Explorer");

    if(isIE){ 
        if(getIEVersonNumber() <= 6) boolIE6=true;
    }
    return boolIE6 ;
}


function ShowError(strCode,strPara)
{ 
    if (root!=null)
    {
        if(strErrorTitle==null) { strTitle=ShowMessage('000023',''); }
        var messageStr = ShowMessage(strCode,strPara);
      
        // show ajax loader
        try{showLoaderMsg(messageStr);}catch(e){self.status = e.message;}
        
    }
    else alert('Loding...,Plase Wait'); 
    return false;
}
function ShowOk(strCode,strPara)
{
    if (root!=null)
    {
        if(strOkTitle==null) { strOkTitle=ShowMessage('000024',''); }
        var messageStr = ShowMessage(strCode,strPara);
        
        try{showLoaderMsg(messageStr);}catch(e){self.status = e.message;}

    }else{alert('Loding...,Plase Wait');}
    return false;
}

function ReplMsgDesc(vstrMsg,vstrPara){

		var vntArray=vstrPara.split('$');
		for(var i=0;i<vntArray.length;i++){
		    var strRepl ='{'+ i +'}' ;
			var intPos = vstrMsg.indexOf(strRepl);
			if(intPos!=-1){
				vstrMsg = vstrMsg.replace(strRepl, vntArray[i]);
			}
		}
		return vstrMsg
}

//ShowMessage(strCode,vstrPara)
//strCode 消息代號
//vstrPara 帶入消息中的字串參數
//ShowMessage('E0001','def$abc') 
//strCode為E0001時得" Save error！Table:{0}{1} ",vstrPara為"def$abc"
//會將{0}用def替換掉.{1}用abc替換掉
function ShowMessage(strCode,vstrPara)
{
    //修改　aeper 20080411 
    var drpLanguage = document.getElementById('drpLanguage');
    if(drpLanguage==null) drpLanguage = document.getElementById('ctl00_drpLanguage');

    var Culture="ZH-TW";//語言類型　
    if(drpLanguage!=null)
    {
        Culture=drpLanguage.value;
    }  
    var strOut;
    try{
        var url = window.location.href;
        if(document.getElementById('hidServerPort')==null)
            url = 'Data/Message'+Culture+'.xml';
        else
        {
            url = /^([^:]*?:\/\/.*?)[\/|:].*$/i.exec(url)[1] + ':' + document.getElementById('hidServerPort').value 
            + document.getElementById('hidAppName').value + '/Data/Message'+Culture+'.xml';
        }

        if (root==null) root = readMsgXML(Culture);

        //采用xml,xpath方式取得資料.
        var o_Node = root.selectSingleNode("/MessageSettingConfig/Messages[ID='" + strCode + "']/TEXT");

        strOut=ReplMsgDesc(o_Node.text,vstrPara);            

    }catch(e)
    {strOut='No '+url+'';}
    return strOut;
}

function PostXML(vstrURL,vstrXML)
{    
    var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    xmlhttp.Open("POST", vstrURL, false);
    xmlhttp.setRequestHeader("Content-Type", "text/xml");
    xmlhttp.Send(vstrXML);
    if(xmlhttp.responseXML.xml!=""){
		var objDoc = new ActiveXObject("Microsoft.XMLDOM");
		objDoc.async = false;
		objDoc.loadXML(xmlhttp.responseXML.xml);
		return objDoc;
    }
}

function Init(){
    var drpLanguage = document.getElementById('drpLanguage');
    if(drpLanguage==null) drpLanguage = document.getElementById('ctl00_drpLanguage'); 
      
    var Culture="ZH-TW";//語言類型　
    if(drpLanguage!=null)  Culture=drpLanguage.value;
    
    jQuery.get(getMsgXmlUrl(Culture),"",function(data, textStatus){root=data;},"xml");
}

// check for XPath implementation
if( document.implementation.hasFeature("XPath", "3.0") )
{
    Element.prototype.__defineGetter__("text",function(){ return this.textContent; });
    // prototying the XMLDocument
    XMLDocument.prototype.selectNodes = function(cXPathString, xNode)
    {
        if( !xNode ) xNode = this;
        var oNSResolver = this.createNSResolver(this.documentElement);
        var aItems = this.evaluate(cXPathString, xNode, oNSResolver, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);

        var aResult = [];
        for( var i = 0; i < aItems.snapshotLength; i++)
        {
            aResult[i] =  aItems.snapshotItem(i);
        }
        return aResult;
    }

    // prototying the Element
    Element.prototype.selectNodes = function(cXPathString)
    {
        if(this.ownerDocument.selectNodes) 
            return this.ownerDocument.selectNodes(cXPathString, this);
        else
            throw "For XML Elements Only";
    }
}

// check for XPath implementation
if( document.implementation.hasFeature("XPath", "3.0") )
{
   // prototying the XMLDocument
   XMLDocument.prototype.selectSingleNode = function(cXPathString, xNode)
   {
      if( !xNode ) xNode = this;
      var xItems = this.selectNodes(cXPathString, xNode);
      if( xItems.length > 0 ) return xItems[0];
      else  return null;
   }
   
   // prototying the Element
   Element.prototype.selectSingleNode = function(cXPathString)
   {   
      if(this.ownerDocument.selectSingleNode) return this.ownerDocument.selectSingleNode(cXPathString, this); 
      else throw "For XML Elements Only"; 
   }
}

function getMsgXmlUrl(Culture){   
    var aLocation =   document.location.href.split("/"); 
    var sRootURL =   aLocation[0]   +   "//"   +   aLocation[2]   +   "/" + aLocation[3]  ;   
    return sRootURL + "/Data/Message" + Culture + ".xml";   
}