﻿/// <reference name="MicrosoftAjax.js" />
/// <reference path="../dnn.js" />

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Extension Methods
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Array.prototype.remove = function(from, to)
{
    var rest = this.slice((to || from) + 1 || this.length);
    
    this.length = from < 0 ? this.length + from : from;
    
    return this.push.apply(this, rest);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

var RangeLog = 
{
    getClassSelector: function(className)
    {
        /// <summary>
        /// Converts the passed in css class name into an css selector used by jQuery
        /// </summary>

        return "." + className;
    },

    getIdSelector: function(elementID)
    {
        /// <summary>
        /// Converts the passed in element id into an id selector used by jQuery
        /// </summary>

        return "#" + elementID;
    }
};

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Namespace declarations
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
RangeLog.Client = {};
RangeLog.Client.UserControls = {};
RangeLog.Generic = {};
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// GLOBAL VARIABLES
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
RangeLog.Client.portalId = null;
RangeLog.Client.RangeLogAjaxManager = null;
RangeLog.Client.RangeLogWindowManager = null;
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// GENERIC JAVASCRIPT ARRAY
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
RangeLog.Generic.list = function()
{
    this.items = new Array();
};

RangeLog.Generic.list.prototype.add = function(item)
{
    if (this.items.length != 0)
    {
        var oldlen = this.items.length;
        var tmp = new Array(oldlen + 1);
        var i = 0;
        
        for (i = 0; i < this.items.length; i++)
        {
            tmp[i] = this.items[i];
        };
        tmp[(tmp.length - 1)] = item;
        this.items = new Array(tmp.length);
        for (i = 0; i < tmp.length; i++)
        {
            this.items[i] = tmp[i];
        };
        tmp = null;
    } 
    else
    {
        this.items = new Array(1);
        this.items[0] = item;
    };
};

RangeLog.Generic.list.prototype.addRange = function(objectArray)
{
    if (this.items.length != 0)
    {
        var oldlen = this.items.length;
        var tmp = new Array(oldlen + objectArray.length);
        var i = 0;
        
        for (i = 0; i < this.items.length; i++)
        {
            tmp[i] = this.items[i];
        };
        for (i = 0; i < objectArray.length; i++)
        {
            tmp[(i + oldlen)] = objectArray[i];
        };
        this.items = new Array(tmp.length);
        for (i = 0; i < tmp.length; i++)
        {
            this.items[i] = tmp[i];
        };
        tmp = null;
    } 
    else
    {
        var iloop = 0;
        
        this.items = new Array(objectArray.length);
        for (iloop = 0; iloop < objectArray.length; iloop++)
        {
            this.items[iloop] = objectArray[iloop];
        };
    };
};

RangeLog.Generic.list.prototype.clear = function()
{
    this.items = new Array();
};

RangeLog.Generic.list.prototype.compare = function(a, b)
{
    return (a == b);
};

RangeLog.Generic.list.prototype.count = function()
{
    return this.items.length;
};

RangeLog.Generic.list.prototype.extendProperty = function(index, obj)
{
    for (key in obj)
    {
        this.setProperty(index, key, obj[key]);
    };
};

RangeLog.Generic.list.prototype.find = function(item)
{
    var index = -1;
    var i = 0;
    
    for (i = 0; i < this.items.length; i++)
    {
        if (this.compare(this.items[i], item))
        {
            index = i;
            break;
        };
    };
    
    return index;
};

RangeLog.Generic.list.prototype.getItem = function(index)
{
    return this.items[index];
};

RangeLog.Generic.list.prototype.getItems = function()
{
    return this.items;
};

RangeLog.Generic.list.prototype.getProperty = function(index, property)
{
    return this.items[index][property];
};

RangeLog.Generic.list.prototype.insert = function(item, index)
{
    if (this.items.length != 0)
    {
        var oldlen = this.items.length;
        var tmp = new Array(oldlen + 1);
        var i = 0;
        var j = 0;
        
        for (i = 0; i < tmp.length; i++)
        {
            if (i == index)
            {
                tmp[i] = item;
            } else
            {
                tmp[i] = this.items[j];
                j++;
            };
        };
        this.items = new Array(tmp.length);
        for (i = 0; i < tmp.length; i++)
        {
            this.items[i] = tmp[i];
        };
        tmp = null;
    } 
    else
    {
        this.items = new Array(1);
        this.items[0] = item;
    };
};

RangeLog.Generic.list.prototype.join = function(seprator, property)
{
    var i = 0;
    var result = "";
    
    for (i = 0; i < this.items.length; i++)
    {
        if (i == (this.items.length - 1))
        {
            result += (property) ? this.items[i][property] : this.items[i];
        } 
        else
        {
            result += (property) ? this.items[i][property] : this.items[i];
            result += seprator;
        };
    };
    
    return result;
};

RangeLog.Generic.list.prototype.remove = function(item)
{
    var index = this.find(item);
    
    if (index != -1)
    {
        var tmp = new Array((this.items.length - 1));
        var i = 0;
        var j = 0;
        
        for (i = 0; i < this.items.length; i++)
        {
            if (i != index)
            {
                tmp[j] = this.items[i];
                j++;
            };
        };
        this.items = new Array(tmp.length);
        for (i = 0; i < tmp.length; i++)
        {
            this.items[i] = tmp[i];
        };
        tmp = null;
        this.count--;
    };
};

RangeLog.Generic.list.prototype.removeAt = function(index)
{
    if (this.items.length == 1)
    {
        this.items = null;
        this.items = new Array();
    } 
    else
    {
        var tmp = new Array((this.items.length - 1));
        var i = 0;
        var j = 0;
        
        for (i = 0; i < this.items.length; i++)
        {
            if (i != index)
            {
                tmp[j] = this.items[i];
                j++;
            };
        };
        this.items = new Array(tmp.length);
        for (i = 0; i < tmp.length; i++)
        {
            this.items[i] = tmp[i];
        };
        tmp = null;
    };
};

RangeLog.Generic.list.prototype.setItem = function(item, index)
{
    this.items[index] = item;
};

RangeLog.Generic.list.prototype.setProperty = function(index, property, value)
{
    this.items[index][property] = value;
};
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// PUBLIC METHODS AND FUNCTIONS
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

/* Description:     Used to add handlers to the DOM objects. 
   Examples:
                    function function1()
                    {
                        alert("first alert");
                    }

                    function function2()
                    {
                        alert("second alert");
                    }

                    RangeLog.Client.addEventHandler(window,"load",function2);
*/
RangeLog.Client.addEventHandler = function(to_element, event, handler) {
    if (to_element.addEventListener) { to_element.addEventListener(event, handler, false); }
    else if (to_element.attachEvent) { to_element.attachEvent("on" + event, handler); }
    else return false;
}

RangeLog.Client.radWindowClientClose = function(window)
{
/// <summary>
/// Used to call the onClientClose method of the RangeLog.Client.RadWindowManager object.
/// </summary>
    
// check if the global variable has been defined
if (RangeLog.Client.RangeLogWindowManager)
{
// variable has been set call the onClientClose method
RangeLog.Client.RangeLogWindowManager.onClientClose(window);
}
}

RangeLog.Client.radWindowClientResize = function(window) {
/// <summary>
/// Used to call the onClientResize method of the RangeLog.Client.RadWindowManager object.
/// </summary>

// check if the global variable has been defined
if (RangeLog.Client.RangeLogWindowManager) {
// variable has been set call the onClientResize method
RangeLog.Client.RangeLogWindowManager.onClientResize(window);
}
}

/* Description: Used to convert a time string value (e.g. 00:00:000) to it's equivalent milliseconds value. */
RangeLog.Client.ConvertTimeStringToMilliseconds = function(timeString) {
    var timeMin = timeString.substr(0, 2);
    var timeSec = timeString.substr(3, 2);
    var timeMillisec = timeString.substr(6);

    // Remove leading zeros from string representation of numbers in the time.
    timeMin = RangeLog.Client.stripLeadingCharacter(timeMin, '0');
    timeSec = RangeLog.Client.stripLeadingCharacter(timeSec, '0');
    timeMillisec = RangeLog.Client.stripLeadingCharacter(timeMillisec, '0');
    
    var timeInMilliseconds = (parseInt(timeMin) * 60000) + (parseInt(timeSec) * 1000) + parseInt(timeMillisec);

    return timeInMilliseconds;
}

RangeLog.Client.stripLeadingCharacter = function(stringValue, charToStrip) {
    var returnValue = '';
    
    var stringLength = stringValue.length;
    var returnStringCharStart = 0;
    for (var i = 0; i < stringValue.length; i++) {
        if (stringValue.charAt(i) != charToStrip) { returnStringCharStart = i; break; }
    }
    if (returnStringCharStart < stringValue.length) { returnValue = stringValue.substr(returnStringCharStart); }
    else { returnStringCharStart = stringValue; }
    
    return returnValue;
}

/* Description: Used to round a number to a particular precision. */
RangeLog.Client.roundNumber = function (numberToRound, precision) {
    if (precision > 0) {
        // Precision is the number of decimal places the resulting number should be.  So we recalculate based on powers of 10 so the calculation (below) will work.
        precision = Math.pow(10, precision);

        return precision > 0 ? (Math.round(numberToRound * precision) / precision) : 0;
    }
    else { return 0; }
}

/* Description: Used to convert a milliseconds value to it's equivalent time string value (e.g. 00:00:000). */
RangeLog.Client.ConvertMillisecondsToTimeString = function(timeInMilliseconds) {
    var timeString;
    if (Math.abs(timeInMilliseconds) > 0) {
        var absTimeInMilliseconds = Math.abs(timeInMilliseconds);

        if (absTimeInMilliseconds >= 60000) {
            var min = Math.floor(absTimeInMilliseconds / 60000);
            absTimeInMilliseconds = (absTimeInMilliseconds - (min * 60000));
            timeString = (min < 10 ? "0" + min.toString() : min.toString()) + ":";
        }
        else { timeString = "00:"; }

        if (absTimeInMilliseconds >= 1000) {
            var sec = Math.floor(absTimeInMilliseconds / 1000);
            absTimeInMilliseconds = (absTimeInMilliseconds - (sec * 1000));
            timeString += (sec < 10 ? "0" + sec.toString() : sec.toString()) + ".";
        }
        else { timeString += "00."; }

        if (absTimeInMilliseconds > 0) {
            if (absTimeInMilliseconds < 10) { timeString += "00" + absTimeInMilliseconds.toString(); }
            else if (absTimeInMilliseconds < 100) { timeString += "0" + absTimeInMilliseconds.toString(); }
            else { timeString += absTimeInMilliseconds.toString(); }
        }
        else { timeString += "000"; }
    }
    else { timeString = "00:00.000"; }
    if (timeInMilliseconds < 0) { timeString = "(" + timeString + ")"; }

    return timeString;
}

RangeLog.Client.stopPropagation = function(e) {
    //cancel bubbling
    e.cancelBubble = true;
    if (e.stopPropagation) {
        e.stopPropagation();
    }
}

/* Description: Used to prevent user from hitting 'Enter' key.  Pass event.keyCode. */
RangeLog.Client.preventEnterKeyPress = function(keyCode) {
    return (keyCode != 13);
}

/* Description: Used to prevent user from hitting 'Backspace' key.  Pass event.keyCode. */
RangeLog.Client.preventBackspacePress = function (e) {
    var keynum;

    if (window.event) { keynum = e.keyCode; }
    else if (e.which) { keynum = e.which; }

    return (keynum != 8);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
