﻿/************************************************************************

  Common.js  共用程式碼
  
  Version:01062005
          01062005 Fixed
            
  使用方法:
    在網頁中加入一段標籤 <Script Language="JavaScript" SRC="Common.js"></Script>
    並將此 Common.js 檔案 Copy 放在與此網頁同一個目錄下
  函數: 
    1.StringToArray(LISTR_DataString)
      功能    : 轉換字串成為陣列
      輸入參數: LISTR_DataString  欲轉換成陣列的字串
      傳回值  : 1.轉換完畢的陣列 (字串中最少有 Column 的分隔符號)
                2.false          (字串中沒有分隔符號)

    2.PrvClearComboBox(Obj)
      功能    : 清除所有 ComboBox 內容
      輸入參數: Obj  欲清除內容的 ComboBox
      傳回值  : 無
      
		  3.PrvClearCobItem(Obj, Index)
      功能    : 清除 ComboBox 的項目
      輸入參數: Obj   欲刪除項目的 ComboBox
                Index 欲刪除項目之索引
      傳回值  : 無

    4.PrvAddComboBoxItem(Obj, Text)
      功能    : 將項目加入 ComboBox
      輸入參數: Obj   ComboBox 物件
                Text  加入的項目字串
      傳回值  : 無

  		5.PrvAddCboItem(Obj, Value, Text)
      功能    : 將項目加入 ComboBox
      輸入參數: Obj   ComboBox 物件
                Value 加入的項目字串Key值
                Text  加入的項目字串文字
      傳回值  : 無
      
    6.Replace(SourceStr, OldStr, NewStr)
      功能    : 字串取代
      輸入參數: SourceStr 來源字串
                OldStr    欲尋找的字串
                NewStr    欲取代的字串
      傳回值  : 取代完畢的字串
    
    7.Trim(String)
      功能    : 將字串的前後空白去除
      輸入參數: 目標字串
      傳回值  : 已去除前後空白的字串
    
    8.SetMousePointer(Pointer)  
      功能    : 設定游標
      輸入參數: 0-普通狀態，11-等待狀態
      傳回值  : 無
    
    9.CheckDate(Format, Date)
      功能    : 檢查日期格式是否正確
      輸入參數: Date		日期字串
      					Format	格式 "YYYYMMDD" or "MMDDYYYY"
      傳回值  : true	日期格式正確
      					false	日期格式錯誤
      					
   10.OnlyNum(evt)
      功能    : 讓 TEXT 物件只能輸入數字
      輸入參數: TEXT 物件的 onkeypress 事件
      傳回值  : 無
      
   11.OnlyNumAndSpot(obj,evt)
      功能    : 讓 TEXT 物件只能輸入數字及 "."
      輸入參數: obj 表示 TEXT 物件
                evt 表示 onkeypress 事件
      傳回值  : 無
      
   12.getFieldValue(field)
   13.CheckLength(Length, Field, FieldsName) 
      功能    : 判斷傳入的物件值的長度
      輸入參數: Length      表示 物件規定的長度
                Field       表示 需要判斷長度的物件
                FieldsName  表示 此物件的名稱
      傳回值  : true ,false
      
   14.CheckSingleQuote(strInput)
      功能    : 將 單引號變為兩個單引號 
      輸入參數: strInput   需要處理的數據
      傳回值  : 處理後的數據
     
   
  注意事項:
    1.PrvClearComboBox 及 PrvAddComboBoxItem 的傳入參數 Obj, 
      其格式為 document.FormName.CboName(or ListName), document 不可省略
    2.StringToArray 所傳回的陣列若為二維, 其用法為 Array[Row][Col]

    3.Netscape 6 PR2, Internet Explorer 4 以上適用

*************************************************************************/
//===========================================================================
//= 將控制項的值清除
//===========================================================================
function CtrValueClear(Ctr)
{
   switch(Ctr.type)  //先檢查傳入控制項的物件型別
   {
      case "text" :
      case "textarea" :
      case "password" :
      case "hidden" :
         Ctr.value="";
         break;
      case "select-one" :
      case "select-multiple" :
         PrvClearComboBox(Ctr);
         break;
      case "button" :
      case "reset" :
      case "submit" :
         break;
      case "radio" :
      case "checkbox" :
        eval("Ctr.checked=false");
        break;
      default :
          alert("無法判別所傳入的控制項到底是那種型別...\n\n" + Ctr.name + ": ???\n\n\n" + str + "\n\nlength = " + Ctr.length);
        break;
   }
   return "";
}

//================================================================================
function SetLabelCaption(LabelObj,Caption)
{
	 LabelObj.childNodes(0).nodeValue=""+Caption;
}
//==================================================================================
//= 取得指定視窗中的唯一的 ID
//==================================================================================
function GetUniqueID(WinObj)
{
	 return WinObj.document.uniqueID;
}

//==================================================================================
//= 算出傳入物件的真正位置
//= 因為物件可能有它的父親,而父親還會有父親,........
//= 而物件的 left 及 top 是以其父親的 client area 的左上角為 (0,0) 來計算的
//= 當然它的父親也是遵守這個原則來運算的.
//= 因此如果你要得到這個物件的絕對座標,那一定要一層一層的往上累計才能得到
//= 一直到沒有父親的那一層為止.
//==================================================================================
function findAbsolutePosition(in_Obj ) 
{
  if( in_Obj.offsetParent ) 
  {
    for( var posX = 0, posY = 0; in_Obj.offsetParent; in_Obj = in_Obj.offsetParent ) 
    {
      posX += in_Obj.offsetLeft;
      posY += in_Obj.offsetTop;
    }
    return [ posX, posY ];
  } else {
    return [ in_Obj.x, in_Obj.y ];
  }
}

