/**
 * Defines the namespace for the preferences UI panel
 */
function ResultsEmailerUI()
{
}

/**
 * Strings used for the interface
 */
ResultsEmailerUI.sendButtonText = "sendButtonText";
ResultsEmailerUI.cancelButtonText = "cancelButtonText";
ResultsEmailerUI.successTitle = "successTitle";
ResultsEmailerUI.successMessage = "successMessage";

/**
 * The settable width of the dialog box.
 */
ResultsEmailerUI.width = "<<uninitialized>>";

/**
 * The preferences dialog
 */
ResultsEmailerUI.resultsEmailerDialog = null;

/**
 * Shows the email results dialog box for the clippings page
 */
ResultsEmailerUI.showDialogForMarkList = function(ssid)
{
    ResultsEmailerUI.showDialog("CLIPPINGS", ssid, "", "", "RANK", "");
};

/**
 * Shows the email results dialog box for the search results
 */
ResultsEmailerUI.showDialogForSearchResults = function(ssid, resultPane, snapshotIndex, resultSortField, collectionCode, searchUrl)
{
    ResultsEmailerUI.showDialog("SEARCH", ssid, resultPane, snapshotIndex, resultSortField, collectionCode, searchUrl);
};

/**
 * Shows the dialog after it has been initialized
 */
ResultsEmailerUI.showDialog = function(emailerType, ssid, resultPane, snapshotIndex, resultSortField, collectionCode)
{

    var pathname = location.pathname.slice(1);

    document.getElementById('emailerType').value = emailerType;
    document.getElementById("ssid").value = ssid;
    document.getElementById("resultPane").value = resultPane;
    document.getElementById("snapshotIndex").value = snapshotIndex;
    document.getElementById("resultSortField").value = resultSortField;
    document.getElementById("collectionCode").value = collectionCode;
    document.getElementById("searchUrl").value = pathname.slice(pathname.indexOf("/"));

    document.getElementById("numResultsDiv").style.display = (emailerType == "SEARCH") ? "" : "none";
    document.getElementById("urlOnlyDiv").style.display = (emailerType == "SEARCH") ? "" : "none";

    if (ResultsEmailerUI.resultsEmailerDialog === null)
    {
        ResultsEmailerUI.initDialog();
    }
    document.getElementById("message").value = "";
    document.getElementById("toAddress").value = "";
    document.getElementById("senderName").value = "";

    ResultsEmailerUI.resultsEmailerDialog.show();
};

/**
 * The error handler for the call to setUserPrefernces
 * @param message the error message
 * @param info the info object which contains additional information about the error.
 */
ResultsEmailerUI.sendEmail_errorHandler = function(message, info)
{
    //DWRHelper.displayDWRError("ResultsEmailerUI.sendEmail", message, info);
    MessageBox.error("Error", message);
};

/**
 * The callback function to the call to sendEmail. Does nothing.
 */
ResultsEmailerUI.sendEmail_callback = function()
{
    MessageBox.info(ResultsEmailerUI.successTitle, ResultsEmailerUI.successMessage);
};

/**
 * The DWR callback object which calls into PreferecnesConnector.sendEmail
 */
ResultsEmailerUI.sendEmailDWRObject = {
    callback:     ResultsEmailerUI.sendEmail_callback,
    errorHandler: ResultsEmailerUI.sendEmail_errorHandler,
    timeout:      DWRHelper.ajaxTimeout
};

/**
 * The method called to initialize the prefernces ui object
 */
ResultsEmailerUI.initDialog = function()
{
    document.getElementById("resultsEmailerDialog").style.display = "";

    // Define various event handlers for Dialog
    var handleSubmit = function()
    {

        if (!ValidationUtils.validateRequiredElement("toAddress", ValidationUtils.emailAddress))
        {
        }
        else if (!ValidationUtils.validateEmailByEleId("toAddress"))
        {
        }
        else if (!ValidationUtils.validateRequiredElement("senderName", ValidationUtils.senderName))
        {
        }
        else
        {
            var urlOnly = false;
            if(document.getElementById("sendUrlOnly").checked)
            {
                urlOnly = true;
            }

            // Create a result emailer object
           var resultsEmailerCommand = {
                resultPane      : document.getElementById("resultPane").value,
                snapshotIndex   : document.getElementById("snapshotIndex").value,
                resultSortField : document.getElementById("resultSortField").value,
                collectionCode  : document.getElementById("collectionCode").value,
                senderName      : document.getElementById("senderName").value,
                toAddress       : document.getElementById("toAddress").value,
                message         : document.getElementById("message").value,
                numResults      : document.getElementById("numResults").value,
                emailFormat     : document.getElementById("emailFormat").value,
                ssid            : document.getElementById("ssid").value,
                emailerType     : document.getElementById("emailerType").value,
                searchUrl       : document.getElementById("searchUrl").value,
                sendUrlOnly     : urlOnly
            };

            //If this is null then the user is only emailing the url
            if(typeof resultsEmailerCommand.numResults == "undefined" || typeof resultsEmailerCommand.numResults === null)
            {
                resultsEmailerCommand.numResults = 0;
            }

            ResultsEmailerConnector.sendEmail(resultsEmailerCommand, ResultsEmailerUI.sendEmailDWRObject);
            this.cancel();
        }
    };

    var handleCancel = function()
    {
        this.cancel();
    };

    // Instantiate the Dialog
    ResultsEmailerUI.resultsEmailerDialog = new YAHOO.widget.Dialog("resultsEmailerDialog",
    { width : ResultsEmailerUI.width,
        fixedcenter : true,
        visible : false,
        constraintoviewport : true,
        modal:true,
        buttons : [
            { text:ResultsEmailerUI.cancelButtonText, handler:handleCancel },
            { text:ResultsEmailerUI.sendButtonText,   handler:handleSubmit, isDefault:true }
        ]
    });

    YahooDlgHelper.attachKeyEventHandlers(ResultsEmailerUI.resultsEmailerDialog, ResultsEmailerUI.resultsEmailerDialog.cancel);
    YahooDlgHelper.enableFocusOnShow(ResultsEmailerUI.resultsEmailerDialog);

    // Render the Dialog
    ResultsEmailerUI.resultsEmailerDialog.render();
};

/**
 * Handles disabling of the numResults select when sendUrlOnly is checked
 * @param checkbox the sendUrlOnly checkbox
 */
ResultsEmailerUI.toggleNumResultsDisplay = function(checkbox)
{
    var numResultsEle =  document.getElementById("numResults");
    var numResultsLblEle = document.getElementById("numResultsLbl");

    if(checkbox.checked)
    {
        numResultsEle.selectedIndex = -1;
        numResultsEle.disabled = true;
        numResultsLblEle.style.color = "#CCCCCC";

    }
    else
    {

        numResultsEle.selectedIndex = 0;
        numResultsEle.disabled = false;
        numResultsLblEle.style.color = "#000000";
    }
};
