﻿// JScript 檔
function beginrefresh()
{
    var id="showTimeDiv";
    var ctrl=document.getElementById(id);
    ctrl.style.backgroundColor="#ff8040";
    ctrl.style.color="#ffffff";
    ctrl.innerHTML=showtime();
    setTimeout("beginrefresh()",1000);
}

function showtime()
{
var now = new Date()
var hours = now.getHours()
var minutes = now.getMinutes()
var seconds = now.getSeconds()
var timeValue = (now.getFullYear()-1911)+"."+(now.getMonth()+1) +"."+now.getDate()+"( "+GetCultureWeekName(now.getDay())+" ) ";
if(hours>=1 && hours <4)
{timeValue += ("凌晨")}
if(hours>=4 && hours<6)
{timeValue += ("清晨")}
if(hours>=6 && hours<9)
{timeValue += ("早上")}
if(hours>=9 && hours<12)
{timeValue += ("上午")}
if(hours>=12 && hours<13)
{timeValue += ("中午")}
if(hours>=13 && hours<18)
{timeValue += ("下午")}
if(hours>=18 && hours <19)
{timeValue += ("傍晚")}
if(hours>=19 && hours <24)
{timeValue += ("晚上")}
if(hours<1)
{timeValue += ("午夜")}
timeValue  += ((hours > 12) ? hours - 12 : hours)
timeValue  += ((minutes < 10) ? ":0" : ":") + minutes
timeValue  += ((seconds < 10) ? ":0" : ":") + seconds
return timeValue
}

function GetCultureWeekName(weekIindex)
{
    var weekName=Array('日','一','二','三','四','五','六');
    return weekName[weekIindex];
}

function ClearMessage2(message,ctrl)
{
    ctrl.innerHTML="<font color='green'>"+message+"</font>";
}

function ClearStatusMessage(message)
{
    window.status=message;
}
function ClearStatusAtTimeOut(message,tNumber)
{
    setTimeout("window.status='"+message+"'",tNumber);
}
function GetSelectedIndex(oo,textValue)
{
            for(i=0;i<oo.options.length;i++)
            {
                if(oo.options[i].text==textValue)
                {
                    oo.options[i].selected=true;
                    return i;
                }
            }
            return -1;
}

function GetSelectedIndexByValue(oo,itemValue)
{
            for(i=0;i<oo.options.length;i++)
            {
                if(oo.options[i].value==itemValue)
                {
                    oo.options[i].selected=true;
                    return i;
                }
            }
            return -1;
}

function doClick(linkId){
            var o = document.getElementById(linkId);   
            if(document.all && typeof(document.all) == "object")   //IE   
            {   
                o.fireEvent("onclick");   
            }   
            else   
            {   
                var  e = document.createEvent('mouseEvent');
                e.initEvent('click',false,false);   
                o.dispatchEvent(e);   
            }
}

function GetCheckBox(ctrl)
        {
            var rv=new Array();
            var rowsCount=ctrl.rows.length;
            var cellsCount;
            for(i=0;i<rowsCount;i++)
            {
                cellsCount=ctrl.rows[i].cells.length;
                for(j=0;j< cellsCount;j++)
                {
                    if(ctrl.rows[i].cells[j].childNodes[1] != null)
                    {
                        if(ctrl.rows[i].cells[j].childNodes[0].checked ==true)
                            rv[rv.length] =ctrl.rows[i].cells[j].childNodes[1].innerText;
                    }
                }
            }
            return rv;
        }
        
function MouseOver(ctrl)
{
    var imgSrc=ctrl.getAttribute("src");
    imgSrc=imgSrc.replace("_over","");
    imgSrc =imgSrc.replace(".gif","_over.gif");
    ctrl.setAttribute("src",imgSrc);
}

function MouseOut(ctrl)
{
    var imgSrc=ctrl.getAttribute("src");
    imgSrc =imgSrc.replace("_over","");
    ctrl.setAttribute("src",imgSrc);
}

function Delete_Cookie(name,path,domain) { 
   if (Get_Cookie(name)) document.cookie = name + "=" + 
      ( (path) ? ";path=" + path : "") + 
      ( (domain) ? ";domain=" + domain : "") + 
      ";expires=Thu, 01-Jan-70 00:00:01 GMT"; 
}

function Get_Cookie(name) { 
    try{
   var start = document.cookie.indexOf(name+"="); 
   var len = start+name.length+1; 
   if ((!start) && (name != document.cookie.substring(0,name.length))) return "0"; 
   if (start == -1) return "0"; 
   var end = document.cookie.indexOf(";",len); 
   if (end == -1) end = document.cookie.length; 
   var result=document.cookie.substring(len,end);
   return result.length==0?"0":unescape(result); 
   }
   catch(err)
   {
   }
   return "0";
} 