//===========================================================================
function setParentAndArragePos(CtrObj,TabDivArray)
{
  var MostLeft=0;
  var MostRight=0;
  var MostTop=0;
  var MostDown=0;
  var CurrLeft=0;
  var CurrTop=0;   
  var DivPos=new Array();
  var CtrPos=new Array();
  var DivLeft=0;
  var DivTop=0;
  var CtrLeft=0;
  var CtrTop=0;
  
  for(var m=TabDivArray.length-1;m>=0;m--)
  {
    //如果物件的 zIndex 比 DIV 的來的大,那就有可能是屬於那個 DIV
    //的控制項,這種控制項就必需把它的父親設為這個 DIV.
    //這裏說的是有可能,但是還是得看看它的範圍是否落在這個 DIV 的範圍內.
    //如果不是,則必需在往下看,因為可能有 DIV 是相同 zIndex 的.
    if(CtrObj.style.zIndex>TabDivArray[m].style.zIndex)
    {
      DivPos=findAbsolutePosition(TabDivArray[m]);
      DivLeft=DivPos[0];
      DivTop=DivPos[1];
      
      CtrPos=findAbsolutePosition(CtrObj);
      CtrLeft=CtrPos[0];
      CtrTop=CtrPos[1];		      
      
      //MostLeft=TabDivArray[m].style.pixelLeft;
      //MostRight=MostLeft+TabDivArray[m].style.pixelWidth;
      //MostTop=TabDivArray[m].style.pixelTop;
      //MostDown=MostTop+TabDivArray[m].style.pixelHeight;

      MostLeft=DivLeft;
      MostRight=MostLeft+TabDivArray[m].style.pixelWidth;
      MostTop=DivTop;
      MostDown=MostTop+TabDivArray[m].style.pixelHeight;
      //判斷控制項是否屬於這個 DIV 的範圍內
      //if((CtrObj.style.pixelLeft>=MostLeft && CtrObj.style.pixelLeft<=MostRight) &&
      //  (CtrObj.style.pixelTop>=MostTop && CtrObj.style.pixelTop<=MostDown))

      if((CtrLeft>=MostLeft && CtrLeft<=MostRight) &&
        (CtrTop>=MostTop && CtrTop<=MostDown))
      {
        //調整控制項的位置為相對於其父 DIV 的位置
        //CurrLeft=CtrObj.style.pixelLeft-MostLeft;
        //CurrTop=CtrObj.style.pixelTop-MostTop;

        CurrLeft=CtrLeft-MostLeft;
        CurrTop=CtrTop-MostTop;
        
        if(CurrLeft>=0)
          CtrObj.style.pixelLeft=CurrLeft;
        else
          CtrObj.style.pixelLeft=0;      
        if(CurrTop>=0)
          CtrObj.style.pixelTop=CurrTop;
        else
          CtrObj.style.pixelTop=0;
        
        //alert(TabDivArray[m].id+"    "+CtrObj.id+"   Top :"+CurrTop+"   Left :"+CurrLeft)  
        TabDivArray[m].appendChild(CtrObj);
        return; 
      }
    }
  }
}
//=======================================================================
//= 以傳入控制項的 cHeight 來排序,由小排到大. 
//= 呼叫法如下 :
//= CtrArray = sortBycHeight(CtrArray, 0, CtrArray.length - 1);
//=======================================================================
function sortBycHeight(CtrArray, start, rest) 
{
  for (var i = rest - 1; i >= start;  i--) 
  {
    for (var j = start; j <= i; j++) 
    {
      if (CtrArray[j+1].cHeight < CtrArray[j].cHeight) 
      {
        var tempCtr = CtrArray[j];
        CtrArray[j] = CtrArray[j+1];
        CtrArray[j+1] = tempCtr;
      }
    }
  }
  return CtrArray;
}
//=======================================================================
//= 以傳入控制項的 zIndex 來排序,由小排到大. 
//= 呼叫法如下 :
//= CtrArray = sortByzIndex(CtrArray, 0, CtrArray.length - 1);
//=======================================================================
function sortByzIndex(CtrArray, start, rest) 
{
  for (var i = rest - 1; i >= start;  i--) 
  {
    for (var j = start; j <= i; j++) 
    {
      if (CtrArray[j+1].style.zIndex < CtrArray[j].style.zIndex) 
      {
        var tempCtr = CtrArray[j];
        CtrArray[j] = CtrArray[j+1];
        CtrArray[j+1] = tempCtr;
      }
    }
  }
  return CtrArray;
}
//=======================================================================
//= 以傳入控制項的絕對位置(Left,Top)來排序,先由 Top 排,再由 Left 排,
//= 由小排到大. Top 的誤差為 2 時視為相同 
//= 呼叫法如下 :
//= CtrArray = sortByAbsoluteTop(CtrArray, 0, CtrArray.length - 1);
//=======================================================================
function sortByAbsoluteTop(CtrArray, start, rest) 
{
  for (var i = rest - 1; i >= start;  i--) 
  {
    for (var j = start; j <= i; j++) 
    {
      if (CtrArray[j+1][1] < CtrArray[j][1]) 
      {
        var tempCtr = CtrArray[j];
        CtrArray[j] = CtrArray[j+1];
        CtrArray[j+1] = tempCtr;
      }
    }
  }
  return CtrArray;
}

//=======================================================================
//= 以傳入控制項的 pixelTop 來排序,由小排到大. 
//= 呼叫法如下 :
//= CtrArray = sortByPixelTop(CtrArray, 0, CtrArray.length - 1);
//=======================================================================
function sortByPixelTop(CtrArray, start, rest) 
{
  for (var i = rest - 1; i >= start;  i--) 
  {
    for (var j = start; j <= i; j++) 
    {
      if (CtrArray[j+1].style.pixelTop < CtrArray[j].style.pixelTop) 
      {
        var tempCtr = CtrArray[j];
        CtrArray[j] = CtrArray[j+1];
        CtrArray[j+1] = tempCtr;
      }
    }
  }
  return CtrArray;
}
//=======================================================================
//= 以傳入控制項的 pixelLeft 來排序,由小排到大.
//= 呼叫法如下 :
//= CtrArray = sortByPixelLeft(CtrArray, 0, CtrArray.length - 1);
//=======================================================================
function sortByPixelLeft(CtrArray, start, rest) 
{
  var i;
  var j;
  for (i = rest - 1; i >= start;  i--) 
  {
    for (j = start; j <= i; j++) 
    {
      if (CtrArray[j+1].style.pixelLeft < CtrArray[j].style.pixelLeft) 
      {
        var tempCtr = CtrArray[j];
        CtrArray[j] = CtrArray[j+1];
        CtrArray[j+1] = tempCtr;
      }
    }
  }
  return CtrArray;
}
//=======================================================================
//= 以傳入控制項的 pixelTop 來排序,由小排到大,如果碰到相等時,則以
//= pixelLeft 來比較,較小的排在前面.
//= 呼叫法如下 :
//= CtrArray = sortByPixelTopLeft(CtrArray, 0, CtrArray.length - 1);
//=======================================================================
function sortByPixelTopLeft(CtrArray, start, rest) 
{
  var tempCtr;
  var FstObjPixelTop;
  var SecObjPixelTop;
  for (var i = rest - 1; i >= start;  i--) 
  {
    for (var j = start; j <= i; j++) 
    {
      FstObjPixelTop=CtrArray[j+1].style.pixelTop;
      SecObjPixelTop=CtrArray[j].style.pixelTop;
      if (FstObjPixelTop < SecObjPixelTop) 
      {
        tempCtr = CtrArray[j];
        CtrArray[j] = CtrArray[j+1];
        CtrArray[j+1] = tempCtr;
      } else if(FstObjPixelTop == SecObjPixelTop)
      {
		      if (CtrArray[j+1].style.pixelLeft < CtrArray[j].style.pixelLeft) 
		      {
		        tempCtr = CtrArray[j];
		        CtrArray[j] = CtrArray[j+1];
		        CtrArray[j+1] = tempCtr;
		      }      	 
      }
    }
  }
  return CtrArray;
}
var ELEMENT_NODE=1; 
var ATTRIBUTE_NODE=2; 
var TEXT_NODE=3;
var CDATA_SECTION_NODE=4; 
var ENTITY_REFERENCE_NODE=5; 
var ENTITY_NODE=6; 
var PROCESSING_INSTRUCTION_NODE=7; 
var COMMENT_NODE=8; 
var DOCUMENT_NODE=9; 
var DOCUMENT_TYPE_NODE=10; 
var DOCUMENT_FRAGMENT_NODE=11 
var NOTATION_NODE=12; 
var DOCUMENT_POSITION_DISCONNECTED=1; 
var DOCUMENT_POSITION_PRECEDING=2; 
var DOCUMENT_POSITION_FOLLOWING=4; 
var DOCUMENT_POSITION_CONTAINS=8;
var DOCUMENT_POSITION_CONTAINED_BY=16; 
var DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC=32; 

//========================================================================
//= 取得傳入節點之子節點中型態為 TEXT_NODE 者,並將它存入 GBL_TextNodeArray
//========================================================================
var GBL_TextNodeArray=new Array();

