﻿/* ---------- Basic v1.2 ---------- */

/* AJAX 完成 */
function ajaxDone(Re) {
    /* [RS] = [ReDataSplit] */
    Re = Re.split('[R2S]');
    for (i = 0; i < Re.length; i++) {
        Re[i] = Re[i].split('[RS]');
        switch (Re[i][0]) {
            case "script":
                /* 程式 */
                setTimeout(Re[i][1]);
                break;

            case "html":
                /* 顯示資料 */
                Re[i][1] = Re[i][1].split('[R3S]');
                $("#" + Re[i][1][0]).innerHTML = Re[i][1][1];
                break;

            case "box":
                /* Box */
                $("#BoxDiv").innerHTML = Re[i][1];
                box(1);
                break;
        }
    }
}

/* AJAX 失敗 */
function ajaxFailure(Re) {
    $("#BoxDiv").innerHTML = "<button type='button' class='primaryCta' tabindex='5' onclick=\"box(0);\">關閉錯誤視窗</button><p />" + Re;
    box(1);
}

/* AJAX 檔案上傳 完成後動作 */
function uploadRe(Re) {
    if (Re == "") return;
    if (Re.indexOf("應用程式中發生伺服器錯誤。") != -1) {
        if (Re.indexOf("超出最大的要求長度。") == -1) {
            ajaxFailure(Re);
        } else {
            $("#BoxDiv").innerHTML = "<a class='keyword'>您選取的檔案大小已超出夾檔總容量上限</a><p /><button type='button' class='primaryCta' tabindex='5' onclick=\"box(0);\">確定</button>";
            box(1);
        }
        return;
    }
    ajaxDone(Re);
}

/* AJAX */
var saving = Array();
function ajax(href, FormName, TextName) {
    for (saving_id = 0; saving_id < saving.length; saving_id++) {
        if (saving[saving_id].readyState == 4) break;
    }
    if (saving_id == saving.length) {
        var saving_id = saving.push(XMLHttp()) - 1;
    }
    saving[saving_id] = XMLHttp();
    saving[saving_id].onreadystatechange = function () { ajaxRe(saving[saving_id]); };
    saving[saving_id].open("POST", href, true);
    saving[saving_id].setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    FormData = "";
    if (FormName != undefined) {
        Form_ = $("*[name='" + FormName + "']")[0];
        for (i = 0; i <= Form_.length - 1; i++) {
            if (Form_.elements[i].name == "" || Form_.elements[i].name == "__VIEWSTATE" || Form_.elements[i].name == "__EVENTVALIDATION") continue;
            if (TextName != undefined && TextName.indexOf(Form_.elements[i].name) == -1) continue;
            if ((Form_.elements[i].type == "checkbox" || Form_.elements[i].type == "radio") && Form_.elements[i].checked == false) continue;
            FormData += Form_.elements[i].name + "=" + input(Form_.elements[i].value) + "&";
        }
    }
    saving[saving_id].send(FormData);
}

/* AJAX 檔案上傳 */
document.write("<iframe style='display: none;' name='upload_iframe' onload='uploadRe(this.contentWindow.document.body.innerHTML);'></iframe>");
function upload(href, FormName) {
    $("#BoxDiv").innerHTML = "上傳中，請梢後";
    box(1);

    Form_ = $("*[name='" + FormName + "']")[0];
    Form_.action = href;
    Form_.encoding = "multipart/form-data";
    Form_.method = "post";
    Form_.target = "upload_iframe";
    Form_.submit();
}

/* AJAX HTML編輯器 */
function AjaxHtml(href, FormName, HtmlName) {
    $("#" + HtmlName).value = CKEDITOR.instances[HtmlName].getData();
    ajax(href, FormName);
}

/* AJAX 背景建立 */
function XMLHttp() {
    if (window.ActiveXObject) {
        try {
            /* 新版 IE */
            Re = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            /* 舊版 IE */
            Re = new ActiveXObject("Microsoft.XMLHTTP");
        }
    } else if (window.XMLHttpRequest) {
        /* Firefox */
        Re = new XMLHttpRequest();
    }
    return Re;
}

