﻿/* © mySupermarket Limited, 2005-2010. All rights reserved. */

$.fn.getClassNames = function()
{
    if (name = this.attr("className"))
    {
        return name.split(" ");
    }
    else
    {
        return [];
    }
}

$.fn.outerHTML = function() {
   return $("<" + this.tagName + ">").append( this.eq(0).clone() ).html();
};
 
 
MSBICS.Utils = new Object();

MSBICS.Utils.Guid = new Object();

MSBICS.Utils.Guid.S4 = function()
{
    return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
}

MSBICS.Utils.Guid.NewGuid = function()
{
    return (MSBICS.Utils.Guid.S4() + MSBICS.Utils.Guid.S4() + "-" + MSBICS.Utils.Guid.S4() + "-" +
    MSBICS.Utils.Guid.S4() + "-" + MSBICS.Utils.Guid.S4() + "-" + MSBICS.Utils.Guid.S4() +
    MSBICS.Utils.Guid.S4() + MSBICS.Utils.Guid.S4());
}

MSBICS.Utils.WindowOpen = function(iUrl, iWidth, iHeight)
{
    window.open(iUrl, "", "scrollbars=1,resizable=1,width=" + iWidth + ",height=" + iHeight);
}

MSBICS.Utils.DateTime = new Object();

