﻿//DHtml DOM 網頁動作
var currentTab,currentCont,chkTab;
var targ;
var MenuItemURL = '/Library/MenuItem.asp'; //功能表串接使用


chkTab = false;
//若AP套範本時未使用Tab切換功能，則本Script有關tab的部份不需作相關判斷
window.onload = init();

function listprop(obj, objname) {
    for (var i in obj)
    document.writeln(objname+".<font color=red>"+i+"</font>=<font color=green>"+obj[i]+"</font><br>");
}

function init()
{
   //alert('init....');
   if ( Boolean(document.all('Tab001')) == true )
    {
     chkTab = true;
     currentTab  = document.all('Tab001');
     currentCont = document.all('Tab001Cont');
     if (window.location.hash == '' || window.location.hash == '#' )
        {
         currentTab.className  = 'selTab';
   		currentCont.className = 'selCont';
        }     
     else
        {
         var nHash  = window.location.hash;
         if (nHash.substr(0,4).toUpperCase() == '#TAB')
            {
             //格式化tab後面的數字
             var tabNo = '000' + nHash.substring(4,nHash.length);
             tabNo = 'Tab' + tabNo.substr(tabNo.length - 3 , 3);
             currentTab.className  = 'Tab';
             currentCont.className = 'Cont';
             currentTab  = document.all(tabNo);
             currentCont = document.all(tabNo + 'Cont');  
             currentTab.className  = 'selTab';
   	       currentCont.className = 'selCont';
             //window.location.hash  = '#'; 
            }
        }  
    }
}

function whichElement(e)
{
   //按Tab頁面之處理
   if (!e) var e = window.event
   if (e.target) targ = e.target
   else if (e.srcElement) targ = e.srcElement
   if (targ.nodeType == 3) targ = targ.parentNode // defeat Safari bug
        
   //alert('currentTab=' + currentTab.id);
   //alert('You clicked on ' + targ.id + ',name=' + targ.tagName + ',class=' + targ.className)
   if (targ.className == 'Tab')
      {
      var tabNo = targ.id;
      currentTab.className  = 'Tab';
      currentCont.className = 'Cont';
      currentTab  = document.all(tabNo);
      currentCont = document.all(tabNo + 'Cont');  
      currentTab.className  = 'selTab';
      currentCont.className = 'selCont';
      }
}


//功能： 指定ID物件隱藏／顯示
function ChangeCollapse(vID,vDis)
{
    for (i=1; i < document.all.length ; i++)
     {
         if ( document.all(i).id == vID )
           document.all(i).style.display = vDis;
     }
}

//==========================================================================
//CheckBox選擇效果
function ChangeChked(vID,vChk)
{
    //alert(vID);
    for (i=1; i < document.all.length ; i++)
     {
         if ( document.all(i).id == vID )
           document.all(i).checked = vChk;
     }
}


//==========================================================================
 //關閉彈出視窗，判斷若有父視窗，會自動更新父視窗
 function PopClose()
 {
 if (""+window.opener == "[object]")
  { window.opener.location.reload();}
 window.parent.close();  
 }

//==========================================================================
//功能： 取得URL中特定參數的值 2005.10.04 Ap21 (request by Alvita )
// vChkCase = 0 時 vParaName 不分大小寫；vChkCase = 1 時則相反
function GetQueryString(vParaName,vChkCase)
{
  var nsearch = location.search;
  var pos1 = 0;
  var pos2 = 0;
  var vResult = '';
  if (nsearch.length > 1)
  {
     nsearch  = nsearch  + '&' ;  //方便程式判斷結尾
     vParaName = vParaName + '=' ;
     // 檢查是否有所需參數名稱
     if (vChkCase == "0")
        //若可忽略大小寫
        {
        Usearch   = nsearch.toUpperCase() ;
        UParaName = vParaName.toUpperCase()
        pos1 = Usearch.indexOf(UParaName) ;
        }
     else
        //若不可忽略大小寫
        {
        pos1 = nsearch.indexOf(vParaName) ;
        }
     if (pos1 > 0 )   
        {
        pos1 = pos1 +  vParaName.length;
        pos2 = nsearch.indexOf('&',pos1 + 1) ;
        vResult = nsearch.substring(pos1,pos2);
        }
  }
  return vResult;
}

