﻿var pos = null;
var chidwin = null;
var bgcolor = null;
  var rootPath = "";
  if (document.getElementsByTagName('script').item(0).attributes.getNamedItem("src") != null) {
      rootPath = document.getElementsByTagName('script').item(0).attributes.getNamedItem("src").nodeValue;
      rootPath = rootPath.replace(/[\/][^js]\w+[\.]js/, "");
      rootPath = rootPath.replace("js", "");
  }
      

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;
       var h = height;	
       var x =parseInt((screen.width-w) / 2);	
       var y = parseInt((screen.height-h)/ 2);	


    var cond = "top=" + y + ",left=" + x + ",width=" + w + ",height=" + h;
    cond = cond + ",scrollbars=yes,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();
    var sl = (document.body.scrollLeft > document.documentElement.scrollLeft ? document.body.scrollLeft : document.documentElement.scrollLeft);
    var st = (document.body.scrollTop > document.documentElement.scrollTop ? document.body.scrollTop : document.documentElement.scrollTop);

    pos[0] = e.x ? (e.clientX + parseInt(sl)) : e.pageX;
    pos[1] = e.y ? (e.clientY + parseInt(st)) : e.pageY;
    return pos;
}


function initCalendar()
{
	var o = document.body.getElementsByTagName("img");
	for(var i=0; i < o.length; i++)
	{
		if (typeof(o[i].attributes["calendar"]) != "undefined") 
		{
			o[i].style.cursor="pointer";
			o[i].onclick = function()
			{
				pos = getMousePos(event);
				window.open('/calendar.htm?objID='+this.getAttribute('calendar')+'&y0=0&y1=1','月曆','width=280,height=200,left=pos[0],top=pos[1]');
			};

		}
	}
}

function openCalendar(id, e) {

    getMousePos(e);

    var cond = "top=" + pos[1] + ",left=" + pos[0] + ",width=210,height=180";
    cond = cond + ",scrollbars=no,toolbar=no,status=no,menubar=no,directories=no";
    cond = cond + ",location=no,resizeable=yes,titlebar=no";
    // oid=objectID & kind= yearType  & from=2000 & to = 2013
    // The year type is 西元年 as kind is null,  民國 is 0
    calwin = window.open(rootPath + "calendar.htm?oid=" + id + "&kind=", "calendar", cond);  
    calwin.moveTo(pos[0], pos[1]);
    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);
        if (ifm.height < screen.availHeight) { ifm.height = screen.availHeight; }
        //  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() {
    var img = "/images/loading.gif";
    if (document.getElementsByTagName('script').item(0).attributes.getNamedItem("src") != null) {
        img = document.getElementsByTagName('script').item(0).attributes.getNamedItem("src").nodeValue;
        img = img.replace(/[\/][^js]\w+[\.]js/, "/loading.gif");
    }
    document.write('<div id="xoom" style="position: absolute; display: none; left: 0px; top: 0px; width: 0px;');
    document.write('    height: 0px; z-index: 10; background-color: #333333; filter: alpha(opacity=50);');
    document.write('    opacity: 0.5; ">');
    document.write('</div>');
    document.write('<div id="xclose" style="border:outset;position:absolute;z-index:93;text-align:right; left:0px; top:0px; width:0px;height:20px;overflow:hidden;background-color:silver;display:none">');
    document.write('        <div id="xtitle" style="color:white;float:left;margin-top:5px;marin-left:2px;font-size:12px; "></div><div style="float:right">');
    document.write('            <input type="button" value="X" style="margin-top:2px;height:18px;font-size:11px;cursor:pointer;" onclick="closeZoom()" /></div>');
    //    document.write('            <font style="font-size:12px;border:outset 2px;cursor:pointer;" onclick="closeZoom()" onmouseover="this.color=\'#ffffff\'" onmouseout="this.color=\'#333333\'" >&nbsp;Close&nbsp;</font></div>');
    //    document.write('        <div id="xtitle" style="color:white;float:left;margin-top:5px;marin-left:2px "></div><div style="float:right"><a href="#" onclick="closeZoom();"  >');
    //    document.write('            <img id="ximg" src="/gjun_elearning_ispeaking/images/error_16x16.gif" align="middle" border="0" /></a></div>');
    document.write('</div>');
    document.write('<div id="zoom" style="border:outset;position: absolute; width:0px; height:0px; left: 0px; top: 0px;background-color: #ffffff;display: none;overflow:auto;');
    document.write('    z-index: 80;"><img id="imgxx" src="' + img + '" alt="Loading..."  / ></div>');
}

