// 換圖效果
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

// 客製決定上下左右位置，沒設定的話就置中
var popupAdminWin;
function openWin2(url, winName, w, h, t, l, s)
{
  var cTop  = screen.height/2 - (h/2);
  var cLeft = screen.width/2 - (w/2);
  var t = (t === (void 0)) ? cTop : t;
  var l = (l === (void 0)) ? cLeft : l;
  var s = (s === (void 0)) ? 'no' : 'yes';
  if (!popupAdminWin) {
    popupAdminWin = window.open(url, winName, 'width=' + w + ',height=' + h + ',scrollbars=' + s + ',top=' + t + ',left=' + l + ',resizable=1');
  } else if (!popupAdminWin.closed) {
    popupAdminWin.location.href = url;
  } else {
    popupAdminWin = window.open(url, winName, 'width=' + w + ',height=' + h + ',scrollbars=' + s + ',top=' + t + ',left=' + l + ',resizable=1');
  }
  popupAdminWin.focus();
}

// 螢幕置中視窗
function openCenterWin(url, nameW, w, h, scroll)
{
  var s = (scroll === (void 0)) ? 0 : 1;
  if (navigator.appVersion.indexOf('4') != -1) {
    var cTop  = screen.height/2 - (h/2);
    var cLeft = screen.width/2 - (w/2);
    popupAdminWin = window.open(url, nameW, 'height=' + h + ',width=' + w + ',scrollbars=' + s + ',resizable=1,menubar=0,toolbar=0,status=0,location=0,directories=0,left=' + cLeft + ',top=' + cTop);
  }
  else {
    popupAdminWin = window.open(url, nameW, 'height=' + h + ',width=' + w + ',scrollbars=' + s + ',resizable=1,menubar=0,toolbar=0,status=0,location=0,directories=0,left=150,top=200');
  }
  popupAdminWin.focus();
}

/**
* 表單輸入欄位 MouseFouse 時，清除預設提示文字
*
* Here is an inline example:
* <code>
* <input type="text" name="k" value="請輸入關鍵字" style="width:90px;"
*        onfocus="clearMe(this);" onblur="restore(this, '請輸入關鍵字');" />
* </code>
*
* 預設的提示文字 ('請輸入關鍵字')，可在 submit 查詢後，由程式動態變化
*
* @param object the form input element object
*/
function clearMe(control)
{
  control.value = '';
}

/**
* 表單輸入欄位 MouseBlur 時，回復預設提示文字
*
* @param object the form input element object
* @param string the string to prompt
*/
function restore(control, str)
{
  if (control.value == '') control.value = str;
}

/**
* 過濾欄位字串前後空白用方法(並去除不合法字元)
* 此函式將在稍後宣告為 String Object's prototype method
*/
function trim()
{
  if (!this) return this;

  // 濾掉字串前後的空白
  for (var begin = 0; begin < this.length; begin++) {
    if (this.charAt(begin) != " ") break;
  }
  for (var end = this.length; end > 0; end--) {
    if (this.charAt(end - 1) != " ") break;
  }

  return this.slice(begin, end);
}

/**
* 檢查欄位是否只包含空白字元
* 此函式將在稍後宣告為 String Object's prototype method
*/
function isEmpty()
{
  if (!this) return true;

  for (var len = 0; len < this.length; len++) {
    if (this.charAt(len) != " ") return false;
  }

  return true;
}

// 將以上 trim 及 isEmpty 兩函式宣告為 String 的屬性
String.prototype.trim = trim;
String.prototype.isEmpty = isEmpty;

/**
* Submit 防呆功能
*
* 在送出表單且 JS 驗證無誤後，
* 將送出及重置的按鈕 disable 起來，
* 以防止傳輸時間過久，使用者懷疑未送出，
* 再重覆送一次資料給程式。
*
* @param object the form object
*/
function submitOnce(fm)
{
    // if IE 4+ or NS 6+
    if (document.all || document.getElementById) {
        // hunt down "submit" and "reset"
        for (var i = 0; i < fm.length; i++) {
            var tempobj = fm.elements[i];
            if (tempobj.type.toLowerCase() == "submit"
                    || tempobj.type.toLowerCase() == "reset") {
                //disable it
                tempobj.value = '傳輸中…';
                tempobj.disabled = true;
            }
        }
    }
}

function getLayerObj(name)
{
    if (document.getElementById)
    {
        return document.getElementById(name);
    }
    else if (document.all)
    {
        return document.all(name);
    }
    else if (document.layers)
    {
        return document.layers[name];
    }
}

/*
* Checks if a value exists in an array
*
* Input: strVal [string] -> the value wanna find
*        objArr [array]  -> search target array data
*
* Return: searches objArr for strVal and returns True
*         if it is found in the array, False otherwise
*/
function inArray(strVal, objArr)
{
    var hasVal = false;
    var size = objArr.length;
    
    if (size > 0)
    {
        for (var i = 0; i < size; i++)
        {
            if (strVal == objArr[i])
            {
                hasVal = true;
                break;
            }
        }
    }

    return hasVal;
}

/**
* 檢查是否為數字型態
*/
function isNumber(str)
{
    var chk_result = true;

    for (var i = 0; i < str.length; i++)
    {
        if (str.charAt(i) < "0" || str.charAt(i) > "9")
        {
            chk_result = false;
            break;
        }
    }

    return chk_result;
}

/**
* 檢查公司統一編號格式是否正確
*/
function isCompID(compID)
{
    var cx = new Array();
    var cy = new Array();
    var cm = 0;
    var cs = 0;

    if (!compID.match(/^[0-9]{8}$/)) { return false; }
    if (compID.match(/^0{8}$/)) { return false; }

    cx[0] = parseInt(compID.substr(0, 1), 10) * 1;
    cx[1] = parseInt(compID.substr(1, 1), 10) * 2;
    cx[2] = parseInt(compID.substr(2, 1), 10) * 1;
    cx[3] = parseInt(compID.substr(3, 1), 10) * 2;
    cx[4] = parseInt(compID.substr(4, 1), 10) * 1;
    cx[5] = parseInt(compID.substr(5, 1), 10) * 2;
    cx[6] = parseInt(compID.substr(6, 1), 10) * 4;
    cx[7] = parseInt(compID.substr(7, 1), 10) * 1;
    
    cy[0] = parseInt(cx[1] / 10, 10);
    cy[1] = cx[1] % 10;
    cy[2] = parseInt(cx[3] / 10, 10);
    cy[3] = cx[3] % 10;
    cy[4] = parseInt(cx[5] / 10, 10);
    cy[5] = cx[5] % 10;
    cy[6] = parseInt(cx[6] / 10, 10);
    cy[7] = cx[6] % 10;

    cs = cx[0] + cx[2] + cx[4] + cx[7] + cy[0] + cy[1] + cy[2] + cy[3] + cy[4] + cy[5] + cy[6] + cy[7];
    cm = cs % 10;

    if (7 == parseInt(compID.substr(6, 1), 10))
    {
        if (0 == cm)
        {
            return true;
        }
        else
        {
            cm = (++cs) % 10;

            return (0 == cm) ? true : false;
        }
    }
    else
    {
        return (0 == cm) ? true : false;
    }
}