﻿var selectedItemColor = "#D5F2FA";
var unSelectItemColor = "";

String.prototype.Trim = function() {
    return this.replace(/(^\s*)|(\s*$)/g, "");
}

function GotoPage(pageNum) {
    PostBackAction("gotopage," + pageNum + "");
}

function DeleteSelectedEmails() {
    var selectedEmails = GetAllSelectedEmails();
    if (selectedEmails == "") {
        alert("Please select message(s)!");
        return;
    }

    if (confirm("Are you sure you want to delete the selected message(s)?")) {
        PostBackAction("deletemails," + selectedEmails + "");
    }
}

function BulkSelectedEmails() {
    var selectedEmails = GetAllSelectedEmails();
    if (selectedEmails == "") {
        alert("Please select message(s)!");
        return;
    }

    if (confirm("Are you sure you want to bulk the selected message(s)?")) {
        PostBackAction("bulkmails," + selectedEmails + "");
    }
}

function UnBulkSelectedEmails() {
    var selectedEmails = GetAllSelectedEmails();
    if (selectedEmails == "") {
        alert("Please select message(s)!");
        return;
    }

    if (confirm("Are you sure you want to unbulk the selected message(s)?")) {
        PostBackAction("unbulkmails," + selectedEmails + "");
    }
}

function EmptyMailbox() {
    if (confirm("Are you sure you want to empty the mailbox?")) {
        PostBackAction("emptymailbox,''");
    }
}

function Markallread(msgboxID, flag) {
    if (msgboxID == null || msgboxID == 0) {
        return;
    }

    try {
        Zwb.Web.EmailServices.MarkasRead(msgboxID, flag, "", onMarkComplete, onFailed, msgboxID);
    }
    catch (e) {
        //debugger;
    }

}

function onMarkComplete(result, msgboxID) {
    __doPostBack("commander", "MailList," + msgboxID);
}

function MoveAlert(path, mailboxID) {
    var selectedEmails = GetAllSelectedEmails();
    if (selectedEmails == "") {
        alert("Please select message(s)!");
        return;
    }
    PostBackAction("movemails," + mailboxID + '|' + selectedEmails + "");
}

function ResortMail(sortField, sortDirection) {
    PostBackAction("resortmails," + sortField + '|' + sortDirection + "");
}

function SearchEmail() {
    PostBackAction("search,''");
}

//commander control defined in index.aspx
function PostBack(args) {
    __doPostBack("commander", args);
}

function PostBackAction(command) {
    var serverCmdControl = document.getElementById("serverCmdControl").value;
    if (serverCmdControl == null || serverCmdControl == "")
        return;
    var serverCmdControlName = serverCmdControl.replace("_", "$");
    //alert(serverCmdControlName);
    __doPostBack(serverCmdControlName, command);
}

function SelectAllEmail(src) {
    var mailArray = document.getElementsByName("emailcheck");
    if (mailArray && mailArray.length > 0) {
        for (var i = 0; i < mailArray.length; i++) {
            mailArray[i].checked = src.checked;
            changeColor(mailArray[i]);
        }
    }
}

function selectAllMsgBox(src) {
    var boxArray = document.getElementsByName("MsgBoxCheckBox");
    if (boxArray && boxArray.length > 0) {
        for (var i = 0; i < boxArray.length; i++) {
            boxArray[i].checked = src.checked;
            changeColor(boxArray[i]);
        }
    }
}

function changeColor(obj) {
    var objParent;
    if ((obj == null) || (objParent = obj.parentNode.parentNode) == null)
        return;
    if (obj.checked)
        objParent.style.backgroundColor = selectedItemColor;
    else
        objParent.style.backgroundColor = unSelectItemColor;
}

function GetAllSelectedEmails() {
    var result = "";
    var mailArray = document.getElementsByName("emailcheck");
    if (mailArray && mailArray.length > 0) {
        for (var i = 0; i < mailArray.length; i++) {
            if (mailArray[i].checked) {
                if (result == "")
                    result = mailArray[i].value;
                else
                    result = result + "," + mailArray[i].value;
            }
        }
    }
    return result;
}


function UpdateProgressHandler(sender, e) {
    var msgDiv = $get("udProcegress");
    msgDiv.style.display = "block";
}

function HideTree(hideController, expandController, win) {
    var hide = document.getElementById(hideController);
    var expand = document.getElementById(expandController);

    document.getElementById(win).parentNode.style.display = "none";

    if (hide) {
        hide.style.display = "none";
    }

    if (expand) {
        expand.style.display = "";
    }
}

function ExpandTree(hideController, expandController, win, size) {
    var hide = document.getElementById(hideController);
    var expand = document.getElementById(expandController);

    document.getElementById(win).parentNode.style.display = "block";

    if (hide) {
        hide.style.display = "";
    }

    if (expand) {
        expand.style.display = "none";
    }
}

