﻿/// <reference name="MicrosoftAjax.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 ITG =
{
    appName: "Inside The Grape",
    
    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;
    },

    numberOfDaysInMonth: function(month, year)
    {
        /// <summary>
        ///
        /// </summary>
        var date = new Date(year, month + 1, 1, -1);

        return date.getDate();
    },

    radComboBox_OnClientTextChange: function(sender, e)
    {
        var item = null;
        var items = sender.get_items();
        var count = items.get_count();

        for (var i = 0; i < count; i++)
        {
            item = items.getItem(i);
            if (item.get_text().toLowerCase() == sender.get_text().toLowerCase())
            {
                item.select();
                break;
            }
        }
    }
};

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Namespace declarations
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ITG.Client = {};
ITG.Generic = {};
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// GLOBAL VARIABLES
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ITG.Client.portalId = null;
ITG.Client.AjaxManager = null;
ITG.Client.eCommerce = null;
ITG.Client.ShoppingCart = null;
ITG.Client.TooltipManager = null;
ITG.Client.WindowManager = null;
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// GENERIC JAVASCRIPT ARRAY
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ITG.Generic.list = function()
{
    this.items = new Array();
};

ITG.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;
    };
};

ITG.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];
        };
    };
};

ITG.Generic.list.prototype.clear = function()
{
    this.items = new Array();
};

ITG.Generic.list.prototype.compare = function(a, b)
{
    return (a == b);
};

ITG.Generic.list.prototype.count = function()
{
    return this.items.length;
};

ITG.Generic.list.prototype.extendProperty = function(index, obj)
{
    for (key in obj)
    {
        this.setProperty(index, key, obj[key]);
    };
};

ITG.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;
};

ITG.Generic.list.prototype.getItem = function(index)
{
    return this.items[index];
};

ITG.Generic.list.prototype.getItems = function()
{
    return this.items;
};

ITG.Generic.list.prototype.getProperty = function(index, property)
{
    return this.items[index][property];
};

ITG.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;
    };
};

ITG.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;
};

ITG.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--;
    };
};

ITG.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;
    };
};

ITG.Generic.list.prototype.setItem = function(item, index)
{
    this.items[index] = item;
};

ITG.Generic.list.prototype.setProperty = function(index, property, value)
{
    this.items[index][property] = value;
};
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// PUBLIC METHODS AND FUNCTIONS
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ITG.Client.radWindowClientClose = function(window)
{
    /// <summary>
    /// Used to call the onClientClose method of the ITG.Client.RadWindowManager object.
    /// </summary>

    // check if the global variable has been defined
    if (ITG.Client.WindowManager)
    {
        // variable has been set call the onClientClose method
        ITG.Client.WindowManager.onClientClose(window);
    }
}

ITG.Client.radWindowClientResize = function(window)
{
    /// <summary>
    /// Used to call the onClientResize method of the ITG.Client.RadWindowManager object.
    /// </summary>

    // check if the global variable has been defined
    if (ITG.Client.WindowManager)
    {
        // variable has been set call the onClientResize method
        ITG.Client.WindowManager.onClientResize(window);
    }
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
