
var hasCMSLicense;
var hasAnalyticsLicense;
var hasCommerceLicense;
var hasMarketierLicense; 

function JSSavePage() {
    iapps_container_contentChanged_ShowAlert = false;
    // __doPostBack('__Page', 'SavePageAsDraft');
    document.getElementById("hfSaveAsDraft").value = "True";
    document.forms["form1"].submit();
}

var flashEvents = "";
function _uFlash() {
    var f = "-", n = navigator;
    if (n.plugins && n.plugins.length) {
        for (var ii = 0; ii < n.plugins.length; ii++) {
            if (n.plugins[ii].name.indexOf('Shockwave Flash') != -1) {
                f = n.plugins[ii].description.split('Shockwave Flash ')[1];
                break;
            }
        }
    }
    else if (window.ActiveXObject) {
        for (var ii = 10; ii >= 2; ii--) {
            try {
                var fl = eval("new ActiveXObject('ShockwaveFlash.ShockwaveFlash." + ii + "');");
                if (fl) {
                    f = ii + '.0';
                    break;
                }
            }
            catch (e) {
            }
        }
    }
    return f;
}
function _uVoid() { return; }
function _uUnloadEvent() { return; }

function GlobalNavItemSelect(sender, eventArgs) {
    if (eventArgs.get_item().get_id() == "GoToHome" || eventArgs.get_item().get_id() == "ViewPageHistory" || eventArgs.get_item().get_id() == "ViewEditProperties") {
        SendTokenRequest(sender, eventArgs);
    }
    if (eventArgs.get_item().get_id() == "DisplayPageNote") {
        ShowHidePageNote();

        eventArgs.set_cancel(true);
    }
    if (eventArgs.get_item().get_id() == "SaveAsDraft" || eventArgs.get_item().get_id() == "Publish" || eventArgs.get_item().get_id() == "Approve" || eventArgs.get_item().get_id() == "SubmitToWorkflow" || eventArgs.get_item().get_id() == "Reject") {
        iapps_container_contentChanged_ShowAlert = false;
        for (var i = 0; i < arrRequiredContainer.length; i++) {
            if (arrRequiredContainer[i][1] == '0') {
                alert("Please insert content for all the required text containers.");

                eventArgs.set_cancel(true);
                break;
            }
        }
    }
    if (eventArgs.get_item().get_id() == "AddToRssChannel") {
        eventArgs.set_cancel(true);
    }

    var id = eventArgs.get_item().get_id();
    switch (id) {
        case 'Analytics':
            if (!hasAnalyticsLicense || !hasAnalyticsPermission) {
                //call popup to display message
                OpenLicenseWarningPopup(id, hasAnalyticsLicense, hasAnalyticsPermission);
                eventArgs.set_cancel(true);
            }
            break;
        case "Commerce":
            if (!hasCommerceLicense || !hasCommercePermission) {
                OpenLicenseWarningPopup(id, hasCommerceLicense, hasCommercePermission);
                eventArgs.set_cancel(true);
            }
            break;
        case "Marketier":
            if (!hasMarketierLicense || !hasMarketierPermission) {
                OpenLicenseWarningPopup(id, hasMarketierLicense, hasMarketierPermission);
                eventArgs.set_cancel(true);
            }
            break;
        case "CMS":
            if (!hasCMSLicense || !hasCMSPermission) {
                OpenLicenseWarningPopup(id, hasCMSLicense, hasCMSPermission);
                eventArgs.set_cancel(true);
            }
            break;            
    }    
    
}
var oureditor;
var srcControl;
function ContentSaveAs() {
    NewContent = true;
    HideEditorToolbar();
    OnClientSubmit(oureditor);
}

function ContentSave() {
    HideEditorToolbar();
    OnClientSubmit(oureditor);
}

function ContentCancel() {
    HideEditorToolbar();
    OnClientCancel(oureditor);
}

var editorTools;

function OnClientLoad(editor, args) {
    oureditor = editor;
    document.getElementById("editableArea").style.display = "none";

    editorTools = editor.get_toolAdapter().get_tools();

    var toolbarWindow = editor.get_toolAdapter().get_window();
    toolbarWindow.set_title("iAPPS Site Editor Toolbar");

    setTimeout("PopulateSnippets();", 250);
}

function OnClientModeChange(editor, args) {
    var mode = editor.get_mode();
    switch (mode) {
        case 1:
            //design mode
            ToggleFloatingToolbar(editor);
            break;
        case 2:
            //html mode
        case 4:
            //preview mode
            HideEditorToolbar();
            break;
    }
}

function OnClientCommandExecuting(editor, args) {
    var name = args.get_name();
    var val = args.get_value();

    if (name == "MailMerge") {
        oureditor.pasteHtml('[Field:' + val + ']')
        //Cancel the further execution of the command as such a command does not exist in the editor command list
        args.set_cancel(true);
    }
}

function OnClientSubmit(editor) {
    srcControl.style.visibility = "visible";
    var actualText;
    if (Data.Container.getAttribute("maxCharacter") != "-1") {
        actualText = oureditor.get_html(true).substring(0, Data.Container.getAttribute("maxCharacter"));
    }
    else {
        actualText = oureditor.get_html(true);
    }

    if (actualText == '')
        actualText = '&nbsp;';

    var pattern = "^<span class=\"?" + Data.Container.className + "\"?>((.|\\n|\\r)+?)</span>$";
    var objRE = new RegExp(pattern, "i");

    if (objRE.test(actualText)) {
        outStr = actualText.match(objRE);
        actualText = outStr[1];
    }

    srcControl.innerHTML = actualText;
    document.getElementById("editableArea").style.display = "none";
    //To update the Content operation and IsModified property of the container
    Data.HiddenControlValue.value = actualText;
    if (NewContent) {
        selectDirectory();
        setContainerOperation(Data, "NewContent");
        var strContainerId = Data.Container.getAttribute("FWContainerID");
        for (var i = 0; i < arrRequiredContainer.length; i++) {
            if (arrRequiredContainer[i][0] == strContainerId) {
                arrRequiredContainer[i][1] = '1';
            }
        }
    }
    else {
        if (Data.HiddenContentOperation.value != "NewContent") {
            setContainerOperation(Data, "Modified");
            //added for commerce  done on 06/08/2009 *
            if (Data.Container.getAttribute("FWContainerType") == "FWProductPropertyControl")
                callbackForAutoSave.callback();
            //added for commerce  done on 06/08/2009 *

        }
    }

    NewContent = false;
    
    eval(flashEvents);
    return false;
}

function OpenLicenseWarningPopup(productName, hasLicenese, hasPermission) {
    var url = "..";
    if (jAdminSiteUrl != undefined && jAdminSiteUrl != null && jAdminSiteUrl != 'undefined' && jAdminSiteUrl != '')
        url = jAdminSiteUrl;
    url += "/Popups/LicenseWarning.aspx?ProductName=" + productName + "&HasLicense=" + hasLicenese + "&HasPermission=" + hasPermission;
    licenseWarningWindow = dhtmlmodal.open('LicenseWarning', 'iframe', url, 'License Warning', 'width=425px,height=400px,center=1,resize=0,scrolling=1');
    licenseWarningWindow.onclose = function() {
        var a = document.getElementById('LicenseWarning');
        var ifr = a.getElementsByTagName("iframe");
        window.frames[ifr[0].name].location.replace("/blank.html");
        return true;
    }
}