/* AJAX 完成後動作 */
function ajaxRe(Re) {
    if (window.attachEvent) {
        if (Re.readyState != 4) return;
    } else {
        if (Re.readyState != 3) return;
    }
    if (Re.status != 200) {
        ajaxFailure(Re.responseText)
        return;
    }
    //alert(Re.responseText);
    ajaxDone(Re.responseText);
}

/* 輸入資料驗證 */
function input(text) {
    text += "";
    if (text == "undefined") return "";

    text = text.replace(/&/g, "#38;");
    return text;
}

/* ---------- 視窗黑屏 ---------- */

/* Box */
document.write("<div id='BlackDiv' style='z-index: 8; background-color: #000000; display: none; position: absolute; top: 0px; left: 0px; width: 100%; filter: alpha(opacity=50); opacity: 0.5;'></div>");
document.write("<div id='BoxDiv' style='z-index: 9; display: none; position: absolute; width: 510px;' class='Circle' align='center'></div>");
function box(type) {
    /* type 0=關閉;1=開啟; */
    if (type == 0) {
        $("#BlackDiv").style.display = "none";
        $("#BoxDiv").style.display = "none";
        $("#BoxDiv").innerHTML = "";
    } else {
        $("#BoxDiv").style.left = (document.documentElement.clientWidth / 2 - 255) + "px";
        $("#BoxDiv").style.top = (document.body.scrollTop + (document.documentElement.clientHeight / 3)) + "px";
        $("#BoxDiv").style.display = "";

        $("#BlackDiv").style.height = document.body.scrollHeight + "px";
        $("#BlackDiv").style.display = "";
    }
}

/* ---------- 滑動橫幅 ---------- */

/* Banner */
var BannerData = new Array();
function BannerLoad() {
    nbsp = "";
    for (i = 1; i <= 6; i++) nbsp += "&nbsp;";

    text = "", text1 = "", text2 = "";
    for (i = 0; i <= 1; i++) text += "<div style='position:relative;top:" + (BannerHeight * -i) + "px;height:" + BannerHeight + "px;' id='BannerDivBannerData#" + (1 - i) + "'></div>";
    for (i = 0; i < Banner.length; i++) {
        BannerData[i] = [Banner[i], 0, 0, 0, 0, 0, false]; //[標題,目前頁數,隱藏DIV編號,DIV座標,向左向右,是否鎖定,Load]
        if (i != 0) text2 += "<td align='center'><img src='images/e.png'></td>";
        text1 += "<td>" + nbsp + BannerData[i][0] + "</td>";
        text2 += "<td><table width='100%'><tr><td width='9'><a onclick='BannerLR(" + i + ",0);'><img src='images/z1.png'></a></td><td><div style='overflow:hidden;height:" + BannerHeight + "px;'>" + text.replace(/BannerData/g, i) + "</div></td><td width='9'><a onclick='BannerLR(" + i + ",1);'><img src='images/z2.png'></a></td></tr></table></td>";
    }

    $("#BannerDiv").innerHTML = "<table width='100%' class='BannerMenu'><tr>" + text1 + "</tr></table><table width='100%' class='Banner'><tr>" + text2 + "</tr></table>";

    for (i = 0; i < BannerData.length; i++) BannerLR(i, 0);
}

function BannerLR(BannerID, LR) {
    if (BannerData[BannerID][5] != 0) {
        return;
    } else {
        BannerData[BannerID][5] = 1;
    }
    if (LR == 0) {
        BannerData[BannerID][1] += 1;
        BannerData[BannerID][3] = BannerWidth;
    } else {
        BannerData[BannerID][1] -= 1;
        BannerData[BannerID][3] = -BannerWidth;
    }
    BannerData[BannerID][4] = LR;
    ajax("ajax/Banner.aspx?BannerID=" + BannerID + "&page=" + BannerData[BannerID][1] + "&div=" + BannerData[BannerID][2]);
}

