
// Youmail.vm.dynaDialogActive
// Youmail.vm.replyDynaDialogData
// Youmail.vm.dynaDialog
// Youmail.vm.curDialogData
// ***************  START simple popup info panel with simple close

/*
Opens a panel with content of the given url, with no buttons.
*/
function openUrlPanel(title, width, height, infoUrl)
{
    if (Youmail.vm.dynaDialogActive == true) {
       alert("Another dialog is still up, please wait or refresh this page");
       return;
    }

    checkVMValidSession();
    Youmail.vm.setDialogActive(true);
    var args = {title: title, width: width, height: height};
    var callback = { success:handleUrlPanelSuccess, failure:handleUrlPanelFailure, argument: args};
    var request = YAHOO.util.Connect.asyncRequest('GET', infoUrl, callback, null);
}

var handleUrlPanelSuccess = function (o)
{
    var resp = o.responseText;
    if (resp == null || resp.indexOf("dialogsuccess") < 0) {
        if (resp == null) {
            alert("Unable to retrieve information from server.  Please refresh this page.");
        } else {
            alert(resp);
        }
        Youmail.vm.setDialogActive(false);
        return;
    }
        
    hidePlayerOnly();
    var panel = null;
    
    var dynaDialogDiv = getChildElementById("ymPopup");
    dynaDialogDiv.style.display = "block";
    
    
    if (Youmail.vm.sharedPage == true) {
        panel = new YAHOO.widget.Panel("ymPopup", { width:o.argument.width, visible:false, draggable:true, fixedcenter : true, underlay:"none", iframe:true, close:true, constraintoviewport:true, modal:true} );
    } else {
        panel = new YAHOO.widget.Panel("ymPopup", { width:o.argument.width, visible:false, draggable:true, fixedcenter:true, iframe:true, close:true, constraintoviewport:true, modal:true, underlay:"none", effect:{effect:YAHOO.widget.ContainerEffect.FADE,duration:0.5}} );
    }

    panel.beforeHideEvent.subscribe(function() {
      showPlayerOnly();
      Youmail.vm.setDialogActive(false);
      var dialogDiv = getChildElementById("ymPopupBodyId");
      if (dialogDiv != null) {
        dialogDiv.innerHTML = null;
      }
      removeYUIMask();
    });
    
    var dialogDiv = getChildElementById("ymPopupBodyId");
    dialogDiv.innerHTML = resp;
    var dialogTitleDiv = getChildElementById("ymPopupTitleId");
    dialogTitleDiv.innerHTML = o.argument.title;
    
    panel.render();
    panel.show();

    if (getChildElementById("focusHereId") != null) {
        ymFocus("focusHereId");
    }
}

var handleUrlPanelFailure = function(o)
{
    Youmail.vm.setDialogActive(false);
    alert("Error when contacting server, please try again.");
}


// ***************  START Edit Contact Dialog

function openEditContactDialog(entryIds)
{
   if (Youmail.vm.dynaDialogActive == true) {
       alert("Another dialog is still up, please wait or refresh this page");
       return;
   }

   var message = getDataByEntryId(entryIds);
   var editContactDialogData = getEditContactDynaDialogData();
   if (message.ct == 1) {
       editContactDialogData.setDialogTitle("Edit " + message.from);
   } else {
       editContactDialogData.setDialogTitle("Add " + message.from + " to contact list");
   }
   editContactDialogData.entryId = entryIds;
   Youmail.vm.dynaDialog.requestDynaDialogInfo(editContactDialogData);
}

function getEditContactDynaDialogData()
{
    if (Youmail.vm.editContactDynaDialogData != null) {
      return Youmail.vm.editContactDynaDialogData;
    }
    
    var editContactDialogData = new Youmail.vm.DynaDialogData();
    Youmail.vm.editContactDynaDialogData = editContactDialogData;
    editContactDialogData.dialogWidth = "375px";
    editContactDialogData.setDialogTitle("Confirm");

    editContactDialogData.getInitRequestInfoUrl = function()
    {
        return "/youmail/vm/inbox/getDialogView.do?dt=ec&" + Youmail.vm.vmNoCacheParam + "&entryId=" + this.entryId + "&" + Youmail.vm.inboxFolder + "&pt=" + Youmail.vm.pageType;
    };

    // returns an array of buttons for the dialogs
    editContactDialogData.getDialogButtons = function()
    {
        // default to Submit and Cancel
        return [ { text:"Save", handler:"Youmail.vm.editContactDynaDialogData.handleSubmit()" }, { text:"Cancel", handler:"Youmail.vm.editContactDynaDialogData.handleCancel()" } ];
    };
    

    // after a successful submit to the server
    editContactDialogData.handleSubmitSuccess = function(resp)
    {
        if (resp.validationError) {
            Youmail.vm.editContactDynaDialogData.showVError(resp.errorMsg);
            return false;
        }
        
        if (!resp.successful) {
            if (resp.errorMsg == null || resp.errorMsg == "") {
                alert("Error occurred when servicing request, please try again.");
            } else {
                alert(resp.errorMsg);
            }
            return true;
        }

        if (resp.successMsg == null) {
            confirm("Contact saved.");
        } else {
            confirm(resp.successMsg);
        }
        
        if (resp.shouldRefresh) {
            Youmail.vm.inboxTableData.retrieveData(inTb);
        }

        reloadCurrentPreview();
        return true;
    };

    // immediately after the dialog is shown to the user
    editContactDialogData.onAfterDialogShow = function()
    {
        ymFocus("firstNameId");
    };
    
    // validate, "this" is the dialog
    editContactDialogData.validate = function(formObj) {

        return true;
    };

    return Youmail.vm.editContactDynaDialogData;
}

// ***************  END Edit Contact Dialog

// ***************  START Edit Folder Dialog

function openEditFolderDialog(createNew, folderId)
{
   if (Youmail.vm.dynaDialogActive == true) {
       alert("Another dialog is still up, please wait or refresh this page");
       return;
   }

   
   var folder = getFolderById(folderId);

   var editFolderDialogData = getEditFolderDynaDialogData();
   if (createNew) {
       editFolderDialogData.setDialogTitle("Add New Folder");
   } else {
       if (folder == null) {
           // invalid folder, do nothing
           return;
       }
       editFolderDialogData.folderKey = folder.folderKey;
       editFolderDialogData.setDialogTitle("Edit " + folder.label);
   }
   editFolderDialogData.createNew = createNew;
   Youmail.vm.dynaDialog.requestDynaDialogInfo(editFolderDialogData);
}

