﻿

//////////////////////////////////////////////////////////
// author : darrenwu@gamania.com
// date   : 2008-04-10
// desc   : display custom MessageBox 
// use public function ShowMsgBox to show message, 
// the message content can be Html.
// the other functions whitch start with "_" are private
// * this js needs to be combined with /modules/MsgBox.ascx
//////////////////////////////////////////////////////////

var _msgbox_waiting_numTimeoutID;
var _msgbox_floatwin_numTimeoutID;

// only for channel default.aspx page use
function ShowFloatWinMsgBox(msg, headerTitle, buttonCloseName, callback) {
    if (!document.getElementById("DivMsgBoxContainer")) {
        alert("No MsgBox Division Found!!");
        return;
    }
    if (headerTitle) document.getElementById("DivMsgBoxTitle").innerHTML = headerTitle;
    if (buttonCloseName) document.getElementById("DivMsgBoxBtn").value = buttonCloseName;
    document.getElementById("DivMsgBoxBtn").style.display = "inline"; //for waiting box

    var objTarget = document.getElementById("floatWindow");
    if (!objTarget) {
        alert("no float window found");
        return;
    }

    var bodyWidth = objTarget.offsetWidth;
    var bodyHeight = objTarget.offsetHeight;
    var bodyLeft = objTarget.offsetLeft;
    var bodyTop = objTarget.offsetTop;

    // back width, and height
    document.getElementById("DivMsgBoxBack").style["width"] = bodyWidth + "px";
    document.getElementById("DivMsgBoxBack").style["height"] = bodyHeight + "px";

    //set position
    document.getElementById("DivMsgBoxBack").style["left"] = bodyLeft + "px";
    document.getElementById("DivMsgBoxBack").style["top"] = bodyTop + "px";
    document.getElementById("DivMsgBoxContainer").style["left"] = ((bodyWidth - 250) / 2 + bodyLeft) + "px";
    document.getElementById("DivMsgBoxContainer").style["top"] = ((bodyHeight - 50) / 2 + bodyTop) + "px";

    document.getElementById("DivMsgBoxContent").innerHTML = msg;

    document.getElementById("DivMsgBoxContainer").style["display"] = "block";
    document.getElementById("DivMsgBoxBack").style["display"] = "block";

    //let msgbox display 20 second
    _msgbox_floatwin_numTimeoutID = window.setTimeout("_HideMsgBox()", 20000);

    _change_tags_visible('hidden');
    if (callback) _msgbox_callback = callback;
}


function ShowMsgBox(msg, headerTitle, buttonCloseName, callback) {
    document.getElementById("DivMsgBoxCancelBtn").style["display"] = "none";

    if (!document.getElementById("DivMsgBoxContainer")) {
        alert("No MsgBox Division Found!!");
        return;
    }
    if (headerTitle) document.getElementById("DivMsgBoxTitle").innerHTML = headerTitle;
    if (buttonCloseName) document.getElementById("DivMsgBoxBtn").value = buttonCloseName;
    document.getElementById("DivMsgBoxBtn").style.display = "inline"; //for waiting box


    var bodyWidth = document.documentElement.scrollWidth;
    document.getElementById("DivMsgBoxBack").style["width"] = (bodyWidth >= _pageWidth() ? bodyWidth : _pageWidth()) + "px";
    var bodyHeight = document.documentElement.scrollHeight;
    document.getElementById("DivMsgBoxBack").style["height"] = (bodyHeight >= _pageHeight() ? bodyHeight : _pageHeight()) + "px";

    //set position
    document.getElementById("DivMsgBoxContainer").style["left"] = ((_pageWidth() - 250) / 2 + _posLeft()) + "px";
    document.getElementById("DivMsgBoxContainer").style["top"] = ((_pageHeight() - 50) / 2 + _posTop()) + "px";

    document.getElementById("DivMsgBoxContent").innerHTML = msg;

    document.getElementById("DivMsgBoxContainer").style["display"] = "block";
    document.getElementById("DivMsgBoxBack").style["display"] = "block";

    _change_tags_visible('hidden');
    if (callback) _msgbox_callback = callback;
}