function GetTextNodeToArray(n) {                      // n is a Node 
  if (n.nodeType == TEXT_NODE)                        // Check if n is TextNode
  {
  	 GBL_TextNodeArray[GBL_TextNodeArray.length]=n;
  }
  var children = n.childNodes;                        // Now get all children of n
  for(var i=0; i < children.length; i++) {            // Loop through the children
    GetTextNodeToArray(children[i]);                  // Recurse on each one
  }
  return;                                             // Return the total number of tags
}
//========================================================================
// Node.ELEMENT_NODE 
// The value of the constant Node.ELEMENT_NODE is 1. 
// Node.ATTRIBUTE_NODE 
// The value of the constant Node.ATTRIBUTE_NODE is 2. 
// Node.TEXT_NODE 
// The value of the constant Node.TEXT_NODE is 3. 
// Node.CDATA_SECTION_NODE 
// The value of the constant Node.CDATA_SECTION_NODE is 4. 
// Node.ENTITY_REFERENCE_NODE 
// The value of the constant Node.ENTITY_REFERENCE_NODE is 5. 
// Node.ENTITY_NODE 
// The value of the constant Node.ENTITY_NODE is 6. 
// Node.PROCESSING_INSTRUCTION_NODE 
// The value of the constant Node.PROCESSING_INSTRUCTION_NODE is 7. 
// Node.COMMENT_NODE 
// The value of the constant Node.COMMENT_NODE is 8. 
// Node.DOCUMENT_NODE 
// The value of the constant Node.DOCUMENT_NODE is 9. 
// Node.DOCUMENT_TYPE_NODE 
// The value of the constant Node.DOCUMENT_TYPE_NODE is 10. 
// Node.DOCUMENT_FRAGMENT_NODE 
// The value of the constant Node.DOCUMENT_FRAGMENT_NODE is 11. 
// Node.NOTATION_NODE 
// The value of the constant Node.NOTATION_NODE is 12. 
// Node.DOCUMENT_POSITION_DISCONNECTED 
// The value of the constant Node.DOCUMENT_POSITION_DISCONNECTED is 0x01. 
// Node.DOCUMENT_POSITION_PRECEDING 
// The value of the constant Node.DOCUMENT_POSITION_PRECEDING is 0x02. 
// Node.DOCUMENT_POSITION_FOLLOWING 
// The value of the constant Node.DOCUMENT_POSITION_FOLLOWING is 0x04. 
// Node.DOCUMENT_POSITION_CONTAINS 
// The value of the constant Node.DOCUMENT_POSITION_CONTAINS is 0x08. 
// Node.DOCUMENT_POSITION_CONTAINED_BY 
// The value of the constant Node.DOCUMENT_POSITION_CONTAINED_BY is 0x10. 
// Node.DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC 
// The value of the constant Node.DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC is 0x20. 
//============================================================================

//============================================================================
// This function is passed a DOM Node object and checks to see if that node 
// represents an HTML tag; i.e., if the node is an Element object. It
// recursively calls itself on each of the children of the node, testing
// them in the same way. It returns the total number of Element objects
// it encounters. If you invoke this function by passing it the
// Document object, it traverses the entire DOM tree.
//============================================================================
var ObjInTheTable;
function countTags(n,objId) {                         // n is a Node 
    if (n.nodeType == 1 /*Node.ELEMENT_NODE*/)        // Check if n is an Element
    {
      if(n.id==objId)
        ObjInTheTable=true;
    }
    var children = n.childNodes;                      // Now get all children of n
    for(var i=0; i < children.length; i++) {          // Loop through the children
      countTags(children[i],objId);                   // Recurse on each one
    }
    return;                                           // Return the total number of tags
}

function BrushColor(n)  {                         // n is a Node 
  try
  {
    if(n.style.backgroundColor==SetMarkColor)
      n.style.backgroundColor="#ffffff";
    else if(n.style.backgroundColor==SetLockColor)
      n.style.backgroundColor="#ffffff";
    else if(n.style.backgroundColor==SetMainRowColor)
      n.style.backgroundColor="#ffffff";
    else if(n.style.backgroundColor==SetDownRowColor)
      n.style.backgroundColor="#ffffff";  
    else if(n.style.backgroundColor==SetMidRowColor)
      n.style.backgroundColor="#ffffff";      
    else if(n.style.backgroundColor==BeSetCtrColor)
      n.style.backgroundColor="#ffffff";
  } catch(e)
  {
  	 //do nothing
  }
  var children = n.childNodes;                // Now get all children of n
  for(var i=0; i < children.length; i++) {    // Loop through the children
    BrushColor(children[i]);                  // Recurse on each one
  }
  return;                                     // Return the total number of tags
}
//=======================================================================
function wait()
{
	 alert("Wait!");
}
//=======================================================================
var prvItgBookMark;

function NewInsertCtrBefText(docObj,CtrObj,TheChr,CtrType,SelectIt)
{
  var txtRange=docObj.body.createTextRange();
  txtRange.moveToElementText(CtrObj);
  var CellStr=txtRange.text;
  var pos=CellStr.indexOf(TheChr);
  var bSuccess;
  prvItgBookMark = txtRange.getBookmark()

  if(pos>=0)
  {
    txtRange.moveStart("character",pos);      //將 textRange 的開頭往後移動pos個單位
    txtRange.collapse(true);                  //將 游標移到 textRange 的開頭
    txtRange.select();
    
    if(CtrType=="Text")
    {
      var CtrObj=CreateControl('Text',CtrObj);
      if(TheChr=="年")
        CtrObj.style.pixelWidth=40;   
      else
        CtrObj.style.pixelWidth=20;   
    } else if(CtrType=="ComboBox",CtrObj)
    {
      var CtrObj=CreateControl('ComboBox',CtrObj);
      CtrObj.style.pixelWidth=150; 	
    }
    CtrObj.ExtName=TheChr;
    CtrObj.Note=CellStr;
    if(SelectIt)
    {
      MutiSelCtrArray[MutiSelCtrArray.length]=CtrObj;
      CtrObj.ParentTD=CellStr;
    }    
    bSuccess = txtRange.moveToBookmark(prvItgBookMark)
  }
}
//=======================================================================
function RplTextWithCtr(txtRange,rplStr,CtrType)
{
  var CtrObj;
  var NoUseObj=new Object();
  prvItgBookMark = txtRange.getBookmark()  
  while(txtRange.findText(rplStr))
  {
    txtRange.select();
    
    if(CtrType=="Text")
    {
      CtrObj=CreateControl('Text',NoUseObj);
      if(TheChr=="年")
        CtrObj.style.pixelWidth=40;   
      else
        CtrObj.style.pixelWidth=20;   
    } else if(CtrType=="ComboBox",NoUseObj)
    {
      CtrObj=CreateControl('ComboBox',NoUseObj);
      CtrObj.style.pixelWidth=150; 	
    } else if(CtrType=="Radio")
    {
      CtrObj=CreateControl('Radio',NoUseObj);
    }   
      
    bSuccess = txtRange.moveToBookmark(prvItgBookMark)
  }
}