function getEditFolderDynaDialogData()
{
    if (Youmail.vm.editFolderDynaDialogData != null) {
      return Youmail.vm.editFolderDynaDialogData;
    }
    
    var editFolderDialogData = new Youmail.vm.DynaDialogData();
    Youmail.vm.editFolderDynaDialogData = editFolderDialogData;
    editFolderDialogData.dialogWidth = "375px";
    editFolderDialogData.setDialogTitle("Confirm");

    editFolderDialogData.getInitRequestInfoUrl = function()
    {
        return "/youmail/vm/inbox/getDialogView.do?dt=ef&" + Youmail.vm.vmNoCacheParam + "&folderKey=" + this.folderKey + "&" + Youmail.vm.inboxFolder + "&cf=" + editFolderDialogData.createNew + "&pt=" + Youmail.vm.pageType;
    };

    // returns an array of buttons for the dialogs
    editFolderDialogData.getDialogButtons = function()
    {
        // default to Submit and Cancel
        return [ { text:"Save", handler:"Youmail.vm.editFolderDynaDialogData.handleSubmit()" }, { text:"Cancel", handler:"Youmail.vm.editFolderDynaDialogData.handleCancel()" } ];
    };
    

    // after a successful submit to the server
    editFolderDialogData.handleSubmitSuccess = function(resp)
    {
        if (resp.validationError) {
            Youmail.vm.editFolderDynaDialogData.showVError(resp.errorMsg);
            return false;
        }
        
        if (!resp.successful) {
            if (resp.errorMsg == null || resp.errorMsg == "") {
                alert("Error occurred when servicing request, please try again.");
            } else {
                alert(resp.errorMsg);
            }
            return true;
        }

        if (resp.successMsg == null) {
            if (editFolderDialogData.createNew) {
                confirm("New folder created.");
            } else {
                confirm("Folder saved.");
            }
        } else {
            confirm(resp.successMsg);
        }
        
        if (resp.fullRefresh || resp.shouldRefresh) {
            window.location = window.location;
        }

        return true;
    };

    // immediately after the dialog is shown to the user
    editFolderDialogData.onAfterDialogShow = function()
    {
        ymFocus("firstNameId");
    };
    
    // validate, "this" is the dialog
    editFolderDialogData.validate = function(formObj) {

        return true;
    };

    return Youmail.vm.editFolderDynaDialogData;
}

// ***************  END Edit Folder Dialog

// ***************  START Spam Dialog

function openSpamDialog(entryIds)
{
   if (Youmail.vm.dynaDialogActive == true) {
       alert("Another dialog is still up, please wait or refresh this page");
       return;
   }

   var message = getDataByEntryId(entryIds);  // there shall only be one
   var spamDialogData = getSpamDynaDialogData();
   spamDialogData.setDialogTitle("Report As Spam - " + message.from);

   spamDialogData.entryId = entryIds;
   Youmail.vm.dynaDialog.requestDynaDialogInfo(spamDialogData);
}

function getSpamDynaDialogData()
{
    if (Youmail.vm.spamDynaDialogData != null) {
      return Youmail.vm.spamDynaDialogData;
    }
    
    var spamDialogData = new Youmail.vm.DynaDialogData();
    Youmail.vm.spamDynaDialogData = spamDialogData;
    spamDialogData.dialogWidth = "430px";
    spamDialogData.setDialogTitle("Confirm");

    spamDialogData.getInitRequestInfoUrl = function()
    {
        return "/youmail/vm/inbox/getDialogView.do?dt=spam&" + Youmail.vm.vmNoCacheParam + "&entryId=" + this.entryId + "&" + Youmail.vm.inboxFolder + "&pt=" + Youmail.vm.pageType;
    };

    // returns an array of buttons for the dialogs
    spamDialogData.getDialogButtons = function()
    {
        // default to Submit and Cancel
        return [ { text:"Submit", handler:"Youmail.vm.spamDynaDialogData.handleSubmit()" }, { text:"Cancel", handler:"Youmail.vm.spamDynaDialogData.handleCancel()" }  ];
    };
    

    // after a successful submit to the server
    spamDialogData.handleSubmitSuccess = function(resp)
    {
        if (resp.validationError) {
            Youmail.vm.spamDynaDialogData.showVError(resp.errorMsg);
            return false;
        }
        
        if (!resp.successful) {
            if (resp.errorMsg == null || resp.errorMsg == "") {
                alert("Error occurred when servicing request, please try again.");
            } else {
                alert(resp.errorMsg);
            }
            return true;
        }

        if (resp.successMsg == null) {
            confirm("Spam reported.");
        } else {
            confirm(resp.successMsg);
        }
        
        if (resp.shouldRefresh) {
            Youmail.vm.inboxTableData.retrieveData(inTb);
        }

        var message = getDataByEntryId(resp.entryId);
        // if previous message is new then update the Spam folder count
        if (message.isNew) {
            var newFolderId = "Spam";
            var newFolderCount = Youmail.vm.getFolderCount(newFolderId);
            Youmail.vm.updateFolderCount(newFolderId, newFolderCount + 1);
        }
            
        clearPreview("Spam has been reported and moved to Trash folder.");
        return true;
    };

    // immediately after the dialog is shown to the user
    spamDialogData.onAfterDialogShow = function()
    {
        ymFocus("publishId");
    };
    
    // validate, "this" is the dialog
    spamDialogData.validate = function(formObj) {

        return true;
    };

    return Youmail.vm.spamDynaDialogData;
}

function toggleSpamPublish(shouldPublish, publishField)
{
    var theForm = publishField.form;

    if (theForm.username != null) {
        theForm.username.disabled = !shouldPublish;
    }
    if (theForm.reportedId != null) {
        onReportedAsChange();
        theForm.reportedId.disabled = !shouldPublish;
    }
    if (!shouldPublish) {
        theForm.providedName.disabled = !shouldPublish;
    }
    
    theForm.category.disabled = !shouldPublish;
    theForm.comment.disabled = !shouldPublish;

    Youmail.vm.curDialogData.showVError('');

}

function onReportedAsChange()
{
    var theForm = getChildElementById("popupFormId", getChildElementById("spamPopupId"));

    var dropDown = theForm.reportedId;

    if (dropDown != null) {
        if (dropDown.selectedIndex != 0) {
            theForm.providedName.disabled = true;
            theForm.providedName.value = dropDown.options[dropDown.selectedIndex].text;
        } else {
            theForm.providedName.disabled = false;
        }
    }
    
    Youmail.vm.curDialogData.showVError('');
}


// ***************  END Spam Dialog

// ***************  START Unspam Dialog

function openUnspamDialog(entryIds)
{
   if (Youmail.vm.dynaDialogActive == true) {
       alert("Another dialog is still up, please wait or refresh this page");
       return;
   }
   
   // check if it is a spam, we can't unspam a non spam
   var entry = getDataByEntryId(entryIds);
   if (entry != null && !(entry.cpt = 120 || entry.cpt == 121)) {
       Youmail.vm.dynaDialogActive == false;
       alert("This message is not spam.");
       return;
   }

   var message = getDataByEntryId(entryIds);  // there shall only be one
   var unspamDialogData = getUnspamDynaDialogData();
   unspamDialogData.setDialogTitle("Not Spam - " + message.from);

   unspamDialogData.entryId = entryIds;
   Youmail.vm.dynaDialog.requestDynaDialogInfo(unspamDialogData);
}