function iframeAutoFit() {
    try {
        var frm = document.getElementById("frmContent");
        var h1 = 0, h2 = 0;
        frm.parentNode.style.height = frm.offsetHeight + "px";
        frm.style.height = "10px";

        var doc = frm.contentWindow.document;
        if (doc.documentElement && doc.documentElement.scrollHeight) {
            h1 = doc.documentElement.scrollHeight;
        }
        if (doc.body) h2 = doc.body.scrollHeight;

        var h = Math.max(h1, h2);
        if (doc.all) { h += 4; }
        if (window.opera) { h += 1; }
        h = h + 40;
        frm.style.height = frm.parentNode.style.height = h + "px";

    }
    catch (ex) { alert(ex); }
}

function PrintMail(id1, id2) {
    window.open("PrintMsg.aspx?msgboxid=" + id1 + "&messageid=" + id2, "", "width=600,height=500,menubar=yes,resizable=yes,titlebar=no");
}

function MM_showHideLayers() { //v6.0
    var i, p, v, obj, args = MM_showHideLayers.arguments;
    for (i = 0; i < (args.length - 2); i += 3) if ((obj = MM_findObj(args[i])) != null) {
        v = args[i + 2];
        if (obj.style) { obj = obj.style; v = (v == 'show') ? 'visible' : (v == 'hide') ? 'hidden' : v; }
        obj.visibility = v;
    }
}

function SelectFromContact(control, from) {
    var iframeNE = document.getElementById("iframeNE");
    if (iframeNE)
        iframeNE.src = "/SubModules/Pages/SelectGroupsAndContacts.aspx?control=" + control;
    if (document.getElementById("PopupTitle"))
        document.getElementById("PopupTitle").innerHTML = "Select from Your Contacts";
}

function SelectMsgBox(c_msgboxID, c_msgboxpath, c_eventcontrol) {
    var iframeNE = document.getElementById("iframeNE");
    if (iframeNE)
        iframeNE.src = "/SubModules/Pages/SelectMsgFolder.aspx?control=" + c_msgboxID + "&eventcontrol=" + c_eventcontrol + "&p_control=" + c_msgboxpath + "&SelectMsgBox=true";
    if (document.getElementById("PopupTitle"))
        document.getElementById("PopupTitle").innerHTML = "Select Your Message Boxes";
}

function CloseSelect() {
    document.getElementById("frmDiv").style.display = "none";
    var box = document.getElementById("frmTd");
    if (box.childNodes.length > 0) {
        for (var i = 0; i < box.childNodes.length; i++) {
            box.removeChild(box.childNodes[i]);
        }
    }
}

function ShowSelectFile(e) {
    var serviceMenu = document.getElementById("divSelectFile");
    var t = e.offsetTop;
    var l = e.offsetLeft;
    while (e = e.offsetParent) {
        t += e.offsetTop;
        l += e.offsetLeft;
    }
    serviceMenu.style.display = "";
    serviceMenu.style.top = t + 12 + "px";
    serviceMenu.style.left = l + "px";
    serviceMenu.style.position = "absolute";
}

function CloseSelectFile() {
    var serviceMenu = document.getElementById("divSelectFile");
    serviceMenu.style.display = "none";
}

function ClosePopupDiv(id) {
    var mpeNewEditMailBox = $find(id);
    if (mpeNewEditMailBox) {
        mpeNewEditMailBox.hidePopup();
    }
}

function SelectContactIcon() {
    var iframeNE = document.getElementById("iframeNE");
    if (iframeNE)
        iframeNE.src = "/file/selectfolderorfile.aspx?control=iconFileID&eventcontrol=newContCommand&selectType=selectoneimage";
    if (document.getElementById("PopupTitle"))
        document.getElementById("PopupTitle").innerHTML = "Select from My Storage";
}

function PutInFolder() {
    var selectedEmails = GetAllSelectedEmails();
    if (selectedEmails == "") {
        alert("Please select message(s)!");
        return;
    }

    var iframeNE = document.getElementById("iframeNE");
    if (iframeNE)
        iframeNE.src = "/SubModules/Pages/SelectMsgFolder.aspx?control=m_DestMsgboxID&eventcontrol=m_MoveControl";
    if (document.getElementById("PopupTitle"))
        document.getElementById("PopupTitle").innerHTML = "Select Your Message Boxes";

}
/*--upload attachment from my storage-*/
function ClickAttachWeb() {
    var iframeNE = document.getElementById("iframeNE");
    if (iframeNE)
        iframeNE.src = "/file/selectfolderorfile.aspx?control=m_AttachFileIDs&eventcontrol=m_AttachWebFilesButton&selectType=selectfiles";
    if (document.getElementById("PopupTitle"))
        document.getElementById("PopupTitle").innerHTML = "Select from My Storage";
}

