// Author: Ricky Cheung at CyberLab
// Date  : 04 June 2002
// All Rights Reserved.
// (C)Copyright 1995-2003 Cybernetics 1 Limited 

function setCookie(theName,theValue,theDay){
  if ((theName != null) && (theValue != null)){
    expDay = "Wed, 01 Jan 2020 18:56:35 GMT";
    if (theDay != null){
      theDay = eval(theDay);
      setDay = new Date();
      setDay.setTime(setDay.getTime()+(theDay*1000*60*60*24));
      expDay = setDay.toGMTString();
    }
    document.cookie = theName + "="+escape(theValue)+";expires="+expDay;
    return true;
  }
  return false;
}

function getCookie(theName) {
  theName += "=";
  theCookie = document.cookie+";";
  start = theCookie.indexOf(theName);
  if (start!=-1) {
   end = theCookie.indexOf(";",start);
   return unescape(theCookie.substring(start+theName.length,end));
  }
  return false;
}


function clearCookie(theName) { 
  if (theName=='') setCookie(theName,'','2003-01-01');
  else setCookie($cookieTask,'','2003-01-01');
}

function isCookieEnabled() {
  setCookie('temp','temp');
  var temp = getCookie('temp');
  if (!temp) {
    if (isEng) {
      alert('Your browser does not support Cookie.\nPlease enable it or you may not use some particular features.');
    } else {
      alert('你的瀏覽器並不接受Cookie, 如果使用Internet Explorer 6 (IE6)的話, 請在IE6版面做以下設定:\n\n[工具] -> [網際網路選項] -> [隱私] -> [進階]\n\n- 選取 [覆寫自動Cookie處理]\n- 選取 [第一方Cookies : "接受"]\n- 選取 [第三方Cookies : "接受"]\n\n然後按 [確定]\n\n至於其他版本的瀏覽器, 請參考有關文件.');
    }
    return false;
  }
  else return true;
}

function selectAll(cb) {
  for (i=0;i<document.printForm.elements.length;i++) {
    e = document.printForm.elements[i];
    if ((e.name!='allbox') && (e.type=='checkbox')) e.checked=document.printForm.allbox.checked;
  }
  selCheck(cb);
}

function addElement(a1,e1) {
  f=(a1.length>0)?(a1.join().indexOf(e1)):(-1);
  if (f==-1) {
    newStr=(a1.length>0)?(a1.join()+","+e1):(e1);
    a1=newStr.split(",");
  }
  return a1;
}

function removeElement(a1,e1) {
  f=(a1.length>0)?(a1.join().indexOf(e1)):(-1);
  if (f!=-1) {
    newStr=a1.join();
    z=newStr.indexOf(",",f);
    newStr1=newStr.substr(0,f-1)+((z==-1)?(""):(newStr.substr(z)));
    a1=newStr1.split(",");
  }
  return a1;
}

function stringToArray(s1) {return s1.split(",");}
function arrayToString(a1) {return a1.join();}

function countPos(s,p1) {
  c=p2=0;
  if (s) c++;
  s=s.substr(0,p1);
  for (kk=0;kk<=selMax&&p2!=-1;kk++){
    p2=s.indexOf(",",p2);
    if(p2!=-1) {
      c++;
      s=s.substr(p2+1);
    }
  }
  return c;
}

function ralign(s1){
if (s1<10) return "&nbsp&nbsp"+s1;
if (s1<100) return "&nbsp"+s1;
return s1;
}

function selCheck(cb){
  selItem=ttlItem=bos=endStr=0;
  oldStr=getCookie($cookieTask);
  cookieStr=(oldStr)?(oldStr):("");
  for (jj=0;jj<document.printForm.elements.length;jj++) {
    e=document.printForm.elements[jj];
    ca=(cb)?(cb.name=='allbox'):(false);
    if ((e.name!='allbox') && (e.type=='checkbox')) {
      curRef=e.value;
      found=cookieStr.indexOf(curRef);
      selCount=countPos(cookieStr,cookieStr.length);
      if (countPos(cookieStr,cookieStr.indexOf(curRef))+1>=selMax) e.checked=false;
      if (found!=-1 && e!=cb && selCount<selMax) e.checked=true;
      if (ca) e.checked=document.printForm.allbox.checked;
      if (e.checked){
      	if (isCookieEnabled()) {
          if (found==-1){
            newStr=(cookieStr=="")?(curRef):(cookieStr+","+curRef);
            cookieStr=newStr;
          }
          eval('document.printForm.p'+(ttlItem)+'.value=(countPos(cookieStr,cookieStr.indexOf(curRef)))');
          selItem++;
        } else {
          jj=99999999;
        }
      } else {
        if (found!=-1){
          newStr=cookieStr;
          endStr=cookieStr.indexOf(",",found);
          bos=(found==0&&endStr!=-1)?(1):(0);
          newStr1=((found==0)?(""):(newStr.substr(0,found-1)))+((endStr==-1)?(""):(newStr.substr(endStr+bos)));
          cookieStr=newStr1;
        }
        eval('document.printForm.p'+(ttlItem)+'.value=""');
      }
      ttlItem++;
    }
  }
  selCount=countPos(cookieStr,cookieStr.length);

  document.printForm.allbox.checked=(selItem==ttlItem);
  document.printForm.selPrint.value=((isEng)?("- Print "+selCount+" selected records -"):("- 列印已預選的項目共 "+selCount+" 個 -"));
  document.printForm.selCount.value=selCount;
  setCookie($cookieTask,cookieStr,"2020-12-31");
}

function selectClear() {
  setCookie($cookieTask,"","2020-12-31");
  document.printForm.allbox.checked=false;
  selCheck(document.printForm.allbox);
}
