/**
 * DATE-UTILS.JS
 *
 * Used to navigate to other publication dates.
 * ATTENTION: This script is updated daily. Do not make changes in the
 *  generated date-utils.js as these changes will be overridden in the
 *  near future.
 *
 * Dependent javascript libraries:
 *  - url-utils.js
 *
 * Author         Dave Snyckers [X-Cago BV]
 * Version        1.2
 */


// most recent publication date
var month = 12 - 1;
var publicationDate = new Date( 2005, month, 22 );

// redirect constants
var pathPnf    = "../../resources/html/pagenotfound.html";
var pathPrefix = "/";
var target     = window.parent;
var targetURL  = "";

// xml http request
var xmlhttpReq;



/**
 * Goto publication by index.
 * Calculates the exact date based on the most recent publication date.
 *
 * @param dayIndex  the day index
 * @param type      the publication type
 */
function gotoPublicationByIndex( dayIndex, type )
{
					
	// take last publication date as starting point
	msDate = publicationDate.valueOf();
	diff = 0;

	if( publicationDate.getDay() > dayIndex )
		diff = publicationDate.getDay() - dayIndex;
	else
	{
		if( publicationDate.getDay() < dayIndex )
		  diff = 7 - ( dayIndex - publicationDate.getDay() );
		else
			diff = 0;
	}

	msDate = msDate - diff * 24 * 60 * 60 * 1000;
	date = new Date( msDate );

	gotoPublicationByDate( date, type );
}

/**
 * Goto publication by date.
 * Constructs the publication path based on the date.
 *
 * @param date    the publication date
 * @param type    the publication type
 */
function gotoPublicationByDate( date, type )
{
	pageURL = pathPrefix;
	pageURL = pageURL + date.getFullYear();

	if ( ( date.getMonth() + 1 ) > 9 )
		pageURL = pageURL + ( date.getMonth() + 1 );
	else
		pageURL = pageURL + '0' +  ( date.getMonth() + 1 );

	if ( date.getDate() > 9 )
		pageURL = pageURL + date.getDate() + '/';
	else
		pageURL = pageURL + '0' + date.getDate() + '/';

	pageURL = pageURL + type + '/index.html';

	gotoPublicationByUrl( pageURL );
}

/**
 * Goto publication by url.
 *
 * @param url  the publication url
 */
function gotoPublicationByUrl( url )
{
  targetURL = url;
  xmlhttpReq = getXMLHTMLRequest();

  xmlhttpReq.open( "HEAD", url ,true );
  xmlhttpReq.onreadystatechange = handleHttpResponse;
  xmlhttpReq.send( null );
}

/**
 * Handles the http response.
 */
function handleHttpResponse()
{
  if( xmlhttpReq.readyState == 4 )
  {
    if( xmlhttpReq.status == 200 )
    {
      target.location = targetURL;
    }
    else if( xmlhttpReq.status == 404 )
    {
      target.location = pathPnf;
    }
    else alert( "status is " + xmlhttpReq.status );
  }
}