﻿/*
    This script allows content to be shown and/or hidden after a certain 
    start/end date.
    
    To use this, add a call to CheckAllTimeSensitiveDivs() onload:
    
        <body onload="CheckAllTimeSensitiveDivs()">
        
    Then enclose expiring content in divs such as this:
    
        <div class="TimeSensitive" id="End.2007.04.10">
            <!-- insert content here that should be hidden after 
                 April 10, 2007 (at 11:59 PM) -->
        </div>
                    
    where the "id" element is used to encode the end date/time of the form:
    
        type.year.month.date.hour.minute.second
        
    and all trailing items after type are optional. Type can be "Start" or "End".
    Missing elements are set to their minimum value (usually 0 or 1) for Start
    items and their maximum values for End items. This allows the natural way
    of expressing that something runs from Start.Date1 to End.Date2 and have the
    two dates be inclusive.
    
    For example, to have something expire on May 19, 2007 at 5 PM, use:
        
        <div class="TimeSensitive" id="End.2007.05.19.17.00">
            <div class="SideBarItemContent">
                <h2><a href="Friends/SpringSale.shtml">Annual Spring Book, Bake and Plant Sale</a></h2>
                <p>Sat. May 19, 2007, 9 AM - 3 PM</p>
            </div>
        </div>
        
    You can also use a start date such as this:
    
        <div class="TimeSensitive" id="Start.2007.04.10">
            <!-- insert content here that should be displayed starting 
                 April 10, 2007 -->
        </div>
                    
    Finally, you can use a start and end date such as this:
    
        <div class="TimeSensitive" id="Start.2007.04.10-End.2007.04.12">
            <!-- insert content here that will only be shown from 
                 April 10th through 12th, 2007 -->
        </div>
                    
    To have two items with the same exact date and time, append another
    item at the end of the string to differentiate them. For example:
    
       End.2007.04.10.23.59.59.A
       End.2007.04.10.23.59.59.B
*/

var displayNone = "none";
var displayInherit = ""; /* can't use "inherit" with IE 6/7 - JavaScript error */

var startDateTime;
var endDateTime;

function CheckAllTimeSensitiveDivs()
{
    // alert("CheckAllTimeSensitiveDivs()");
    CheckAllTimeSensitiveTags("div"); 
}

function CheckAllTimeSensitiveTags(tagName)
{
    // alert("CheckAllTimeSensitiveTags(" + tagName + ")");
    divs = document.getElementsByTagName(tagName); 
    for (var i = 0; i < divs.length; ++i) 
        if (divs[i].className == "TimeSensitive")
            CheckTimeSensitiveElement(divs[i]); 
}

function CheckTimeSensitiveElement(element)
{
    startDateTime = new Date(0, 0, 1);  // init to some time way in the past
    endDateTime = new Date(3000, 0, 1); // init to some time way in the future
    
    // parse into Start and End dates (if any)
    // alert("element.id = " + element.id);
    var dateTimes = element.id.split("-");

    // alert("dateTimes.length = " + dateTimes.length);
    
    // for each date, parse it
    for (var i = 0; i < dateTimes.length; ++i)
        ProcessDateTime(dateTimes[i]);
    
    // now show/hide the element based on the start/end dates
    var currentDateTime = new Date();
    element.style.display = ((currentDateTime < startDateTime) || (currentDateTime > endDateTime)) ? displayNone : displayInherit;
}

function ProcessDateTime(dateTimeString)
{
    // alert("ProcessDateTime(" + dateTimeString + ")");
        
    var strings = dateTimeString.split(".");
    
    var type    = strings[0];
    var year    = (strings.length > 1) ? parseInt(strings[1], 10) : ((type == "Start") ? 0 : 3000);
    var month   = (strings.length > 2) ? parseInt(strings[2], 10) : ((type == "Start") ? 0 : 11);
    var day     = (strings.length > 3) ? parseInt(strings[3], 10) : ((type == "Start") ? 1 : 28);
    var hours   = (strings.length > 4) ? parseInt(strings[4], 10) : ((type == "Start") ? 0 : 23);
    var minutes = (strings.length > 5) ? parseInt(strings[5], 10) : ((type == "Start") ? 0 : 59);
    var seconds = (strings.length > 6) ? parseInt(strings[6], 10) : ((type == "Start") ? 0 : 59);
    
    var dateTime = new Date(year, month-1, day, hours, minutes, seconds, 0);
    
    switch (type)
    {
        case "Start":
            startDateTime = dateTime;
            break;
            
        case "End":
            endDateTime = dateTime;
            break;
            
        default:
            alert("Invalid date type: '" + type + "'. Should be 'Start' or 'End'");
            break;
    }
}
