﻿//驗證式
var reg_email = /^([a-zA-Z0-9_-])+([a-zA-Z0-9_-]|\.[a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/;
var reg_phone = /^([0-9]|[\-])+$/;//電話號碼
var reg_num = /^([0-9])+$/; //數字檢查
var reg_float = /^[0-9]+(\.[0-9]{1,2})?$/; //檢查小數點兩位
var reg_double = /^[0-9]+(\.[0-9]{1,6})?$/; //檢查小數點6位
//var reg_url =/^[A-Za-z0-9]+/.[A-Za-z0-9]+[//=/?%/-&_~`@[/]/':+!]*([^<>/"/"])*$/
var reg_time = /^(\d+):(\d{1,2})$/;//檢查時間

//按鍵觸發滑鼠事件
function triggerOnclick(id, event) {
    if (event.keyCode == 13) {
        event.keyCode = 9;
        event.returnValue = false;
        document.all[id].click();
    }
}

//檢查陣列是否有值
function inArray(sChkVal, aChkVal) {
    for (var i = 0; i < aChkVal.length; i++) {
        if (aChkVal[i] == sChkVal) {
            return true;
        }
    }
}

//取網址參數
function getUrlParameter(name, url) {
    if (url == null) {
        url = document.URL;
    }
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = url.substr(url.indexOf("?") + 1).match(reg);
    if (r != null) return unescape(r[2]);
    return null;
}

//加入onload事件
function addLoadEvent(func) {   
  var oldonload = window.onload;   
  if (typeof window.onload != 'function') {   
    window.onload = func;   
  } else {   
    window.onload = function() {   
      if (oldonload) {   
        oldonload();   
      }   
      func();   
    }   
  }   
} 


//去空白
function removeBlank(id) {
    var reg_blank = /^\s+|\s+$/g; // /^[ \t\n\r]+/g;
    if (document.getElementById(id) != null) {
        document.getElementById(id).value = document.getElementById(id).value.replace(reg_blank, "");
    }
}

//去空白(陣列處理)
function removeBlankAry(aryTxt) {
    if(aryTxt != null){
        for (var i = 0; i < aryTxt.length; i++) {
            removeBlank(aryTxt[i]);
        }
    }
}

//去冒號
function removeColon(id) {
    if (document.getElementById(id) != null) {
        document.getElementById(id).value = document.getElementById(id).value.replace("：", ":");
    }
}

//去冒號(陣列處理)
function removeColonAry(aryTxt) {
    if (aryTxt != null) {
        for (var i = 0; i < aryTxt.length; i++) {
            removeColon(aryTxt[i]);
        }
    }
}

//填入值
function getVal2Id(id, val) {
    if (document.getElementById(id) != null) {
        document.getElementById(id).value = val;
    }
}

//選單值填入文字欄位
function getVal2Txt(id) {
    if (document.getElementById(id) != null) {
        var optVal = document.getElementById(id).value;
        if (optVal != "0") {
            var val = (optVal != "-1") ? optVal : "";
            var idName = "txt" + id;
            getVal2Id(idName, val);
        }
    }
}

//取日期填入隱藏欄位
function getDateYMD(id) {
    var DateY = document.getElementById(id+"Y").value;
    var DateM = document.getElementById(id+"M").value;
    var DateD = document.getElementById(id+"D").value;
    if (DateY != "") {
        var val = DateY + DateM + DateD;
        var idName = "hid" + id;
        getVal2Id(idName, val);
    } else {
        getVal2Id(idName, "");
    }
}

//填固定長度字元
function getStrPad(strIn, cPad, nPadlength, bPadRight) {
    var strOut = "";
    var strPad = "";
    if (strIn.length < nPadlength) {
        for (var i = 0; i < (nPadlength - strIn.length); i++) {
            strPad += cPad;
        }
        if (bPadRight) {
            strOut = strIn + strPad;
        } else {
            strOut = strPad + strIn;
        }
    } else {
        strOut = strIn;
    }
    return strOut;
}

//區塊列印輸出
function printScreen(block) {
    var value = block.innerHTML;
    var printPage = window.open("", "printPage", "style='position:absolute',top=0,left=0,height=520");
    printPage.document.open();
    printPage.document.write("<HTML><head></head><TITLE>列印輸出</TITLE><BODY onload='window.print();window.close()'>");
    printPage.document.write("<PRE>");
    printPage.document.write(value);
    printPage.document.write("</PRE>");
    printPage.document.close("</BODY></HTML>");
}

//區塊列印預覽
function previewScreen(block) {
    var value = block.innerHTML;
    var printPage = window.open("", "printPage", "style='position:absolute',top=0,left=0,height=520");
    printPage.document.open();
    printPage.document.write("<OBJECT classid=’CLSID:8856F961-340A-11D0-A96B-00C04FD705A2′ height=0 id=wc name=wc width=0></OBJECT>");
    printPage.document.write("<HTML><head></head><TITLE>資料預覽</TITLE><BODY onload='javascript:print();window.close()'>");
    printPage.document.write("<PRE>");
    printPage.document.write(value);
    printPage.document.write("</PRE>");
    printPage.document.close("</BODY></HTML>");
}

//===================================================================================================
//GooglePDF
function openGooglePDF(keyword) {
    if (keyword==undefined) keyword = "%E8%AD%B0%E5%93%A1";
    url = "/Common/ReadPDF.html?q=" + keyword;
    var popWin = window.open(url, "popWin"); //, "left=0,top=0,width=900,height=500,toolbar=no,scrollbars=yes,status=no"
}
//===================================================================================================
//產生PDF連結
function setPDFLink() {
    var divPDFLink = document.getElementById("divPDFLink");
    var url = "/Common/OutputPDF.ashx";
    var link = window.location.href;
    var host = "http://" + top.location.host;
    var linkUrl = link.substring(host.length, link.length);
    linkUrl = linkUrl.replace("&", "*");
    url += "?keyword=" + linkUrl;
    
    if (divPDFLink != null) {
        var aLink = document.createElement("a");
        if (aLink != null) {
            aLink.setAttribute("id", "downPDF");
            aLink.setAttribute("href", url);
            aLink.setAttribute("target", "_blank");
            aLink.innerText = "產生PDF";
            divPDFLink.appendChild(aLink);
        }
        //downloadPDF();
    }
}

function downloadPDF() {
    var downPDF = document.getElementById("downPDF");
    if (downPDF != null) {
        downPDF.click();
    }
    //var url = "/Common/OutputPDF.ashx";
    //window.location.href = url; 
}
