
function showAllowActionDialog(obj) {

    mytime = null;

    if (isLogin() || _later) return false;

    obj = (obj == undefined) ? new Object() : obj;
    var action = obj.action, arg = obj.arg, always = (obj.always == undefined) ? false : obj.always;

    if (_allowActionDialog == null) {
        var innerHtml = "<div id='unregDlgBox' class='wizardBox clearfix'><h3>Please Sign Up or Sign In</h3>" +
            "<div id='unregDlgContent' class='clearfix'>Subscribe for FREE to access 30-minute delayed quotes and a custom myFinancials page." +
            "  Paid subscribers can also download 3-years of daily data for thousands of instruments." +
           "</div><div class='DlgButtonsBox clearfix'>" +
            "<a href=\"javascript:allowActionLater('" + action + "');\" class='unregDlgLater'></a>" +
            "<a href='login.aspx' class='unregDlgSignIn'></a>" +
           "<a href='register.aspx' class='unregDlgSignUp'></a>" +
            "</div></div>";

        _allowActionDialog = new dijit.Dialog({ title: "", content: innerHtml });

        _allowActionDialog.titleBar.style.display = "none";
        _allowActionDialog.refocus = false;

        // set fixed dialog size. This doesn't work via css!
        dojo.byId('unregDlgBox').style.width = 390 + "px";
        dojo.byId('unregDlgBox').style.height = 240 + "px";
    }

    _arg = arg;

    _allowActionDialog.startup();
    _allowActionDialog.show();

    // panel creates hidden fix
    window.scrollBy(0, 1);
    //window.scrollBy(0, -1);

    return true;
}


function showUpgradeDialog(obj) {

    if (isPaid() || _later) return false;

    obj = (obj == undefined) ? new Object() : obj;
    var action = obj.action, arg = obj.arg, always = (obj.always == undefined) ? false : obj.always;

    if (_upgradeDialog == null) {
        var innerHtml = "<div id='upgradeDlgBox' class='wizardBox clearfix'><h3>Please upgrade your account</h3>" +
            "<div id='upgradeDlgContent'> If you want to perform this action you have to upgrade your account. Press Upgrade if you want to do it now, otherwise click Later to skip the action." +
            "</div><div class='DlgButtonsBox clearfix'>" +

            "<a href=\"javascript:upgradeLater('" + action + "');\" class='upgradeDlgLater'></a>" +
            "<a href='subscription.aspx' class='upgradeDlgUpgrade'></a>" +

            "</div></div>";

        _upgradeDialog = new dijit.Dialog({ title: "", content: innerHtml });

        _upgradeDialog.titleBar.style.display = "none";
        _upgradeDialog.refocus = false;

        // set fixed dialog size. This doesn't work via css!
        dojo.byId('upgradeDlgBox').style.width = 390 + "px";
        dojo.byId('upgradeDlgBox').style.height = 240 + "px";

        //dojo.byId('allowActionHeader').style.width = 420 + "px";
    }

    _arg = arg;

    _upgradeDialog.startup();
    _upgradeDialog.show();

    // panel creates hidden fix
    window.scrollBy(0, 1);
    //window.scrollBy(0, -1);

    return true;
}


var _arg, _noAction = false;

function allowActionLater(action) {
    _allowActionDialog.hide();

    _later = true;

    if (!_noAction) {

        switch (action) {
            case "addPanel":
                addPanelDialog();
                break;
            case "editPanel":
                editDialog(_arg);
                break;
            case "deletePanel":
                deletePanel(_arg);
                break;
        }
    }

    dojo.query(".mousePointer").forEach(function(node, index, arr) {
        node.className = "mouseHolder";
        node.style.display = "none";
    });
}

function upgradeLater(action) {
    _upgradeDialog.hide();

    if (!isLogin()) return;

    _later = true;

    if (action == "addPanel") {
        addPanelDialog();
    }

    dojo.query(".mousePointer").forEach(function(node, index, arr) {
        node.className = "mouseHolder";
        node.style.display = "none";
    });
}

