//*******************************************************//
//  程式代碼: Common.js                                  //
//  程式名稱: 共用javascript                             //
//  撰寫日期: 2009.11.09                                 //
//  撰寫人員: Gordon                                     //
//*******************************************************//
//  修改日期: 2010/04/09                                 //
//  修改人員: Morris                                     //
//*******************************************************//

/********************
    組合xml 和xslt
*********************/
function CreateHTML(xml,xsltFile,target,funName){
    var strXML = xml;
    if(strXML =="" || strXML == null){
        strXML ="<row/>";
    }
    
    var xmlDocTest = getXmlDom();
    
    if(!xmlDocTest)
    {
        CreateHTML2(xml,xsltFile,target,funName);
        return;
    }
    
    //取得後臺資料
    var xmlDoc = getXmlDom();
    xmlDoc.async = false;
    if(!xmlDoc.loadXML(strXML)){
        alert("(" + funName + ")" + "xml ERROR !!\n" + strXML);
        return;
    }
  
    // 取得樣式XSLT
    var xsl = getXmlDom();
    xsl.async = false;
    
   
    if(!xsl.load(xsltFile)){
        alert("(" + funName + ")" + xsltFile + " ERROR!!");
        return;  
    }

    // XSL transform
    try{
        strHTML= xmlDoc.transformNode(xsl);
        //document.write("<textarea>" + xmlDoc.xml + "</textarea>")
        document.getElementById(target).innerHTML = strHTML;   
        
    }
    catch(e){
      alert(e.message);
    }
}

/********************
    生成XMLDOM
*********************/
function getXmlDom(){
  try {
    xmlDom = new ActiveXObject("Msxml2.XMLDOM");
  } catch (e) {
    try {
      xmlDom = new ActiveXObject("Microsoft.XMLDOM");
    } catch (e2) {
      xmlDom = false;
    }
  }
  return xmlDom;
}  

function CreateHTML2(xml,xsltFile,target,funName){
    var strXML = xml;
    if(strXML =="" || strXML == null){
        strXML ="<row/>";
    }
    
    var objDOMParser = new DOMParser(); 
    var xml = objDOMParser.parseFromString(strXML, "text/xml");
    xml.async = false;
    
    xsl = document.implementation.createDocument("","",null);    
    try
    {
        xsl.async=false;
        xsl.load(xsltFile);
    }
    catch(e)
    {
        alert(e.message);
    }
    
    var oProcessor = new XSLTProcessor()
    oProcessor.importStylesheet(xsl);

    var oResultDom = oProcessor.transformToDocument(xml);
    var serializer = new XMLSerializer();
    
    document.getElementById(target).innerHTML = serializer.serializeToString(oResultDom);
}

/********************************************
   挑選選單(DIV) - 實體DIV放在 Main.Master
*********************************************/
//function OpenChoiceDIV(){ 

//    RightMenuMove = false;
//    
//    document.getElementById ('ChoicePopupDiv').style.visibility='visible';
//    document.getElementById ('ChoicePopupDiv').style.display='';
//    document.getElementById ('ChoicePopupDiv').style.top= Math.round ((document.documentElement.clientHeight/2)+ document.documentElement.scrollTop)-200 + 'px';
//    document.getElementById ('ChoicePopupDiv').style.left='450px';
//        
//    document.getElementById ('MaskedDiv').style.display='';
//    document.getElementById ('MaskedDiv').style.visibility='visible';
//    document.getElementById ('MaskedDiv').style.top='0px';
//    document.getElementById ('MaskedDiv').style.left='0px';
//    document.getElementById ('MaskedDiv').style.width=  Math.round(document.documentElement.clientWidth) + 'px';
//    document.getElementById ('MaskedDiv').style.height= Math.round(document.documentElement.clientHeight + document.documentElement.scrollTop +200) + 'px';
//}

//function CloseChoiceDIV(){
//    document.getElementById ('MaskedDiv').style.display='none';
//    document.getElementById ('ChoicePopupDiv').style.display='none';
//    RightMenuMove = true;
//}