function BannerMove(BannerID) {
    if (BannerData[BannerID][4] == 0) {
        if (BannerData[BannerID][6]) {
            BannerData[BannerID][3] -= 4;
        } else {
            BannerData[BannerID][3] = 0;
            BannerData[BannerID][6] = true;
        }
        if (BannerData[BannerID][3] < 0) BannerData[BannerID][3] = 0;
        x = BannerData[BannerID][3] - BannerWidth;
    } else {
        BannerData[BannerID][3] += 4;
        if (BannerData[BannerID][3] > 0) BannerData[BannerID][3] = 0;
        x = BannerData[BannerID][3] + BannerWidth;
    }
    $("#BannerDiv" + BannerID + "#" + (1 - BannerData[BannerID][2])).style.left = x + "px";
    $("#BannerDiv" + BannerID + "#" + BannerData[BannerID][2]).style.left = BannerData[BannerID][3] + "px";
    if (BannerData[BannerID][3] == 0) {
        BannerData[BannerID][2] = 1 - BannerData[BannerID][2];
        BannerData[BannerID][5] = 0;
    } else {
        setTimeout("BannerMove(" + BannerID + ");", 25);
    }
}

/* ---------- 圖片緩出 ---------- */
function Slow(SlowID, SlowAlpha) {
    if (SlowAlpha == undefined) SlowAlpha = 0;
    SlowAlpha += 4;
    if (window.attachEvent) {
        $("#" + SlowID).filters.alpha.opacity = SlowAlpha;
    } else {
        $("#" + SlowID).style.opacity = SlowAlpha * 0.01;
    }

    setTimeout("Slow('" + SlowID + "'," + SlowAlpha + ");", 25);
}

/* ---------- 圖片漸變 ---------- */

/* Img Alpha */
var ImgAlphaData = new Array();
function ImgAlphaStart() {
    for (i = 0; i < ImgAlphaData.length; i++) {
        if (ImgAlphaData[i][6] < ImgAlphaStay) {
            ImgAlphaData[i][6] += 1;
            continue;
        }
        if (ImgAlphaData[i][5] == 0) {
            ImgAlphaData[i][4] -= 4;
            if (ImgAlphaData[i][4] <= 0) {
                if (ImgAlphaData[i][3] != -1) $("#" + ImgAlphaData[i][1] + "#" + ImgAlphaData[i][3]).className = "ImgAlphaButton1";

                ImgAlphaData[i][3] += 1;
                if (ImgAlphaData[i][3] == ImgAlphaData[i][0].length) ImgAlphaData[i][3] = 0;
                $("#" + ImgAlphaData[i][1] + "img").src = ImgAlphaData[i][0][ImgAlphaData[i][3]][0];
                if (ImgAlphaData[i][0][ImgAlphaData[i][3]][1] != undefined) {
                    if (ImgAlphaData[i][0][ImgAlphaData[i][3]][2] == undefined) {
                        $("#" + ImgAlphaData[i][1] + "img_a").href = ImgAlphaData[i][0][ImgAlphaData[i][3]][1];
                    } else {
                        $("#" + ImgAlphaData[i][1] + "img_a").onclick = Function(ImgAlphaData[i][0][ImgAlphaData[i][3]][2] + "('" + ImgAlphaData[i][0][ImgAlphaData[i][3]][1] + "');");
                    }
                }
                $("#" + ImgAlphaData[i][1] + "#" + ImgAlphaData[i][3]).className = "ImgAlphaButton2";
                ImgAlphaData[i][5] = 1;
            }
        } else {
            ImgAlphaData[i][4] += 4;
            if (ImgAlphaData[i][4] >= 100) {
                ImgAlphaData[i][5] = 0;
                ImgAlphaData[i][6] = 0;
            }
        }
        if (window.attachEvent) {
            $("#" + ImgAlphaData[i][1] + "img").filters.alpha.opacity = ImgAlphaData[i][4];
        } else {
            $("#" + ImgAlphaData[i][1] + "img").style.opacity = ImgAlphaData[i][4] * 0.01;
        }
    }
    setTimeout("ImgAlphaStart();", 25);
}

function ImgAlphaIN(ImgAlphaID, i) {
    ImgAlphaData[ImgAlphaID][3] = i - 1;
    ImgAlphaData[ImgAlphaID][4] = 0;
    ImgAlphaData[ImgAlphaID][5] = 0;
    ImgAlphaData[ImgAlphaID][6] = ImgAlphaStay;
    for (i = 0; i < ImgAlphaData[ImgAlphaID][0].length; i++) $("#" + ImgAlphaData[ImgAlphaID][1] + "#" + i).className = "ImgAlphaButton1";
}