function AttachWebFiles() {
    var fileIDs = document.getElementById("m_AttachFileIDs").value;
    if (fileIDs == "")
        return;
    if (typeof (validate) == "function") {
        validate(false);
    }

    var controlID = document.getElementById("serverCmdControl2").value;
    __doPostBack(controlID, "attachwebfiles," + fileIDs + "");
}

function SubmitIframe() {
    document.getElementById("iframeNE").contentWindow.document.getElementById("clickButton").click();
}

/*--upload attachment from local-*/
function Attach() {
    var iframeNE = document.getElementById("iframeNE");

    if (iframeNE)
        iframeNE.src = "/SubModules/Pages/UploadAttachment.aspx?control=m_AttachFileIDs&eventcontrol=m_AttachWebFilesButton&selectType=selectfiles";
    else
        return;
    if (document.getElementById("PopupTitle"))
        document.getElementById("PopupTitle").innerHTML = "Upload Local Files";
    //AutoPopupModal(iframeNE);
}

var LoadIframe = true;

function CancelModal() {
    var iframeNE = document.getElementById("iframeNE");
    if (iframeNE == null)
        return;

    iframeNE.src = "";
    iframeNE.removeAttribute("src");
    if (iframeNE.childNodes.length > 0) {
        for (var i = 0; i < iframeNE.childNodes.length; i++) {
            iframeNE.removeChild(iframeNE.childNodes[i]);
        }
    }
    LoadIframe = true;
}

function AutoPopupModal(frm) {
    if (!LoadIframe)
        return;

    if (frm == null || frm.src == null || frm.src == "")
        return;

    var PopupModalID = document.getElementById("Msg_PlPopupModa_ID").value;
    var PopupModal = document.getElementById(PopupModalID);
    if (PopupModal == null) {
        return;
    }

    LoadIframe = false;

    PopupModal.style.display = "";
    PopupModal.parentNode.style.display = ""; //very import when DropShadow=true

    var h1 = 0, h2 = 0, w1 = 0, w2 = 0;
    frm.parentNode.style.height = frm.offsetHeight + "px";
    frm.style.height = "10px";

    frm.parentNode.style.width = frm.offsetWidth + "px";
    frm.style.width = "10px";

    var doc = frm.contentWindow.document;
    if (doc.documentElement && doc.documentElement.scrollHeight && doc.documentElement.scrollWidth) {
        h1 = doc.documentElement.scrollHeight;
        w1 = doc.documentElement.scrollWidth;
    }
    if (doc.body) {
        h2 = doc.body.scrollHeight;
        w2 = doc.body.scrollWidth;
    }

    var h = Math.max(h1, h2);
    var w = Math.max(w1, w2);

    //if(doc.all) {h += 4; w+=4;}
    // if(window.opera) {h += 1; w+=1;}

    frm.style.height = frm.parentNode.style.height = h + "px"; //
    frm.style.width = frm.parentNode.style.width = w + "px"; //

    PopupModal.style.height = PopupModal.parentNode.style.height = (h + 30) + "px";
    PopupModal.style.width = PopupModal.parentNode.style.width = (w) + "px";

    if (document.getElementById("fakeOK"))
        document.getElementById("fakeOK").click();
}

/*--end upload attachment from local-*/

//download attachment

function DownloadAttachFileById(attachFileID, msgID, sesID) {
    location = "../file/DF.aspx?sesID=" + sesID + "&msgID=" + msgID + "&attachFileID=" + attachFileID + "&forcedDownload=true";
}

function SelectFolderID(attachmentID) {
    if (attachmentID <= 0)
        return;
    //check whether saving another file or not.
    if (document.getElementById("m_Saving") && document.getElementById("m_Saving").value == 1) {
        alert("Saving Another File!");
        return;
    }
    document.getElementById("m_AttachmentID").value = attachmentID;

    var iframeNE = document.getElementById("iframeNE");
    if (iframeNE)
        iframeNE.src = "/file/selectfolderorfile.aspx?control=m_FolderID&eventcontrol=m_SaveToAccountButton&selectType=selectonefolder";
    if (document.getElementById("PopupTitle"))
        document.getElementById("PopupTitle").innerHTML = "Select Folder From My Storage";
}

function SaveToAccount() {
    var attachmentID = document.getElementById("m_AttachmentID").value;
    var folderID = document.getElementById("m_FolderID").value;

    if (document.getElementById("m_Saving"))//mark as saving.
        document.getElementById("m_Saving").value = 1;

    PostBackAction("SaveAttachment," + attachmentID + "|" + folderID + "");
}