function closeZoom() {
    document.getElementById('zoom').style.display = 'none';
    document.getElementById('zoom').style.overflow = "auto";
    document.getElementById('zoom').innerHTML = "";
    document.getElementById('xtitle').innerHTML = "";

    if (typeof (objZoom) != "undefined" && objZoom != null) { objZoom.style.display = 'none'; objZoom = null; }
    document.getElementById('xclose').style.display = 'none';

    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(visible, left1, top1) {
    if (document.getElementById('xoom') != null && document.getElementById('xoom').style.display == 'block') {
        moveZoom(visible, left1, top1);
    }
}

function innerZoom() {

    var p = innerZoom.arguments
    var objID;
    var width = 500;
    var height = 400;
    var left = '';
    var top = '';
    var scrollbar = '';
    var button = '';
    var title = '';

    if (p == 0) { return; }

    objID = p[0];

    if (p.length == 2) {
        objID = p[0];
        var ary = new Array();
        ary = p[1].split(',');
        for (var i = 0; i < ary.length; i++) {
            eval(ary[i]);
        }
    } else {
        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) { scrollbar = 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");

    if (title != "") {
        document.getElementById('xtitle').innerHTML = title;
    }

    if (scrollbar == "no") {
        document.getElementById('objZoom').style.overflow = 'hidden';
    }

    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';
    objZoom.style.border = "outset";

    moveZoom("block", left, top);

}


function ajaxZoom() {

    var p = ajaxZoom.arguments;
    var url = '';
    var param = '';
    var resultype = 'html';
    var width = 500;
    var height = 400;
    var left = '';
    var top = '';
    var fun1 = '';
    var fun2 = '';
    var scrollbar = '';
    var button = '';
    var title = '';

    if (p == 0) { return; }

    url = p[0];

    if (p.length == 2) {
        var ary = new Array();
        ary = p[1].split(',');
        for (var i = 0; i < ary.length; i++) {
            eval(ary[i]);
        }
    } else {
        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) { scrollbar = 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, "zoom", resultype, fun1, fun2);

    document.getElementById("zoom").style.width = width + 'px';
    document.getElementById("zoom").style.height = height + 'px';

    if (title != "") {
        document.getElementById('xtitle').innerHTML = title;
    }

    if (scrollbar == "no") {
        document.getElementById('zoom').style.overflow = 'hidden';
    }

    moveZoom("block", left, top);
}


function iframeZoom() {

    var p = iframeZoom.arguments;
    var url = '';
    var width = 500;
    var height = 400;
    var left = '';
    var top = '';
    var scrollbar = '';
    var button = '';
    var title = '';

    if (p == 0) { return; }

    url = p[0];

    if (p.length == 2) {
        url = p[0];
        var ary = new Array();
        ary = p[1].split(',');
        for (var i = 0; i < ary.length; i++) {
            eval(ary[i]);
        }
    } else {
        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) { scrollbar = p[5]; }
        if (p.length > 6) { button = p[6]; }
        if (p.length > 7) { title = p[7]; }
    }

    if (title != "") {
        document.getElementById('xtitle').innerHTML = title;
    }


    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('zoom').style.overflow = 'hidden';

    if (scrollbar == "no") {
        document.getElementById("zoom").innerHTML = "<iframe src='" + url + "' frameborder='0' width='100%' height='100%' scrolling='no' ></iframe>";
    }
    else {
        document.getElementById("zoom").innerHTML = "<iframe src='" + url + "' frameborder='0' width='100%' height='100%' scrolling='auto' ></iframe>";
    }
    
    moveZoom("block", left, top);
}

function moveZoom(visible, left1, top1) {
    var xclose = document.getElementById('xclose');

    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 + "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) / 2  + offx;
        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; }



    if (zoom.style.display == "block" && (left1 == "" || top1 == "")) {  //有定址,就不隨scroll bar 移動
      if (h < 400)     //-- 利用 fixed 方式,將框鎖住螢幕中間
      {
           zoom.style.position = "fixed";
           xclose.style.position = "fixed";
           var tx = (screen.availHeight - h) / 2 ;
           var lx = (screen.availWidth - w) / 2;

           if (tx < top) { top = tx; }
           if (lx < left) {left = lx; }
           
       } else {     //-- 利用 timeOut 方式,將框鎖住螢幕中間

        var t0 = setTimeout("scrollZoom('" + visible + "','" + left1 + "','" + top1 + "')", 500);
      }
   }

  zoom.style.top = top + "px"; // px for FF
  zoom.style.left = left + "px";

  if (xclose.style.display == "block") {
      xclose.style.top = top + 'px';
      xclose.style.left = left + 'px';
      xclose.style.width = zoom.style.width;

      var app = navigator.appName;
      if (app.match("Internet Explorer")) {
          zoom.style.top = (parseInt(top) + 28) + "px";
      }
      else {
          zoom.style.top = (parseInt(top) + 26) + "px";

      }
  }
    
}



function sleep(milliseconds) {
    var aTime0 = new Date().getTime();
    var i = 0;
    do {
        var aTime1 = new Date().getTime();
        i = aTime1 - aTime0;
    } while (i < milliseconds);

}

function addCookie(ckName, value, days) {
    aDay = new Date();
    aDay.setTime(aDay.getTime() + (days * 1000 * 60 * 60 * 24));
    expDay = aDay.toGMTString();
    document.cookie = ckName + "=" + escape(value) + ";expires=" + expDay + ";path=/";
}

function deleteCookie(ckName) {
    document.cookie = ckName + "=;expires=Mon,02-Oct-28 00:00:01 GMT";
}

function getCookie(ckName) {
    var results = document.cookie.match(new RegExp("(^|;) ?" + ckName + "=([^;]*)(;|$)"));
    if (results)
        return (unescape(results[2]));
    else
        return "";
}

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")

*/