function getUnspamDynaDialogData()
{
    if (Youmail.vm.unspamDynaDialogData != null) {
      return Youmail.vm.unspamDynaDialogData;
    }
    
    var unspamDialogData = new Youmail.vm.DynaDialogData();
    Youmail.vm.unspamDynaDialogData = unspamDialogData;
    unspamDialogData.dialogWidth = "430px";
    unspamDialogData.setDialogTitle("Confirm");

    unspamDialogData.getInitRequestInfoUrl = function()
    {
        return "/youmail/vm/inbox/getDialogView.do?dt=unspam&" + Youmail.vm.vmNoCacheParam + "&entryId=" + this.entryId + "&" + Youmail.vm.inboxFolder + "&pt=" + Youmail.vm.pageType;
    };

    // returns an array of buttons for the dialogs
    unspamDialogData.getDialogButtons = function()
    {
        // default to Submit and Cancel
        return [ { text:"Submit", handler:"Youmail.vm.unspamDynaDialogData.handleSubmit()" }, { text:"Cancel", handler:"Youmail.vm.unspamDynaDialogData.handleCancel()" }  ];
    };
    

    // after a successful submit to the server
    unspamDialogData.handleSubmitSuccess = function(resp)
    {
        if (resp.validationError) {
            Youmail.vm.unspamDynaDialogData.showVError(resp.errorMsg);
            return false;
        }
        
        if (!resp.successful) {
            if (resp.errorMsg == null || resp.errorMsg == "") {
                alert("Error occurred when servicing request, please try again.");
            } else {
                alert(resp.errorMsg);
            }
            return true;
        }

        if (resp.successMsg == null) {
            confirm("Removed from spam list.");
        } else {
            confirm(resp.successMsg);
        }
        
        var message = getDataByEntryId(resp.entryId);
        // if previous message is new then update the Spam folder count
        if (message.isNew) {
            var newFolderId = "Inbox";
            var newFolderCount = Youmail.vm.getFolderCount(newFolderId);
            Youmail.vm.updateFolderCount(newFolderId, newFolderCount + 1);
        }
            
        if (resp.shouldRefresh) {
            Youmail.vm.inboxTableData.retrieveData(inTb);
        }

        clearPreview("Caller removed from spam list.");
        return true;
    };

    // immediately after the dialog is shown to the user
    unspamDialogData.onAfterDialogShow = function()
    {
        ymFocus("publishId");
    };
    
    // validate, "this" is the dialog
    unspamDialogData.validate = function(formObj) {

        return true;
    };

    return Youmail.vm.unspamDynaDialogData;
}

function toggleUnspamPublish(shouldPublish, publishField)
{
    var theForm = publishField.form;

    if (theForm.username != null) {
        theForm.username.disabled = !shouldPublish;
    }
    
    theForm.comment.disabled = !shouldPublish;

    Youmail.vm.curDialogData.showVError('');

}
// ***************  END Unspam Dialog



// ***************  START Forward Dialog

function openForwardDialog(entryIds)
{
   if (Youmail.vm.dynaDialogActive == true) {
     alert("Another dialog prompt is still up, please wait or refresh this page");
     return;
   }
   
   var forwardDialogData = getForwardDynaDialogData();
   forwardDialogData.entryId = entryIds;
   forwardDialogData.mk = entryIds;
   Youmail.vm.dynaDialog.requestDynaDialogInfo(forwardDialogData);
}

function getForwardDynaDialogData()
{
    if (Youmail.vm.forwardDynaDialogData != null) {
      return Youmail.vm.forwardDynaDialogData;
    }
    
    var forwardDialogData = new Youmail.vm.DynaDialogData();
    Youmail.vm.forwardDynaDialogData = forwardDialogData;
    forwardDialogData.dialogWidth = "450px";
    forwardDialogData.setDialogTitle("Forward Message");

    forwardDialogData.getInitRequestInfoUrl = function()
    {
        if (Youmail.vm.sharedPage == true) {
            return "/youmail/voicemail/getDialogSharedView.do?dt=forward&mk=" + this.mk;
        } else {
            return "/youmail/vm/inbox/getDialogView.do?dt=forward&" + Youmail.vm.vmNoCacheParam + "&entryId=" + this.entryId + "&pt=" + Youmail.vm.pageType;
        }
    };

    // returns an array of buttons for the dialogs
    forwardDialogData.getDialogButtons = function()
    {
        // default to Submit and Cancel
        return [ { text:"Forward", handler:"Youmail.vm.forwardDynaDialogData.handleSubmit()" }, { text:"Cancel", handler:"Youmail.vm.forwardDynaDialogData.handleCancel()" }
         ];
    };
    

    // after a successful submit to the server, "this" is the dialog
    forwardDialogData.handleSubmitSuccess = function(resp)
    {
        if (resp.validationError) {
            Youmail.vm.forwardDynaDialogData.showVError(resp.errorMsg);
            return false;
        }
        
        if (!resp.successful) {
            if (resp.errorMsg == null || resp.errorMsg == "") {
                alert("Error occurred when servicing request, please try again.");
            } else {
                alert(resp.errorMsg);
            }
            return true;
        }
    
        if (resp.shouldRefresh) {
            Youmail.vm.inboxTableData.retrieveData(inTb);
            return true;
        }
        
        
        if (resp.successMsg == null) {
            confirm("Message forwarded.");
        } else {
            confirm(resp.successMsg);
        }
        
        return true;
    };

    // immediately after the dialog is shown to the user
    forwardDialogData.onAfterDialogShow = function()
    {
        ymFocus("msgTextAreaId");
    };
    
    // validate, "this" is the dialog
    forwardDialogData.validate = function(formObj) {
        var data = formObj;
        var scv = trimSpaces(data.contactIds.value);
        var to = data.to.value;

        if (to == "contact" && scv == null) {
            Youmail.vm.curDialogData.showVError("Please select a contact.");
            return false;
        }
        return true;
        
    };

    return Youmail.vm.forwardDynaDialogData;
}

function toggleForwardTo(toField)
{
    var value = toField.value;
    
    var emailSel = getChildElementById("emailSel");
    var contactSel = getChildElementById("contactSel");
    var groupSel = getChildElementById("groupSel");

    var emailSelLabel = getChildElementById("emailSelLabel");
    var contactSelLabel = getChildElementById("contactSelLabel");
    var groupSelLabel = getChildElementById("groupSelLabel");
    
    var emailHelp = getChildElementById("emailHelp");
    var contactHelp = getChildElementById("contactHelp");
    var groupHelp = getChildElementById("groupHelp");

    var isEmail = (value == "email") ? "block" : "none";
    var isContact = (value == "contact") ? "block" : "none";
    var isGroup = (value == "group") ? "block" : "none";
    
    emailSel.style.display = isEmail;
    contactSel.style.display = isContact;
    groupSel.style.display = isGroup;
    
    emailSelLabel.style.display = isEmail;
    contactSelLabel.style.display = isContact;
    groupSelLabel.style.display = isGroup;
    
    emailHelp.style.display = isEmail;
    contactHelp.style.display = isContact;
    groupHelp.style.display = isGroup;
    
    Youmail.vm.curDialogData.showVError('');

}


//****************  END Forward Dialog


// ***************  START Invite Dialog

function openInviteDialog(entryIds)
{
   if (Youmail.vm.dynaDialogActive == true) {
     alert("Another dialog prompt is still up, please wait or refresh this page");
     return;
   }
   
   var message = getDataByEntryId(entryIds);
   var inviteDialogData = getInviteDynaDialogData();
   inviteDialogData.entryId = entryIds;
   Youmail.vm.dynaDialog.requestDynaDialogInfo(inviteDialogData);
}