function DownloadAttachFileEditMsg() {
    var attachFileID = GetAttachedID();
    if (attachFileID > 0) {
        location = "../file/DF.aspx?attachFileID=" + attachFileID + "&forcedDownload=true";
        return;
    }
}

function SaveAttachmentEditMsg(messageID) {
    var attachFileID = GetAttachedID();
    if (attachFileID > 0 && messageID > 0) {
        __doPostBack("commander", "saveattachmenteditmsg," + attachFileID + "|" + messageID + "");
    }
}

function GetAttachedID() {
    var c = document.getElementById("attachmentCount").value;
    if (c <= 0) {
        return 0;
    }

    for (var i = 0; i < c; i++) {
        if (document.getElementById("AttachmentIDs" + i).checked != true) {
            continue;
        }

        var attachFileID = document.getElementById("V_Attachment" + i).value;
        var reg = new RegExp("^[0-9]+$");

        if (!attachFileID.match(reg) || parseInt(attachFileID) <= 0) {
            alert("Please save message first!");
            return 0;
        }
        return attachFileID;
    }

    if (i >= c) {
        alert("Please select a file!");
        return 0;
    }
}

function GotoMailList(msgBoxID) {
    __doPostBack("commander", "MailList," + msgBoxID + "");
}

function SaveContactAddress(email, controlID) {
    if (email == null || email == "")
        return;
    var iframeNE = document.getElementById("iframeNE");
    if (iframeNE)
        iframeNE.src = "/SubModules/Pages/NewEditContact.aspx?email=" + email + "&control=" + controlID;
    else
        return;
    if (document.getElementById("PopupTitle"))
        document.getElementById("PopupTitle").innerHTML = "Save Contact Address";
}

function Compose() {
    __doPostBack('commander', 'writemessage,');
}

function AddMailBox() {
    var parentID = document.getElementById("mb_parentID").value;
    NewMailBox(parentID);
}

function NewMailBox(parentID)//tree method
{
    var iframeNE = document.getElementById("iframeNE");

    if (iframeNE)
        iframeNE.src = "/SubModules/Pages/Neweditmsgbox.aspx?control=e_msgBoxInfo&eventcontrol=cmdAddMsgBox&parentID=" + parentID;
    else
        return;
    if (document.getElementById("PopupTitle"))
        document.getElementById("PopupTitle").innerHTML = "Add Folder";
}


function AfterAddMsgBox() {
    //parentID, mailBoxID, msgBoxName
    var msgBoxInfo = GetMsgBoxInfo();
    var url = "javascript:__doPostBack(\"commander\",\"MailList," + msgBoxInfo.msgBoxID + "\")";

    var pId = 0;
    if (tree != null) {
        var pUrl = "javascript:__doPostBack(\"commander\",\"MailList," + msgBoxInfo.parentID + "\")";
        pId = tree.UrlClientID[pUrl];
        if (pId == null) pId = 1;
        tree.AddNode(pId, url, msgBoxInfo.msgBoxName, '', '');
    }

    var mailboxlist = document.getElementById("mb_mailboxlist");
    if (mailboxlist != null && mailboxlist.value == "mb_mailboxlist") {
        PostBackAction("refresh,''");
    }
}

function RenameMsgBox()//mail box list method
{
    var iSel = 0;
    var msgboxIDs = "";
    var BoxName = "";
    var boxArray = document.getElementsByName("MsgBoxCheckBox");
    var parentID = document.getElementById("mb_parentID").value;
    if (boxArray && boxArray.length > 0) {
        var i = 0;
        for (i = 0; i < boxArray.length; i++) {
            if (boxArray[i].checked == false)
                continue;

            if (parentID <= 0) {
                BoxName = boxArray[i].id.toLowerCase();
                if (BoxName == "inbox" || BoxName == "sent" || BoxName == "draft" || BoxName == "trash" || BoxName == "bulk") {
                    alert("You can't rename system mail box!");
                    return;
                }
            }
            msgboxIDs = boxArray[i].value;
            iSel++;
            break;
        }
    }

    if (iSel == 0) {
        alert("Please select a folder!");
        return;
    }
    //window.location.href="<%=UrlBase%>/msg/NewEditMsgBox.aspx?msgBoxID="+msgboxIDs;
    EditMailBox(msgboxIDs);
}

function EditMailBox(mailBoxID)//tree method.
{
    var iframeNE = document.getElementById("iframeNE");

    if (iframeNE)
        iframeNE.src = "/SubModules/Pages/Neweditmsgbox.aspx?control=e_msgBoxInfo&eventcontrol=cmdEditMsgBox&msgBoxID=" + mailBoxID;
    else
        return;
    if (document.getElementById("PopupTitle"))
        document.getElementById("PopupTitle").innerHTML = "Edit Folder";
}