MSBICS.Utils.DateTime.Months = ["", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
MSBICS.Utils.DateTime.Days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"];

MSBICS.Utils.DateTime.DaysInMonth = function(iYear, iMonth)
{
    var m = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
    if (iMonth != 2) return m[iMonth - 1];
    if (iYear % 4 != 0) return m[1];
    if (iYear % 100 == 0 && iYear % 400 != 0) return m[1];
    return m[1] + 1;
}

MSBICS.Utils.DateTime.ConvertDateFromWrappedString = function(iDateString)
{
    var year = parseInt(iDateString.substring(0, 4));
    var month = iDateString.substring(4, 6);
    var day = iDateString.substring(6, 8);
    if (month.indexOf("0") == 0)
    {
        month = month.substring(1);
    }
    if (day.indexOf("0") == 0)
    {
        day = day.substring(1);
    }
    month = parseInt(month);
    day = parseInt(day);
    return new Date(year, month - 1, day)
}

MSBICS.Utils.DateTime.ConvertDateToWrappedString = function(iDate)
{
    var year = iDate.getFullYear();
    var month = iDate.getMonth();
    month++;
    if (month < 10)
    {
        month = "0" + month;
    }
    var day = iDate.getDate();
    if (day < 10)
    {
        day = "0" + day;
    }
    return year + "" + month + "" + day;
}

MSBICS.Utils.DateTime.ConvertDateToString = function(iDate)
{
    var year = iDate.getFullYear();
    var month = iDate.getMonth();
    month++;
    if (month < 10)
    {
        month = "0" + month;
    }
    var day = iDate.getDate();
    if (day < 10)
    {
        day = "0" + day;
    }
    return day + "/" + month + "/" + year;
}

MSBICS.Utils.DateTime.ConvertDateToLongString = function(iDate)
{
    var year = iDate.getFullYear();
    var month = iDate.getMonth();
    month++;
    var day = iDate.getDay();

    return MSBICS.Utils.DateTime.Days[day] + " " + MSBICS.Utils.DateTime.Months[month].substring(0, 3) + " " + iDate.getDate() + " " + year;
}

MSBICS.Utils.DateTime.FormatShortDateRange = function(iDate1, iDate2)
{
    var month1 = iDate1.getMonth();
    month1++;
    var monthString1 = MSBICS.Utils.DateTime.Months[month1].substring(0, 3);

    var month2 = iDate2.getMonth();
    month2++;
    var monthString2 = MSBICS.Utils.DateTime.Months[month2].substring(0, 3);
    
    return iDate1.getDate() + " " + monthString1 +
        " - " + iDate2.getDate() + " " + monthString2;
}

MSBICS.Utils.DateTime.FormatDateRange = function(iDate1, iDate2)
{
    var year1 = iDate1.getFullYear();
    var year2 = iDate2.getFullYear();
    
    var month1 = iDate1.getMonth();
    month1++;
    var monthString1 = MSBICS.Utils.DateTime.Months[month1].substring(0, 3);

    var month2 = iDate2.getMonth();
    month2++;
    var monthString2 = MSBICS.Utils.DateTime.Months[month2].substring(0, 3);

    return iDate1.getDate() + " " + monthString1 + " " + year1 +
        " - " + iDate2.getDate() + " " + monthString2 + " " + year2;
}

MSBICS.Utils.DateTime.ConvertDateToMonthRangeString = function(iDate, iAddYear)
{
    var year = iDate.getFullYear();
    var month = iDate.getMonth();
    
    month++;
    var lastDayInMonth = MSBICS.Utils.DateTime.DaysInMonth(year, month);
    
    var day = iDate.getDate();

    var monthString = MSBICS.Utils.DateTime.Months[month].substring(0, 3);
    
    var temp = iDate.getDate() + " " + monthString +
        " - " + lastDayInMonth + " " + monthString;
    if (iAddYear)
    {
        temp = temp + " " + year;
    }
    return temp;
}

MSBICS.Utils.DateTime.ConvertDateToWeekRangeString = function(iDate, iAddYear)
{
    var year = iDate.getFullYear();
    var month = iDate.getMonth();

    month++;
    var lastDayInMonth = MSBICS.Utils.DateTime.DaysInMonth(year, month);

    var day = iDate.getDate();

    var monthString = MSBICS.Utils.DateTime.Months[month].substring(0, 3);

    var temp = iDate.getDate() + " " + monthString + " - ";

    if (day + 7 <= lastDayInMonth)
    {
        temp = temp + (day + 7) + " " + monthString;
    }
    else
    {
        day = day + 7 - lastDayInMonth;
        monthString = MSBICS.Utils.DateTime.Months[(month % 12) + 1].substring(0, 3);
        temp = temp + day + " " + monthString;
    }

    if (iAddYear)
    {
        temp = temp + " " + year;
    }
    return temp;
}

MSBICS.Utils.String = new Object();

MSBICS.Utils.String.FormatTrackingValue = function(iString)
{
    var retVal = iString;
    retVal = retVal.replace(' ', '_');
    retVal = retVal.replace("&", "and");
    retVal = retVal.replace("'", "");
    retVal = retVal.replace("\"", "");
    return retVal;
}

MSBICS.Utils.Scroll = new Object();

MSBICS.Utils.Scroll.Top = function()
{
    window.scroll(0, 0);
}

MSBICS.Utils.Units = new Object();
MSBICS.Utils.Units.Round = function(iValue, iNumOfDecimals)
{
    return Math.round(iValue*(Math.pow(10,iNumOfDecimals)))/(Math.pow(10,iNumOfDecimals));
}
MSBICS.Utils.Units.FormatPriceChange = function(iValue)
{
    var value = eval(iValue*100);
    if (iValue > 10)
    {
        value = MSBICS.Utils.Units.Round(value, 0);
    }
    else if (iValue > 1)
    {
        value = MSBICS.Utils.Units.Round(value, 1);
    }
    else
    {
        value = MSBICS.Utils.Units.Round(value, 2);
    }
    if (eval(value) > 0)
    {
        value = ("+" + value);
    }
    return value + "%";
}
MSBICS.Utils.Units.FormatSalesChange = function(iValue)
{
    var value = eval(iValue);
    var decimals = 0;
    if (Math.abs(value) < 100)
    {
        decimals++;
    }
    if (Math.abs(value) < 10)
    {
        decimals++;
    }
    if (Math.abs(value) < 1)
    {
        decimals++;
    }
    
    value = MSBICS.Utils.Units.Round(value, decimals);
    if (eval(value) > 0)
    {
        value = ("+" + value);
    }

    return value + "%";
}

MSBICS.Utils.Units.FormatPrice = function (iValue, iDecimals)
{
    if (!iDecimals)
    {
        iDecimals = 2;
    }

    var value = iValue + "";
    if (value.indexOf(".") == -1)
    {
        value += ".00";
    }

    if (value.indexOf(".") == value.length - 2)
    {
        value += "0";
    }

    if (value.indexOf("£") == -1)
    {
        value = "£" + value;
    }

    return value.substring(0, value.indexOf(".") + iDecimals + 1)
}

/* ========================
Wrappers
========================= */
MSBICS.Wrappers = new Object();
MSBICS.Wrappers.SECTION_SEPERATOR = ".";
MSBICS.Wrappers.INNER_SECTION_SEPERATOR = "~";

MSBICS.Wrappers.DateRange = new Object();
MSBICS.Wrappers.DateRange.ToWrappedString = function(iJSONObj)
{
    return MSBICS.Utils.DateTime.ConvertDateToWrappedString(iJSONObj.StartDate) +
        MSBICS.Wrappers.INNER_SECTION_SEPERATOR +
        MSBICS.Utils.DateTime.ConvertDateToWrappedString(iJSONObj.EndDate);
}
MSBICS.Wrappers.DateRange.FromWrappedString = function(iWrappedSring)
{
    var dates = iWrappedSring.split(MSBICS.Wrappers.INNER_SECTION_SEPERATOR);
    var temp = new Object();
    temp.StartDate = MSBICS.Utils.DateTime.ConvertDateFromWrappedString(dates[0]);
    if (dates.length == 2)
    {
        temp.EndDate = MSBICS.Utils.DateTime.ConvertDateFromWrappedString(dates[1]);
    }
    else
    {
        temp.EndDate = MSBICS.Utils.DateTime.ConvertDateFromWrappedString(dates[0]);
    }
    return temp;
}


MSBICS.Wrappers.BusinessViewContext = new Object();
MSBICS.Wrappers.BusinessViewContext.ToWrappedString = function(iJSONObj)
{
    var selectors = iJSONObj.SelectedValues.join(MSBICS.Wrappers.INNER_SECTION_SEPERATOR);
    var stores = iJSONObj.StoreList.join(MSBICS.Wrappers.INNER_SECTION_SEPERATOR);
    var seperator = MSBICS.Wrappers.SECTION_SEPERATOR;
    var temp = iJSONObj.PageResourceKey + seperator +
        iJSONObj.UserSettings + seperator +
        iJSONObj.ActiveSelector + seperator +
        selectors + seperator +
        stores + seperator +
        iJSONObj.DateRange + seperator +
        iJSONObj.NavigationReference;
    return temp;
}

MSBICS.Wrappers.TableViewContext = new Object();
MSBICS.Wrappers.TableViewContext.ToWrappedString = function(iJSONObj)
{
    var seperator = MSBICS.Wrappers.SECTION_SEPERATOR;

    var isSortingASC = iJSONObj.IsSortingASC ? "1" : "0";
    var temp = iJSONObj.PageNumber + seperator +
               iJSONObj.PageSize + seperator +
               iJSONObj.SortingColumnKey + seperator +
               isSortingASC + seperator +
               iJSONObj.CacheKey;

    return temp;
}