function SetCursorPos(txtRange,pos)
{
  //移動 textRange 的頭,以字為單位,一次移動 pos 個
  txtRange.moveStart("character",pos);
  txtRange.collapse(true);
  txtRange.select();      
}
//==========================================================================
function getSelectedRadio(buttonGroup) {
   // returns the array number of the selected radio button or -1 if no button is selected
   if (buttonGroup[0]) { // if the button group is an array (one button is not an array)
      for (var i=0; i<buttonGroup.length; i++) {
         if (buttonGroup[i].checked) {
            return i
         }
      }
   } else {
      if (buttonGroup.checked) { return 0; } // if the one button is checked, return zero
   }
   // if we get to this point, no radio button is selected
   return -1;
} // Ends the "getSelectedRadio" function
//=========================================================================
function getSelectedRadioValue(buttonGroup) {
   // returns the value of the selected radio button or "" if no button is selected
   var i = getSelectedRadio(buttonGroup);
   if (i == -1) {
      return "";
   } else {
      if (buttonGroup[i]) { // Make sure the button group is an array (not just one button)
         return buttonGroup[i].value;
      } else { // The button group is just the one button, and it is checked
         return buttonGroup.value;
      }
   }
} // Ends the "getSelectedRadioValue" function
//==========================================================================
function getSelectedCheckbox(buttonGroup) {
   // Go through all the check boxes. return an array of all the ones
   // that are selected (their position numbers). if no boxes were checked,
   // returned array will be empty (length will be zero)
   var retArr = new Array();
   var lastElement = 0;
   if (buttonGroup[0]) { // if the button group is an array (one check box is not an array)
      for (var i=0; i<buttonGroup.length; i++) {
         if (buttonGroup[i].checked) {
            retArr.length = lastElement;
            retArr[lastElement] = i;
            lastElement++;
         }
      }
   } else { // There is only one check box (it's not an array)
      if (buttonGroup.checked) { // if the one check box is checked
         retArr.length = lastElement;
         retArr[lastElement] = 0; // return zero as the only array value
      }
   }
   return retArr;
} // Ends the "getSelectedCheckbox" function
//==============================================================================
function getSelectedCheckboxValue(buttonGroup) {
   // return an array of values selected in the check box group. if no boxes
   // were checked, returned array will be empty (length will be zero)
   var retArr = new Array(); // set up empty array for the return values
   var selectedItems = getSelectedCheckbox(buttonGroup);
   if (selectedItems.length != 0) { // if there was something selected
      retArr.length = selectedItems.length;
      for (var i=0; i<selectedItems.length; i++) {
         if (buttonGroup[selectedItems[i]]) { // Make sure it's an array
            retArr[i] = buttonGroup[selectedItems[i]].value;
         } else { // It's not an array (there's just one check box and it's selected)
            retArr[i] = buttonGroup.value;// return that value
         }
      }
   }
   return retArr;
} // Ends the "getSelectedCheckBoxValue" function

//=========================================================================
function removeFromList(listField) {
   if ( listField.length == -1) {  // If the list is empty
      alert("There are no values which can be removed!");
   } else {
      var selected = listField.selectedIndex;
      if (selected == -1) {
         alert("You must select an entry to be removed!");
      } else {  // Build arrays with the text and values to remain
         var replaceTextArray = new Array(listField.length-1);
         var replaceValueArray = new Array(listField.length-1);
         for (var i = 0; i < listField.length; i++) {
            // Put everything except the selected one into the array
            if ( i < selected) { replaceTextArray[i] = listField.options[i].text; }
            if ( i > selected ) { replaceTextArray[i-1] = listField.options[i].text; }
            if ( i < selected) { replaceValueArray[i] = listField.options[i].value; }
            if ( i > selected ) { replaceValueArray[i-1] = listField.options[i].value; }
         }
         listField.length = replaceTextArray.length;  // Shorten the input list
         for (i = 0; i < replaceTextArray.length; i++) { // Put the array back into the list
            listField.options[i].value = replaceValueArray[i];
            listField.options[i].text = replaceTextArray[i];
         }
      } // Ends the check to make sure something was selected
   } // Ends the check for there being none in the list
}

//===================================================================================
function addToList(listField, newText, newValue) {
   if ( ( newValue == "" ) || ( newText == "" ) ) {
      alert("You cannot add blank values!");
   } else {
      var len = listField.length++; // Increase the size of list and return the size
      listField.options[len].value = newValue;
      listField.options[len].text = newText;
      listField.selectedIndex = len; // Highlight the one just entered (shows the user that it was entered)
   } // Ends the check to see if the value entered on the form is empty
}

//=================================================================================
function moveUpList(listField) {
   if ( listField.length == -1) {  // If the list is empty
      alert("There are no values which can be moved!");
   } else {
      var selected = listField.selectedIndex;
      if (selected == -1) {
         alert("You must select an entry to be moved!");
      } else {  // Something is selected 
         if ( listField.length == 0 ) {  // If there's only one in the list
            alert("There is only one entry!\nThe one entry will remain in place.");
         } else {  // There's more than one in the list, rearrange the list order
            if ( selected == 0 ) {
               alert("The first entry in the list cannot be moved up.");
            } else {
               // Get the text/value of the one directly above the hightlighted entry as
               // well as the highlighted entry; then flip them
               var moveText1 = listField[selected-1].text;
               var moveText2 = listField[selected].text;
               var moveValue1 = listField[selected-1].value;
               var moveValue2 = listField[selected].value;
               listField[selected].text = moveText1;
               listField[selected].value = moveValue1;
               listField[selected-1].text = moveText2;
               listField[selected-1].value = moveValue2;
               listField.selectedIndex = selected-1; // Select the one that was selected before
            }  // Ends the check for selecting one which can be moved
         }  // Ends the check for there only being one in the list to begin with
      }  // Ends the check for there being something selected
   }  // Ends the check for there being none in the list
}
//====================================================================================
function dumpProps(obj, parent) {
  // Go through all the properties of the passed-in object 
  for (var i in obj) {
    // if a parent (2nd parameter) was passed in, then use that to 
    // build the message. Message includes i (the object's property name) 
    // then the object's property value on a new line 
    if (parent) { var msg = parent + "." + i + "\n" + obj[i]; } else { var msg = i + "\n" + obj[i]; }
    // Display the message. If the user clicks "OK", then continue. If they 
    // click "CANCEL" then quit this level of recursion 
    if (!confirm(msg)) { return; }
    // If this property (i) is an object, then recursively process the object 
    if (typeof obj[i] == "object") { 
      if (parent) { dumpProps(obj[i], parent + "." + i); } else { dumpProps(obj[i], i); }
    }
  }
}
//================================================================================
function openCenteredWindow(url, height, width, name, parms) {
   var left = Math.floor( (screen.width - width) / 2);
   var top = Math.floor( (screen.height - height) / 2);
   var winParms = "top=" + top + ",left=" + left + ",height=" + height + ",width=" + width;
   if (parms) { winParms += "," + parms; }
   var win = window.open(url, name, winParms);
   if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
   return win;
}
//===================================================================================
function clearFieldValue(field)
{
  switch(field.type)
  {
    case "text" :
    case "textarea" :
    case "password" :
    case "hidden" :
      field.value="";
    case "radio" :
    case "checkbox" :
      field.checked=false;
    default :
      field.type=field.type;
  }
}
//===================================================================================
function AutoSelComboBox(field,value)
{
  for(i = 0; i < field.options.length; i++)
  {
	 	 if(field.options[i].value==value)
	 	 {
	 	 	 field.options[i].selected=true;
	 	 	 break;
	 	 }
	 }
}
//===================================================================================
function getFieldValue(field)
{
   switch(field.type)
   {
      case "text" :
      case "textarea" :
      case "password" :
      case "hidden" :
         return field.value;

      case "select-one" :
         var i = field.selectedIndex;
         if (i == -1)   return "";
         else   return (field.options[i].value == "") ? field.options[i].text : field.options[i].value;

      case "select-multiple" :
         var allChecked = new Array();
         for(i = 0; i < field.options.length; i++)
            if(field.options[i].selected)
               allChecked[allChecked.length] = (field.options[i].value == "") ? field.options[i].text : field.options[i].value;
         return allChecked;

      case "button" :
      case "reset" :
      case "submit" :
         return "";

      case "radio" :
      case "checkbox" :
         if (field.checked) { return field.value; } else { return ""; }
      default :
         if(field[0].type == "radio")
         {
            for (i = 0; i < field.length; i++)
               if (field[i].checked)
                  return field[i].value;

            return "";
         }
         else if(field[0].type == "checkbox")
         {
            var allChecked = new Array();
            for(i = 0; i < field.length; i++)
               if(field[i].checked)
                  allChecked[allChecked.length] = field[i].value;

            return allChecked;
         }
         else
            var str = "";
            for (x in field) { str += x + "\n"; }
            alert("I couldn't figure out what type this field is...\n\n" + field.name + ": ???\n\n\n" + str + "\n\nlength = " + field.length);
         break;
   }
   
   return "";
}