function getInviteDynaDialogData()
{
    if (Youmail.vm.inviteDynaDialogData != null) {
      return Youmail.vm.inviteDynaDialogData;
    }
    
    var inviteDialogData = new Youmail.vm.DynaDialogData();
    Youmail.vm.inviteDynaDialogData = inviteDialogData;
    inviteDialogData.dialogWidth = "400px";
    inviteDialogData.setDialogTitle("Invite to YouMail");

    inviteDialogData.getInitRequestInfoUrl = function()
    {
        return "/youmail/vm/inbox/getDialogView.do?dt=invite&" + Youmail.vm.vmNoCacheParam + "&entryId=" + this.entryId + "&pt=" + Youmail.vm.pageType;
    };

    // returns an array of buttons for the dialogs
    inviteDialogData.getDialogButtons = function()
    {
        // default to Submit and Cancel
        return [ { text:"Invite", handler:"Youmail.vm.inviteDynaDialogData.handleSubmit()" }, { text:"Cancel", handler:"Youmail.vm.inviteDynaDialogData.handleCancel()" }
         ];
    };
    

    // after a successful submit to the server, "this" is the dialog
    inviteDialogData.handleSubmitSuccess = function(resp)
    {
        if (resp.validationError) {
            Youmail.vm.inviteDynaDialogData.showVError(resp.errorMsg);
            return false;
        }
        
        if (!resp.successful) {
            if (resp.errorMsg == null || resp.errorMsg == "") {
                alert("Error occurred when servicing request, please try again.");
            } else {
                alert(resp.errorMsg);
            }
            return true;
        }
    
        if (resp.successMsg == null) {
            confirm("Invitation sent.");
        } else {
            confirm(resp.successMsg);
        }
        
        return true;
    };

    // immediately after the dialog is shown to the user
    inviteDialogData.onAfterDialogShow = function()
    {
        var smsInputEl = getChildElementById("smsInputId");
        if (smsInputEl != null) {
            if (smsInputEl.checked) {
                // call this to remove the fields that is not required by sms
                toggleInviteType(3);
            }
        }

        if (getChildElementById("inviteMsgTextId") == null) {
            ymFocus("msgId");
        } else {
            ymFocus("inviteMsgTextId");
        }
    };

    
    // validate, "this" is the dialog
    inviteDialogData.validate = function(formObj) {
        var data = formObj;
        var msg = trimSpaces(data.message.value);
        if (msg != null) {
            var inviteType = "email";
            var it = data.inviteType;
            if (it != null) {
                if (it[1].checked) {
                    inviteType = "text";
                }
                if (it[2].checked) {
                    inviteType = "both";
                }
            }
            if (data.inviteEmail != null && data.inviteEmail.value != null && data.inviteEmail.value.length > 0) {
                inviteType = "email";
            }
            if (data.inviteMobile != null && data.inviteMobile.value != null && data.inviteMobile.value.length > 0) {
                inviteType = "text";
            }
            if (inviteType == "email" && msg.length > 1000) {
                Youmail.vm.curDialogData.showVError("Email body cannot be longer than 1000 characters.");
                return false;
            }
            if ((inviteType == "text" || inviteType == "both")  && msg.length > 64) {
                Youmail.vm.curDialogData.showVError("Text message cannot be longer than 64 characters.");
                return false;
            }
        }
        return true;
    };



    return Youmail.vm.inviteDynaDialogData;
}


// change email or sms message on popup invite dialog
function toggleInviteType(cType)
{
    // 2 = email only
    // 3 = txt only
    // 4 = both
    var emailLabel = getChildElementById("emailLabelId");
    if (emailLabel != null) {
        if (cType == 2 || cType == 4) {
            getChildElementById("emailLabelId").style.display="block";
            getChildElementById("emailInputId").style.display="block";
        } else if (cType == 3) {
            getChildElementById("emailLabelId").style.display="none";
            getChildElementById("emailInputId").style.display="none";
        } else {
            getChildElementById("emailLabelId").style.display="block";
            getChildElementById("emailInputId").style.display="block";
        }
    }
}

// ***************  END Invite Dialog


// ***************  START Ditch Dialog

function openDitchDialog(entryIds)
{
   if (Youmail.vm.dynaDialogActive == true) {
     alert("Another dialog prompt is still up, please wait or refresh this page");
     return;
   }
   
   var message = getDataByEntryId(entryIds);
   var ditchDialogData = getDitchDynaDialogData();
   ditchDialogData.setDialogTitle("Ditch " + message.from);
   ditchDialogData.entryId = entryIds;
   Youmail.vm.dynaDialog.requestDynaDialogInfo(ditchDialogData);
}

function getDitchDynaDialogData()
{
    if (Youmail.vm.ditchDynaDialogData != null) {
      return Youmail.vm.ditchDynaDialogData;
    }
    
    var ditchDialogData = new Youmail.vm.DynaDialogData();
    Youmail.vm.ditchDynaDialogData = ditchDialogData;
    ditchDialogData.dialogWidth = "375px";
    ditchDialogData.setDialogTitle("Confirm");

    ditchDialogData.getInitRequestInfoUrl = function()
    {
        return "/youmail/vm/inbox/getDialogView.do?dt=ditch&" + Youmail.vm.vmNoCacheParam + "&entryId=" + this.entryId + "&pt=" + Youmail.vm.pageType;
    };

    // returns an array of buttons for the dialogs
    ditchDialogData.getDialogButtons = function()
    {
        // default to Submit and Cancel
        return [ { text:"Ditch", handler:"Youmail.vm.ditchDynaDialogData.handleSubmit()" }, { text:"Cancel", handler:"Youmail.vm.ditchDynaDialogData.handleCancel()" }
         ];
    };
    

    // after a successful submit to the server, "this" is the dialog
    ditchDialogData.handleSubmitSuccess = function(resp)
    {
        if (resp.validationError) {
            Youmail.vm.ditchDynaDialogData.showVError(resp.errorMsg);
            return false;
        }
        
        if (!resp.successful) {
            if (resp.errorMsg == null || resp.errorMsg == "") {
                alert("Error occurred when servicing request, please try again.");
            } else {
                alert(resp.errorMsg);
            }
            return true;
        }
    
        if (resp.successMsg == null) {
            confirm("Caller ditched.");
        } else {
            confirm(resp.successMsg);
        }
        return true;
    };

    // immediately after the dialog is shown to the user
    ditchDialogData.onAfterDialogShow = function()
    {
        if (getChildElementById("firstNameId") == null) {
            ymFocus("greetingUIId");
        } else {
            ymFocus("firstNameId");
        }
    };

    
    // validate, "this" is the dialog
    ditchDialogData.validate = function(formObj) {
        return true;
    };

    return Youmail.vm.ditchDynaDialogData;
}


//****************  END Ditch Dialog

// ***************  START Reply Dialog

function openReplyDialog(entryIds, type)
{
   if (Youmail.vm.dynaDialogActive == true) {
     alert("Another dialog prompt is still up, please wait or refresh this page");
     return;
   }
   
   var message = getDataByEntryId(entryIds);
   var replyDialogData = getReplyDynaDialogData();
   replyDialogData.setDialogTitle("Reply to " + message.from);
   replyDialogData.entryId = entryIds;
   replyDialogData.type = type;
   Youmail.vm.dynaDialog.requestDynaDialogInfo(replyDialogData);
}