/********************************************
   挑選選單(DIV)2 - 實體DIV放在 Main.Master
*********************************************/
//function OpenChoiceDIV2(){ 
//    RightMenuMove = false;
//    document.getElementById ('ChoicePopupDiv2').style.visibility='visible';
//    document.getElementById ('ChoicePopupDiv2').style.display='';
//    document.getElementById ('ChoicePopupDiv2').style.top= Math.round ((document.documentElement.clientHeight/2)+ document.documentElement.scrollTop)-200 + 'px';
//    document.getElementById ('ChoicePopupDiv2').style.left='450px';
//        
//    document.getElementById ('MaskedDiv2').style.display='';
//    document.getElementById ('MaskedDiv2').style.visibility='visible';
//    document.getElementById ('MaskedDiv2').style.top='0px';
//    document.getElementById ('MaskedDiv2').style.left='0px';
//    document.getElementById ('MaskedDiv2').style.width=  Math.round(document.documentElement.clientWidth) + 'px';
//    document.getElementById ('MaskedDiv2').style.height= Math.round(document.documentElement.clientHeight + document.documentElement.scrollTop +400) + 'px';
//}

//function CloseChoiceDIV2(){
//    document.getElementById ('MaskedDiv2').style.display='none';
//    document.getElementById ('ChoicePopupDiv2').style.display='none';
//    
//    RightMenuMove = true;
//}


/********************************************
   檢查函式 - 空白,字串長度,字串內容
*********************************************/
function FunCheck_ISNULL(ObjectID){  //是否空白

    if(FunTrimStr(document.getElementById(ObjectID).value) == '')
    {
        return true;
    }
    else
    {
        return false;
    }
}

function FunisNumber(str) //數值检测
{
     if(str.length == 0)
        return false;
        
     for(i = 0; i < str.length; i++) {
        if(str.charAt(i) < "0" || str.charAt(i) > "9")
            return false;
     }
     
     return true;
}

function FunisNumberByObject(ObjectID) //數值检测
{
    var sValue = document.getElementById(ObjectID).value;
    
    return FunisNumber(sValue);
}

function FunisIllegalStr(str)  //字串合法性測試
{
    var sErrMsg ="";
    
    if(str.indexOf("\<script") != -1)
    {
        sErrMsg ="內容包含不合法字串"
    }
    
    return sErrMsg;
}

function FunisIllegalStrByObject(ObjectID)  //字串合法性測試
{
    var sValue = document.getElementById(ObjectID).value;
    
    return FunisIllegalStr(sValue);
}

function cleanXSS(str)  //清除XSS攻擊字串
{
    str = ReplaceAll(str,"<","& lt;");
    str = ReplaceAll(str,">","& gt;");

    str = ReplaceAll(str,"\\(","& #40;");
    str = ReplaceAll(str,"\\)","& #41;");

    str = ReplaceAll(str,"'","& #39;");

    str = ReplaceAll(str,"eval\\((.*)\\)","");
    str = ReplaceAll(str,"[\\\"\\\'][\\s]*javascript:(.*)[\\\"\\\']","\"\"");   

    str = ReplaceAll(str,"script","");

    return str;
}

function FunTrimStr(str) {   //清除空白
    var start = -1,   
    end = str.length;   
    while (str.charCodeAt(--end) < 33);   
    while (str.charCodeAt(++start) < 33);   
    return str.slice(start, end + 1);   
};  

function ReplaceAll(strOrg,strFind,strReplace){  //替換全部指定字串
    var index = 0;
    while(strOrg.indexOf(strFind,index) != -1){
        strOrg = strOrg.replace(strFind,strReplace);
        index = strOrg.indexOf(strFind,index);
    }
    return strOrg
}

function FunCheckLen(str,len){  //檢查文字長度是否符合指定長度
    if(str.length == len)
    {
        return true;
    }
    else
    {
        return false;
    }
}

function FunCheckLenByObject(ObjectID,len){  //檢查文字長度是否符合指定長度
    var sValue = document.getElementById(ObjectID).value;
    
    return FunCheckLen(sValue,len);
}

function FunAlertAndFocus(str,ObjectID){    //顯示訊息與Focus
    alert(str);
    document.getElementById(ObjectID).focus();
}

