// Name:	issue.js
// Purpose:	This script calculates and displays the number and date of the current issue.
// Syntax:	writeCurrentIssue(target) or writeCurrentIssue()
// 		target = target document; defaults to the current document if unspecified
// Author:	This script was written by Will Hesse and Tim Cole - Inspired Web Design.

// Constants

// The date and number of the first issue
// (although in practise it can be any issue number/date combination)
// Note that the issue date is put on the same day
// of the week as this date, so it should be a Friday.
var firstIssueNumber = 1008;
var firstIssueDate = new Date(2002, 0, 11); // YYYY, M(M), D(D)
// NOTE that January is month zero - 
// so you have to subtract one from the month number.
// Uncomment the next line to check it's a Friday
// if ( firstIssueDate.getDay() != 5 ) { alert('Warning!\nThe first issue date is not a Friday:\n' + firstIssueDate); }

// The number of milliseconds per week
var msPerWeek = 1000 * 60 * 60 * 24 * 7;

// Use global variables to store these so they are only calculated once
// and can be accessed from elsewhere
var iIssueNumber; // as an integer
var dIssueDate = new Date(); // as a date
var cIssueDate = new Date(); // next issue as date
var sIssueNumber; // as a string
var sIssueDate; // as a string
var nIssueNumber; // next issue as a string
var nIssueDate; // next issue date as a string


function writeCurrentIssue(target) {
  if ( ! target ) { target = document; }
  target.writeln(sIssueNumber + ' - ' + sIssueDate);
}

function writeNextIssue(target) {
  if ( ! target ) { target = document; }
  nIssueNumber = (sIssueNumber*1)+1; // Convert current issue to integer and increment
  target.writeln(nIssueNumber + ' - ' + nIssueDate);
}

function calculateCurrentIssue(testDate) {
  // Get the current date
  var now = new Date();
  if ( testDate ) {
    now = testDate;
    document.writeln('Testing date ' + now + '... ');
  }

  // Calculate the issue number
  iIssueNumber = firstIssueNumber + ( now - firstIssueDate ) / msPerWeek; // Dates work in milliseconds
  iIssueNumber = parseInt(iIssueNumber); // Chop off any remainder (i.e. round down)

  // Calculate the issue date
  dIssueDate = now;
  while ( dIssueDate.getDay() != firstIssueDate.getDay() ) {
    dIssueDate.setDate( dIssueDate.getDate() -1 );}

  // Build the strings to display from the numerical variables
  sIssueNumber = ''+iIssueNumber; // Convert integer to string (for what it's worth)

  sIssueDate = '';
  if ( dIssueDate.getDate() < 10 ) { sIssueDate += '0'; }
  sIssueDate += dIssueDate.getDate();
  sIssueDate += '/';
  if ( dIssueDate.getMonth() < 9 ) { sIssueDate += '0'; }
  sIssueDate += (dIssueDate.getMonth() + 1); // +1 is because January is zero
  sIssueDate += '/';
  sIssueDate += ('' + dIssueDate.getFullYear() ).substring(2, 4); // last two digits

  cIssueDate = dIssueDate;
  cIssueDate.setDate( dIssueDate.getDate() +7 );
  
  nIssueDate = '';
  if ( cIssueDate.getDate() < 10 ) { nIssueDate += '0'; }
  nIssueDate += cIssueDate.getDate();
  nIssueDate += '/';
  if ( cIssueDate.getMonth() < 9 ) { nIssueDate += '0'; }
  nIssueDate += (cIssueDate.getMonth() + 1); // +1 is because January is zero
  nIssueDate += '/';
  nIssueDate += ('' + cIssueDate.getFullYear() ).substring(2, 4); // last two digits
}

// Calculate the current issue once (when the script loads)
// so we don't have to recalculate it each time
// calculateCurrentIssue(new Date(2002, 0, 11)); // test mode
calculateCurrentIssue(); // normal mode
// If you want to override the default settings (without modifying the script)
// you can always call this function again (from the HTML) with a different parameter.