// Client 端效果控制
//==================================================

//功能： 在指定物件後方加入文字
function AddText(tObj,addTxt) {   
     var revisedTitle;   
     var currentTitle = document.all(tObj).value;   
     revisedTitle =  currentTitle + addTxt;   
     document.all(tObj).value=revisedTitle;   
     document.all(tObj).focus();   
     return; }
     
//功能： 切換展開／收合單ㄧ功能表
function ToggleCollapse(pOpenIconFile,pCloseIconFile) {
   //alert('You clicked on id=[ ' + targ.id + '],tagName=[' + targ.tagName + '],class=[' + targ.className + '],attr=[' + targ.getAttribute + ']')
   var tgrObj,tgrDisplay;
   //往後檢查(下層或循序)是否有DIV物件可供展開收合
   if (targ.nextSibling)
      {tgrObj = targ.nextSibling;} //下層
   else   
      {tgrObj = targ.parentNode.nextSibling;} //循序

   for (i=1;i<=5;i++)
     {
     //alert('i=' + i + ',tag=' + tgrObj.tagName);
     if (tgrObj.tagName == 'DIV')
       {
       if (tgrObj.style.display == 'none' ) tgrDisplay = '';else tgrDisplay = 'none'; 
       tgrObj.style.display = tgrDisplay; 
       break;
       }
     if (tgrObj.nextSibling) tgrObj = tgrObj.nextSibling; else break;
     }

   //往前檢查是否有Span物件可供切換圖示 
   if (targ.previousSibling)
     {
     var vOpenIcon,vCloseIcon;
     if ("" + pOpenIconFile  == "undefined" ) vOpenIcon  = "";  else  vOpenIcon  = "<img src='" + pOpenIconFile + "' border='0'>";
     if ("" + pCloseIconFile == "undefined" ) vCloseIcon = "";  else  vCloseIcon = "<img src='" + pCloseIconFile + "' border='0'>";
     tgrObj = targ.previousSibling;
     for (i=1;i<=5;i++)
       {
       if (tgrObj.tagName == 'SPAN')
         {
         //alert('inner=[' + tgrObj.innerHTML + ']');
         if (tgrDisplay == 'none')
           {if (vCloseIcon != "") tgrObj.innerHTML = vCloseIcon;}
         else
           {if (vOpenIcon != "")  tgrObj.innerHTML = vOpenIcon;}
         break;
         }
       if (tgrObj.previousSibling) tgrObj = tgrObj.previousSibling; else break;
       }
     }

}
//==================================================
//功能： 功能表串接，供MainMenu.asp,MenuItem.asp呼叫
function MenuChain(vMenuTitle,vPara,vObj)

