﻿var pos = null;
var chidwin = null;
var bgcolor = null;



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]; }
    }
}

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 key13(e) {
    if (e) { var keycode = e.keyCode; }
    else { var keycode = e.which; }

    if (keycode == 13) { return true; }
    else { return false; }
}

function modalWin(url, title, width, height) {
    if (window.showModalDialog) {
        window.showModalDialog(url, title, "dialogWidth:" + width + "px;dialogHeight:" + height + "px");
    } else {
        window.open(url, title, 'height=' + height + ',width=' + width
    + ',toolbar=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no ,modal=yes');
    }
}

function newopen(url, title, width, height) {
    //   var w = width+width*0.05;
    //   var h = height+height*0.05;	
    //   var x =parseInt((screen.width-w) / 2);	
    //   var y = parseInt((screen.height-h)/ 2);	
    var w = width;
    var h = height;
    var x = 0;
    var y = 0;

    var cond = "top=" + y + ",left=" + x + ",width=" + w + ",height=" + h;
    cond = cond + ",scrollbars=no,toolbar=no,status=no,menubar=no,directories=no";
    cond = cond + ",location=no,resizable=yes,titlebar=no";
    chidwin = window.open(url, title, cond);
    chidwin.focus();
}

function overcolor(obj) {
    bgcolor = obj.style.backgroundColor;
    obj.style.backgroundColor = "#FFFF77";
    return;
}

function outcolor(obj) {
    obj.style.backgroundColor = bgcolor;
    return;
}

function closeMe() {
    window.opener = null;
    window.open("", "_self");
    window.close();
}

function isSameSite(baseUrl) {
    if (opener == null)  //直接呼叫,showMessageDialog  opener = null
    {
        closeMe();
    }
    else {
        var srcUrl = opener.location.href;
        if (srcUrl.match(baseUrl) == "" || srcUrl.indexOf(baseUrl) != 0) { closeMe(); }  // 非同網站
    }
}


function getMousePos(e) {
    pos = new Array();
    pos[0] = e.x ? (e.clientX + document.body.scrollLeft) : e.pageX;
    pos[1] = e.y ? (e.clientY + document.body.scrollTop) : e.pageY;


    return pos;
}

function openCalendar(id) {
    var app = navigator.appName;
    if (app.match("Internet Explorer")) { // IE
        x = event.screenX;
        y = event.screenY + 20;
    } else { //FF
        x = pos[0];
        y = pos[1];
    }
    var dh = (y + 180) - screen.availHeight;
    if (dh > 0) { y = y - dh - 50; }

    var cond = "top=" + y + ",left=" + x + ",width=210,height=180";
    cond = cond + ",scrollbars=no,toolbar=no,status=no,menubar=no,directories=no";
    cond = cond + ",location=no,resizeable=yes,titlebar=no";
    calwin = window.open(parent.rootPath + "/calendar.aspx?oid=" + id + "&kind=", "calendar", cond);  // kind='' west kind='0' chinese
    calwin.moveTo(x, y);
    calwin.focus();
}

function isLeapYear(yy) {
    return (((0 == yy % 4) && (0 != (yy % 100))) || (0 == yy % 400)) ? true : false;
}



function checkDate(sender, args)     // useing for .net customervalidate  to check date format on client 
{
    args.IsValid = isDate(args.Value);
    return;
}

function isCheckListSelected(sender, args)     // useing for .net customervalidate  to check date format on client
{
    var obj = args.Value;
    var b = false;
    for (var i = 0; i < obj.length; i++) {
        if (obj.option[i].checked) { b = true; break; }
    }
    args.IsValid = b;
    return;
}