/*********************
   擷取物件值函式區
**********************/
//取網頁物件值(陣列 1-物件名稱 2-節點名稱 3-擷取物件型態)
function getPageObjValue(sCmdArray)
{
    var sResultXML = "";
    var sID;
    var sNodeName;
    var sType;
    var i;
    

        for(i =0;i<sCmdArray.length;i++)
        {
     try
    {
                sID = sCmdArray[i][0];
                sNodeName = sCmdArray[i][1];
                sType = sCmdArray[i][2];
                switch(sType)
                {
                    case "getTextValue":
                        sResultXML += getTextValue(sID,sNodeName);
                        break;
                    case "getSelectOptionID":
                        sResultXML += getSelectOptionID(sID,sNodeName);
                        break;
                    case "getSelectOptionValue":
                        sResultXML += getSelectOptionValue(sID,sNodeName);
                        break;
                    case "getCheckboxValue":
                        sResultXML += getCheckboxValue(sID,sNodeName);
                        break;
                    case "getRadioValue":
                        sResultXML += getRadioValue(sID,sNodeName);
                        break;
                }
                
                    }
    catch(e)
    {
        var sErr = e;
    }
    
           
        }

    return sResultXML
}

//Text
function getTextValue(sID,sNodeName)
{
    var Obj;
    var ObjValue;
    var sResultXML = "";

    //1.ByName
     Obj = document.getElementsByName(sID);
     if(Obj != null)
     {
        try
        {
            ObjValue = cleanXSS(Obj[0].value);            
            sResultXML = "<" + sNodeName + ">" + ObjValue + "</" + sNodeName + ">";
            return sResultXML;
        }
        catch(ex)
        {
        }
     }
    
    //2.ByID
    Obj = document.getElementById(sID);
     if(Obj != null)
     {
        try
        {
            ObjValue = cleanXSS(Obj.value);            
            sResultXML = "<" + sNodeName + ">" + ObjValue + "</" + sNodeName + ">";
            return sResultXML;
        }
        catch(ex)
        {
        }
     }
    
    return sResultXML;
}

//Select(Option ID)
function getSelectOptionID(sID,sNodeName)
{
    var Obj;
    var ObjID;
    var sResultXML = "";
    
    //1.ByName
     Obj = document.getElementsByName(sID);
     if(Obj[0] !=undefined)
     {
        try
        {
            ObjID = Obj[0][Obj[0].selectedIndex].id;
            sResultXML = "<" + sNodeName + ">" + ObjID + "</" + sNodeName + ">";
            return sResultXML;
        }
        catch(ex)
        {
        }
     }
    
    //2.ByID
    Obj = document.getElementById(sID);
     if(Obj != null)
     {
        try
        {
            ObjID = Obj[Obj.selectedIndex].id;
            sResultXML = "<" + sNodeName + ">" + ObjID + "</" + sNodeName + ">";
            return sResultXML;
        }
        catch(ex)
        {
        }
     }
    
    return sResultXML;
}

//Select(Option Value)
function getSelectOptionValue(sID,sNodeName)
{
    var Obj;
    var ObjValue;
    var sResultXML = "";

    //1.ByName
     Obj = document.getElementsByName(sID);
     if(Obj[0] !=null)
     {
        try{            
            ObjValue = Obj[0][Obj[0].selectedIndex].value;
            sResultXML = "<" + sNodeName + ">" + ObjValue + "</" + sNodeName + ">";
            return sResultXML;
        }
        catch(ex)
        {
        }
     }
    
    //2.ByID
    Obj = document.getElementById(sID);
     if(Obj != null)
     {
        try
        {
            ObjValue = Obj[Obj.selectedIndex].value;
            sResultXML = "<" + sNodeName + ">" + ObjValue + "</" + sNodeName + ">";
            return sResultXML;
        }
        catch(ex)
        {
        }
     }
    
    return sResultXML;
}