function AfterEditMsgBox() {
    var msgBoxInfo = GetMsgBoxInfo();
    //mailBoxID, msgBoxName
    if (msgBoxInfo.msgBoxID <= 0)//faild
        return;

    var url = "javascript:__doPostBack(\"commander\",\"MailList," + msgBoxInfo.msgBoxID + "\")";

    var cId = 0;
    if (tree != null) {
        cId = tree.UrlClientID[url];
        if (cId != null && cId != 0) {
            tree.RenameNode(cId, msgBoxInfo.msgBoxName);
        }
    }

    var spanMsgBoxName = document.getElementById("mailBoxName" + msgBoxInfo.msgBoxID);
    if (spanMsgBoxName) {
        spanMsgBoxName.innerHTML = msgBoxInfo.msgBoxName;
    }

    var mailboxlist = document.getElementById("mb_mailboxlist");
    if (mailboxlist != null && mailboxlist.value == "mb_mailboxlist") {
        PostBackAction("refresh,''");
    }
}

function GetMsgBoxInfo() {
    var strMsgBoxInfo = document.getElementById("e_msgBoxInfo").value;

    var MsgBoxInfo = new Object();
    if (strMsgBoxInfo == "")
        return null;
    var arr = strMsgBoxInfo.split("|");
    if (arr == null || arr.length == 0)
        return null;
    MsgBoxInfo.parentID = arr[0];
    MsgBoxInfo.msgBoxID = arr[1];
    MsgBoxInfo.msgBoxName = unescape(arr[2]);
    return MsgBoxInfo;
}

function DeleteMailBox(msgBoxIDs, previousID, completeFunc)//treeview method
{
    var progressID = Math.floor(Math.random() * 1000000) * ((new Date()).getTime() % 1000);
    var progressBar = new ProgressBar("pnlProrgress");
    progressBar.title = "Deleting, please wait...";
    progressBar.BuildProgressBar();
    progressBar.ShowProgressBar();
    if (completeFunc == null)
        completeFunc = onDeleteMailBoxComplete;

    if (typeof (msgBoxIDs) != "object") {
        var t = msgBoxIDs;
        msgBoxIDs = new Array();
        msgBoxIDs[0] = t;
    }

    var obj = new Object();
    obj[0] = progressID;
    obj[1] = progressBar;
    obj[2] = completeFunc;
    obj[3] = msgBoxIDs;
    obj[4] = previousID;

    Zwb.Web.EmailServices.DeleteMessageBox(msgBoxIDs, progressID, obj[2], onFailed, obj);

    GetServiceProgress(obj);
}

function onDeleteMailBoxComplete(result, obj) {
    clearTimeout(timmer);
    obj[1].HideProgress();
    AfterDeleteMailBox(obj[3][0]); //delete tree node.
    __doPostBack("commander", "MailList," + obj[4]);
}

function DeleteMsgBox()//msgbox list method
{
    var iSel = 0, j = 0;
    var msgboxIDs = new Array();
    var BoxName = "";
    var boxArray = document.getElementsByName("MsgBoxCheckBox");
    var parentID = document.getElementById("mb_parentID").value;
    if (boxArray && boxArray.length > 0) {
        var i = 0;
        for (i = 0; i < boxArray.length; i++) {
            if (boxArray[i].checked == false)
                continue;

            if (parentID <= 0) {
                BoxName = boxArray[i].id.toLowerCase();
                if (BoxName == "inbox" || BoxName == "sent" || BoxName == "draft" || BoxName == "trash" || BoxName == "bulk") {
                    alert("You can't delete system mail box!");
                    return;
                }
            }
            msgboxIDs[j++] = boxArray[i].value;
            //msgboxIDs += boxArray[i].value + ",";
            iSel++;
        }
    }

    if (iSel == 0) {
        alert("Please select a folder!");
        return;
    }
    if (iSel == 0) {
        alert("Please select the folder(s)!");
        return;
    }
    else if (iSel == 1) {
        if (!window.confirm("Are you sure you want to delete this folder and all of its contents?"))
            return;
    }
    else if (iSel > 1) {
        if (!window.confirm("Are you sure you want to delete the selected folders and all of their contents?"))
            return;
    }

    DeleteMailBox(msgboxIDs, 0, onDeleteComplete);
}

function onDeleteComplete(result, obj) {
    clearTimeout(timmer);
    obj[1].HideProgress(); //delete tree node.

    for (var i = 0; i < obj[3].length; i++) {
        AfterDeleteMailBox(obj[3][i]);
    }
    PostBackAction("refresh, ");
}

function AfterDeleteMailBox(mailBoxID) {
    var url = "javascript:__doPostBack(\"commander\",\"MailList," + mailBoxID + "\")";

    var cId = 0;
    if (tree != null) {
        cId = tree.UrlClientID[url];
        if (cId != null && cId != 0) {
            tree.removeNode(cId);
        }
    }
}