//==================================================================================
//This JavaScript function will check to see if the passed-in time is valid. 
//The format can be hh:mm or hh:mm:ss and the hour part can be military time (0-23) 
//or meridian format (1-12 with AM/PM at the end). It will return true if the time 
//is valid. Next week we'll look at this same function but use regular expressions 
//to shorten it quite a bit. 

function isValidTime(value) {
   var colonCount = 0;
   var hasMeridian = false;
   for (var i=0; i<value.length; i++) {
      var ch = value.substring(i, i+1);
      if ( (ch < '0') || (ch > '9') ) {
         if ( (ch != ':') && (ch != ' ') && (ch != 'a') && (ch != 'A') && (ch != 'p') && (ch != 'P') && (ch != 'm') && (ch != 'M')) {
            return false;
         }
      }
      if (ch == ':') { colonCount++; }
      if ( (ch == 'p') || (ch == 'P') || (ch == 'a') || (ch == 'A') ) { hasMeridian = true; }
   }
   if ( (colonCount < 1) || (colonCount > 2) ) { return false; }
   var hh = value.substring(0, value.indexOf(":"));
   if ( (parseFloat(hh) < 0) || (parseFloat(hh) > 23) ) { return false; }
   if (hasMeridian) {
      if ( (parseFloat(hh) < 1) || (parseFloat(hh) > 12) ) { return false; }
   }
   if (colonCount == 2) {
      var mm = value.substring(value.indexOf(":")+1, value.lastIndexOf(":"));
   } else {
      var mm = value.substring(value.indexOf(":")+1, value.length);
   }
   if ( (parseFloat(mm) < 0) || (parseFloat(mm) > 59) ) { return false; }
   if (colonCount == 2) {
      var ss = value.substring(value.lastIndexOf(":")+1, value.length);
   } else {
      var ss = "00";
   }
   if ( (parseFloat(ss) < 0) || (parseFloat(ss) > 59) ) { return false; }
   return true;
}

//==========================================================================
function fillInEmptiesTD() {
   if (document.getElementsByTagName) {
      var tableCells = document.getElementsByTagName("TD");
      var usesInnerText = false;
      for (var i=0; i<tableCells.length; i++) {
         if (tableCells[i].innerText) { usesInnerText = true; i = tableCells.length+1; }
      }
      if (usesInnerText) {
         for (var i=0; i<tableCells.length; i++) {
            if (!tableCells[i].innerText) { 
               if (tableCells[i].innerHTML == "") { tableCells[i].innerHTML = "&nbsp;"; }
            } else if (tableCells[i].innerText == "") { 
               tableCells[i].innerText = "&nbsp;"; 
            }
         }
      }
   }
}
//========================================================================
function ResetAndFillArrayWith(InArray,MaxIndexNum,FillVal)
{
	 InArray.length=0;
	 for(var i=0;i<=MaxIndexNum;i++)
    InArray[i]=FillVal;
}
//========================================================================
function IsNumeric(sText)
{
  var ValidChars = "0123456789.";
  var IsNumber=true;
  var Char;

 
  for (i = 0; i < sText.length && IsNumber == true; i++) 
  { 
    Char = sText.charAt(i); 
    if (ValidChars.indexOf(Char) == -1) 
    {
      IsNumber = false;
      break;
    }
  }
  return IsNumber;   
}
//=========================================================================
function StringToArray(LISTR_DataString, ColSplit, RowSplit) {
	var RowCount = 0;
	var ColCount = 0;
	var ReturnCode = "";
	
	var LSTR_ColDelimitChar=(typeof(ColSplit)=="undefined")?String.fromCharCode(1):ColSplit; 
  var LSTR_RowDelimitChar=(typeof(RowSplit)=="undefined")?String.fromCharCode(1,1):RowSplit;
	  
  if (LISTR_DataString.indexOf(LSTR_RowDelimitChar) != -1) {
  	var LVAR_TempArray1 = LISTR_DataString.split(LSTR_RowDelimitChar);
  	RowCount = LVAR_TempArray1.length - 1;
  	var LVAR_TempArray2 = LVAR_TempArray1[0].split(LSTR_ColDelimitChar);
  	ColCount = LVAR_TempArray2.length;
  	LVAR_TempArray2 = new Array;
  	var LVAR_TempArray3 = new Array;
  	for (var LINT_Row=0; LINT_Row<RowCount; LINT_Row++) {
  		LVAR_TempArray2 = LVAR_TempArray1[LINT_Row].split(LSTR_ColDelimitChar);  		
  		LVAR_TempArray3[LINT_Row] = new Array;
  		for (var LINT_Col=0; LINT_Col<ColCount; LINT_Col++) 
  			LVAR_TempArray3[LINT_Row][LINT_Col] = LVAR_TempArray2[LINT_Col];
  	}
  	return LVAR_TempArray3;
  }
  else {
  	if (LISTR_DataString.indexOf(LSTR_ColDelimitChar) == -1)
  		return false;
  		
  	var LVAR_TempArray1 = LISTR_DataString.split(LSTR_ColDelimitChar);
  	ColCount = LVAR_TempArray1.length - 1;
		var LVAR_TempArray2 = new Array;
  	for (var LINT_Col=0; LINT_Col<ColCount; LINT_Col++) {
  		LVAR_TempArray2[LINT_Col] = LVAR_TempArray1[LINT_Col];
  	}
  	return LVAR_TempArray2;
  }  	
}

function PrvAddCboItem(Obj, Value, Text) {
	var opt = document.createElement("OPTION");
	opt.value = Value;
	opt.text = Text;
	if (navigator.appName=="Netscape") {
		Obj.appendChild(opt);
	}
	else {
		Obj.options.add(opt);
	}
}

function PrvAddComboBoxItem(Obj, Text) {
	var opt = document.createElement("OPTION");
	opt.value = Text;
	opt.text = Text;
	if (navigator.appName=="Netscape") {
		Obj.appendChild(opt);
	}
	else {
		Obj.options.add(opt);
	}
}

function PrvClearComboBox(Obj) {
	if (navigator.appName=="Netscape") {
		while (Obj.firstChild!=null) {
			Obj.removeChild(Obj.firstChild);
		}
	}
	else {
		while (Obj.options.length!=0) {
			Obj.options.remove(0);
		}
	}
}