function isDate(sDate) {
    var aDate = new Date();
    var daysofMonth = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

    if (sDate == "") { return false; }
    sDate = sDate.replace(/\./g, "/").replace(/\-/g, "/");
    var aryDate = new Array(3);
    aryDate = sDate.split("/");
    if (aryDate[0] == undefined || aryDate[1] == undefined || aryDate[2] == undefined) { return false; }

    try {

        var yy = eval(aryDate[0]);
        var mm = eval(aryDate[1]);
        var dd = eval(aryDate[2]);


        if (isLeapYear(yy)) { daysofMonth[1] = 29; }
        if (mm < 1 || mm > 12 || dd < 1) { return false; }
        if (dd > daysofMonth[mm - 1]) { return false; }

        aDate.setFullYear(yy);
        aDate.setMonth(mm - 1);
        aDate.setDate(dd);
        return true;
    }
    catch (e) {
        return false;
    }
}

function ResizeIframe(objId) {
    var ifm = parent.document.getElementById(objId);
    if (ifm != null) {
        ifm.height = (document.body.clientHeight > document.documentElement.clientHeight ? document.body.clientHeight : document.documentElement.clientHeight);
        //  ifm.width = (document.body.clientWidth > document.documentElement.clientWidth ? document.body.clientWidth : document.documentElement.clientWidth);
    }
}

function toSizeIframe(objId, width, height) {
    var ifm = parent.document.getElementById(objId);
    if (ifm != null) {
        if (height != "") { ifm.height = height; }
        if (width != "") { ifm.width = width; }
    }
}

function radioValue(obj) {
    var r = "";
    for (var i = 0; i < obj.length; i++) {
        if (obj[i].checked) { return obj[i].value; };
    }
    return r;
}

function showError(errText, br) {
    return br + "&nbsp;<font style='font-size:12px; background-color:#ffffff; color:red'>" + errText + "</font>";
}


function counterKey(obj1, obj2, ln, e)  // auto next tab
{
    if (e) { var keycode = e.keyCode; }
    else { var keycode = e.which; }

    if (obj1.value.length == ln) { obj2.focus(); }
}

function isInstalled(_software, _mimeType, _ocx) {
    navigator.plugins.refresh();
    if (navigator.plugins && navigator.plugins.length) {
        x = navigator.plugins[_software];
        if (x) { return true; }
    } else if (navigator.mimeTypes && navigator.mimeTypes.length) {
        x = navigator.mimeTypes[_mimeType];
        if (x && x.enabledPlugin) { return true; }
    } else if (window.ActiveXObject) {
        try {
            if (new ActiveXObject(_ocx)) { return true; }
        } catch (e) { return false; }
    }
    return false;
}



function createZoom() {
    document.write('<div id="xoom" style="position: absolute; display: none; left: 0px; top: 0px; width: 16px;');
    document.write('    height: 13px; z-index: 10; background-color: #333333; filter: alpha(opacity=50);');
    document.write('    opacity: 0.5; ">');
    document.write('</div>');
    document.write('<div id="xclose1" style="position:absolute;z-index:93;text-align:right; left:0px; top:0px; width:1px;height:20px;overflow:hidden;background-color:#0066FF">');
    document.write('        <a href="#" onclick="closeZoom();"  >');
    document.write('            <img id="ximg" src="images/error_16x16.gif" border="0" /></a>');
    document.write('</div>');
    document.write('<div id="zoom" style="position: absolute; width:0px; height:0px; left: 0px; top: 0px; background-color: #ffffff;display: none;');
    document.write('    z-index: 90;">');
    document.write('<div id="xclose" style="text-align:right; left:0px; top:0px; width:100%;height:20px;overflow:hidden;background-color:#0066FF">');
    document.write('        <a href="#" onclick="closeZoom();"  >');
    document.write('            <img id="ximg" src="images/error_16x16.gif" border="0" /></a>');
    document.write('</div>');
    document.write('    <div id="xcontent" style="vertical-align:top; lef: 0px; top: auto; width:100%; height:100%; overflow: auto;');
    document.write('        background-color: #ffffff; white-space: nowrap;">');
    document.write('    </div>');
    document.write('</div>    ');
}