function Set_Cookie(name,value,expires,path,domain,secure) { 
    var cookieString = name + "=" +escape(value) + 
       ( (expires) ? ";expires=" + expires.toGMTString() : "") + 
       ( (path) ? ";path=" + path : "") + 
       ( (domain) ? ";domain=" + domain : "") + 
       ( (secure) ? ";secure" : ""); 
    document.cookie = cookieString; 
}
function CheckNumeric(ctrl) {
    var v1 = document.getElementById(ctrl);
    if (IsNumeric(v1.value)==false)
        v1.value = v1.title;
}
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;
        }
    }
    return IsNumber;
}

function KeyDownProcessNext() {
    var x = 0;
    var kv = 0;
    var rv = false;
    var isTextarea = false;
    var charLength = 1;
    if (document.all)
        kv = event.keyCode;
    else
        kv = evt.keyCode;
    if (kv >= 96 && kv <= 105) {
        var cts = document.all ? document.all.tags("input") : document.getElementsByTagName('input');
        if (document.getElementById(event.srcElement.id).value.length >= charLength) {
            var nextId = '';
            for (ii = 0; ii < cts.length; ii++) {
                if (cts[ii].id == event.srcElement.id && ii < cts.length - 1) {
                    nextId = cts[ii + 1].id;
                }
            }
            if (nextId.length > 0) {
                setTimeout("document.getElementById('"+nextId+"').focus()",20);
            }
        }
    }
}

function KeyDownProcess() {
    //-----------------------------------------------------------
    //網頁親和性輸入控制元件
    //元件功能：可以讓網頁資料的輸入更親切，功能如下：
    //1.按下Enter鍵自動找到下一個可輸入輸入元件
    //2.按下Enter鍵自動找尋submit按鍵，如果找到則模擬Click的功能
    //3.當輸入元件設定唯讀時，按下backspace鍵時，防止網頁轉址
    //版本：1.2(2009.08.21)
    //作者：科碩資訊有限公司
    //引用本元件，請保留作者和版本資訊
    //聯絡mail:cursor@cursorinfo.com.tw
    //------------------------------------------------------------
    var kv = 0;
    var rv = false;
    var isTextarea = false;
    var currentId = null; //目前元件
    if (document.all && typeof (document.all) == "object") {
        kv = event.keyCode;
        isTextarea = (event.srcElement.tagName == "TEXTAREA");
        currentId = event.srcElement.id;
    }
    else {
        kv = evt.keyCode;
        isTextarea = (evt.target == "TEXTAREA");
        currentId = evt.id;
    }
    switch (kv) {
        case 8:
            if (currentId.length == 0)
                return false;
            return !document.getElementById(currentId).readOnly;
        case 13:
            if (currentId.length == 0)
                return false;
            if (isTextarea == true) {
                rv = isTextarea;
            }
            else {
                var cts = document.all && typeof (document.all) == "object" ? document.all : document.getElementsByTagName('input');
                var cid = "";
                var queryBu = null;
                var beginNext = false; //開始比對是否為輸入元件
                var nextInput = null; //下一個輸入元件
                var specialSubmitId = "textQueryBU"; //指定特例的submit id
                var exceptionSubmitId = "waitting"; //指定例外的submit Id
                for (ii = 0; ii < cts.length; ii++) {
                    if (cts[ii].id == currentId) {
                        beginNext = true;
                        continue;
                    }
                    if (beginNext == true && (cts[ii].id.indexOf(specialSubmitId) > -1 || ((cts[ii].type == "submit" || cts[ii].type == "image") && cts[ii].id.indexOf(exceptionSubmitId) == -1))) {
                        queryBu = cts[ii]; //找到submit元件
                        break;
                    }
                    else if (beginNext == true && (cts[ii].type == "text" || cts[ii].type == "textarea" || cts[ii].type == "password") && cts[ii].readOnly == false && cts[ii].style.display != "none") {
                        nextInput = cts[ii]; //找到下一個文字輸入元件
                        break;
                    }
                }
                if (null != queryBu) {
                    setTimeout("doClickId('" + queryBu.id + "');", 100);
                    rv = false;
                }
                else if (null != nextInput) {
                    try {
                        nextInput.focus();
                    } catch (Error) { return true; }
                    rv = false;
                }
            }
            break;
        default:
            rv = true;
            break;
    }
    return rv;
}

//實做模擬Click的功能
function doClickId(source) {
    var o = document.getElementById(source);
    if (document.all && typeof (document.all) == "object") //IE 
        o.click();
    else {
        var e = document.createEvent('mouseEvent');
        e.initEvent('click', false, false);
        o.dispatchEvent(e);
    }
}

function OpenEditPage(papgeName, id, width, height) {
    var sFeatures = "dialogWidth=" + width + "px;dialogHeight=" + height + "px;scrollbars=no;resizable=no;help=no;status=no;";
    var aInfo = new Array();
    aInfo[0] = id;
    var rValue = window.showModalDialog(papgeName + "?id=" + id, aInfo, sFeatures);
    if (aInfo[0] == "True")
        return true;
    else
        return false;
}      