﻿// IMCA CMID Website JavaScript Library


// Go to anchor function
function goToAnchor(pageName, anchorName) {
    location.href = pageName + "#" + anchorName;
}

// Finds a server control on the client side by id
function findObjWithClientId(Id) {
    var ctrls = document.getElementsByTagName("body")[0].getElementsByTagName("*");
    for (var count = 0; count < ctrls.length; count++) {
        var index = ctrls[count].id.indexOf(Id);
        if (index != -1) {
            if ((ctrls[count].id.length - index) == Id.length) {
                return ctrls[count];
            }
        }
    }
    return null;
}

// Checks for enter key and forces a postback to occur with the target event argument to be the same as the actionName parameter
function performPostbackOnEnter(e, actionName) {
    var characterCode;

    if (e && e.which) {
        e = e;
        characterCode = e.which;
    }
    else {
        e = event;
        characterCode = e.keyCode;
    }

    if (characterCode == 13) {
        __doPostBack('__Page', actionName);
        return false;
    }
    else {
        return true;
    }
}


// Submit Postback routine
// (causes a postback for page load to pick up)
function submitPostback(actionName) {
    __doPostBack('__Page', actionName);
}

// Confirm delete function
function confirmDelete(name) {
    return confirm("Are you sure you want to delete this " + name + "?");
}

// Confirm publish function
function confirmPublish(newTitleTextBoxId, nextIssueNumber, issueDate) {
    var newTitleTextBox = findObjWithClientId(newTitleTextBoxId);
    var newTitle = newTitleTextBox.value;
    return confirm("You have chosen to publish a new version of the Common Marine Inspection Document.\n\nTitle: " + newTitle + "\nIssue: " + nextIssueNumber + "\nDate: " + issueDate + "\n\nClick OK if you wish to continue with the publication or Cancel to abort.");
}

// Confirm delete function
function confirmOrganisationSelect() {
    return confirm("This company will be added to the list of organisations with access to this vessel's CMIDs.");
}

// Confirm remove function
function confirmCmidAccessOrganisationRemove(accessText) {
    if (accessText == 'Single Vessel') {
        return confirm("The selected Organisations's access to this vessels CMIDs will be removed.\n\nClick OK to continue.");
    }
    else {
        return confirm("The selected Organisation currently has access to 'All' CMIDs.\n\nThis action will remove the Organisation's access to this vessel\n                              AND\ndowngrade it's access level for other vessels to 'Single'.\n\nClick OK to continue.");
    }
}

// Clear clip board function
function clearClipboard(details) {
    try {
        window.clipboardData.clearData();
    }
    catch (e) {
        //catch and just suppress error
    }    
}


// Handles the OnClientContextMenuShowing event for the viewCmidAccess treeview
function viewCmidAccessOnClientContextMenuShowing(sender, args) {
    var treeNode = args.get_node();
    treeNode.set_selected(true);
    setViewCmidAccessContextMenuItemState(args.get_menu().get_items(), treeNode);
}

// Sets the viewCmidAccess context menu options
function setViewCmidAccessContextMenuItemState(menuItems, treeNode) {
    var nodeValue = treeNode.get_value();
    var nodeImage = treeNode.get_imageUrl();
    var menuEnabledArray = new Array(false, true, false, false, true, false);

    // Company
    if (nodeImage.indexOf("img_hexegon_grey") > 0) {
        // Root
        menuEnabledArray[5] = true;
    }
    else if (nodeImage.indexOf("img_vessel_blue_cancelaccessnotallowed.png") > 0) {
        menuEnabledArray[0] = true;
    }
    else if (nodeImage.indexOf("img_vessel_blue_accesstypeall.png") > 0) {
        menuEnabledArray[0] = true;
        menuEnabledArray[5] = true;
    }
    else if (nodeImage.indexOf("img_vessel_blue_accesstypesingle.png") > 0) {
        menuEnabledArray[0] = true;
        menuEnabledArray[5] = true;
    }
    else if (nodeImage.indexOf("img_cmid_green.png") > 0) {
        menuEnabledArray[2] = true;
        menuEnabledArray[3] = true;
    }
    else if (nodeImage.indexOf("img_cmid_redgreen.png") > 0) {
        menuEnabledArray[2] = true;
        menuEnabledArray[3] = true;
    }


    // Now set the menu items to be enabled or not as the case maybe
    for (var i = 0; i < menuItems.get_count(); i++) {
        menuItem = menuItems.getItem(i);
        menuItem.set_enabled(menuEnabledArray[i]);
    }
}