// 若vPara中的參數名稱與繼承的物件有衝突時，以vPara為優先 2000.01.06
{
   if ( vObj != "1" )
        var nsearch = "" ;
    else
      {
        var nsearch = location.search;
        // 檢查是否有MenuTitle參數
       Usearch = nsearch.toUpperCase() ;
       pos = Usearch.indexOf('MENUTITLE') ;
       if (pos > 0)
           {
             other = nsearch.indexOf('&',pos);
             if (other > 0)        //若 MenuTitle後有別的參數
                nsearch = nsearch.substring(0,pos - 1) + nsearch.substring(other,nsearch.length) ;
             else
                nsearch = nsearch.substring(0,pos - 1) ;
            }

        // 檢查是否有 ?& 的符號存在，即檢查nsearch是否有不合法的參數語法
       pos = nsearch.indexOf('?&') ;
       if (pos > 0)
          nsearch = nsearch.substring(0,pos) + nsearch.substring(pos + 1,nsearch.length) ;
       //檢查nsearch是否第一字母為?
       if (nsearch.substring(0,1) == '&')
          nsearch = '?' + nsearch.substring(1,nsearch.length) ;

       }

  //vObj檢查完畢，開始組合所需參數

  if ( nsearch == "" )
      // var nUrl = '/Menu/MenuItem.aspx?MenuTitle=' + vMenuTitle ;
      var nUrl = MenuItemURL + '?MenuTitle=' + vMenuTitle ;
  else
      //var nUrl = '/Menu/MenuItem.aspx' + nsearch + '&MenuTitle=' + vMenuTitle ;
      var nUrl = MenuItemURL + nsearch + '&MenuTitle=' + vMenuTitle ;

  //alert('now Url = ' + nUrl);
   if (vPara != "")
      { vPara = vPara + '&' ;    //方便程式判斷參數結尾點
        var i = 0 ;
        // alert('Original Url=' + nUrl);
        while( i < vPara.length )
         { var pos1 = vPara.indexOf('=',i) ;
           var pos2 = vPara.indexOf('&',i) ;
           var ParaName = vPara.substring(i,pos1) ;
           var ParaValue = vPara.substring( pos1+1 , pos2 ) ;
           pos1 = nUrl.indexOf(ParaName) ;
           if (pos1 > 0)   //若vPara的參數名稱存在於所繼承之物件字串中,則置換物件字串之值為vPara中之值
             { nUrl = nUrl + '&' ;  //方便程式判斷參數結尾點
               pos2 = nUrl.indexOf('&',pos1) ;
               nUrl = nUrl.substring(0,pos1) + ParaName + '=' + ParaValue + nUrl.substring(pos2,nUrl.length - 1 ) ;
             }
           else            //若vPara的參數名稱不存在於所繼承之物件字串中,則將其加入欲連結之Url中
               nUrl = nUrl + '&' + ParaName + '=' + ParaValue;

            i = pos2 + 1;
           } // end of  while loop
       }   // end of vPara != ""

  //alert('Final Url = ' + nUrl);
  //parent.frames(0).location.href = nUrl ;
  parent.MenuPage.location.href = nUrl ;
}

//==========================================================================
//功能： 呼叫新Page，並顯示在右邊Frame 或 PopUp 彈出新視窗
//供MainMenu.asp,MenuItem.asp呼叫 
function Link(vTitle,vPage,vPara,vObj,vHash)