function closeZoom() {
    document.getElementById('zoom').style.display = 'none';
    if (typeof (objZoom) != "undefined" && objZoom != null) { objZoom.style.display = 'none'; objZoom = null; }
    document.getElementById('xclose1').style.display = 'none'; // 使用時機獨立div 不包裝在 zoom
    document.getElementById("xcontent").innerHTML = "&nbsp";

    document.getElementById('xoom').style.height = "0px";
    document.getElementById('xoom').style.width = "0px";
    document.getElementById('zoom').style.height = "0px";
    document.getElementById('zoom').style.width = "0px";
    document.getElementById('xoom').style.display = 'none';

    var oo = document.getElementsByTagName("select");
    for (var i = 0; i < oo.length; i++) { oo[i].style.visibility = "visible"; }
}

function scrollZoom() {
    if (document.getElementById('xoom') != null && document.getElementById('xoom').style.display == 'block') {
        moveZoom('block', '', '', '');
    }
}

function innerZoom() {

    var p = innerZoom.arguments
    var objID;
    var width=500;
    var height=400;
    var left='';
    var top='';
    var parentIframe='';
    var button='';
    var title='';

    if (p == 0) {return;}
    if (p.length > 0) { objID = p[0]; }
    if (p.length > 1) { width = p[1]; }
    if (p.length > 2) { height = p[2]; }
    if (p.length > 3) { left = p[3]; }
    if (p.length > 4) { top = p[4]; }
    if (p.length > 5) { parentIframe = p[5]; }
    if (p.length > 6) { button = p[6]; }
    if (p.length > 7) { title = p[7]; }

    document.getElementById("xclose1").style.display = (button == "no" ? "none" : "block");

    objZoom = document.getElementById(objID);
    objZoom.style.position = "absolute";
    objZoom.style.zIndex = "92";
    objZoom.style.backgroundColor = "#ffffff";
    objZoom.style.width = width + 'px';
    objZoom.style.height = height + 'px';

    moveZoom("block", left, top, parentIframe, objZoom);

}


function ajaxZoom() {
    var p = ajaxZoom.arguments;

    var url='';
    var param = '';
    var resultype = '';
    var width = 500;
    var height = 400;
    var left = '';
    var top = '';
    var fun1 = '';
    var fun2 = '';
    var parentIframe = '';
    var button = '';
    var title = '';

    if (p == 0) { return; }
    if (p.length > 0) { url = p[0]; }
    if (p.length > 1) { param = p[1]; }
    if (p.length > 2) { resultype = p[2]; }
    if (p.length > 3) { width = p[3]; }
    if (p.length > 4) { height = p[4]; }
    if (p.length > 5) { left = p[5]; }
    if (p.length > 6) { top = p[6]; }
    if (p.length > 7) { fun1 = p[7]; }
    if (p.length > 8) { fun2 = p[8]; }
    if (p.length > 9) { parentIframe = p[9]; }
    if (p.length > 10) { button = p[10]; }
    if (p.length > 11) { title = p[11]; }

    document.getElementById("xclose").style.display = (button == "no" ? "none" : "block");

    var ajax = new myAjax();
    ajax.runAjax(url, "POST", param, "xcontent", resultype, fun1, fun2);
    document.getElementById("zoom").style.width = width + 'px';
    document.getElementById("zoom").style.height = height + 'px';

    moveZoom("block", left, top, parentIframe);
}


function iframeZoom() {
    var p = iframeZoom.arguments;

    var url = '';
    var width = 500;
    var height = 400;
    var left = '';
    var top = '';
    var parentIframe = '';
    var button = '';
    var title = '';
    if (p == 0) { return; }
    if (p.length > 0) { url = p[0]; }
    if (p.length > 1) { width = p[1]; }
    if (p.length > 2) { height = p[2]; }
    if (p.length > 3) { left = p[3]; }
    if (p.length > 4) { top = p[4]; }
    if (p.length > 5) { parentIframe = p[5]; }
    if (p.length > 6) { button = p[6]; }
    if (p.length > 7) { title = p[7]; }

    document.getElementById("xclose").style.display = (button == "no" ? "none" : "block");

    document.getElementById("zoom").style.width = width + 'px';
    document.getElementById("zoom").style.height = height + 'px';


    document.getElementById("xcontent").innerHTML = "<iframe src='" + url + "' frameborder='0' width='98%' height='98%' scrolling='auto' ></iframe>";
    moveZoom("block", left, top, parentIframe);
}

