﻿var WeekTools = {
    secPerWeek: 7 * 24 * 60 * 60,
    months: new Array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'),

    // offset a date by a number of days
    AddDays: function (adate, days) {
        return new Date(adate.getTime() + (days * 86400000));
    },

    // offset a date by a number of minutes
    AddMinutes: function (adate, minutes) {
        return new Date(adate.getTime() + (minutes * 1000 * 60));
    },

    // return the week number for a given date
    Yearweek: function (now) {
        // assume that the week begins on monday getDay() returns 0-6, 6=saturday
        var nyd = new Date(now.getFullYear(), 0, 1);

        var nyd_dow = nyd.getDay();

        // if Jan 1 of this year wasn't on a monday, then it is part of week 52 from the 
        // prior year.  add a few days to find the first monday of this year.
        var firstMonday = this.AddDays(nyd, (8 - nyd_dow) % 7);

        return 1 + Math.ceil((now.getTime() / 1000 - firstMonday.getTime() / 1000) / this.secPerWeek);
    },

    // given a weeknumber in the form YYYYNN, return the date of the first day of that week.
    // weeks begin on saturday
    DateOfWeeknum: function (weeknumber) {
        var curDate = new Date();
        // First, split weeknumber into the year component and the week component.  for example the weeknumber 201013
        // has a year component of 2010 and a week component of 13.
        var NN = weeknumber.slice(4);
        var YYYY = weeknumber.slice(0, 4);

        // create a new date variable based on the year portion only in order to get the DATE of the first day of the year
        // to do this, convert the year component (2010) to an int and use the Date() function like this:
        // var newyearsday = Date(parseInt(YYYY), 0, 1);
        // Where YYYY is the year component of weeknumber
        var newyearsday = new Date(parseInt(YYYY), 0, 1);
        // find the date of the first saturday by doing the same thing as the 'var firstMonday' line in the Yearweek function
        var nyd_dow = newyearsday.getDay();
        var firstSaturday = this.AddDays(newyearsday, (8 - nyd_dow) % 7);
        // Now convert the week coponent of the weenumber to an int and multiply it by 7 in order to get the number of days in
        // that number of weeks.

        // Hack to fix parseInt bug failing on 08 and 09
        if (NN.indexOf('0') == 0) NN = NN.substring(1, 2);

        var noOfDays = parseInt(NN) * 7;
        // now use the AddDays function to add the number of days to the date of the first saturday.
        var newnoOfDays = noOfDays - 7;
        curDate = WeekTools.AddDays(firstSaturday, newnoOfDays);
        //var curWeekMon = curDate.getDate();
        // SAH 7/28/10: return the date to the caller, let them format as desired.  This is to support localization, etc.       
        //var newDate = new Date();
        //newDate = curDate.format("M/d/yyyy");
        //return newDate; 
        // return curWeekMon;
        return curDate;
    },

    // given a weeknumber in the form YYYYNN, return just the week (NN) portion.
    // leading zeros should be stripped from the return value (i.e. week "01" should be returned as "1")
    WeekOfWeeknum: function (weeknumber) {
        var weekNum = weeknumber.slice(4);
        return "Week " + weekNum.replace(/^[0]+/g, "");
    },

    // given a weeknumber in the form YYYYNN, return a string of formatted like "Week No".  Remove leading zeros.
    WeeknumLabel: function (weeknumber) {
        var NN = weeknumber.slice(4);
        var YYYY = weeknumber.slice(0, 4);
        var weekLabel = NN + '/' + YYYY;
        return weekLabel.replace(/^[0]+/g, "");
    },

    isCutOff: function (weekdate) {
        var myCSR = jQuery('#ctl00_wpm_SearchPage_ctl00_IsCSR').attr("value");
        var now = new Date();

        //var testDate = this.AddDays(now,-1);

        if (myCSR == "1") {
            weekdate = this.AddDays(weekdate, -2);
        }
        else {
            weekdate = this.AddDays(weekdate, -5);
            weekdate = this.AddMinutes(weekdate, -1 * 6.5 * 60); // Tuesday at 4 so subtracting 8 hours from 12AM
        }
        //   return weekdate < testDate;
        return weekdate < now;
    }


};