function checkAndAddPanel() {
    if (showAllowActionDialog({ action: "addPanel" })) return;

    if (showUpgradeDialog({ action: "addPanel" })) return;

    addPanelDialog();
}

function addPanelDialog() {

    createWizard();

    _wizard.startup();

    resetWizard(null);

    _wizard.show();
}


function editDialog(tfid) {

    if (showAllowActionDialog({ action: "editPanel", arg: tfid })) return;

    createWizard();

    _wizard.startup();

    resetWizard(tfid);

    _wizard.show();
}

var _cnt = 0, mytime = null;

function showLeaveDialog() {

    if (dojo.isIE) {
        event.cancelBubble = true;
        if (!isPaid() && !isLogin() && _later) {
            _cnt++;

            if (_cnt % 9 == 0) {
                _later = false;
                _noAction = true;
                showAllowActionDialog();
            }
        }
    } else // if(dojo.isFF || dojo.isSafari)
    {
        if (!_later) {
            event.cancelBubble = true;
            return;
        }
        else {
            _later = false;
            _noAction = true;
            
            mytime = setTimeout('CallLater()', 1000); 

            return "Your changes will be lost if you don't login or register.";
        }
    }
}

function CallLater()
{
    mytime = null;
    mytime = setTimeout('showAllowActionDialog()', 1000); 
}

function hideLeaveDialog() {
    _later = true;
        
    if(_allowActionDialog && _allowActionDialog.open) {
        _allowActionDialog.hide();
        _allowActionDialog.destroy();
    }
}

function noConfirmClick(url) {
    window.onbeforeunload = null;
    window.location = url;
    return false;
}

function openFeedbackDialogBox(linkElement) {
    var feedbackDialogBox = dojo.byId("feedbackDialogBox");
    if (feedbackDialogBox) {
        feedbackDialogBox.style.display = "block";
        return false;
    }

    var dialogHTML = new String();

    dialogHTML += '<div class="feedbackContentBg"></div>' +
        '<div id="feedbackContentBox">' +
        '<p>Comments/Questions about this site:</p>' +
        '<textarea id="feedbackComment"></textarea>' +
        '<p>Your email address:</p>' +
        '<input id="feedbackEmail" type="text" maxlength="256"/>' +
        '<div id="feedbackButtonsBox" class="clearfix">' +
        '<a href="javascript:closeFeedbackDialogBox();" id="feedbackClose" ></a>' +
        '<a href="javascript:processFeedback();" id="feedbackSend" ></a>' +
        '</div></div>';

    var feedbackDialogBox = document.createElement("div");
    feedbackDialogBox.id = "feedbackDialogBox";
    feedbackDialogBox.innerHTML = dialogHTML;

    //feedbackDialogBox.style.left = (getX(linkElement) + 100)+"px";
    //feedbackDialogBox.style.top = (getY(linkElement) + 20)+"px";

    feedbackDialogBox.style.left = (getX(dojo.byId("mainContentBox")) + 5) + "px";
    feedbackDialogBox.style.top = "440px";

    document.body.appendChild(feedbackDialogBox);

    return false;
}

function closeFeedbackDialogBox() {
    var feedbackDialogBox = dojo.byId("feedbackDialogBox");
    if (feedbackDialogBox) {
        document.body.removeChild(feedbackDialogBox);
    }
}

function processFeedback() {
    var isError = false, errorStr = "", emailValue, msgValue;

    emailValue = safeString(dojo.byId("feedbackEmail").value);
    msgValue = safeString(dojo.byId("feedbackComment").value);

    var re = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

    if (emailValue.length == 0 || !re.test(emailValue)) {
        errorStr += "- Invalid Email. Please enter a valid Email address.\n"
        isError = true;
    }
    if (msgValue.length == 0) {
        errorStr += "- Comments field is empty. Please enter Comments before sending email.\n"
        isError = true;
    }

    if (isError) alert(errorStr);
    else {
        sendFeedback(emailValue, msgValue);
        closeFeedbackDialogBox();
        alert("Message sent -?thank you.")
    }
}