function ShowMsgBoxWithoutBackground(msg, headerTitle, buttonCloseName, callback) {
    document.getElementById("DivMsgBoxCancelBtn").style["display"] = "none";

    if (!document.getElementById("DivMsgBoxContainer")) {
        alert("No MsgBox Division Found!!");
        return;
    }
    if (headerTitle) document.getElementById("DivMsgBoxTitle").innerHTML = headerTitle;
    if (buttonCloseName) document.getElementById("DivMsgBoxBtn").value = buttonCloseName;
    document.getElementById("DivMsgBoxBtn").style.display = "inline"; //for waiting box

    //var bodyWidth = document.documentElement.scrollWidth;
    //document.getElementById("DivMsgBoxBack").style["width"] = (bodyWidth>= _pageWidth() ? bodyWidth : _pageWidth()) + "px";
    //var bodyHeight = document.documentElement.scrollHeight;
    //document.getElementById("DivMsgBoxBack").style["height"] = (bodyHeight>= _pageHeight() ? bodyHeight : _pageHeight()) + "px";

    //set position
    document.getElementById("DivMsgBoxContainer").style["left"] = ((_pageWidth() - 250) / 2 + _posLeft()) + "px";
    document.getElementById("DivMsgBoxContainer").style["top"] = ((_pageHeight() - 50) / 2 + _posTop()) + "px";

    document.getElementById("DivMsgBoxContent").innerHTML = msg;

    document.getElementById("DivMsgBoxContainer").style["display"] = "block";
    //document.getElementById("DivMsgBoxBack").style["display"] = "block";

    _change_tags_visible('hidden');
    if (callback) _msgbox_callback = callback;
}

function ShowConfirmMsgBox(msg, headerTitle, buttonConfirmName, buttonCancelName, confirmFuncName) {
    document.getElementById("DivMsgBoxBtn").style["display"] = "inline";
    document.getElementById("DivMsgBoxCancelBtn").style["display"] = "inline";

    if (!document.getElementById("DivMsgBoxContainer")) {
        alert("No MsgBox Division Found!!");
        return;
    }
    if (headerTitle) document.getElementById("DivMsgBoxTitle").innerHTML = headerTitle;
    if (buttonConfirmName) document.getElementById("DivMsgBoxBtn").value = buttonConfirmName;
    if (buttonCancelName) document.getElementById("MsgCancelBtn").value = buttonCancelName;

    var bodyWidth = document.documentElement.scrollWidth;
    document.getElementById("DivMsgBoxBack").style["width"] = (bodyWidth >= _pageWidth() ? bodyWidth : _pageWidth()) + "px";
    var bodyHeight = document.documentElement.scrollHeight;
    document.getElementById("DivMsgBoxBack").style["height"] = (bodyHeight >= _pageHeight() ? bodyHeight : _pageHeight()) + "px";

    //set position
    document.getElementById("DivMsgBoxContainer").style["left"] = ((_pageWidth() - 250) / 2 + _posLeft()) + "px";
    document.getElementById("DivMsgBoxContainer").style["top"] = ((_pageHeight() - 50) / 2 + _posTop()) + "px";

    document.getElementById("DivMsgBoxContent").innerHTML = msg;

    document.getElementById("DivMsgBoxContainer").style["display"] = "block";
    document.getElementById("DivMsgBoxBack").style["display"] = "block";

    _change_tags_visible('hidden');
    _msgbox_callback = confirmFuncName;
}

function ShowFloatWinWaitingMsgBox(SecondOfPause) {
    ShowFloatWinMsgBox(_msgbox_waiting_html);
    document.getElementById("DivMsgBoxBtn").style.display = "none";
    _msgbox_waiting_numTimeoutID = window.setTimeout("_HideMsgBox()", SecondOfPause * 1000);
}

function ShowWaitingMsgBox(SecondOfPause) {
    window.setTimeout("ShowWaitingMsgBox2(" + SecondOfPause + ")", 1000);
}