// Handles the OnClientContextMenuItemClicking event for the viewCmidAccess treeview
function viewCmidAccessOnClientContextMenuItemClicking(sender, args) {
    var treeNode = args.get_node();
    var nodeImage = treeNode.get_imageUrl();
    var menuItem = args.get_menuItem();
 
 
    // Company
    if (menuItem.get_text() == "Cancel CMID Access") {

        if (nodeImage.indexOf("img_hexegon_grey") > 0) {
            // Root
            if (confirm('Are you sure that you want to cancel CMID access to all vessels operated by this vessel operator.') != true) {
                args.set_cancel(true);
            }
        }
        else if (nodeImage.indexOf("img_vessel_blue_accesstypeall.png") > 0) {
            if (confirm('Are you sure that you want to cancel CMID access to this vessel.\n\nNOTE: Your CMID Access level with regards to this vessel operator is currently set to All. If you\nremove access to this vessel you will still be able to access the CMIDs for other vessels but you will\nnot get automatic access to the CMIDs of any new vessels operated by this vessel operator.') != true) {
                args.set_cancel(true);
            }
        }
        else if (nodeImage.indexOf("img_vessel_blue_accesstypesingle.png") > 0) {
            if (confirm('Are you sure that you want to cancel CMID access to this vessel.') != true) {
                args.set_cancel(true);
            }
        }




    }
    
}



// Gets the current RadWindow
function getRadWindow() {
    var oWindow = null;
    if (window.radWindow)
        oWindow = window.radWindow;
    else if (window.frameElement.radWindow)
        oWindow = window.frameElement.radWindow;
    return oWindow;
}

// Closes the current RadWindow
function closeRadWindow() {
    var oWindow = getRadWindow();
    oWindow.argument = null;
    oWindow.close();
}


// Routine used to only allow numbers in textboxes
function numbersOnly(e) {
    //  Check if the key press is valid e.g. a number if it isn't ignore it
    var characterCode = getCharacterCode(e);
    if ((characterCode < 48) || (characterCode > 57)) {
        e.cancelBubble = true;
        e.returnValue = false;
        return false;
    }
    return true;
}


//  Gets a character code from an event
function getCharacterCode(e) {
    //  Check if the key press is valid e.g. a number if it isn't ignore it
    var characterCode;
    if (e && e.which) {
        e = e;
        characterCode = e.which;
    }
    else {
        e = event;
        characterCode = e.keyCode;
    }
    return characterCode;
}


// Converts a string value to a number (if invalid number string then return 0)
function convertStringValueToNumber(data) {
    if (trimAll(data) == '') {
        return 0;
    }
    else {
        var number = parseFloat(data);
        return number;
    }
}


// Trim routine for strings
function trimAll(stringToTrim) {
    while (stringToTrim.substring(0, 1) == ' ') {
        stringToTrim = stringToTrim.substring(1, stringToTrim.length);
    }
    while (stringToTrim.substring(stringToTrim.length - 1, stringToTrim.length) == ' ') {
        stringToTrim = stringToTrim.substring(0, stringToTrim.length - 1);
    }
    return stringToTrim;
}



//Routine to get the id from the page
function getQuerystring(key, default_) {
    if (default_ == null) default_ = "";
    key = key.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regex = new RegExp("[\\?&]" + key + "=([^&#]*)");
    var qs = regex.exec(window.location.href);
    if (qs == null)
        return default_;
    else
        return qs[1];
}


// Swap Checkbox Checks routine
function swapCheckboxChecked(checkedName, otherName) {
    var checkedCheckBox = findObjWithClientId(checkedName);
    if (checkedCheckBox.checked == false) {
        return;
    }
    var newState = !checkedCheckBox.checked;
    var otherCheckBox = findObjWithClientId(otherName);
    otherCheckBox.checked = newState;
}