// 若vPara中的參數名稱與繼承的物件有衝突時，以vPara為優先
// 2004.03.31 修正當Para後面參數為前面參數之SubString時誤判的問題
{
  var GoLink    = false;
  var PopUpRate = 0;
  if (vPara.indexOf('PressSubmit=刪除') > 0)
    {if (confirm('您確定要繼續執行『刪　除』作業嗎?'))
         GoLink = true;
    }
  else
    {var GoLink = true; }
  //若GoLink OK才往下判斷
  if (GoLink == true)
  {
    var nsearch = location.search;
   //組合繼承字串
   if ( vObj == '1' )
      {var nUrl = nsearch.substring(1,nsearch.length) ;}
   else
      {var nUrl = '' ; }
      
   //alert('Orriginal vPara=['+ vPara +']');   
   //alert('Orriginal nUrl=['+ nUrl +']');
      
   if (vPara != '' ){
       vPara = '&' + vPara + '&' ;  //方便程式判斷
       var i = 0 ;
       while( i < vPara.length - 1 )
        { 
          nUrl  = '&' + nUrl  + '&' ;  //方便程式判斷
          var pos1 = vPara.indexOf('=',i) ;
          var pos2 = vPara.indexOf('&', pos1 + 1) ;
          var ParaName = vPara.substring(i,pos1 + 1) ;
          var ParaValue = vPara.substring( pos1+1 , pos2 ) ;
          if (ParaName.toUpperCase() == "&POPUP=")
             {PopUpRate = ParaValue;}
             
          pos1 = nUrl.indexOf(ParaName) ;
          if (pos1 > 0 )   
            //若vPara的參數名稱存在於所繼承之物件字串中,則置換物件字串之值為vPara中之值
            { 
              pos2 = nUrl.indexOf('&',pos1 + 1) ;
              nUrl = nUrl.substring(1,pos1) + ParaName + ParaValue + nUrl.substring(pos2,nUrl.length - 1) ;
            }
          else            //若vPara的參數名稱不存在於所繼承之物件字串中,則將其加入欲連結之Url中
            { nUrl = nUrl.substring(1,nUrl.length - 1) + ParaName + ParaValue; }
          //alert(' ParaName=[' + ParaName + ']\n ParaValue=[' + ParaValue + ']\n nUrl=[' + nUrl  + ']');  
          i = pos2 ;
        } // end of  while loop
   } // end of vPara != ""
   
   if (nUrl.substring(0,1) == "&")
      nUrl = vPage + '?ApTitle=' + vTitle + nUrl + vHash ;
   else
      nUrl = vPage + '?ApTitle=' + vTitle + '&' + nUrl + vHash ;
      
   //alert('Final Url = ' + nUrl);
   
   if (PopUpRate > 0)
     {
       //alert('PopUpRate = ' + PopUpRate);
       if (PopUpRate < 1)
         {
          var PopTop    = Window.screen.Height * ( 1 - PopUpRate) * 0.5;
          var PopLeft   = Window.screen.Width  * ( 1 - PopUpRate) * 0.5;
          var PopWidth  = Window.screen.Width  * PopUpRate;
          var PopHeight = Window.screen.Height * PopUpRate;
         }
       else
         {
          var PopTop    = 1;
          var PopLeft   = 1;
          var PopWidth  = Window.screen.Width  - 12;
          var PopHeight = Window.screen.availHeight - 30;
         }
     //alert('Top[' + PopTop + '],Left[' + PopLeft + '],Width[' + PopWidth + '],Height[' + PopHeight + ']');
     PopUp = window.open(nUrl,'PopWindow', 'top='+ PopTop +',left='+ PopLeft +',width='+ PopWidth +',height='+ PopHeight +',location=0,scrollbars=1,resizable=1,menubar=1,toolbar=1',0);
     PopUp.focus();
     }
   else
     { 
      //parent.frames(1).location.href= nUrl;
      parent.ContPage.location.href= nUrl;
     }
   } // end of GoLink
}
//==========================================================================

//功能： 遞迴呼叫目前Page
function SelfLink(vTitle,vPara,vObj,vHash)
// 若vPara中的參數名稱與繼承的物件有衝突時，以vPara為優先
{
  if (vPara.indexOf('PressSubmit=刪除') > 0)
     vMsg = '您確定要繼續執行『刪　除』作業嗎?';
  else
     vMsg = '您確定要繼續執行嗎?' ;

  if (confirm(vMsg))
  {

    var nsearch = location.search;
   //組合繼承字串
   if ( vObj == '1' )
      {var nUrl = nsearch.substring(1,nsearch.length) ;}
   else
      {var nUrl = '' ; }
      
   if (vPara != '' ){
       vPara = '&' + vPara + '&' ;  //方便程式判斷
       var i = 0 ;
       while( i < vPara.length - 1 )
        { 
          nUrl  = '&' + nUrl  + '&' ;  //方便程式判斷
          var pos1 = vPara.indexOf('=',i) ;
          var pos2 = vPara.indexOf('&', pos1 + 1) ;
          var ParaName = vPara.substring(i,pos1 + 1) ;
          var ParaValue = vPara.substring( pos1+1 , pos2 ) ;
          pos1 = nUrl.indexOf(ParaName) ;
          if (pos1 > 0 )   
            //若vPara的參數名稱存在於所繼承之物件字串中,則置換物件字串之值為vPara中之值
            { 
              pos2 = nUrl.indexOf('&',pos1 + 1) ;
              nUrl = nUrl.substring(1,pos1) + ParaName + ParaValue + nUrl.substring(pos2,nUrl.length - 1 ) ;
            }
          else            //若vPara的參數名稱不存在於所繼承之物件字串中,則將其加入欲連結之Url中
            { nUrl = nUrl.substring(1,nUrl.length - 1 ) + ParaName + ParaValue; }
          //alert(' ParaName=[' + ParaName + ']\n ParaValue=[' + ParaValue + ']\n nUrl=[' + nUrl  + ']');    
          i = pos2 ;
        } // end of  while loop
   } // end of vPara != ""
   
   vPage = 'http://' + location.hostname + location.pathname
   if (nUrl.substring(0,1) == "&")
      nUrl = vPage + '?ApTitle=' + vTitle + nUrl + vHash ;
   else
      nUrl = vPage + '?ApTitle=' + vTitle + '&' + nUrl + vHash ;

   //alert('Final Url = ' + nUrl);
  location.href= nUrl;
  } // for confirm=true
}


