﻿function pageLoaded() {
    var base_url = document.getElementById('ctl00_MainContent_ContentPath');
    if (base_url) {
        if (base_url.value.length > 0) {
            tinyMCE.settings['document_base_url'] = base_url.value;
        }
    }
}

function ConfirmAction() {
    confirmed = true;

    // how many documents are selected
    var checkedDocuments = CountSelectedDocuments();

    // which action is it
    var action = GetAction();

    // check that the action is valid for the number of selected documents
    if (checkedDocuments == 0) {
        if (action != 'Append') {
            if (action != 'Remove') {
                window.alert('Select a document and click Apply again.');
                confirmed = false;
            }
            else {
                window.alert('Select one or more documents and click Apply again.');
                confirmed = false;
            }
        }
    }
    else {
        if (action == 'Remove') {
            // user is allowed to Remove any number of documents at a time
            var singleOrPluralDocument = ' document';
            if (checkedDocuments > 1) { singleOrPluralDocument = singleOrPluralDocument + 's'; }
            confirmed = window.confirm('Please confirm that you want to remove the selected ' + checkedDocuments + singleOrPluralDocument + '.');
        }
        else {
            if (checkedDocuments > 1 && action != 'Append') {
                window.alert('For the requested action (' + action + ') you should select only one document.');
                confirmed = false;
            }
            if (action == "Rename") {
                window.confirm("show window");
                var newName = window.prompt("Enter the new name", "default string");
            }
        }
    }
    return confirmed;
}

function CountSelectedDocuments() {
    var checkedDocumentCount = 0;

    // get the form
    var bookView = document.forms[0];

    // if Tab3 available
    if (bookView) {
        for (i = 0; i < bookView.elements.length; i++) {
            if (bookView.elements[i].type == "checkbox") {
                if (bookView.elements[i].id != 'SelectDeselectAll'
                            && bookView.elements[i].id != 'chkOrganise') {
                    if (bookView.elements[i].checked) {
                        checkedDocumentCount++;
                    }
                }
            }
        }
    }
    else {
        window.alert('element not found');
    }
    return checkedDocumentCount;
}
function GetAction() {
    // find the control
    var action = document.getElementById('ctl00_MainContent_ddlAction');

    if (action) {
        // for each option
        for (i = 0; i < action.all.length; i++) {
            // if it's selected
            if (action[i].selected) {
                return action[i].value;
            }
        }
    }
    else {
        window.alert('ddlAction not found');
    }
    return '';
}
function SelectDeselectAll() {
    // get the form
    var bookView = document.forms[0];
    var controllingCheckBox = document.getElementById('SelectDeselect');

    // if the form is available
    if (bookView) {
        if (controllingCheckBox) {
            for (i = 0; i < bookView.elements.length; i++) {
                if (bookView.elements[i].type == "checkbox") {
                    if (bookView.elements[i].id != 'SelectDeselect') {
                        bookView.elements[i].checked = controllingCheckBox.checked;
                    }
                }
            }
        }
        else {
            window.alert('checkbox SelectDeselectAll not found.');
        }
    }
    else {
        window.alert('element not found');
    }
    return false;
}