function ImgAlphaOUT(ImgAlphaID, i) {

}

function ImgAlpha(ImgAlphaID, Data, Script) {
    ImgAlphaData[ImgAlphaData.length] = [Data, ImgAlphaID, Script, Data.length - 1, 0, 0, ImgAlphaStay];

    text = "";
    for (i = 0; i < Data.length; i++) text += "<a onmouseover='ImgAlphaIN(" + (ImgAlphaData.length - 1) + "," + i + ");' onmouseout='ImgAlphaOUT(" + (ImgAlphaData.length - 1) + "," + i + ");'><div style='float:left;margin-right:8px;text-align:center;line-height:12px;width:12px;height:12px;' class='ImgAlphaButton1' id='" + ImgAlphaID + "#" + i + "'>" + (i + 1) + "</div></a>";
    $("#" + ImgAlphaID).innerHTML = "<a id='" + ImgAlphaID + "img_a'><img style='filter:alpha(opacity=0);opacity:0;' id='" + ImgAlphaID + "img'></a><div style='position: relative; top: -30px; left: 20px;'>" + text + "</div>";
}

/* ---------- 樹狀選單 ---------- */

/* List Menu */
var ListMenuClose_;
document.write("<div id='ListMenuDiv' style='position:absolute;text-align:center;' onmouseover='ListMenuIN();' onmouseout='ListMenuOUT();'></div>");
function ListMenu(ListMenuDataID, objectX, objectY, ListMenuBox) {
    ListMenuIN();

    if (ListMenuBox == undefined) ListMenuBox = ['', 0, 0, 0, 0]; //[cssName,div寬,div高,X偏移,Y偏移]
    if (ListMenuBox[0] != '') $("#ListMenuDiv").className = ListMenuBox[0];
    if (ListMenuBox[1] != 0) $("#ListMenuDiv").style.width = ListMenuBox[1] + "px";
    if (ListMenuBox[2] != 0) $("#ListMenuDiv").style.height = ListMenuBox[2] + "px";
    $("#ListMenuDiv").style.left = posX(objectX) + ListMenuBox[3] + "px";
    $("#ListMenuDiv").style.top = posY(objectY) + $("#" + objectY).offsetHeight + ListMenuBox[4] + "px";

    text = "";
    for (i = 0; i < ListMenuData[ListMenuDataID].length; i++) {
        if (i != 0) text += "｜";
        text += "<a onclick=\"" + ListMenuData[ListMenuDataID][i][1] + "\">" + ListMenuData[ListMenuDataID][i][0].replace(/ /g, "&nbsp;&nbsp;") + "</a>";
    }
    $("#ListMenuDiv").innerHTML = text;

    $("#ListMenuDiv").style.display = "";
}

function ListMenuIN() {
    ListMenuClose_ = false;
}

function ListMenuOUT() {
    ListMenuClose_ = true;
    setTimeout("ListMenuClose();", 10000);
}

function ListMenuClose() {
    if (ListMenuClose_) $("#ListMenuDiv").style.display = "none";
}

/* ---------- 使用權限 ---------- */

function AuthorityReset(AuthorityID) {
    Authority_ = $("*[name='Authority" + AuthorityID + "']")[0].value;

    text = "<table>";
    Authority_ = Authority_.split('[P2S]');
    for (i = 0; i < Authority_.length; i++) {
        deleteText = Authority_[i];
        if (i != 0) {
            deleteText = "[P2S]" + deleteText;
        } else {
            if (Authority_.length != 1) deleteText += "[P2S]";
        }
        Authority_[i] = Authority_[i].split('[PS]');
        text += "<tr><td><a class='menu' onclick=\"AuthorityDelete('" + deleteText + "'," + AuthorityID + ");\"><img alt='' src='images/block--minus.png' />" + Authority_[i][0] + "</a></td></tr>";
    }
    text += "</table>";
    if (Authority_ == "") {
        $("#AuthorityDiv" + AuthorityID).innerHTML = "";
    } else {
        $("#AuthorityDiv" + AuthorityID).innerHTML = text;
    }
}