function PrvClearCobItem(Obj, ItemNo) {
	if (navigator.appName=="Netscape") {
		if (Obj.childNodes[ItemNo]!=null) {
			Obj.removeChild(Obj.childNodes[ItemNo]);
		}
	}
	else {
		if (Obj.options[ItemNo]!=null) {
			Obj.options.remove(ItemNo);
		}
	}
}

/*
function Trim(LISTR_String)
{
  var LINT_I;
  if (LISTR_String == null)
    return "";
    
  for(LINT_I=0;LINT_I<LISTR_String.length;LINT_I++) {
  	if (LISTR_String.substr(LINT_I,1)!=" ") {
      LISTR_String=LISTR_String.substr(LINT_I,LISTR_String.length);
      break;  
    }      
  }  
  for(LINT_I=LISTR_String.length-1;LINT_I>=0;LINT_I--) {
    if (LISTR_String.substr(LINT_I,1) != " ") {
      LISTR_String=LISTR_String.substr(0,LINT_I + 1);
      break;  
    } 
  } 
  return LISTR_String;
}

function Trim(LSTR_String)
{
	var LINT_I;
	var LINT_Length;
	if(LSTR_String==null)
	{
		return LSTR_String;
	}
	else
	{
		LINT_Length=LSTR_String.length;
	}
	if (""+LSTR_String=="undefined") {
		return "";
	}
	for(LINT_I=1;LINT_I<=LINT_Length;LINT_I++)
	{
		if(LSTR_String.indexOf(" ")==0)
		{
			LSTR_String=LSTR_String.substr(1);
		}
		else
		{
			break;
		}
	}	

	for(LINT_I=1;LINT_I<=LINT_Length;LINT_I++)
	{
		if(LSTR_String.lastIndexOf(" ")==LSTR_String.length-1)
		{
			LSTR_String=LSTR_String.substr(0,LSTR_String.length-1);
		}
		else
		{
			break;
		}
	}		
	return LSTR_String;
}
*/
function Trim(str) {
	if (typeof(str)!="undefined") {
		return (""+str).replace(/^\s+/g, '').replace(/\s+$/g, '');
	}
	else {
		return str;
	}
}

function Replace(SourceStr, OldStr, NewStr) {
	var nRegExp = eval("/" + OldStr + "/g");
	var DestStr = SourceStr.replace(nRegExp, NewStr);	
	return DestStr;
}

//鎖住畫面
function ScreenDisabled(obj) {
  var LINT_Count;
  var LGRP_Input = obj.getElementsByTagName("INPUT");
  for (LINT_Count=0;LINT_Count<LGRP_Input.length;LINT_Count++) {
    LGRP_Input[LINT_Count].disabled=true; 
    LGRP_Input[LINT_Count].style.backgroundColor = "#C0C0C0";    
 	}
  var LGRP_Select= obj.getElementsByTagName("SELECT");
  for (LINT_Count=0;LINT_Count<LGRP_Select.length;LINT_Count++) {
    LGRP_Select[LINT_Count].disabled=true;     
    LGRP_Select[LINT_Count].style.backgroundColor = "#C0C0C0";    
 	}  	
}

function CheckDate(LISTR_Type,LISTR_Date)
{
   //檢查日期格式
   //LISTR_Type 表示日期格式:(此格式為 "YYYYMMDD" 或 "MMDDYYYY") 
   //LISTR_Date 為相對應日期格式的資料,此欄位須為 8 碼
   var LSTR_YY;
   var LSTR_MM;
   var LSTR_DD;   
   if (Trim(LISTR_Date).length!=8) {
     return false;
   } 
   
   if (Trim(LISTR_Type) == "YYYYMMDD") {  
     LSTR_YY=LISTR_Date.substr(0,4);
     LSTR_MM=LISTR_Date.substr(4,2);
     LSTR_DD=LISTR_Date.substr(6,2);
   }
   else {
     LSTR_YY=LISTR_Date.substr(4,4);
     LSTR_MM=LISTR_Date.substr(0,2);
     LSTR_DD=LISTR_Date.substr(2,2);
   }
   switch (LSTR_MM) {
     case "02":
       if ((LSTR_YY % 4) != 0) {
         if (LSTR_DD > "28")
           return false;
       }     
       else {
       	 if (((LSTR_YY % 100) == 0) && ((LSTR_YY % 400) != 0)) {
           if (LSTR_DD > "28")
             return false;
         }     
         else {
           if (LSTR_DD > "29")
             return false;              
         }
       }     
       break;
     case "04":
     case "06":
     case "09":
     case "11":
       if (LSTR_DD > "30")
         return false;
       break;
     case "01":
     case "03":
     case "05":
     case "07":
     case "08":
     case "10":
     case "12":
       if (LSTR_DD > "31")
         return false;
       break;  
     default:
       return false;
       break;  
  }
  return true;
}

//只能輸入數字和點
function OnlyNumAndSpot(obj,evt)
{
	if(navigator.appName=="Netscape")
	{
		var evtobj=evt;
	}
	else
	{
		var evtobj=window.event;
	}
  var keyascii=evtobj.keyCode;
	if(keyascii>=48 && keyascii<=57 || keyascii==46)
	{
	}
	else
	{
		evtobj.keyCode=0;
	}
	
	var a=obj.value.indexOf(".");//判斷有無小數點(-1:沒有)
	var b=obj.value.length;//字串長度

	if(b==0&&evtobj.keyCode==46)
	{
		evtobj.keyCode=0;
	}
	if(a!=-1)
	{
		if(evtobj.keyCode==46)
		{
			evtobj.keyCode=0;
		}
	}
}

//只能輸入數字
function OnlyNum(evt)
{
	if(navigator.appName=="Netscape")
	{
		var evtobj=evt;
	}
	else
	{
		var evtobj=window.event;
	}
 var keyascii=evtobj.keyCode;
	if(keyascii>=48&&keyascii<=57)
	{
	}
	else
	{
		if (keyascii==13){
		  return 13;
		}	
		else{
		  evtobj.keyCode=0;
		}
	}
}