function MsgBoxManage() {
    __doPostBack("commander", "msgBoxManage,''");
}

function SendCommand(cmd) {
    var msgObj = document.getElementById("v_messageID");
    var msgBoxObj = document.getElementById("v_msgboxID");

    var messageID = 0;
    if (msgObj)
        messageID = msgObj.value;

    var msgboxID = 0;
    if (msgObj)
        msgboxID = msgBoxObj.value;

    switch (cmd) {
        case ("send"):
            PostBackAction("send,''");
            break;
        case ("save"):
            PostBackAction("save,''");
            break;
        case ("edit"):
            PostBackAction("edit,''");
            break;
        case ("reply"):
            PostBack("reply, " + messageID + "");
            break;
        case ("replyall"):
            PostBack("replyall, " + messageID + "");
            break;
        case ("forward"):
            PostBack("forward, " + messageID + "");
            break; bulk
        case ("delete"):
            if (confirm("Are you sure you want to delete this message?")) {
                PostBackAction("deletemail, " + messageID + "|" + msgboxID);
            }
            break;
        case ("bulk"):
            if (confirm("Are you sure you want to bulk this message?")) {
                PostBackAction("bulkmails, " + messageID + "|" + msgboxID);
            }
            break;
        case ("saveaddress"):
            PostBackAction("saveaddress," + messageID + "");
            break;
        case ("print"):
            window.open("PrintMsg.aspx?msgboxid=" + msgboxID + "&messageid=" + messageID, "", "width=600,height=500,menubar=yes,resizable=yes,titlebar=no");
            break;
        case ("neweditmailbox"):
            PostBackAction("ok");
            break;
        case ("cancleneweditmailbox"):
            __doPostBack("commander", "MailList," + msgboxID + "");
            break;
    }
}

function ClickPreview() {
    if (typeof (validate) == "function") {
        if (!validate(true))
            return;
    }
    else {
        return;
    }
    PostBackAction("preview,''");
}

function SendMail() {
    if (typeof (validate) == "function") {
        if (!validate(true))
            return;
    }
    else {
        return;
    }
    PostBackAction("sendmail,''");
}

function ClickSave() {
    if (typeof (validate) == "function") {
        if (!validate(false))
            return;
    }
    else {
        return;
    }
    PostBackAction("savemail,''");
}

function showSenderDiv(controlID, e) {
    var msgFrom = document.getElementById(controlID);
    if (msgFrom == null)
        return;

    var senderDiv = document.getElementById("divSenderEmail");
    if (senderDiv == null)
        return;
    var o = msgFrom;
    var t = o.offsetTop;
    var l = o.offsetLeft;
    while (o = o.offsetParent) {
        t += o.offsetTop;
        l += o.offsetLeft;
    }
    senderDiv.style.display = "";
    senderDiv.style.top = (t + 15) + "px";
    senderDiv.style.left = l + "px";
    senderDiv.style.position = "absolute";
}

function selectSender(obj, msgFromControl) {
    var msgFrom = document.getElementById(msgFromControl);
    if (msgFrom == null)
        return;
    if (obj == null)
        return;
    msgFrom.value = obj.innerHTML;
    var senderDiv = document.getElementById("divSenderEmail");
    if (senderDiv == null)
        return;
    senderDiv.style.display = "none";
}

function SaveAttachment(controlID, messageID) {
    var obj = document.getElementById(controlID);
    if (obj) {
        if (obj.selectedIndex < 0) {
            alert("Please select a attach file!");
            return;
        }
        var attachmentID = obj.options[obj.selectedIndex].value;
        __doPostBack("commander", "saveattachment," + attachmentID + "|" + messageID + "");
    }
}

function Addmailbox(nameControlID, submitControlID, edit) {
    var obj = document.getElementById(nameControlID);
    if (obj) {
        if (obj.value == "") {
            alert("Folder name is required!");
            obj.focus();
            return;
        }
        if (edit == "True") {
            __doPostBack("commander", "editfolder,");
        }
        else {
            __doPostBack("commander", "addfolder,");
        }
    }

}

function RemoveAttachment(ClientID) {
    //if(!confirm("Are you sure to remove this attachment?"))
    //    return;

    var objDiv = document.getElementById(ClientID);
    var spTotalSize = document.getElementById("spTotalSize");
    var totalSize = document.getElementById("totalSize");
    var fileSize = document.getElementById("S_Attachment" + ClientID).value;
    if (objDiv && objDiv.childNodes.length > 0) {
        if (objDiv.childNodes.length > 0) {
            for (var i = 0; i < objDiv.childNodes.length; i++) {
                objDiv.removeChild(objDiv.childNodes[i]);
            }
        }
    }

    objDiv.parentNode.removeChild(objDiv); //remove self

    var size = parseFloat(totalSize.value) - parseFloat(fileSize);

    if (size <= 0) {
        size = 0;
        document.getElementById("attachmentSize").style.display = "none";
    }
    totalSize.value = size;
    spTotalSize.innerHTML = FormatSize(size);
}