function AuthorityAdd(AuthorityID) {
    Authority_ = $("*[name='Authority" + AuthorityID + "']")[0];

    text = "";
    type_ = $("*[name='AuthorityType']")[0].value;
    switch (type_) {
        case "0":
            text_ = "所有人[PS]0";
            Authority_.value = text_;
            break;

        case "1":
            TeamID_ = $("*[name='TeamID']");
            for (i = 0; i < TeamID_.length; i++) {
                if (TeamID_[i].checked == true) {
                    text_ = TeamID_[i].alt + "[PS]1[PS]" + TeamID_[i].value;
                    if (Authority_.value.indexOf(text_) != -1) continue;
                    if (text != "") text += "[P2S]";
                    text += text_;
                }
            }
            break;

        case "2":
            PositionID_ = $("*[name='PositionID']");
            for (i = 0; i < PositionID_.length; i++) {
                if (PositionID_[i].checked == true) {
                    text_ = PositionID_[i].alt + "[PS]2[PS]" + PositionID_[i].value;
                    if (Authority_.value.indexOf(text_) != -1) continue;
                    if (text != "") text += "[P2S]";
                    text += text_;
                }
            }
            break;

        case "3":
            TeamID_ = $("*[name='TeamID']")[0];
            PositionID_ = $("*[name='PositionID']");
            for (i = 0; i < PositionID_.length; i++) {
                if (PositionID_[i].checked == true) {
                    text_ = TeamID_[TeamID_.selectedIndex].text + "-" + PositionID_[i].alt + "[PS]3[PS]" + TeamID_.value + "[PS]" + PositionID_[i].value;
                    if (Authority_.value.indexOf(text_) != -1) continue;
                    if (text != "") text += "[P2S]";
                    text += text_;
                }
            }
            break;

        case "4":
            UserID_ = $("*[name='UserID']");
            for (i = 0; i < UserID_.length; i++) {
                if (UserID_[i].checked == true) {
                    text_ = UserID_[i].alt + "[PS]4[PS]" + UserID_[i].value;
                    if (Authority_.value.indexOf(text_) != -1) continue;
                    if (text != "") text += "[P2S]";
                    text += text_;
                }
            }
            break;
    }
    if (text != "") {
        if (Authority_.value == "所有人[PS]0") Authority_.value = "";
        if (Authority_.value != "") Authority_.value += "[P2S]";
        Authority_.value += text;
    }
    AuthorityReset(AuthorityID);
    box(0);
}

function AuthorityDelete(text, AuthorityID) {
    Authority_ = $("*[name='Authority" + AuthorityID + "']")[0];
    Authority_.value = Authority_.value.replace(text, "");
    AuthorityReset(AuthorityID);
}

/* ---------- 基本工具 ---------- */

/* Load */
if (window.attachEvent) {
    window.attachEvent("onload", load);
} else {
    window.addEventListener("load", load, false);
}

/* jQuery */
function $(Label) {
    /* id = $('#test') ; name = $("*[name='test']") ; */
    if (Label.indexOf("#") != -1) {
        Re = document.getElementById(Label.substring(1, Label.length));
    } else {
        Re = document.getElementsByName(Label.substring(8, Label.length - 2));
    }
    return Re;
}

/* 物件座標 X */
function posX(objID) {
    var elmt = $("#" + objID);
    var x = 0;
    for (var e = elmt; e; e = e.offsetParent) {
        x += e.offsetLeft;
    }
    for (e = elmt.parentNode; e && e != document.body; e = e.parentNode) {
        if (e.scrollLeft) x -= e.scrollLeft;
    }
    return x;
}

/* 物件座標 Y */
function posY(objID) {
    var elmt = $("#" + objID);
    var y = 0;
    for (var e = elmt; e; e = e.offsetParent) {
        y += e.offsetTop;
    }
    for (e = elmt.parentNode; e && e != document.body; e = e.parentNode) {
        if (e.scrollTop) y -= e.scrollTop;
    }
    return y;
}