//設定MousePointer
//Pointer = 0 --> 普通游標
//Pointer = 11 --> 等待游標
function SetMousePointer(Pointer) {
	var MouseBK=document.getElementById("MouseBK");
	if (MouseBK==null) {
		var MouseBK=document.createElement("DIV");
		MouseBK.id="MouseBK";
		MouseBK.style.height="1px";
		MouseBK.style.width="1px";
		MouseBK.style.left="0px";
		MouseBK.style.top="0px";
		MouseBK.style.zIndex=-99;
		MouseBK.style.display="none";
		MouseBK.style.position="absolute";
		
		document.body.appendChild(MouseBK);
	}	
		
	switch (Pointer) {
		case 0:
			MouseBK.style.height="1px";
			MouseBK.style.width="1px";
			MouseBK.style.left="0px";
			MouseBK.style.top="0px";
			MouseBK.style.zIndex=-99;
			MouseBK.style.display="none";
			MouseBK.style.cursor="auto";
			
			var AllSelect=document.getElementsByTagName("SELECT");
			for (i=0;i<AllSelect.length;i++) {
				AllSelect[i].style.cursor="auto";
			}
			break;
		case 11:
			if (navigator.appName=="Netscape") {
				if (document.body.offsetHeight<window.innerHeight) {
					MouseBK.style.height=window.innerHeight;
				}
				else {
					MousBK.style.height=document.body.offsetHeight;
				}
				MouseBK.style.width=document.body.offsetWidth;
			}
			else {
				if (document.body.offsetHeight<document.body.scrollHeight) {
					MouseBK.style.height=document.body.scrollHeight;
				}
				else {
					MouseBK.style.height=document.body.offsetHeight-20;
				}
				MouseBK.style.width=document.body.offsetWidth-20;
			}
			MouseBK.style.left="0px";
			MouseBK.style.top="0px";
			MouseBK.style.zIndex=99;
			MouseBK.style.display="block";
			MouseBK.style.cursor="wait";

			var AllSelect=document.getElementsByTagName("SELECT");
			for (i=0;i<AllSelect.length;i++) {
				AllSelect[i].style.cursor="wait";
			}
			break;
		default:
			break;
	}
}
//--------------------------------------------------------------
function FillTDWithEmpties(inDocument) 
{
  if (inDocument.getElementsByTagName) 
  {
    var tableCells = inDocument.getElementsByTagName("TD");
    var usesInnerText = false;
    for (var i=0; i<tableCells.length; i++) 
    {
      if (tableCells[i].innerText) { usesInnerText = true; i = tableCells.length+1; }
    }
    if (usesInnerText) 
    {
      for (var i=0; i<tableCells.length; i++) 
      {
        if (!tableCells[i].innerText) 
        { 
          if (tableCells[i].innerHTML == "") { tableCells[i].innerHTML = "&nbsp;"; }
        } else if (tableCells[i].innerText == "") 
        { 
          tableCells[i].innerText = "&nbsp;"; 
        }
      }
    }
  }
}
//----------------------------------------------------------------
function moveUpList(listField) {
   if ( listField.length == -1) {  // If the list is empty
      alert("此列示不存在任何可以移動的項目!");
   } else {
      var selected = listField.selectedIndex;
      if (selected == -1) {
         alert("請選擇一個項目來移動!");
      } else {  // Something is selected 
         if ( listField.length == 0 ) {  // If there's only one in the list
            alert("此列示只有一個項目!\n沒有移動的必要.");
         } else {  // There's more than one in the list, rearrange the list order
            if ( selected == 0 ) {
               alert("此移動的項目已經到頂了,無法再上移!");
            } else {
               // Get the text/value of the one directly above the hightlighted entry as
               // well as the highlighted entry; then flip them
               var moveText1 = listField[selected-1].text;
               var moveText2 = listField[selected].text;
               var moveValue1 = listField[selected-1].value;
               var moveValue2 = listField[selected].value;
               listField[selected].text = moveText1;
               listField[selected].value = moveValue1;
               listField[selected-1].text = moveText2;
               listField[selected-1].value = moveValue2;
               listField.selectedIndex = selected-1; // Select the one that was selected before
            }  // Ends the check for selecting one which can be moved
         }  // Ends the check for there only being one in the list to begin with
      }  // Ends the check for there being something selected
   }  // Ends the check for there being none in the list
}

function moveDownList(listField) {
   if ( listField.length == -1) {  // If the list is empty
      alert("此列示不存在任何可以移動的項目!");
   } else {
      var selected = listField.selectedIndex;
      if (selected == -1) {
         alert("請選擇一個項目來移動!");
      } else {  // Something is selected 
         if ( listField.length == 0 ) {  // If there's only one in the list
            alert("此列示只有一個項目!\n沒有移動的必要.");
         } else {  // There's more than one in the list, rearrange the list order
            if ( selected == listField.length-1 ) {
               alert("此移動的項目已經到底了,無法再下移!");
            } else {
               // Get the text/value of the one directly below the hightlighted entry as
               // well as the highlighted entry; then flip them
               var moveText1 = listField[selected+1].text;
               var moveText2 = listField[selected].text;
               var moveValue1 = listField[selected+1].value;
               var moveValue2 = listField[selected].value;
               listField[selected].text = moveText1;
               listField[selected].value = moveValue1;
               listField[selected+1].text = moveText2;
               listField[selected+1].value = moveValue2;
               listField.selectedIndex = selected+1; // Select the one that was selected before
            }  // Ends the check for selecting one which can be moved
         }  // Ends the check for there only being one in the list to begin with
      }  // Ends the check for there being something selected
   }  // Ends the check for there being none in the list
}
//-----------------------------------------------------------------------------
function isValidDate(dateStr, format) {
   if (format == null) { format = "MDY"; }
   format = format.toUpperCase();
   if (format.length != 3) { format = "MDY"; }
   if ( (format.indexOf("M") == -1) || (format.indexOf("D") == -1) || _
      (format.indexOf("Y") == -1) ) { format = "MDY"; }
   if (format.substring(0, 1) == "Y") { // If the year is first
      var reg1 = /^\d{2}(\-|\/|\.)\d{1,2}\1\d{1,2}$/
      var reg2 = /^\d{4}(\-|\/|\.)\d{1,2}\1\d{1,2}$/
   } else if (format.substring(1, 2) == "Y") { // If the year is second
      var reg1 = /^\d{1,2}(\-|\/|\.)\d{2}\1\d{1,2}$/
      var reg2 = /^\d{1,2}(\-|\/|\.)\d{4}\1\d{1,2}$/
   } else { // The year must be third
      var reg1 = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{2}$/
      var reg2 = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/
   }
   // If it doesn't conform to the right format (with either a 2 digit year or 4 digit year), fail
   if ( (reg1.test(dateStr) == false) && (reg2.test(dateStr) == false) ) { return false; }
   var parts = dateStr.split(RegExp.$1); // Split into 3 parts based on what the divider was
   // Check to see if the 3 parts end up making a valid date
   if (format.substring(0, 1) == "M") { var mm = parts[0]; } else _
      if (format.substring(1, 2) == "M") { var mm = parts[1]; } else { var mm = parts[2]; }
   if (format.substring(0, 1) == "D") { var dd = parts[0]; } else _
      if (format.substring(1, 2) == "D") { var dd = parts[1]; } else { var dd = parts[2]; }
   if (format.substring(0, 1) == "Y") { var yy = parts[0]; } else _
      if (format.substring(1, 2) == "Y") { var yy = parts[1]; } else { var yy = parts[2]; }
   if (parseFloat(yy) <= 50) { yy = (parseFloat(yy) + 2000).toString(); }
   if (parseFloat(yy) <= 99) { yy = (parseFloat(yy) + 1900).toString(); }
   var dt = new Date(parseFloat(yy), parseFloat(mm)-1, parseFloat(dd), 0, 0, 0, 0);
   if (parseFloat(dd) != dt.getDate()) { return false; }
   if (parseFloat(mm)-1 != dt.getMonth()) { return false; }
   return true;
}

//The function returns true if the date is valid and false if it is not. So, 
//your validation could look something like: 

//if (!isValidDate(myDateString, "DMY")) { alert("The date is not in the correct format."); }
//轉成 16 進位文字
function ToHex(dec) 
{
  var hexCharacters = "0123456789ABCDEF"
  if (dec < 0)
    return "00"
  if (dec > 255)
    return "FF"
  var i = Math.floor(dec / 16)
  var j = dec % 16
  return hexCharacters.charAt(i) + hexCharacters.charAt(j)
}

//將 totalNum 除以 perNum,有餘數則將無條件進一
function ToDiv(totalNum,perNum)
{
  if(totalNum % perNum == 0){
  	 return totalNum / perNum;
  }else{
   	 return parseInt(totalNum / perNum)+1;
  }	
}