function DelAttachment(cmdcontrolID) {
    var countObj = document.getElementById("attachmentCount");
    var checked = false;
    if (countObj && countObj.value > 0) {
        var k = 0;
        var checkboxObj;
        for (; k < countObj.value; k++) {
            checkboxObj = document.getElementById("AttachmentIDs" + k);
            if (checkboxObj && (checkboxObj.checked == true)) {
                if (!checked && !confirm("Are you sure to remove this attachment?"))
                    return;

                checked = true;
                var objDiv = document.getElementById(k);
                var totalFileSize = document.getElementById("spTotalSize");
                var fileSize = document.getElementById("S_Attachment" + k).value;
                if (objDiv && objDiv.childNodes.length > 0) {
                    if (objDiv.childNodes.length > 0) {
                        for (var i = 0; i < objDiv.childNodes.length; i++) {
                            objDiv.removeChild(objDiv.childNodes[i]);
                        }
                    }
                }
                objDiv.style.display = "none";
                var t = (parseFloat(totalFileSize.innerHTML) * 1024 - parseFloat(fileSize)) / 1024;
                t = t < 0 ? 0 : t;
                t = Math.round(t * 100.00) / (100.00);
                totalFileSize.innerHTML = t;
            }
        }
        if (!checked) {
            alert("Please select the file(s)!");
            return;
        }
        else {
            //if(confirm("Are you sure to remove this attachment?"))
            //  PostBackdByControlID(cmdcontrolID)
        }
    }
}
function PostBackdByControlID(cmdcontrolID) {
    if (cmdcontrolID == "")
        return;
    var obj = document.getElementById(cmdcontrolID);
    if (obj) {
        var href = obj.href;
        if (href.lenght != 0) {
            var command = href.substring(11);
            if (command != "") {
                command = unescape(command);
                eval(command);
            }
        }
    }
}

function resizeFrm() {
    AdaptWindowSize();
}

function AddCC(obj, ccID) {
    if (obj == null)
        return;
    var toCC = document.getElementById("rowMsgCc");
    if (toCC == null)
        return;
    if (toCC.style.display == "") {
        toCC.style.display = "none";
        obj.innerHTML = "Add CC";
        document.getElementById(ccID).value = "";
    }
    else {
        toCC.style.display = "";
        obj.innerHTML = "Delete CC";
    }
}

function AddBCC(obj, ccID) {
    if (obj == null)
        return;

    var toCC = document.getElementById("rowMsgBCc");
    if (toCC == null)
        return;
    if (toCC.style.display == "") {
        toCC.style.display = "none";
        obj.innerHTML = "Add BCC";
        document.getElementById(ccID).value = "";
    }
    else {
        toCC.style.display = "";
        obj.innerHTML = "Delete BCC";
    }
}

function GetMsgBoxImg(boxname) {
    var bname;
    bname = boxname.toLowerCase();
    switch (bname) {
        case ("inbox"):
            document.write("<img src=\"/images/Msg/Icon_Inbox.gif\">");
            break;
        case ("sent"):
            document.write("<img src=\"/images/Msg/Icon_Sent.gif\">");
            break;
        case ("draft"):
            document.write("<img src=\"/images/Msg/Icon_Draft.gif\">");
            break;
        case ("trash"):
            document.write("<img src=\"/images/Msg/Icon_Trash.gif\">");
            break;
        case ("bulk"):
            document.write("<img src=\"/images/Msg/Icon_Bulk.gif\">");
            break;
        default:
            document.write("<img src=\"/images/Msg/Icon_Folder.gif\">");
            break;
    }
}


function GetColor(nrow) {
    if (nrow % 2 == 0)
        document.write("<tr bgcolor=#F0F0F0>");
    else
        document.write("<tr bgcolor=#FFFFFF>");
}

function selectAll() {
    var elems = ContactGroupListID.elements;
    var tlen = elems.length;
    for (var i = 0; i < tlen; i++) {
        var aName = elems[i].name;
        if (aName.match("ContactGroupCheckBox") != "ContactGroupCheckBox") continue;
        elems[i].checked = ContactGroupListID.selectAllFlag.checked;
    }
}

function GotoMsgBox(msgBoxID) {
    __doPostBack("commander", "MailList, " + msgBoxID + "");
}