function moveZoom(visible, left1, top1, parentIframe,title) {
    
    var xclose1 = document.getElementById('xclose1');

    var oo = document.getElementsByTagName("select");
    for (var i = 0; i < oo.length; i++) { oo[i].style.visibility = "hidden"; }

    //oo = document.getElementsByTagName("input");
    //for (var i = 0; i < oo.length; i++) { oo[i].style.visibility = "hidden"; }

    var left = left1;
    var top = top1;
    
    var zoom = document.getElementById("zoom");
    if (typeof (objZoom) != "undefined" && objZoom != null) {
        zoom = objZoom;
    }

    var xoom = document.getElementById("xoom");


    if (visible == "block") {
        zoom.style.display = "block";
        xoom.style.display = "block";

        xoom.style.height = document.documentElement.scrollHeight + "px";
        xoom.style.width = (document.documentElement.scrollWidth - 20) + "px";
    }

    if (zoom.style.display == "none") { return; }

    var h = zoom.style.height.replace('px', '');
    var w = zoom.style.width.replace('px', '');


    var cH = document.documentElement.clientHeight; // 可以看到的範圍
    var cW = document.documentElement.clientWidth;

    var cH1 = document.body.clientHeight; // 實際文件範圍
    var cW1 = document.body.clientWidth;


    var offy = document.documentElement.scrollTop;
    var offx = document.documentElement.scrollLeft;


    // 先從可以看到的範圍計算中間位置
    if (top == "" && left == "") {
        left = (cW - w) / 2 + offx;
        top = (cH - h) / 2 + offy;
    }
    else if (top != "" && left == "") {
        left = (cW - w + offx) / 2;
        if (left < 0) { left = 1; }
    }
    else if (top == "" && left != "") {
        top = (cH - h) / 2 + offy;
        if (top < 0) { top = 1; }
    }

    var y = parseInt(top) + parseInt(h);
    var x = parseInt(left) + parseInt(w);


    //---- thick box + 位移時 寬高大於原畫面高寬時固定,不隨scroll bar 移動,如果thick box寬高小於原畫面高寬,切齊左邊或底部
    if (y > cH1) {
         top = (h < cH1 ? cH1 - h : 2); 
     }

     if (x > cW1) {
         left = (w < cW1 ? cW1 - w : 2);
     }
     
     if (top < 0) {top = 1;}
     if (left < 0) { left = 1; }

    zoom.style.top = top + "px"; // px for FF
    zoom.style.left = left + "px";

    if (typeof (objZoom) != "undefined" && objZoom != null && xclose1.style.display == "block") {  //for innerZoom
        xclose1.style.top = (parseInt(top) - 100) + 'px';
        xclose1.style.left = left + 'px';
        xclose1.style.width = zoom.style.width;
        zoom.style.top = (parseInt(top) -80) + "px";

    }

    if (zoom.style.display  == "block" && (left1 == "" || top1 == "")) {  //有定址,就不隨scroll bar 移動
        var t0 = setTimeout("scrollZoom()", 500);
    }
}


function sleep(milliseconds) {
    var aTime0 = new Date().getTime();
    var i = 0;
    do {
        var aTime1 = new Date().getTime();
        i = aTime1 - aTime0;
    } while (i < milliseconds);

}

createZoom();

var message="";
//function clickIE() {if (document.all) {(message);return false;}}
//function clickNS(e) {if 
//(document.layers||(document.getElementById&&!document.all)) {
//if (e.which==2||e.which==3) {(message);return false;}}}
//if (document.layers) 
//{document.captureEvents(Event.MOUSEDOWN);document.onmousedown=clickNS;}
//else{document.onmouseup=clickNS;document.oncontextmenu=clickIE;}
//document.oncontextmenu=new Function("return false") 