//==========================================================

//功能： 以蹦現式視窗顯示報表Page

function RepPopLink(vTitle,vPage,vPara,vObj)
{
    var nsearch = location.search;
   if ( vObj == "1" )
      if (nsearch != "")
         var nUrl = '/intranet/util/RepAdmin/Default.asp' + nsearch + '&RepBody=' + vPage ;
      else
         var nUrl = '/intranet/util/RepAdmin/Default.asp?RepBody=' + vPage ;
   else
      {var nUrl = '/intranet/util/RepAdmin/Default.asp?RepBody=' + vPage}

   if (vPara != "")
      nUrl = nUrl + '&' + vPara ;

   if (vTitle != "")       // 若有傳入vTitle ,檢查現有nUrl是否有MenuTitle存在,若有則置換物件字串之值為vTitle中之值
    {
       vUrl = nUrl.toUpperCase() ;
       pos = vUrl.indexOf('MENUTITLE') ;
       if (pos > 0)  //若現有nUrl有MenuTitle參數，先將其拿掉再加入
           {
             other = nUrl.indexOf('&',pos);
             if (other > 0)        //若 MenuTitle後有別的參數
                nUrl = nUrl.substring(0,pos - 1) + nUrl.substring(other,nUrl.length) ;
             else
                nUrl = nUrl.substring(0,pos - 1) ;
            }

        //設定新nUrl之值
        nUrl = nUrl + '&MenuTitle=' + vTitle ;
     }

  // alert(nUrl);

   if (window.screen.width == 640)
      window.open(nUrl,'RepWindow', 'top=60,left=40,fullscreen=0,width=560,height=360,resizable=1,scrollbars=no,toolbar=no,location=no,menubar=no',0);
   else
      window.open(nUrl,'RepWindow', 'top=50,left=30,fullscreen=0,width=720,height=440,resizable=1,scrollbars=no,toolbar=no,location=no,menubar=no',0);
}

//===========================================================

//功能： 呼叫新Page，並顯示在左邊Frame
function MenuPage(vUrl) {
   //parent.frames(0).location.href= vUrl  ;
   parent.MenuPage.location.href= vUrl  ;
}

//功能： 呼叫新Page，並顯示在右邊Frame
function ContPage(vUrl) {
  //parent.frames(1).location.href= vUrl  ;
  parent.ContPage.location.href= vUrl  ;
}


//功能： 呼叫新Page，並顯示在左右Frame
function MenuContPage(vUrl1,vUrl2) {
   //parent.frames(0).location.href= vUrl1  ;
   //parent.frames(1).location.href= vUrl2  ;
   parent.MenuPage.location.href= vUrl1  ;
   parent.ContPage.location.href= vUrl2  ;
}