function EmptyMsgBoxByMsgBoxID(msgBoxID) {
    if (msgBoxID <= 0)
        return;

    var progressID = Math.floor(Math.random() * 1000000) * ((new Date()).getTime() % 1000);
    if (Zwb && Zwb.Web && Zwb.Web.EmailServices && Zwb.Web.EmailServices.EmptyMessageBoxOnWebMail) {
        var progressBar = new ProgressBar("pnlProrgress");
        progressBar.title = "Deleting, please wait...";
        progressBar.BuildProgressBar();
        progressBar.ShowProgressBar();
        var obj = new Object();
        obj[0] = progressID;
        obj[1] = progressBar;
        obj[2] = onEmptyCompleted;
        obj[3] = msgBoxID;
        Zwb.Web.EmailServices.EmptyMessageBoxOnWebMail(msgBoxID, progressID, onEmptyCompleted, onFailed, obj);

        GetServiceProgress(obj);
    }
}

function onEmptyCompleted(result, obj) {
    clearTimeout(timmer);
    obj[1].HideProgress();
    __doPostBack("commander", "MailList, " + obj[3]);
}

function FormatSize(size) {
    size = parseFloat(size);
    var isNeg = false;
    if (size < 0) {
        isNeg = true;
        size = 0 - size;
    }

    var retStr;
    if (size < 1024)
        retStr = "" + size + " B";
    else if (size < 1024 * 1024)
        retStr = "" + Math.round((size / (1024)) * 100) / (100.00) + " KB";
    else if (size < 1024 * 1024 * 1024)
        retStr = "" + Math.round((size / (1024 * 1024)) * 100) / (100.00) + " MB";
    else // >1GB
        retStr = "" + Math.round((size / (1024 * 1024 * 1024)) * 100) / (100.00) + " GB";

    if (isNeg)
        return "-" + retStr;
    else
        return retStr;
}

function resizeCommonButtonDiv() {
    if ($get("tbCommonButton") && $get("divTree")) {
        $get("tbCommonButton").style.width = $get("divTree").style.width;
    }
}

function LoadEmailOption() {
    PostBack("EmailOption-EmailOption-1");
}

/*--------ProgressBar Start---------*/
function GetServiceProgress(obj) {
    Zwb.Web.EmailServices.GetProgress(obj[0], onReturn, onFailed, obj);
}

function onFailed(args, obj) {
    showMessage("An error has occurred on server!");
    if (obj != null && obj[1] != null) {
        obj[1].HideProgress();
    }
}

var timmer;
function onReturn(result, obj) {
    if (result != null) {
        if (obj[1].command == "stop") {
            Zwb.Web.EmailServices.Stop(obj[0], obj[2], onFailed, obj);
        }
        else {
            obj[1].value = result[0];
            obj[1].total = result[1];
            obj[1].etime = result[2];
            obj[1].ShowProgressBar();
        }
    }

    clearTimeout(timmer);
    timmer = setTimeout(function() { GetServiceProgress(obj) }, 1000);
}


var currProgressBar;
function ProgressBar(parentDomID) {
    this.total = 1;
    this.value = 0;
    this.title = "";
    this.progressBarWidth = 240;
    this.etime = "";
    this.command = "";
    this.parentDom = document.getElementById(parentDomID);
    if (this.parentDom == undefined || this.parentDom == null) {
        this.parentDom = document.createElement("div");
        this.parentDom.id = parentDomID;
        this.parentDom.className = "progress";
        document.body.appendChild(this.parentDom);
    }
    currProgressBar = this;
};

ProgressBar.prototype.BuildProgressBar = function() {
    var str = "<div class=\"p_container\">";
    str += "<div class=\"p_header\" id=\"p_title\">" + this.title + "</div>";
    str += "    <div class=\"p_body\">";
    str += "        <div class = \"p_bar\">";
    str += "             <div id=\"progressBar\"><div id=\"progressValue\"></div></div>";
    str += "             <div class=\"p_stop hand\" onclick=\"currProgressBar.Stop()\"></div>";
    str += "        </div>";
    str += "       <div class=\"p_etime\">Estimated Time Left:&nbsp;&nbsp;<span id=\"etime\"></span></div>";
    str += "    </div>";
    str += "</div>";
    this.parentDom.innerHTML = str;
};

ProgressBar.prototype.ShowProgressBar = function() {
    if ($get("progressValue") == null) {
        return false;
    }

    $get("progressValue").style.width = (((this.value * 1.0) / (this.total * 1.0)) * this.progressBarWidth) + "px";
    $get("etime").innerHTML = this.etime;
    this.parentDom.style.display = "block";
};

ProgressBar.prototype.Stop = function() {
    this.command = "stop";
    this.HideProgress();
};

ProgressBar.prototype.HideProgress = function() {
    if (this.parentDom) {
        this.parentDom.style.display = "none";
        this.parentDom.innerHTML = "";
    }
};
/*--------ProgressBar End---------*/