﻿var _AccessLevel = undefined;
var _AddressBook = 0;

function getFileName(url) {
    if (url != undefined) {
        //this removes the query after the file name, if there is one
        url = url.substring(0, (url.lastIndexOf("'") == -1) ? url.length : url.lastIndexOf("'"));
        //this removes the anchor at the end, if there is one
        url = url.substring(0, (url.indexOf("#") == -1) ? url.length : url.indexOf("#"));
        //this removes the query after the file name, if there is one
        url = url.substring(0, (url.indexOf("?") == -1) ? url.length : url.indexOf("?"));
        //this removes everything before the last slash in the path
        url = url.substring(url.lastIndexOf("/") + 1, url.length);
    }
    //return
    return url;
}

// ACCESS LEVEL
function SetAccessLevel(access) {
    //_AccessLevel = parseInt(access);
    _AccessLevel = access;
}

function GetAccessLevel(form, dashboard) {
    var _retVal = -1;
    var _dash = (dashboard == undefined ? false : true);
    
    if (_AccessLevel != undefined) {
        // Loop through the items within the security list
        for (x in _AccessLevel.SecurityList) {
            if (_AccessLevel.SecurityList[x].WebFormName == form) {
                if (_AccessLevel.SecurityList[x].IsPublic)
                    _retVal = 1;
                else
                    _retVal = _AccessLevel.SecurityList[x].AccessLevel;

                // If called from the dashboard page
                if (_dash == true) {
                    // If checking on the Address Book Setup page, ensure that they also
                    // have access to the address book list.
                    if (form == 'AddressBookSetup.aspx')
                        _retVal = GetAccessLevel('AddressBook.aspx');
                }
                
                break;
            }
        }
    }
    else {
        // Read only for not logged in
        _retVal = 1;
    }
    
    // Return the access Level
    return _retVal
}

function CheckAdminAccess() {
    // Return the access Level
    return _AccessLevel.IsSystemAdmin;
}

// ADDRESS BOOK
function SetAddressBook(ab) {
    _AddressBook = parseInt(ab);
    document.getElementById('_UserID').value = ab;
}

function GetAddressBook() {
    return _AddressBook;
}

function DisableLink(obj) {
    if (obj != undefined) {
        obj.disabled = true;
        obj.onclick = 'function(){return false;}';
        obj.href = '#';
        obj.removeAttribute("href");
    }
}

function CheckPageSecurity(url, editArray, delArray, newArray) {
    // Disable the delete if NO ACCESS / READ ONLY is selected
    if (delArray != undefined) {
        if (GetAccessLevel(getFileName(url.href)) < 1) {
            for (var i = 0; i < delArray.length; i++) {
                DisableLink(delArray[i]);
            }
        }
    }
    
    // Disable the edit if NO ACCESS is selected
    if (editArray != undefined) {
        if (GetAccessLevel(getFileName(url.href)) < 0) {
            for (var i = 0; i < editArray.length; i++) {
                DisableLink(editArray[i]);
            }
        }
    }

    // Disable the edit if NO ACCESS is selected
    if (newArray != undefined) {
        if (GetAccessLevel(getFileName(url.href)) < 1) {
            for (var i = 0; i < newArray.length; i++) {
                DisableLink(newArray[i]);
            }
        }
    }
}
