
// last publication date initialization
var month = 3 - 1;
var publicationDate = new Date(2006, month, 18);

// redirect initialization
var errorURL   = "../../resources/html/pagenotfound.html";
var pathPrefix = "/";
var target     = window.parent;
var targetURL  = "/";

var xmlhttpP;



function gotoPublicationByIndex( dayIndex, type )
{
	// Sunday = 0;
	// Monday = 1;
	// ....
	// ...
	// Saturday = 6;

	// 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 );
}

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 );
}

function gotoPublicationByUrl( url )
{
  targetURL = url;

  xmlhttpP = getXMLHTMLRequest();

  xmlhttpP.open("HEAD", url ,true);
  xmlhttpP.onreadystatechange = handleHttpResponse;
  xmlhttpP.send(null)
}

function handleHttpResponse()
{
  if( xmlhttpP.readyState == 4 )
  {
    if( xmlhttpP.status == 200 )
    {
//      alert( "URL [" + targetURL + "] exists\ntarget = [" + target + "]" );
      target.location = targetURL;
    }
    else if( xmlhttpP.status == 404 )
    {
//      alert( "URL [" + targetURL + "] doesn't exist\nerror page = [" + errorURL + "]" );
      target.location = errorURL;
    }
    else alert( "status is " + xmlhttp.status );
  }
}