//功能： 改變Menu Frame的寬度
function chgMenuFrameCol()  {
   //alert(top.document.all('MenuFrame').cols);
   var w = top.document.all('MenuFrame').cols;
   var n = w.substring(0,w.indexOf(','));
   if (eval(n) > 100)
     top.document.all('MenuFrame').cols = '30,*';
   else
     top.document.all('MenuFrame').cols = '160,*';  
   //alert('w=['+w+'],n=['+n+']')
}
//===========================================================

//功能： 呼叫表格 Column 自動選擇功能 Ap21 2002.03.20
//修正原VBScript的ColSelect(),RowSelect() 呼叫時會發生的Error
function ColSelect(pTable,pCol,pChk){
    /*
    pTable表格名稱 , pCol 該欄位Column序數, pChk 打勾／取消打勾 true/false
    Ex: ColSelect('UserList',1,'True')
    */
    //alert('pTable=['+pTable+'],pCol=['+pCol+'],pChk=['+pChk+']');
    var oTab = document.all(pTable);
    for(i=0;i < oTab.rows.length;i++){
    //  if (oTab.rows(i).cells(pCol).children(0).tagName=="INPUT")
    //         oTab.rows(i).cells(pCol).children(0).checked = (pChk.toUpperCase() == "TRUE")?true:false;
      if (oTab.rows(i).cells(pCol).childNodes(0).type=="checkbox")
         oTab.rows(i).cells(pCol).childNodes(0).checked = (pChk.toUpperCase() == "TRUE")?true:false;
    }
}

function RowSelect(pTable,pRow,pChk){
    /*
    pTable表格名稱 , pRow 該欄位Row序數, pChk 打勾／取消打勾 true/false
    Ex: RowSelect('UserList',1,'True')
    */
    //alert('pTable=['+pTable+'],pRow=['+pRow+'],pChk=['+pChk+']');
    var oTab = document.all(pTable);
    for(i=0;i < oTab.rows(pRow).cells.length;i++){
      if (oTab.rows(pRow).cells(i).childNodes(0).type=="checkbox")
         oTab.rows(pRow).cells(i).childNodes(0).checked = (pChk.toUpperCase() == "TRUE")?true:false;
    }
}

//==== 清單項目選擇 ================
function markall(s)
{
  var i;
  if (""+s == "[object]" ) {
    for (i=0;i < s.length;i++) {
      if (s.options[i].value == "" ) 
        s.options[i].selected = false;
      else  
        s.options[i].selected = true;
    }
  }
}

function move_elem(s,t)
{
  var i,j,si,ti;
  if (""+s == "[object]" && ""+t == "[object]") {
    for (si=0;si<s.length;) {
      if (s.options[si].selected) {
        ti = t.length++;
        t.options[ti].value = s.options[si].value;
        t.options[ti].text  = s.options[si].text;

        for (j=si;j<s.length-1;j++) {
          s.options[j].value = s.options[j+1].value;
          s.options[j].text  = s.options[j+1].text;
          s.options[j].selected = s.options[j+1].selected;
        }
        s.length--;
      }
      else si++;
    }
  }
}

function exchg(sob,i,j)
{
  var tmp;
  tmp = sob.options[i].value;
  sob.options[i].value = sob.options[j].value;
  sob.options[j].value = tmp;

  tmp = sob.options[i].text;
  sob.options[i].text = sob.options[j].text;
  sob.options[j].text = tmp;

  tmp = sob.options[i].selected;
  sob.options[i].selected = sob.options[j].selected;
  sob.options[j].selected = tmp;
}

function move_up(sob)
{
  var i=sob.selectedIndex;
  for (i=1;i<sob.length;i++) {
    if (sob.options[i].selected)
      if (!sob.options[i-1].selected)
        exchg(sob,i-1,i);
  }
}

function move_down(sob)
{
  var i;
  for (i=sob.length-2;i>=0;i--) {
    if (sob.options[i].selected)
      if (!sob.options[i+1].selected)
        exchg(sob,i,i+1);
  }
}



