﻿function Master() {
    
}

Master.authenticated = true;
Master.loginUrl = "";

Master.isAuthenticated = function() {
    if (Master.authenticated)
        return true;
    else
        Master.forwardToLogin();

    return false;
}

//Master.openLogin = function() {
    //try {
        //var msglabel = document.getElementById("MsgLabel");
        //msglabel.innerHTML = "You must first log in to perform that action.";
    //} catch (er) { }
//}

Master.forwardToLogin = function() {
    try {
        var curUrl = window.location;
        var newUrl = Master.loginUrl + "?RedirectUrl=" + curUrl;

        window.location = newUrl;
    } catch (e) { }
}


/*
Below is everything used to sort tables.
*/

// Keeps track of what we sorted last and how we sorted it
var prevMethod;

// Sorts the table the passed in object belongs to (selection sort)
// obj = Cell of the table to sort
function sort(obj) {
    var colIndex = obj.cellIndex;
    var rows = obj.parentNode.parentNode.parentNode.rows;
    var panelID = obj.parentNode.parentNode.parentNode.parentNode.clientID;

    // Determines the current method
    var method = panelID + "_" + colIndex + "_asc";
    if (prevMethod != null && method == prevMethod) {
        method = method.replace("_asc", "_desc");
    }

    prevMethod = method;

    // Outer loop of rows
    for (var i = 1; i < rows.length - 1; i++) {
        var cols = rows[i].getElementsByTagName("td");

        // Ensure the column exists in this row
        if (cols.length > colIndex) {
            var newIndex = i;
            var newVal = cols[colIndex].innerHTML;

            // Inner loop of rows
            for (var k = i + 1; k < rows.length; k++) {
                var innerCols = rows[k].getElementsByTagName("td");

                // Ensure the column exists in this row
                if (innerCols.length > colIndex) {
                    var curVal = innerCols[colIndex].innerHTML

                    // Set the new replacement values depending on the method and values
                    if (method.match("_asc") && curVal < newVal) {
                        newVal = curVal;
                        newIndex = k;
                    }
                    else if (method.match("_desc") && curVal > newVal) {
                        newVal = curVal;
                        newIndex = k;
                    }
                }
            }

            swapRow(rows[newIndex], rows[i]);
        }
    }

    // Change the CSS class of the column we sorted by
    for (var i = 1; i < rows.length; i++) {
        var cols = rows[i].getElementsByTagName("td");
        var newClass = "";
        
        for (var k = 1; k < cols.length; k++) {
            newClass = "";
            if (k == colIndex)
                newClass = "testList-cellalt";

            cols[k].className = newClass;
        }
    }
}

// In IE you cannot simply swap the row's innerHTML, you have to swap the rows column by column
function swapRow(row1, row2) {
    var cols1 = row1.getElementsByTagName("td");
    var cols2 = row2.getElementsByTagName("td");

    if (cols1.length != cols2.length)
        return;

    for (var i = 0; i < cols1.length; i++) {
        var tempVal = cols1[i].innerHTML;
        cols1[i].innerHTML = cols2[i].innerHTML;
        cols2[i].innerHTML = tempVal;
    }
}


/*
Below is everything used to animate the shrink and expand
functionalities.
*/

// How many pixels are shown/hidden every interval
var pixelsPerInterval = 28;
// How many milliseconds between each interval
var msecPerInterval = 20;
// Used to keep track of the original panel size after it's been shrunk
var panelSize = new Array(10);
// Used to keep track of the current state of a panel
var panelStatus = new Array(10);
// Used to determine if an animation for a panel is currently in process
var panelLock = new Array(10);

// Begins the expand/shrink process
// obj = partial ID of the panel
function slowTogglePanel(obj) {
    // Check for a lock on this panel, otherwise lock it
    if (panelLock[obj] == null || panelLock[obj] != 'Locked')
        panelLock[obj] = 'Locked';
    else
        return false;
    
    // Try to retrieve the saved status for this panel
    if (panelStatus[obj] == null || panelStatus[obj] == '')
        panelStatus[obj] = 'Shown';

    var panel = document.getElementById(obj + 'Content');

    // If shown, hide
    if (panelStatus[obj] == 'Shown') {
        // Save the height before collapsing the panel
        var height = 0;

        if (panel.clientHeight == null || panel.clientHeight == '' || panel.clientHeight <= 0) {
            // If the clientHeight is 0 it's likely an IE quirk, the below attempts a workaround
            var overflow = panel.style.overflow;
            
            panel.style.overflow = 'hidden';
            height = panel.clientHeight;
            panel.style.overflow = overflow;
        } else
            height = panel.clientHeight;

        // Saving the height before the overflow is changed is important
        panelSize[obj] = height;
        panel.style.overflow = 'hidden';

        changeSize(obj, 0);
    } else if (panelStatus[obj] == 'Hidden') {
        changeSize(obj, panelSize[obj]);
    }
    
    return true;
}

// Changes the size of the passed in panel, will asynchronously (not recursively) call itself
// obj = partial ID of the panel; maxHeight = 'final' height to increment/decrement to
function changeSize(obj, maxHeight) {
    var panel = document.getElementById(obj + 'Content');
    var height = panel.clientHeight;
    
    var shrink = false;
    if (panelStatus[obj] == 'Shown')
        shrink = true;

    if (isFinished(height, maxHeight))
        return;

    // This handles the increments, if the increment is too large for the remainder, it simply jumps to the end
    if (shrink && height > maxHeight && height < pixelsPerInterval)
        height = maxHeight;
    else if (!shrink && height < maxHeight && (maxHeight - height) < pixelsPerInterval)
        height = maxHeight;
    else if (shrink)
        height = height - pixelsPerInterval;
    else
        height = height + pixelsPerInterval;

    panel.style.height = height + 'px';

    //alert("Height: " + height + "\nFinal Height: " + maxHeight);
    if (isFinished(height, maxHeight)) {
    
        changeComplete(obj);

        return;
    } else
        setTimeout('changeSize("' + obj + '", ' + maxHeight + ')', msecPerInterval);
}

// Called after the expand/shrink process is complete for that specific panel, unlocks the panel and cleans up
// obj = partial ID of the panel
function changeComplete(obj) {
    var panel = document.getElementById(obj + 'Content');
    var title = document.getElementById(obj + 'Title');

    if (panelStatus[obj] == 'Hidden') {
        panelSize[obj] = null;
        panel.style.overflow = 'visible';

        panelStatus[obj] = 'Shown';

        title.innerHTML = title.innerHTML.replace('+', '-');
    } else {
        panelStatus[obj] = 'Hidden';

        title.innerHTML = title.innerHTML.replace('-', '+');
    }

    panelLock[obj] = null;
}

function isFinished(measurement, end) {
    if (measurement == null || end == null || measurement == end)
        return true;
    else
        return false;
}




// Old - Instantly hides/shows the panel
function togglePanel(obj) {
    var item = document.getElementById(obj + 'Content');
    var title = document.getElementById(obj + 'Title');

    if (item.style.display != 'none') {
        item.style.display = 'none';
        title.innerHTML = title.innerHTML.replace('-', '+');
    } else {
        item.style.display = 'block';
        title.innerHTML = title.innerHTML.replace('+', '-');
    }
}