//Checkbox
function getCheckboxValue(sID,sNodeName)
{
    var Obj;
    var ObjValue;
    var sResultXML = "<" + sNodeName + ">0</" + sNodeName + ">";
    
    //1.ByName
     Obj = document.getElementsByName(sID);
     if(Obj[0]!=undefined)
     {
        try
        {
            if(Obj[0].checked == true)
            {
                sResultXML= "<" + sNodeName + ">1</" + sNodeName + ">";
            }
            else
            {
                sResultXML= "<" + sNodeName + ">0</" + sNodeName + ">";
            }
            return sResultXML;
        }
        catch(ex)
        {
        }
     }
    
    //2.ByID
    Obj = document.getElementById(sID);
     if(Obj !=null)
     {
        try
        {
            if(Obj.checked == true)
            {
                sResultXML= "<" + sNodeName + ">1</" + sNodeName + ">";
            }
            else
            {
                sResultXML= "<" + sNodeName + ">0</" + sNodeName + ">";
            }
            return sResultXML;
        }
        catch(ex)
        {
        }
     }
    
    return sResultXML;
}

//Radio
function getRadioValue(sID,sNodeName)
{
    var Obj;
    var ObjValue;
    var sResultXML = "";
    
    //1.ByName
     Obj = document.getElementsByName(sID);
     if(Obj[0]!=undefined)
     {
        try
        {
            for(i=0;i<Obj.length;i++)
            {
                if(Obj[i].checked)
                {
                    ObjValue = Obj[i].value;
                    sResultXML = "<" + sNodeName + ">" + ObjValue + "</" + sNodeName + ">";
                    return sResultXML;
                }
            }
        }
        catch(ex)
        {
        }
     }
    
    //2.ByID
    Obj = document.getElementById(sID);
     if(Obj!=null)
     {
        try
        {
            for(i=0;i<Obj.length;i++)
            {
                if(Obj.checked)
                {
                    ObjValue = Obj.value;
                    sResultXML = "<" + sNodeName + ">" + ObjValue + "</" + sNodeName + ">";
                    return sResultXML;
                }
            }
        }
        catch(ex)
        {
        }
    }       
    
    if(sResultXML == "")
        sResultXML = "<" + sNodeName + "></" + sNodeName + ">";
    
    return sResultXML;
}

//*******************************************************//
//  程式代碼: CommonMo.js                                //
//  程式名稱: Morris 共用 javascript                     //
//  撰寫日期: 2010.01.06                                 //
//  撰寫人員: Morris 彤                                  //
//*******************************************************//
//  修改日期:                                            //
//  修改人員:                                            //
//*******************************************************//

/*Window Open*/
function OpenWin(sUrl,iWidth,iHeight,WinName,sBars){

	var iLeft=(screen.width-iWidth)/2-5;
	var iTop=(screen.height-iHeight)/2;	
	
	if (!sBars){sBars="yes"}
	
	window.open(sUrl,WinName,"left=" + iLeft + ",top=" + iTop + ",width=" + iWidth + ",height=" + iHeight + ",scrollbars=" + sBars)	
}

function pageReadOnly(){
  for(var i = 0;i<document.all.length; i++){
    o = document.all(i);
    on = o.nodeName;
    if(o.work==1)
      continue;
    if(on =="INPUT" || on=="TEXTAREA" || on=="SELECT"){
      o.readOnly = true;
      o.disabled = true;;
    }
 
    if(on=="IMG"){
      o.style.display ="none";
      if(o.nextSibling){
        o.nextSibling.style.display ="none";
      }
      
    }
  }
}