function getReplyDynaDialogData()
{
    if (Youmail.vm.replyDynaDialogData != null) {
      return Youmail.vm.replyDynaDialogData;
    }
    
    var replyDialogData = new Youmail.vm.DynaDialogData();
    Youmail.vm.replyDynaDialogData = replyDialogData;
    replyDialogData.dialogWidth = "420px";
    replyDialogData.setDialogTitle("Confirm");

    replyDialogData.getInitRequestInfoUrl = function()
    {
        return "/youmail/vm/inbox/getDialogView.do?dt=reply&" + Youmail.vm.vmNoCacheParam + "&entryId=" + this.entryId + "&type=" + this.type + "&pt=" + Youmail.vm.pageType;
    };

    // clean up any resources before dialog is destroyed
    replyDialogData.cleanup = function()
    {
        //alert("new cleanup: " + flashRecorderGetRecordingId());

        // clear the flash stuff, problem in ff
        var replyPlayArea = getChildElementById("replyPlayArea");
        if (replyPlayArea != null) {
            replyPlayArea.innerHTML = null;
            replyPlayArea.style.display = "none";
        }
        
        hideRecorderWindow();
        //showPlayerOnly();
    };

    // immediately after the dialog is shown to the user
    replyDialogData.onAfterDialogShow = function()
    {
        createPlayOpaqueDiv("/mcs/voicemail/message.do?id=" + this.entryId, "no", "replyPlayArea");
        
        toggleReplyType(this.type);

        
        //hidePlayerOnly();

    };

    // returns an array of buttons for the dialogs
    replyDialogData.getDialogButtons = function()
    {
        // default to Submit and Cancel
        return [ { text:"Send", handler:"Youmail.vm.replyDynaDialogData.handleSubmit()" }, { text:"Cancel", handler:"Youmail.vm.replyDynaDialogData.handleCancel()" }
         ];
    };
    

    // after a successful submit to the server, "this" is the dialog
    replyDialogData.handleSubmitSuccess = function(resp)
    {
        if (resp.validationError) {
            Youmail.vm.replyDynaDialogData.showVError(resp.errorMsg);
            return false;
        }
            
        if (!resp.successful) {
            if (resp.errorMsg == null || resp.errorMsg == "") {
                alert("Error occurred when servicing request, please try again.");
            } else {
                alert(resp.errorMsg);
            }
            return true;
        }
    
        var message = getDataByEntryId(Youmail.vm.replyDynaDialogData.entryId);
        if (message != null) {
            message.share = true;
            message.info = "Replied To";
            Youmail.vm.updateRowUIById(Youmail.vm.replyDynaDialogData.entryId);
        }
        getChildElementById("replyPlayArea").style.display = "none";
        
        if (resp.successMsg == null) {
            confirm("Reply sent.");
        } else {
            confirm(resp.successMsg);
        }
        
        if (resp.shouldRefresh) {
            Youmail.vm.inboxTableData.retrieveData(inTb);
        }
        

        return true;
    };

    // validate, "this" is the dialog
    replyDialogData.validate = function(formObj) {
        var data = formObj;
        var msg = trimSpaces(data.message.value);
        var email = trimSpaces(data.email.value);
        var tmpCType = data.tmpCType.value;
        if (tmpCType == "email") {
            if (msg == null) {
               Youmail.vm.replyDynaDialogData.showVError("Please enter a reply message.");
               return false;
            }
           if (!isValidEmail(email)) {
             Youmail.vm.replyDynaDialogData.showVError("Please enter a valid email address.");
             return false;
           }
           if (msg.length > 1000) {
             Youmail.vm.replyDynaDialogData.showVError("Email body cannot be longer than 1000 characters.");
             return false;
           }
           data.message.value = msg;
           data.email.value = email;
        } else if (tmpCType == "sms") {
           if (msg == null) {
              Youmail.vm.replyDynaDialogData.showVError("Please enter a reply message.");
              return false;
           }
           if (msg.length > 128) {
             Youmail.vm.replyDynaDialogData.showVError("Text message cannot be longer than 128 characters.");
             return false;
           }
           data.message.value = msg;
        } else if (tmpCType == "vm") {
            data.recId.value = flashRecorderGetRecordingId();
        } else {
            Youmail.vm.replyDynaDialogData.showVError("Incorrect reply by type");
            return false;
        }
        return true;
    };

    return Youmail.vm.replyDynaDialogData;
}

// change email or sms message on popup reply dialog
function toggleReplyType(cType)
{
    if (cType == null || cType == "email") {
        getChildElementById("tmpCTypeId").value="email";
        getChildElementById("phoneLabelId").style.display="none";
        getChildElementById("phoneInputId").style.display="none";
        getChildElementById("imId").style.display="block";
        getChildElementById("emailLabelId").style.display="block";
        getChildElementById("emailInputId").style.display="block";
        getChildElementById("messageLabelId").style.display="block";
        getChildElementById("messageLabelId").innerHTML = "Email Message:";

        getChildElementById("userId").style.display="none";
        getChildElementById("userInputId").style.display="none";
        getChildElementById("phoneInputVMId").style.display="none";
        getChildElementById("phoneLabelVMId").style.display="none";
        getChildElementById("msgTextAreaId").style.display="block";
        getChildElementById("charCountId").style.display="block";
        getChildElementById("vmLabelId").style.display="none";
        getChildElementById("vmId").style.display="none";

        //getChildElementById("replyMsgId").rows = 10;
        var ccDiv = getChildElementById("ccId");
        if (ccDiv != null) {
            ccDiv.style.display="block";
        }
        var em = getChildElementById("updateEmailId");
        if (em != null) {
            em.style.display="block";
        }
        updateCharCount();

        ymFocus("replyMsgId");
    } else if (cType == "sms") {
        getChildElementById("tmpCTypeId").value = "sms";
        getChildElementById("emailLabelId").style.display="none";
        getChildElementById("emailInputId").style.display="none";
        getChildElementById("imId").style.display="none";
        getChildElementById("messageLabelId").style.display="block";
        getChildElementById("messageLabelId").innerHTML = "Text Message:";
        getChildElementById("phoneLabelId").style.display="block";
        getChildElementById("phoneInputId").style.display="block";
        
        getChildElementById("userId").style.display="none";
        getChildElementById("userInputId").style.display="none";
        getChildElementById("phoneInputVMId").style.display="none";
        getChildElementById("phoneLabelVMId").style.display="none";
        getChildElementById("msgTextAreaId").style.display="block";
        getChildElementById("charCountId").style.display="block";
        getChildElementById("vmLabelId").style.display="none";
        getChildElementById("vmId").style.display="none";
        
        //getChildElementById("replyMsgId").rows = 3;
        var ccDiv = getChildElementById("ccId");
        if (ccDiv != null) {
            ccDiv.style.display="none";
        }
        var em = getChildElementById("updateEmailId");
        if (em != null) {
            em.style.display="none";
        }
        updateCharCount();
        
        ymFocus("replyMsgId");
    } else if (cType == "vm") {
        getChildElementById("tmpCTypeId").value = "vm";
        getChildElementById("emailLabelId").style.display="none";
        getChildElementById("emailInputId").style.display="none";
        getChildElementById("imId").style.display="none";
        getChildElementById("phoneLabelId").style.display="none";
        getChildElementById("phoneInputId").style.display="none";
        getChildElementById("messageLabelId").style.display="none";

        
        getChildElementById("userId").style.display="block";
        getChildElementById("userInputId").style.display="block";
        getChildElementById("phoneInputVMId").style.display="block";
        getChildElementById("phoneLabelVMId").style.display="block";
        getChildElementById("msgTextAreaId").style.display="none";
        getChildElementById("charCountId").style.display="none";
        getChildElementById("vmLabelId").style.display="block";
        getChildElementById("vmId").style.display="block";
        
        //getChildElementById("replyMsgId").rows = 3;
        var ccDiv = getChildElementById("ccId");
        if (ccDiv != null) {
            ccDiv.style.display="none";
        }
        var em = getChildElementById("updateEmailId");
        if (em != null) {
            em.style.display="none";
        }
        ymFocus("replyById");
    }
    
    getChildElementById("tmpCTypeId").value = cType;
}