function ShowWaitingMsgBoxWithoutBackground(SecondOfPause) {
    ShowMsgBoxWithoutBackground(_msgbox_waiting_html);
    document.getElementById("DivMsgBoxBtn").style.display = "none";
    _msgbox_waiting_numTimeoutID = window.setTimeout("_HideMsgBox()", SecondOfPause * 1000);
}

function ShowWaitingMsgBox2(SecondOfPause) {
    ShowMsgBox(_msgbox_waiting_html);
    document.getElementById("DivMsgBoxBtn").style.display = "none";
    _msgbox_waiting_numTimeoutID = window.setTimeout("_HideMsgBox()", SecondOfPause * 1000);
}

function _change_tags_visible(vType) {
    // IE7 plus won't need this function
    //there's risk that the element would be display if original hidden.....
    var tags = document.getElementsByTagName('select');
    for (i = tags.length - 1; i >= 0; i--)
        tags[i].style["visibility"] = vType;
    //	    var tags = document.getElementsByTagName('iframe');
    //	    for(i=tags.length-1;i>=0;i--)
    //		    tags[i].style["visibility"] = vType;
    //	    var tags = document.getElementsByTagName('object');
    //	    for(i=tags.length-1;i>=0;i--)
    //		    tags[i].style["visibility"] = vType;		    		    
}

function _pageWidth() { return window.innerWidth != null ? window.innerWidth : document.documentElement && document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body != null ? document.body.clientWidth : null; }
function _pageHeight() { return window.innerHeight != null ? window.innerHeight : document.documentElement && document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body != null ? document.body.clientHeight : null; }
function _posLeft() { return typeof window.pageXOffset != 'undefined' ? window.pageXOffset : document.documentElement && document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft ? document.body.scrollLeft : 0; }
function _posTop() { return typeof window.pageYOffset != 'undefined' ? window.pageYOffset : document.documentElement && document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop ? document.body.scrollTop : 0; }


function _HideMsgBox() {
    //clear auto close function
    window.clearTimeout(_msgbox_waiting_numTimeoutID);
    window.clearTimeout(_msgbox_floatwin_numTimeoutID);

    _change_tags_visible('visible');
    document.getElementById("DivMsgBoxBtn").style.display = "inline"; //for waiting msgbox
    document.getElementById("DivMsgBoxContainer").style["display"] = "none";
    document.getElementById("DivMsgBoxBack").style["display"] = "none";

    document.getElementById("DivMsgBoxTitle").innerHTML = _msgbox_default_title;
    document.getElementById("DivMsgBoxBtn").value = _msgbox_default_btn_name;
    document.getElementById("DivMsgBoxContent").innerHTML = _msgbox_default_content;
    if (document.all && event) event.cancelBubble = true;
    //if (_msgbox_callback!="") window.execScript(_msgbox_callback);
    if (_msgbox_callback != "") eval(_msgbox_callback);
    _msgbox_callback = "";
}

function _JustHideMsgBox() {
    _msgbox_callback = "";
    _HideMsgBox();
}

function _msgbox_EnterEvent(ev) {
    if (ev == null) ev = window.event; //IE
    var keyCode = ev.keyCode ? ev.keyCode : ev.charCode;
    if (keyCode == 13) {
        if (document.getElementById("DivMsgBoxContainer").style.display == "block") {
            document.getElementById("DivMsgBoxBtn").click();
            //stop event
            if (ev.stopPropagation) { ev.stopPropagation() }
            if (ev.preventDefault) { ev.preventDefault() }
            try { ev.cancelBubble = true } catch (e) { }
            try { ev.returnValue = false } catch (e) { }
        }
    }
}

// when user press enter, close msgbox
if (document.attachEvent) {
    document.attachEvent("onkeypress", _msgbox_EnterEvent);
    document.getElementById("DivMsgBoxBtn").className = "commonBtn70";
    document.getElementById("MsgCancelBtn").className = "commonBtn70";
}
else {
    document.addEventListener("keypress", _msgbox_EnterEvent, false);
}


   
 