var CheckPageInput = {
    "checkInputIsEmpty" : function( CheckArray ){
/*
 Function Array 定義
 Array[ "objID"       , "ObjChName" , "TestItem"                  , "Parameter"   ]
        objID or Name   "欄位中文名"  1(不可為空)                 , "限制輸入長度"
                                      2(不可為空 & 限輸入數字)    , "限制輸入長度"
                                      3(可為空 & 限輸入數字)      , "限制輸入長度"
                                      4(不可為空 & 0 & 限輸入數字)
                                      5(不可為空 & 限輸入數字 & 限制大小 , "長度" , "最小" , "最大"
*/
        var blnOK = true;
        var ArrayLength = CheckArray.length
        //var strMsg1 = " : 欄位不得為空值，如無值請輸入[---]或[0]\n";
        var strMsg1 = " : 欄位不得為空值\n";
        var strMsg2 = " : 欄位限定輸入數字且不可為空\n";
        var strMsg3 = " : 欄位限定輸入數字\n";
        var strMsg4 = " : 欄位限定輸入數字且不可為空 或 0\n";
        var AlertMsg1 = "" , AlertMsg2 = "" , AlertMsg3 = "" , AlertMsg4 = "" , AlertMsg5 = "" , ResultMsg = "" ;
        var objID , ObjChName , TestItem , Parameter , ObjMin , ObjMax ;
        var thisObj , thisObjs ;
        
        try
        {
            for( i = 0 ; i < ArrayLength ; i++ )
            {
                //取所需值
                objID       = CheckArray[i][0];
                ObjChName   = CheckArray[i][1];
                TestItem    = CheckArray[i][2];
                Parameter   = CheckArray[i][3];
                
                //當 getElementById 查無物件時改用getElementsByName
                thisObj = document.getElementById( objID );
                if(thisObj == null )
                {
                    thisObjs = document.getElementsByName( objID );
                    if(thisObjs[0] != null)
                        thisObj = thisObjs[0];
                }
                    
                //物件存在，繼續檢查；物件不存在，檢查下一筆
                if(thisObj != null )
                {
                    switch(TestItem)
                    {
                        //(不可為空)
                        case "1":
                            if(!CheckPageInput.CheckInput_to_Empty(thisObj))
                                AlertMsg1 += "[" + ObjChName + "]" + strMsg1
                            else if(!CheckPageInput.CheckInput_MaxLength(thisObj , Parameter))
                                AlertMsg1 += "[" + ObjChName + "]" + " 欄位長度不得大於" + Parameter + "個字\n";
                            break;
                        //(不可為空 & 限輸入數字)
                        case "2":
                            if(!CheckPageInput.CheckInput_to_Int(thisObj))
                                AlertMsg2 += "[" + ObjChName + "]" + strMsg2
                            else if(!CheckPageInput.CheckInput_MaxLength(thisObj , Parameter))
                                AlertMsg2 += "[" + ObjChName + "]" + " 欄位長度不得大於" + Parameter + "個字\n";
                            break;
                        //(可為空 & 限輸入數字)
                        case "3":
                            if(!CheckPageInput.CheckInput_to_Int_Empty(thisObj))
                                AlertMsg3 += "[" + ObjChName + "]" + strMsg3
                            else if(!CheckPageInput.CheckInput_MaxLength(thisObj , Parameter))
                                AlertMsg3 += "[" + ObjChName + "]" + " 欄位長度不得大於" + Parameter + "個字\n";                                                    
                            break;             
                        //(不可為空 & 0 & 限輸入數字)
                        case "4":
                            if(!CheckPageInput.CheckInput_to_Int_Zero(thisObj))
                                AlertMsg4 += "[" + ObjChName + "]" + strMsg4
                            else if(!CheckPageInput.CheckInput_MaxLength(thisObj , Parameter))
                                AlertMsg4 += "[" + ObjChName + "]" + " 欄位長度不得大於" + Parameter + "個字\n";
                            break;
                        //(不可為空 & 限輸入數字 & 限制大小值)
                        case "5":
                            ObjMin = (CheckArray[i][4]!=null) ? CheckArray[i][4] : "" ;
                            ObjMax = (CheckArray[i][5]!=null) ? CheckArray[i][5] : "" ;
                            var str = CheckPageInput.CheckInput_to_Int_Min_Max( thisObj , ObjMin , ObjMax );
                            if( str != "")
                                AlertMsg5 += "[" + ObjChName + "]" + str
                            else if(!CheckPageInput.CheckInput_MaxLength(thisObj , Parameter))
                                AlertMsg5 += "[" + ObjChName + "]" + " 欄位長度不得大於" + Parameter + "個字\n";
                            break;           
                        default :
                            break;
                    }
                }
            }
        }
        catch(e)
        {
            var sErr = e;
        }
        
        ResultMsg = AlertMsg1 + AlertMsg2 + AlertMsg3 + AlertMsg4 + AlertMsg5 ;
        if(ResultMsg != "")
        {
            alert( ResultMsg )
            return false;
        }
        else    
            return true;         
                                  
    }
    ,"CheckInput_to_Empty" : function( obj )
    {
        //限制輸入框不可為空
        var Val = cleanSpace(obj.value);               
        r = true;        
        if (Val == "" )
        {                   
            obj.focus();                    
            obj.value = "";
            r = false;
        }
        return r;
    }     
    ,"CheckInput_MaxLength" : function( obj , MaxLength )
    {
        //檢查輸入框長度不可超過限定位數
        var Val = cleanSpace(obj.value);               
        r = true;        
        
        if(MaxLength == "" || MaxLength == "0")
            return r ;          
        
        if (Val.length > MaxLength)
        {                
            obj.focus();                    
            obj.value = "";
            r = false;
        }             
        return r;
    }     
    ,"CheckInput_to_Int" : function( obj )
    {
        //限制輸入框只能輸入數值，且不可為空
        var Val = cleanSpace(obj.value);               
        r = true;        
        if (Val == '' || isNaN(Val))
        {                
            obj.focus();                    
            obj.value = "";
            r = false;
        }
        return r;
    }    
    ,"CheckInput_to_Int_Zero" : function( obj )
    {
        //限制輸入框只能輸入數值，且不可為空 和 0
        var Val = cleanSpace(obj.value);               
        r = true;        
        if (Val == '' || Val == '0' || isNaN(Val) )
        {                
            obj.focus();                    
            obj.value = "";
            r = false;
        }
        return r;
    }  
    ,"CheckInput_to_Int_Min_Max" : function( obj , ObjMin , ObjMax )
    {   
        
        var strMsg1 = " : 欄位限定輸入數字且不可為空\n";
        var strMsg2 = " : 欄位限定輸入數值應介於 " + ObjMin + " 到 " + ObjMax + " 之間 \n";
        var strMsg3 = " : 欄位限定輸入數值應大於 " + ObjMin + " \n";
        var strMsg4 = " : 欄位限定輸入數值應小於 " + ObjMax + " \n";
        //限制輸入框只能輸入數值，且不可為空並判斷最大最小值
        var Val = cleanSpace(obj.value);               
        
        alert("val , obj , ObjMin , ObjMax :" + Val + "," + obj + "," + ObjMin + "," + ObjMax);
        

        if (Val == '' || isNaN(Val) )
        {                
            obj.focus();                    
            obj.value = "";   
            return strMsg1;     
        }
        else if ( ObjMax == "" && parseInt(Val,10) < parseInt(ObjMin,10) )
        {
            obj.focus();                    
            return strMsg3;   
        }       
        else if (ObjMin == "" && parseInt(Val,10) > parseInt(ObjMax,10) )
        {
            obj.focus();                    
            return strMsg4;   
        }       
        else if ( ObjMin != "" && ObjMax != "" )
        {
            if (parseInt(Val,10) < parseInt(ObjMin,10) || parseInt(Val,10) > parseInt(ObjMax,10) )
            {
                obj.focus();                    
                return strMsg2;   
            }
            return "";
        }
        else
            return "";              
    }     
    ,"CheckInput_to_Int_Empty" : function( obj )
    {
        //限制輸入框只能輸入數值，但可為空
        var Val = cleanSpace(obj.value);               
        r = true;        
        if (Val != '' && isNaN(Val))
        {                
            obj.focus();                    
            obj.value = "";
            r = false;
        }
        return r;
    }
    ,"CheckInput_to_Mail" : function( objID ){
        obj = document.getElementById(objID);
        obj.value = trim(obj.value) ;
        if(!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(obj.value)))
        {
            alert("信箱格式錯誤請確實填寫!!");
            obj.focus();
        }
        else
            return false;
        
        return true ;
    }
}

/*清除空白字元*/
function cleanSpace(str)
{
    var re = /^[ ]+|[ ]+$/g;
    return str.replace(re,"");
}

/* 即時檢查輸入，非數字不得輸入	check integer Input */
/* 用法:	onkeypress="javascript:return(checkNumber())" */
function checkNumber()
{
    var iE =window.event.keyCode;
    return ((iE >= 48 && iE <=57) || iE==46)
}