function updateCharCount()
{
    var msgInput = getChildElementById("replyMsgId");
    var mCount = 0;
    if (msgInput.value != null) {
        mCount = msgInput.value.length;
    }
    var charC = getChildElementById("charCountId");
    var type = getChildElementById("tmpCTypeId").value;
    var countText = "/1000 characters";
    
    if (type != null && type == "sms") {
        countText = "/128 characters";
        charC.innerHTML = mCount + countText;
        if (mCount > 128) {
            charC.style.color = "red";
            charC.title ="";
        } else if (mCount > 64) {
            charC.style.color = "orange";
            charC.title ="Depending on the recipient's carrier, this message MAY be sent via two separate text messages";
        } else {
            charC.style.color = "";
            charC.title = "";
        }
    } else {
        charC.style.color = "";
        charC.title = "";
        charC.innerHTML = mCount + countText;
    }
    
}

// ***************  END reply Dialog


// ***************  START Transcription Dialog

function openTranscriptionDialog(entryIds)
{
  openTranscriptionDialogWithStar(Youmail.vm.star, entryIds);
}

function openTranscriptionDialogWithStar(star, entryIds)
{
   if (Youmail.vm.dynaDialogActive == true) {
     alert("Another dialog prompt is still up, please wait or refresh this page");
     return;
   }
   
   var message = getDataByEntryId(entryIds);
   var transcriptionDialogData = getTranscriptionDynaDialogData();
   transcriptionDialogData.setDialogTitle("Edit Transcript From " + message.from);
   transcriptionDialogData.entryId = entryIds;
   transcriptionDialogData.star = star;
   Youmail.vm.dynaDialog.requestDynaDialogInfo(transcriptionDialogData);
}

function getTranscriptionDynaDialogData()
{
    if (Youmail.vm.transcriptionDynaDialogData != null) {
      return Youmail.vm.transcriptionDynaDialogData;
    }
    
    var transcriptionDialogData = new Youmail.vm.DynaDialogData();
    Youmail.vm.transcriptionDynaDialogData = transcriptionDialogData;
    transcriptionDialogData.dialogWidth = "420px";
    transcriptionDialogData.setDialogTitle("Transcription");

    transcriptionDialogData.getInitRequestInfoUrl = function()
    {
        return "/youmail/vm/inbox/getDialogView.do?dt=trans&" + Youmail.vm.vmNoCacheParam + "&star=" + this.star + "&entryId=" + this.entryId + "&pt=" + Youmail.vm.pageType;
    };

    // clean up any resources before dialog is destroyed
    transcriptionDialogData.cleanup = function()
    {
        // clear the flash stuff, problem in ff
        var transcriptionPlayArea = getChildElementById("transPlayArea");
        if (transcriptionPlayArea != null) {
            transcriptionPlayArea.innerHTML = null;
            transcriptionPlayArea.style.display = "none";
        }
        
        // clean up the menus
        if (Youmail.vm.transMenus != null) {
            var length = Youmail.vm.transMenus.length;
            
            for (var i = length - 1; i >= 0; i--) {
                Youmail.vm.transMenus[i].unsubscribe("render", Youmail.vm.onWordChoiceMenuRender);
                Youmail.vm.transMenus[i].destroy();
            }
            Youmail.vm.transMenus = new Array();
        }
        
        reloadCurrentPreview();
        //showPlayerOnly();
    };

    // immediately after the dialog is shown to the user
    transcriptionDialogData.onAfterDialogShow = function()
    {
        createPlayOpaqueDiv("/mcs/voicemail/message.do?id=" + this.entryId, "no", "transPlayArea");
        
        //hidePlayerOnly();
        transEnableMenu(getChildElementById("transPopupId"));

        //ymFocus("transPlayArea");
    };

    // returns an array of buttons for the dialogs
    transcriptionDialogData.getDialogButtons = function()
    {
        // default to Submit and Cancel
        return [ { text:"Submit", handler:"Youmail.vm.transcriptionDynaDialogData.handleSubmit()" }, { text:"Cancel", handler:"Youmail.vm.transcriptionDynaDialogData.handleCancel()" }
         ];
    };
    

    // after a successful submit to the server, "this" is the dialog
    transcriptionDialogData.handleSubmitSuccess = function(resp)
    {
        if (resp.validationError) {
            Youmail.vm.transcriptionDynaDialogData.showVError(resp.errorMsg);
            return false;
        }

        if (!resp.successful) {
            if (resp.errorMsg == null || resp.errorMsg == "") {
                alert("Error occurred when servicing request, please try again.");
            } else {
                alert(resp.errorMsg);
            }
            return true;
        }
    
        getChildElementById("transPlayArea").style.display = "none";
        
        if (resp.successMsg == null) {
            confirm("Transcription Suggestion Updated.");
        } else {
            confirm(resp.successMsg);
        }
        
        if (resp.shouldRefresh) {
            Youmail.vm.inboxTableData.retrieveData(inTb);
        }
        
        return true;
    };

    // validate, "this" is the dialog
    transcriptionDialogData.validate = function(formObj) {
        var data = formObj;

        if (data.ff.value == "true") {
            if (data.origLength.value != 0 && (data.correction.value == null ||
                                            data.correction.value.length == 0)) {
               Youmail.vm.curDialogData.showVError("Transcription correction can not be empty.");
                return false; 
            }
        } else {
            var words = getChildElementById("wordFormId", getChildElementById("popupFormId"));
            var txt = null;
            if (ie) {
                txt = words.innerText;
            } else {
                txt = words.textContent;
            }
            if (data.origLength.value != 0 && (words == null || txt == null || txt.length == 0)) {
                
                Youmail.vm.curDialogData.showVError("Word count 0, please edit transcription.");
                return false;
            }
            data.byWordCorrection.value = txt;
        }
        
        // validation successful
        return true;
    };

    return Youmail.vm.transcriptionDynaDialogData;
}

function setDialogRating(imgId, star)
{
    var formEl = getChildElementById("popupFormId");
    var newStar = 3;
    if (star == null) {
        if (formEl != null && formEl.star != null) {
            newStar = formEl.star.value;
        }
    } else {
        newStar = star;
    }
    

    var imgEl = getChildElementById(imgId);
    if (imgEl != null) {
        imgEl.src = "http://ymstat.com/web/img/vm/myrating" + newStar + ".gif";
        if (star != null && (formEl != null && formEl.star != null)) {
            formEl.star.value = newStar;
        }
    }
}


function changeTransForm(freeForm)
{
    var formEl = getChildElementById("popupFormId");
    var transEl = getChildElementById("transPopupId");
    formEl.ff.value = freeForm ? "true" : "false" ;
    triggerDisplay(freeForm, "ffHeadId", transEl);
    triggerDisplay(!freeForm, "wordHeadId", transEl);
    triggerDisplay(freeForm, "freeFormId", formEl);
    triggerDisplay(!freeForm, "wordFormId", formEl);
    triggerDisplay(!freeForm, "ratingMsgId", formEl);
    
}

// ***************  END transcription Dialog


// ***************  START Dynamic Dialog

