// Do not remove/modify this header.
// Dated-Cookie v1.0
// Copyright (c) Ravi Jayagopal 2002 || Ravi@CyberConneXions.com
// This script is available at www.CyberConneXions.com/dated_cookie.html


// Function: GetDate
// Parameters: iDaysToAdd
// Returns: Cookie Date in "mmmm dd, yyyy" format, +/- iDaysToAdd

var todaysDate;
var expiryDate;

function GetDate( iDaysToAdd ) {

var theDate = GetDateFromCookie( iDaysToAdd );
return theDate;

}

// Function: WriteDate
// Parameters: iDaysToAdd
// Returns: null
// Notes: Write out the Cookie date in "mmmm dd, yyyy" format +/- iDaysToAdd in hard-coded format

function WriteDate( ) {

todaysDate = GetTimeString(new Date());
expiryDate = GetDateFromCookie(0);

if (todaysDate == expiryDate)
{
	PutDateInCookie(5);
	var expiryDate = GetDateFromCookie(0);
//	document.write( "Date in the Cookie = " + expiryDate);
}
}

// Function: GetTimeString
// Parameters: time - type Date
// Returns: Date from time variable in "mmmm dd, yyyy" format

function GetTimeString( time ) {

var months=new Array( 12 );

months[0]="January";
months[1]="February";
months[2]="March";
months[3]="April";
months[4]="May";
months[5]="June";
months[6]="July";
months[7]="August";
months[8]="September";
months[9]="October";
months[10]="November";
months[11]="December";

if ( time == null ) { time = new Date(); }

var result=null;
var lmonth=months[ time.getMonth() ];
var dDate=time.getDate();
var year=time.getYear();

if (year < 2000) { year = year + 1900; }

result = lmonth + " " + dDate + ", " + year;

return result;

}


// Function: GetToday
// Parameters: iDaysToAdd
// Returns: Today's date in "mmmm dd, yyyy" format, +/- iDaysToAdd


function GetToday( iDaysToAdd ) {

var time=new Date();
var result=null;

if ( iDaysToAdd == null ) { iDaysToAdd = 0;}

time.setTime( time.getTime() + ( iDaysToAdd * 86400000 ) );

result = GetTimeString( time );

return result;

}


// Function: PutDateInCookie
// Parameters: iDaysToAdd
// Returns: null
// Function: Set the page cookie to Today +/- iDaysToAdd

function PutDateInCookie( iDaysToAdd ) {

var expDate = new Date();
var expiry = 0 * 24 * 60 * 60 * 1000;

// Set to expire in 5 days
// To modify, change the 31 in the above formula to be the # of days until expiry

expDate.setTime( expDate.getTime() + expiry );
document.cookie = " DatedCookie=" + escape( GetToday(iDaysToAdd) ) + "; expires=" + expDate.toGMTString();

}

// Function: DeleteDateCookie
// Parameters: none
// Returns: null
// Function: Deletes the cookie
// Notes: Testing Function

function DeleteDateCookie() {
// Test function to clear out the cookie

var expDate = new Date();
var twoDays =  2 * 24 * 60 * 60 * 1000;

expDate.setTime( expDate.getTime() - twoDays );
document.cookie = " DatedCookie=Expiring; expires=" + expDate.toGMTString();

}

// Function: GetDateFromCookie
// Parameters: iDaysToAdd
// Returns: null
// Function: Cookie Date in "mmmm dd, yyyy" format, +/- iDaysToAdd

function GetDateFromCookie(iDaysToAdd) {

var result=null;
var searchName = " DatedCookie="; // May want to change this to something more descriptive
var theCookie = " " + document.cookie + ";";
var startOfCookie = theCookie.indexOf( searchName );
var endOfCookie;
var cookieTime=null;

if (startOfCookie != -1) {	
	// Cookie Found
	startOfCookie += searchName.length; //Skip past cookie name
	endOfCookie = theCookie.indexOf( ";", startOfCookie );
	cookieTime = unescape( theCookie.substring( startOfCookie, endOfCookie ) );
//	alert("cookieTime = " + cookieTime);
	result = cookieTime;
}	
else {
	// Cookie not found, add it with current date
	PutDateInCookie();
	result = unescape(GetToday( iDaysToAdd) );
}

return result;

}