//將傳入的文數字三位一撇
function AddComma(number) 
{
  number = '' + number;
  if (number.length > 3) 
  {
    var mod = number.length % 3;
    var output = (mod > 0 ? (number.substring(0,mod)) : '');
    for (i=0 ; i < Math.floor(number.length / 3); i++) {
      if ((mod == 0) && (i == 0))
        output += number.substring(mod+ 3 * i, mod + 3 * i + 3);
  else
    output+= ',' + number.substring(mod + 3 * i, mod + 3 * i + 3);
  }
  return (output);
  }
  else return number;
}
//==========================================================================
//= 在一個 TextArea 中,將整個內容 Mark 起來並複製到 ClipBoard 中
//= 用法 : <input onclick="copyit('select1')" type="button" 
//=         value="Press to Copy the Text" name="cpy">
//=        <p>
//=        <textarea name="select1" rows="3" cols="25">
//=        If this is highlighted, then it has been copied.
//         </textarea>
//==========================================================================
function copyit(theField) 
{
  var tempval=eval("document."+theField)
  tempval.focus()
  tempval.select()
  therange=tempval.createTextRange()
  therange.execCommand("Copy")
}
//==========================================================================
//= 在 [整個 document 中] 尋找指定的 字串
//==========================================================================
function findInPage(str) 
{
  var n   = 0;
  var txt, i, found;
  if (str == "")
    return false;
  //------------------------------------------------------------------------  
  //- 如果不要找整個 document 而是要找指定的某一個 TextArea ,
  //- 則這個 TextRange 就必需要考慮用傳入的.
  //------------------------------------------------------------------------
  txt = window.document.body.createTextRange();
  for (i = 0; i <= n && (found = txt.findText(str)) != false; i++) 
  {
    txt.moveStart("character", 1);
    txt.moveEnd("textedit");
  }
  if (found) 
  {
    txt.moveStart("character", -1);
    txt.findText(str);
    txt.select();
    txt.scrollIntoView();
    n++;
  }
  else 
  {
    if (n > 0) 
    {
      n = 0;
      findInPage(str);
    }
    else
     alert("在整個文件中找不到您指定的 '"+str + "' !");
  }
  return false;
}
//=======================================================================
//= 以 Enter 鍵來執行欄位跳越的動作,在 Focus 時記載下一個要跳越的欄位
//= 物件為何
//= Box 1: <input type=text name=box1 onFocus="nextfield ='box2';"><br>
//= Box 2: <input type=text name=box2 onFocus="nextfield ='box3';"><br>
//= Box 3: <input type=text name=box3 onFocus="nextfield ='box4';"><br>
//= Box 4: <input type=text name=box4 onFocus="nextfield ='done';"><br>
//=======================================================================
/*
var ItgNextFocusField = "box1"; // name of first box on page

function keyDown() 
{ 
	 // handles keypress
  k = event.keyCode;
  if (k == 13) 
  { 
  	 // enter key pressed
    if (NextFocusField == 'done') return true; // submit, we finished all fields
    else 
    { // we're not done yet, send focus to next box
      eval('document.' + NextFocusField + '.focus()');
      return false;
    }
  }
}
*/


//========================================================================
//= Login FTP Server
//========================================================================
function Login(username,password,server) 
{
  if (username && password && server) 
  {
    var ftpsite = "ftp://" + username + ":" + password + "@" + server;
    window.location = ftpsite;  //這會取代原來的 Window 的內容,這要考慮作修正
  } else 
  {
    alert("請輸入正確的 username, password, 及 FTP server 的位址.");
  }
}

//========================================================================
//自動跳到下一個,這個與 Form 有關,暫時還沒改,先不能用
//========================================================================
		//************************************************************************
		//*Comment  :
		//*Parameter: true/false, id(s) --> setMustKeyIn(true, "txtCustID,txtCustName");
		//*Return   : N/A
		//*Author   : Dino
		//*Update   :
		//************************************************************************
		function setMustKeyIn(BOL_ON, STR_IDS) {
		  var LVAR_ID = STR_IDS.split(",");
		  var LSTR_ID;
		  var LINT_Row ;
		  var LOBJ_Ref;
		  var LSTR_Type;

		  for (LINT_Row = 0; LINT_Row < LVAR_ID.length; LINT_Row++) {
		    LSTR_ID = LVAR_ID[LINT_Row];
		    LOBJ_Ref = document.getElementById(LSTR_ID);
		    if (LOBJ_Ref) {
		      if (BOL_ON) {
		        if (LOBJ_Ref.itgPrevStyle) {
		        } else {
		          LOBJ_Ref.itgPrevBack = LOBJ_Ref.style.backgroundColor;
		        }
		        LOBJ_Ref.style.backgroundColor = "#FFFF00";
		      }
		      else {
		        if (LOBJ_Ref.itgPrevBack) {
		          LOBJ_Ref.style.backgroundColor = LOBJ_Ref.itgPrevBack;
		        } else {
		          LOBJ_Ref.style.backgroundColor = "";
		        }
		      }
		    }
		  }
		}
		
//================================================================================================================================================================================
// Comment   : 對欄位的長度驗證
// Parameter : Length 為規定的長度 ,Field 驗證長度的控件,FieldsName  驗證長度的控件的名字
// Returns   : true  ,false
// Author    : Hardy
// Update    : 20080318001
//================================================================================================================================================================================
function CheckLength(Length, Field, FieldsName) 
{
	var psValue = "";
	if(typeof Field == "string" )
	{
	  psValue	= Field;
	}
	else
	{
		 psValue = Field.value;	
	}
	Length = parseInt(Length,10)
	var sCheckString = new String(psValue);
	var iCharCounter = sCheckString.length;
	/*for (var i = 0; i < sCheckString.length; i++)
	{
	    if (sCheckString.charCodeAt(i) > 255) 
	    {
	    	iCharCounter += 2;
	    } 
	    else 
	    {
	    	iCharCounter++;
	    }
	}*/
	if (iCharCounter > Length) 
	{
		if(typeof Field == "string" )
		{
			if(FieldsName!="")
			{
				alert(FieldsName + "最大長度為" + Length + "碼");	
		    //Field.focus();
		  }
		}
		else
		{
			if(FieldsName!="")
			{
				alert(FieldsName + "最大長度為" + Length + "碼");	
		    Field.focus();
		  }
	  }
	  return false;
	}
	return  true;
}

//================================================================================================================================================================================
// Comment   : 檢查傳入的字串是否含有單引號,如果有,就用兩個單引號代替
// Parameter : 處理前的值
// Returns   : 處理後的值
// Auther    : Hardy
// Update    : 20080318001
//================================================================================================================================================================================
function  CheckSingleQuote(strInput){
	//var strInput="wos'hang'zhang";
	var strOutput="";	
	if(strInput != undefined && strInput != "undefined" && strInput != "" )
	{
		var indexD=strInput.indexOf("'");
		do{
		indexD=strInput.indexOf("'");
		if(indexD!=-1){
			strOutput+=strInput.substring(0,strInput.indexOf("'"))+"''";
			strInput=strInput.substring(strInput.indexOf("'")+1);
			}else{
					strOutput+=strInput;
				}
		}
		while(indexD!=-1);
	}
	
	return strOutput;
}
function divLeftMenuOnclick(intDivIndex,intDivCount)
{
    for(i=1;i<=intDivCount;i++)
    {
        eval(document.getElementById("divLeftMenu"+i)).className="main_gcs5_browse";
        eval(document.getElementById("divLeftMenuC"+i)).style.display="none";
    }
    eval(document.getElementById("divLeftMenu"+intDivIndex)).className="main_gcs5_browse_01";
    eval(document.getElementById("divLeftMenuC"+intDivIndex)).style.display="block";
}