Youmail.vm.DynaDialogData = function()
{
    this.dialogWidth = "420px";
    this.dialogTitle = "Confirm";

    // methods
    this.getInitRequestInfoUrl = getInitRequestInfoUrl;
    this.cleanup = cleanup;
    this.onInitRequestData = onInitRequestData;
    this.onAfterDialogShow = onAfterDialogShow;
    this.getDialogButtons = getDialogButtons;
    this.onCancel = onCancel;
    this.handleSubmit = handleSubmit;
    this.handleCancel = handleCancel;
    this.handleSubmitSuccess = handleSubmitSuccess;
    this.validate = validate;
    this.showVError = showVError;
    this.asyncSubmit = asyncSubmit;
    this.handleSuccessAfterSubmit = handleSuccessAfterSubmit;
    this.handleFailureAfterSubmit = handleFailureAfterSubmit;
    this.setDialogTitle = setDialogTitle;
    
    

    // returns the url of the page containing the ui info
    function getInitRequestInfoUrl()
    {
        // override
        return "";
    }

    // clean up any resources before dialog is destroyed
    function cleanup()
    {
        // override
        //showPlayerOnly();
    }

    // before dialog is displayed, the div of the dialog and the resp from the init request for dialog info
    // returns true to display dialog
    function onInitRequestData(resp, dynaDialogDiv)
    {
        //clearPreview();
        hidePlayerOnly();
        return true;
    }

    // immediately after the dialog is shown to the user
    function onAfterDialogShow()
    {
        // override
    }

    // returns an array of buttons for the dialogs
    function getDialogButtons()
    {
        // default to Submit and Cancel
        return [ { text:"Cancel", handler:"Youmail.vm.curDialogData.handleCancel()" }, { text:"Submit", handler:"Youmail.vm.curDialogData.handleSubmit()" }
         ];
    }
    

    // user selected to close dialog with acting
    function onCancel()
    {
        Youmail.vm.dynaDialog.cleanupDialog(true);
    }

    // default Submit button
    function handleSubmit()
    {
        // get the form object and execute based on it
        
        var popupDiv = document.getElementById(Youmail.vm.dynaDialog.dynaDialogDivId);

        if (popupDiv != null) {
            var formObj = getChildElementById("popupFormId", popupDiv);
            if (formObj != null) {
                if (Youmail.vm.curDialogData.validate(formObj)) {
                    Youmail.vm.curDialogData.asyncSubmit(formObj);
                }
            }
        }
        // the dialog will be closed by async method handleSubmitSuccess()
    }

    // do the actual submission of the forms
    // don't override this
    function asyncSubmit(formObj)
    {
        checkVMValidSession();
        var callback =
        {
            success: handleSuccessAfterSubmit,
            failure: handleFailureAfterSubmit
        }
        YAHOO.util.Connect.setForm(formObj);
        var cObj = YAHOO.util.Connect.asyncRequest('POST', formObj.action, callback);
    }
    
    // default Cancel button
    function handleCancel()
    {
        // "this" is the dialog
        Youmail.vm.dynaDialog.cleanupDialog();
    }

    // after a successful submit to the server, "this" is the dialog
    // return true to close dialog, false to leave dialog up (for validation error, etc)
    function handleSubmitSuccess(resp)
    {
        // override this function
        confirm("Successful");
        return true;
    }

    // validate, "this" is the dialog
    function validate(formObj) {
        // override
        return true;
    }

        // success after the form has been posted
    var handleSuccessAfterSubmit = function(o) {
        var resp = o.responseText;
        if (resp == null) {
            alert("No response from the server");
            Youmail.vm.dynaDialog.cleanupDialog();
            return;
        }
        var ajaxResp = eval('(' + resp + ')');
        if (Youmail.vm.curDialogData.handleSubmitSuccess(ajaxResp)) {
            Youmail.vm.dynaDialog.cleanupDialog();
        }
        // else
        // the popup will still be visible
    };
    
    var handleFailureAfterSubmit = function(o) {
        alert("Unable to contact server");
        Youmail.vm.dynaDialog.cleanupDialog();
    };
    
    function showVError(msg)
    {
        var validationDiv = getChildElementById("vErrorId");
        if (validationDiv != null) {
            validationDiv.style.display = "block";
            validationDiv.innerHTML = msg;
        }
    }
    function setDialogTitle(title)
    {
        if (title.length > 30) {
            title = title.substring(0, 30) + "...";
        }

        this.dialogTitle = title;
    }
}

Youmail.vm.DynaDialog = function() {

    this.dynaDialogContainer = "ymPopup";
    this.dynaDialogDivId = "ymPopupBodyId";
    this.dynaDialogTitleDivId = "ymPopupTitleId";
    this.domDialog = null;

    // methods
    this.requestDynaDialogInfo = requestDynaDialogInfo;
    this.cleanupDialog = cleanupDialog;
    this.handleInitDynaSuccess = handleInitDynaSuccess;
    this.handleInitDynaFailure = handleInitDynaFailure;
    this.dynaDialogDomInit = dynaDialogDomInit;
    
    
function requestDynaDialogInfo(dynaDialogData)
{
    if (Youmail.vm.dynaDialogActive == true) {
      return;
    }
    checkVMValidSession();
    Youmail.vm.setDialogActive(true);
    Youmail.vm.curDialogData = dynaDialogData;
    var callback = { success:handleInitDynaSuccess, failure:handleInitDynaFailure};
    var request = YAHOO.util.Connect.asyncRequest('GET', dynaDialogData.getInitRequestInfoUrl(), callback, null);
}

function cleanupDialog(shouldNotHide)
{
    if (Youmail.vm.curDialogData != null) {
        Youmail.vm.curDialogData.cleanup();
    }

    // remove all the "mask" class
    removeYUIMask();
        
    try {
        if (this.domDialog != null) {
            if (!shouldNotHide) {
                //this.domDialog.destroy();
                
                this.domDialog.hide();
                var dialogDiv = getChildElementById(Youmail.vm.dynaDialog.dynaDialogDivId);
                dialogDiv.innerHTML = null;

            }
        }
    } catch (e) {}

    this.domDialog = null;
    Youmail.vm.curDialogData = null;
    Youmail.vm.setDialogActive(false);

    showPlayerOnly();
    //enableAllAds();
}

var handleInitDynaSuccess = function (o)
{
    trackLater(Youmail.vm.curDialogData.getInitRequestInfoUrl());
    
    //disableAllAds();
    
    if (o != undefined && o.responseText != undefined) {
        var dynaDialogData = Youmail.vm.curDialogData;
        var resp = o.responseText;
        
        if (resp == null || resp.indexOf("dialogsuccess") < 0) {
            if (resp == null) {
                alert("Unable to retrieve information from server.  Please refresh this page.");
            } else {
                alert(resp);
            }
            Youmail.vm.setDialogActive(false);
            return;
        }
        var dynaDialogDiv = getChildElementById(Youmail.vm.dynaDialog.dynaDialogContainer);
        dynaDialogDiv.style.display = "block";


        if (dynaDialogData.onInitRequestData(resp, dynaDialogDiv)) {
            // Render the Dialog
            dynaDialogDomInit(resp + "<div id='vErrorId' class='errorMessage' style='display:none; padding-top:5px'></div>");

            //Youmail.vm.dynaDialog.domDialog.render();
            Youmail.vm.dynaDialog.domDialog.show();
            dynaDialogData.onAfterDialogShow();
            hidePlayerOnly();
            hidePlayerOnlyLater();  // crazy delay bug

            fixFFCursorBug();
        } else {
            Youmail.vm.setDialogActive(false);
        }
        
        var loadingImgDiv = getChildElementById("dialogLoadingImgId");
        if (loadingImgDiv != null) {
            loadingImgDiv.style.display = "none";
        }
        
    } else {
        Youmail.vm.setDialogActive(false);
        alert("Unable to retrieve information from the server.  Please refresh this page.");
    }
}

var handleInitDynaFailure = function(o)
{
    Youmail.vm.setDialogActive(false);
    alert("Error when contacting server, please try again.");
}

function dynaDialogDomInit(theBody) {

    // success after the form has been posted
    var handleSuccess = function(o) {
        var resp = o.responseText;
        if (resp == null) {
            alert("No response from the server");
        }
        var ajaxResp = eval('(' + resp + ')');
        if (Youmail.vm.curDialogData.handleSubmitSuccess(ajaxResp)) {
            Youmail.vm.dynaDialog.cleanupDialog();
        } else {
            window.setTimeout("Youmail.vm.dynaDialog.domDialog.show()", 600);
        }
    };
    
    var handleFailure = function(o) {
        alert("Unable to contact server");
        Youmail.vm.dynaDialog.cleanupDialog();
    };

    //getChildElementById(Youmail.vm.dynaDialog.dynaDialogTitleDivId).innerHTML = Youmail.vm.curDialogData.dialogTitle;

    // adds the buttonsLoading ...
    theBody += '<div style="width: 100%; margin-top:10px" align="right">';
    var buttons = Youmail.vm.curDialogData.getDialogButtons();
    for (var i = buttons.length - 1; i >= 0; i--) {
        var b = buttons[i];
        theBody += '<div style="float: right; padding-right:10px"><span id="pub' + i + '" class="ymDefault yui-button yui-push-button"><span class="first-child"><button type="button" onclick="' + b.handler + '">' + b.text + '</button></span></span></div>';
    }
    theBody += '<div style="clear:both"></div><div style="clear:both"></div>';
    var dialogDiv = getChildElementById(Youmail.vm.dynaDialog.dynaDialogDivId);
    dialogDiv.innerHTML = theBody;
    var dialogTitleDiv = getChildElementById(Youmail.vm.dynaDialog.dynaDialogTitleDivId);
    dialogTitleDiv.innerHTML = Youmail.vm.curDialogData.dialogTitle;

    
    // Instantiate the Dialog, and render the "ymPopup" html
    
    Youmail.vm.dynaDialog.domDialog = null;
    if (Youmail.vm.sharedPage == true) {
        Youmail.vm.dynaDialog.domDialog = new YAHOO.widget.Panel("ymPopup", 
                                                    { width : Youmail.vm.curDialogData.dialogWidth,
                                                      modal : true,
                                                      draggable:true, 
                                                      fixedcenter : true,
                                                      visible : false,
                                                      iframe: true,
                                                      underlay: "none",
                                                      constraintoviewport : true
                                                     } );
    } else {
        Youmail.vm.dynaDialog.domDialog = new YAHOO.widget.Panel("ymPopup", 
                                                    { width : Youmail.vm.curDialogData.dialogWidth,
                                                      modal : true,
                                                      draggable:true, 
                                                      fixedcenter : true,
                                                      visible : false,
                                                      underlay: "none",
                                                      iframe: true,
                                                      constraintoviewport : true,
                                                      effect:{effect:YAHOO.widget.ContainerEffect.FADE,duration:0.5}
                                                     } );
    }

    Youmail.vm.dynaDialog.domDialog.beforeHideEvent.subscribe(Youmail.vm.curDialogData.onCancel);
    Youmail.vm.dynaDialog.domDialog.render();
    
}

}

Youmail.vm.setDialogActive = function(isActive)
{
    Youmail.vm.dynaDialogActive = isActive;
    var loadingImgDiv = getChildElementById("dialogLoadingImgId");
    if (loadingImgDiv != null) {
        loadingImgDiv.style.display = isActive ? "block" : "none";
    }
}

// **********  END DYNAMIC DIALOG *********

// util method

function fixFFCursorBug(docParent)
{
    if (docParent == null) {
        docParent = document;
    }
    var wrapper = getChildElementById("ymPopup", docParent);
    if (wrapper != null) {
        wrapper.style.display   = "none";
        wrapper.style.overflow  = "auto";
        wrapper.style.display = "block";
    }
}

function removeYUIMask()
{
    // remove the mask, can't seem to reuse the same yui mask
    var bodyEl = document.getElementsByTagName("body");
    if (bodyEl != null) {
        var parent = bodyEl.item(0);
        var allMyChildren = parent.childNodes;
        var len = allMyChildren.length;
        for (var i = len - 1; i >= 0; i--) {
            var aNode = allMyChildren.item(i);
            if (aNode.className == "mask") {
                parent.removeChild(aNode);
            } else if (aNode.nodeName.toLowerCase() == "iframe" && (aNode.id == null || aNode.id == "")) {
                parent.removeChild(aNode);
            }
        }
    }
    
    // remove the div on below the popup, yui doesn't remove this either
    var container = getChildElementById("ymPopup_c");
    // the container contains the popup, so we need to move out and kill the containter
    if (container != null) {
        var popup = getChildElementById("ymPopup", container);
        if (popup != null) {
            container.removeChild(popup);
            container.parentNode.appendChild(popup);
            container.parentNode.removeChild(container);
            popup.style.display = "none";
        }
    }
    
    

}




/*
*/

function transEnableMenu(popupEl) {


    var menuEl = getChildElementById("menuId", popupEl);
    if (menuEl == null) {
        return;
    }
    var menu = menuEl.innerHTML;
    var all = eval('(' + menu + ')');

        
    var length = all.length;
    Youmail.vm.transMenus = new Array(length);
    
    for (var i = 0; i < length; i++) {

        var wordChoiceMenu = new YAHOO.widget.ContextMenu(
                                        "trWordMenu_" + i,
                                        {
                                            trigger: all[i].wId,
                                            itemdata: all[i].menu,
                                            lazyload: true
                                        }
                                    );
        wordChoiceMenu.subscribe("render", Youmail.vm.onWordChoiceMenuRender);
        Youmail.vm.transMenus[i] = wordChoiceMenu;
    }
    


    function wordChosen(p_sType, p_aArgs, p_sWord)
    {
      var menu = this.parent;
      var wordEl = menu.contextEventTarget;
      wordEl.className = "low changed";
      wordEl.innerHTML = p_sWord;

      //alert("this.value: " + this.value + "; parent: " + menu + "; active: " + p_sWord + "; trigger: " + menu.contextEventTarget);
    }

};


Youmail.vm.onWordChoiceMenuRender = function()
{

    // increase the zindex so that it will appear on top of popup
    var bodyEl = document.getElementsByTagName("body");
    if (bodyEl != null) {
        var parent = bodyEl.item(0);
        var allMyChildren = parent.childNodes;
        var len = allMyChildren.length;
        for (var i = len - 1; i >= 0; i--) {
            var aNode = allMyChildren.item(i);
            if (aNode.id != null && aNode.id.startsWith("trWordMenu_")) {
                  aNode.style.zIndex = 10;
            }
        }
    }
};


// crazy delay bug
function hidePlayerOnlyLater()
{
    window.setTimeout("hidePlayerOnly()", 500);
}


function ymFocus(elId)
{
    try {
        getChildElementById(elId).focus();
        window.setTimeout("getChildElementById(\"" + elId + "\").focus();", 100); // firefox bug
    } catch (e) {}
}
