function myalert(cmsg,showalerts){
	if(showalerts){
		showalerts = confirm(cmsg + "\r\n\r\nClick Cancel to disable future alerts. ");
	}
}

function CalcSeason(ozdate){
var y = ozdate.getFullYear();
var pindx;
var xday = new Date(y-1,11,25)
var dx = days_between(xday,ozdate,0);	// days since xmas last year
var gf = new Date(y,gfm[y - 2004]-1,gfd[y - 2004]);	// days till good friday this year
var dg = days_between(ozdate,gf,0);	// days till good friday this year
pindx = 2;	// default, between easter and xmas
if((dx<=38) || (dx > 365)) {
	pindx = 0;
	return pindx;
	}
if(dg> 0 ) {
	pindx = 1;
	}
	return pindx;
}		 

	//																**********	FUNCTION CALC_UKPUBDATE  *************************

function calc_ukpubdate(ozpubdate){
	//	calculate uk publishing date depending on the day of the week and time of year
	//	Determine the relevant period of the yearas one of 
	//	SeasonalAdjust = 0, corresponding to Christmas until xmaspubdate (about Feb 1)
	//	SeasonalAdjust = 1, corresponding to xmaspubdate until Good Friday
	//	SeasonalAdjust = 2, corresponding to Good Friday until Christmas
	var SeasonalAdjust = CalcSeason(ozpubdate);
	if(SeasonalAdjust==0) {
//		case 0:  // Xmas to Feb 1
			
    	switch(ozpubdate.getDay()){
	    	case 1:	//Monday
	    		pubdiff = 38;
	   		break;
	    	case 2:	//Tuesday	Saturday Crossword
	    		pubdiff = 30;
	    		break;
	    	default:
	   		pubdiff = 37;
    	}
    }else if(SeasonalAdjust==1){
// 		case 1:  // Feb 1 to Good Friday
    	switch(ozpubdate.getDay()) {
	    	case 1:	//Monday 	Saturday Crossword
	    		pubdiff = 30;
	   			break;
	    	default:
	   		pubdiff = 36;
	   		break;
	    	}
	    } else if(SeasonalAdjust==1){
// 		case 2:  // Good Friday to Xmas
	    switch(ozpubdate.getDay()) {
	    	case 1:	//Monday 	Saturday Crossword
	    		pubdiff = 38;
	   			break;
	    	case 2:	//Tuesday 	Saturday Crossword
	    		pubdiff = 31;
	   			break;
	    	default:
	   			pubdiff = 37;
    		}
		}  	
		ukpubdate = new Date();
		ukpubdate.setTime(ozpubdate.getTime() - pubdiff * ONE_DAY);
		
		// disallow xmas and sunday publication in uk
		if((ukpubdate.getDate() == 25) && (ukpubdate.getMonth() == 11)){
			switch(ukpubdate.getDay()){
				case 0:	//Sunday - should not occur
					ukpubdate.setTime(ukpubdate.getTime() + 1 * ONE_DAY); 
				break;
				case 6:  // Saturday - the following Saturday 1st January
					ukpubdate.setTime(ukpubdate.getTime() + 2 * ONE_DAY); 
	   			break;
				case 5:  // Friday - blog on the following Monday 28th December
					ukpubdate.setTime(ukpubdate.getTime() + 3 * ONE_DAY); 
	   			break;
	    	default:	// the next day
					ukpubdate.setTime(ukpubdate.getTime() + 1 * ONE_DAY);
			} 
		}
return ukpubdate;
}
function puzznodescr(ozpuzzno){
	return " [ " + ozpuzzno + " = " + (ozpuzzno + 15813) + "]";
}
	//																**********	FUNCTION SETUP  *************************
function setup(){
	 count1 = 0;
	 count2 = 0;
	 count3 = 0;
	 showalerts = true;
	 mylink = "";

	//	establish today's date
	thisdate = new Date();
	ozpubdate = new Date();
 ONE_DAY = 1000 * 60 * 60 * 24
 // DAYS OF THE WEEK **********
weekday=new Array(7);
weekday[0]="Sunday";
weekday[1]="Monday";
weekday[2]="Tuesday";
weekday[3]="Wednesday";
weekday[4]="Thursday";
weekday[5]="Friday";
weekday[6]="Saturday";
	
baseserialno = 8558;
basedateyr = 2009;
basedatemo = 11;	//December ... Javascript style
basedateday = 7;
basedate = new Date(basedateyr,basedatemo,basedateday);
var dayno = basedate.getDay();
// variables to cope with misfiling of blogs on livejournal - normally default values in case
misfiled = 1111;
misfiledwith = 1112;
// ensure basedate is a sunday with baseserialno corresponding to the day before, even if it is Xmas day
while(dayno!=0)
{
		basedate.setDate(basedate.getDate() - 1);
	baseserialno = baseserialno - 1;

dayno  = basedate.getDay();
}
// calculate date of publication of Xmas day in oz (Feb 1st)
xmaspubdate = new Date(thisdate.getFullYear(),1,1);

// handle good fridays when no papers are published
gfm = new Array(4,3,  4,4, 3,   4,4, 4,4, 3, 4,4, 3, 4, 4, 4, 4,4, 4,4 );	//month of good friday 2004 to 2019
gfd = new Array(9,25,14,6,21,  10,2,22,6,29,18,3,25,14,30,19,10,2,15,7);	//day of good friday 2004 to 2019
/*gfm[0]=4;	//2004
gfd[0]=9;	*/

today= new Date();
fy = today.getFullYear();
// get index for above arrays
gfindex = fy -2008;
// get date of good friday this year
gfdate = new Date(fy,gfm[gfindex],gfd[gfindex]);
var dayssincegf = days_between(today, gfdate);
// IsEaster = 	(dayssincegf >= 1)&& (dayssincegf < 7);
xmasdate = new Date(fy,11,25);
var dayssincexmas = days_between(today, xmasdate);
IsChristmas = 	(dayssincexmas >= 1);
}
	//																**********	FUNCTION ISCHRISTMAS  *************************


function isxmas() {
var today = new Date();
var xmasday = new Date(today.getFullYear(),11,25);
var dayssincexmas = days_between(xmasday,today,0);
return (dayssincexmas >= 1)
}
	//																**********	FUNCTION ISEASTER  *************************

function IsEaster() {
var today = new Date();
var gfriday = new Date(today.getFullYear(),gfm[gfindex]-1,gfd[gfindex]);
var dayssincegf = days_between(gfriday,today,0);
return (dayssincegf <= 6) && (dayssincegf > 0)
}

	//																**********	FUNCTION DAYS_BETWEEN  *************************


function days_between(date1,date2,res_type) {

    // The number of milliseconds in one day
   
    // Convert both dates to milliseconds
    var date1_ms = date1.getTime()
    var date2_ms = date2.getTime()
		//	res_type = 0	returns integer calendar days from date1 to date2
		//	res_type = 1	returns rounded integer days from date1 to date2
		//	res_type = 2	returns integer complete days from date1 to date2
		//	res_type = 3	returns fractional days from date1 to date2
		//	res_type = 4	returns ABSOLUTE (integer calendar days from date1 to date2)
		//	res_type = 5	returns ABSOLUTE (rounded integer days from date1 to date2)
		//	res_type = 6	returns ABSOLUTE (integer complete days from date1 to date2)
		//	res_type = 7	returns ABSOLUTE (fractional days from date1 to date2)

    // Calculate the difference in milliseconds
//    var difference_ms = Math.abs(date2_ms - date1_ms)  // **** ABSOLUTE ****
    var difference_ms = date2_ms - date1_ms
    
    // Convert back to days 
    var fracdays = difference_ms/ONE_DAY;
    // var fracdays = Math.round(difference_ms/ONE_DAY);
    var res
    switch(res_type)
    {
    	case 0:
		//	res_type = 0	returns integer calendar days from date1 to date2
    		res =  Math.round(fracdays + 0.4999);	
    	break;
     	case 1:
		//	res_type = 1	returns rounded integer days from date1 to date2
    		res =  Math.round(fracdays);	
    	break;
    	case 2:
		//	res_type = 3	returns fractional days from date1 to date2
    		res =  Math.floor(fracdays);	
    	break;
    	case 3:
		//	res_type = 3	returns fractional days from date1 to date2
				res = fracdays;
    	break;
    	case 4:
		//	res_type = 4	returns ABSOLUTE (integer calendar days from date1 to date2)
    		res =  Math.round(fracdays + 0.4999);
    		res = Math.abs(res);	
    	break;
    	case 5:
		//	res_type = 5	returns ABSOLUTE (rounded integer days from date1 to date2)
    		res =  Math.round(fracdays);	
    		res = Math.abs(res);	
    	break;
    	case 6:
		//	res_type = 6	returns ABSOLUTE (integer complete days from date1 to date2)
    		res =  Math.floor(fracdays);	
    		res = Math.abs(res);	
    	break;
   		case 7:
		//	res_type = 7	returns ABSOLUTE (fractional days from date1 to date2)
				res = fracdays;
   		res = Math.abs(res);	
     	break;
     	default:
    }
    	return res

}	
		dayname = "";  //global
	//																**********	FUNCTION day_of_week  *************************
//	returns weekday name for any date	object
function day_of_week(anydate) {
return weekday[anydate.getDay()]
}


	//																**********	FUNCTION DAYSAGO  *************************
	function daysago(daysoffset){

	if(daysoffset==0) {msg = "Today\'s Australian - "};
	if(daysoffset==1) {msg = "Yesterday\'s Australian - "};
	if(daysoffset>=2) {msg = "The Australian " + daysoffset + " days ago - "};
	var dd = new Date();	
	dd.setDate (dd.getDate() - daysoffset);
	var zyrno = dd.getFullYear();
	zmthno = dd.getMonth() + 1;
	zdno = dd.getDate();
	dayname = weekday[dd.getDay()];
	ozpuzzno = calcozpuzzno(zyrno,zmthno,zdno);
if (zdno <= 9) zdno = "0" + zdno; 
if (zmthno <= 9) zmthno = "0" + zmthno; 
msg = msg + "  " + dayname + ", " + zdno + "/" + zmthno + "/" + zyrno;
		return msg;
}

	//																**********	FUNCTION SHORTDATE  *************************
function shortdate(anydate) {
	return anydate.getDate() + "/" + (anydate.getMonth() + 1) + "/" + anydate.getFullYear();
}
	
	//																**********	FUNCTION WRITELINK  *************************
function writelink() {
	// calculate oz publication date
 ozpubdate.setTime(thisdate.getTime() - offset * ONE_DAY);
 // exit if a Sunday in oz
 if(ozpubdate.getDay() == 0){
 	return;
}
	// calculate uk publication date
			// calculate publishing delay (pubdiff)
			ukpubdate = calc_ukpubdate(ozpubdate);
			var ozpuzzno = calcozpuzzno(ozpubdate.getFullYear(),ozpubdate.getMonth(),ozpubdate.getDate());
		var isSunday =(ozpubdate.getDay() == 0);

		var isgf = (ozpubdate.getMonth() == gfm[gfindex]) && (ozpubdate.getDate() == gfd[gfindex]);
		var daypuboz = daysago(offset);

		var isXmas = (ozpubdate.getMonth() == 11) && (ozpubdate.getDate() == 25);
		var mthno = ukpubdate.getMonth() + 1;
		if(mthno < 10) mthno = "0" + mthno;
		var dno = ukpubdate.getDate();
		if(dno < 10) dno = "0" + dno;
		var linkstr = ukpubdate.getFullYear() + "/" + mthno + "/" +dno + "/";
if (!isSunday && !isgf && !isXmas) {
//	alert("puzznodescr(ozpuzzno) = " + puzznodescr(ozpuzzno));
	document.write('<A HREF=\"http://community.livejournal.com/times_xwd_times/' + linkstr + '\"><br>' + daypuboz +'</A>' + puzznodescr(ozpuzzno)) + "puzznodescr = " + puzznodescr(ozpuzzno);
if(ozpuzzno == misfiled) document.write("<br><b><font size=3 color=orange>****** The Blog for the above crossword [8598 = 24411] has been misfiled to the page with number " + misfiledwith + "</b></font>");
}
}
	//																**********	FUNCTION DAILYTIMESLINK  *************************
function dailytimeslink() {	//basically a clone of writelink with a link to dailytimeslink.html
	alert("dailylink called");
	// calculate oz publication date
 ozpubdate.setTime(thisdate.getTime() - offset * ONE_DAY);
 // exit if a Sunday in oz
 if(ozpubdate.getDay() == 0){
 	return;
}
	// calculate uk publication date
			// calculate publishing delay (pubdiff)
			ukpubdate = calc_ukpubdate(ozpubdate);
			var ozpuzzno = calcozpuzzno(ozpubdate.getFullYear(),ozpubdate.getMonth(),ozpubdate.getDate());
		var isSunday =(ozpubdate.getDay() == 0);

		var isgf = (ozpubdate.getMonth() == gfm[gfindex]) && (ozpubdate.getDate() == gfd[gfindex]);
		var daypuboz = daysago(offset);

		var isXmas = (ozpubdate.getMonth() == 11) && (ozpubdate.getDate() == 25);
		var mthno = ukpubdate.getMonth() + 1;
		if(mthno < 10) mthno = "0" + mthno;
		var dno = ukpubdate.getDate();
		if(dno < 10) dno = "0" + dno;
		var linkstr = ukpubdate.getFullYear() + "/" + mthno + "/" +dno + "/";
if (!isSunday && !isgf && !isXmas) {
//	document.write('<A HREF=\"dailytimeslink.html"><br>' + daypuboz +'</A>' + puzznodescr(ozpuzzno));
	document.write('<A HREF="http://community.livejournal.com/times_xwd_times/' + linkstr + '\"><br>' + daypuboz +'</A>' + puzznodescr(ozpuzzno));
if(ozpuzzno == misfiled) document.write("<br><b><font size=3 color=orange>****** The Blog for the above crossword [8598 = 24411] has been misfiled to the page with number " + misfiledwith + "</b></font>");
}
}
	//																**********	FUNCTION CALCOZPUZZNO  *************************
function calcozpuzzno(yno,mno,dno)  {
	//	returns Australian crossword serial number, given publication date [month is Javascript style (0 - 11)]
ozdate = new Date(yno,mno,dno);
//Get difference in days
var daysdiff = days_between(basedate,ozdate,0);
var weeksdiff = Math.floor(daysdiff/7);
// reduce serial no. by 1 for each intervening Xmas
var xmascount = 0;
xmascount = yno - basedate.getFullYear();
if((mno == 11) && (dno > 25)) xmascount ++;	// if post xmas in december

// reduce serial no. by 1 if xmas or good friday
var nopub = 0;
if((mno == 11) && (dno == 25)) nopub = 1;
if((mno == gfm[yno - 2010]) && (dno == gfd[yno - 2010])) nopub = 1;

return weeksdiff * 6 + daysdiff % 7 + baseserialno - nopub - xmascount;
}
	//																**********	FUNCTION SHOWSELECTED  *************************
function showselected (form) {
    var selecteddate = form.inputbox.value;
    var spl = selecteddate.split(" ",2);
    var datebits = spl[1].split("/",3);
    var yy = datebits[2].split(" ",1);
    var datepicked = new Date(yy[0],datebits[1]-1,datebits[0]);
    // if published date is Sunday, use the saturday
		if(datepicked.getDay()==0) 
		{
			datepicked.setDate(datepicked.getDate() - 1);
		}
    ukpubdate = calc_ukpubdate(datepicked);
    var ozpuzzno = calcozpuzzno(yy[0],datebits[1]-1,datebits[0]);
    // modify ukpubdate if a sunday to the saturday before
		if(ukpubdate.getDay()==0) 
		{
			ukpubdate.setDate(ukpubdate.getDate() -1);
		}
    // modify link if a saturday to the following saturday
 		if(ukpubdate.getDay()==6) 
		{
			ukpubdate.setDate(ukpubdate.getDate() + 7);
		}
    // Disallow if date precedes first blog on 1/11/2006
   var firstukdate = new Date(2006,10,2);
    if(ukpubdate < firstukdate) {
    	myalert("You have selected an Australian publication date of " + shortdate(datepicked) + " The earliest blog available is for the puzzle published in The London Times on 2/11/2006 which corresponds approximately to 9/12/2006 in The Australian",showalerts);
    	return;
    } 
			mthno = ukpubdate.getMonth() + 1;
			if(mthno < 10) mthno = "0" + mthno;
			dno = ukpubdate.getDate();
			if(dno < 10) dno = "0" + dno;
						
			linkstr = ukpubdate.getFullYear() + "/" + mthno + "/" +dno + "/";
			var mylink = "http://community.livejournal.com/times_xwd_times/" + linkstr;
    window.open(mylink,"_self","status=1,menubar=1,resizable=1,scrollbars=1");
 }
// Title: Timestamp picker
// Description: See the demo at url
// URL: http://us.geocities.com/tspicker/
// Script featured on: http://javascriptkit.com/script/script2/timestamp.shtml
// Version: 1.0
// Date: 12-05-2001 (mm-dd-yyyy)
// Author: Denis Gritcyuk <denis@softcomplex.com>; <tspicker@yahoo.com>
// Notes: Permission given to use this script in any kind of applications if
//    header lines are left unchanged. Feel free to contact the author
//    for feature requests and/or donations
	function getcal() {
		document.getElementById("mytext").value='';
		document.getElementById("myf").style.visibility='hidden';
		show_calendar('document.myform.inputbox', document.myform.inputbox.value);
	}

function show_calendar(str_target, str_datetime) {
	str_datetime = "";
	var arr_months = ["January", "February", "March", "April", "May", "June",
		"July", "August", "September", "October", "November", "December"];
	var week_days = ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"];
	var n_weekstart = 1; // day week starts from (normally 0 or 1)

	var dt_datetime = (str_datetime == null || str_datetime =="" ?  new Date() : str2dt(str_datetime));
	var dt_prev_month = new Date(dt_datetime);
	dt_prev_month.setMonth(dt_datetime.getMonth()-1);
	var dt_next_month = new Date(dt_datetime);
	dt_next_month.setMonth(dt_datetime.getMonth()+1);
	var dt_firstday = new Date(dt_datetime);
	dt_firstday.setDate(1);
	dt_firstday.setDate(1-(7+dt_firstday.getDay()-n_weekstart)%7);
	var dt_lastday = new Date(dt_next_month);
	dt_lastday.setDate(0);
	
	// html generation (feel free to tune it for your particular application)
	// print calendar header
	var str_buffer = new String (
		"<html>\n"+
		"<head>\n"+
		"	<title>Calendar</title>\n"+
		"</head>\n"+
		"<body bgcolor=\"White\">\n"+
		"<table class=\"clsOTable\" cellspacing=\"0\" border=\"0\" width=\"100%\">\n"+
		"<tr><td bgcolor=\"#4682B4\">\n"+
		"<table cellspacing=\"1\" cellpadding=\"3\" border=\"0\" width=\"100%\">\n"+
		"<tr>\n	<td bgcolor=\"#4682B4\"><a href=\"javascript:window.opener.show_calendar('"+
		str_target+"', '"+ dt2dtstr(dt_prev_month)+"');\">"+
		"<img src=\"images/prev.gif\" width=\"16\" height=\"16\" border=\"0\""+
		" alt=\"previous month\"></a></td>\n"+
		"	<td bgcolor=\"#4682B4\" colspan=\"5\">"+
		"<font color=\"white\" face=\"tahoma, verdana\" size=\"2\">"
		+arr_months[dt_datetime.getMonth()]+" "+dt_datetime.getFullYear()+"</font></td>\n"+
		"	<td bgcolor=\"#4682B4\" align=\"right\"><a href=\"javascript:window.opener.show_calendar('"
		+str_target+"', '"+dt2dtstr(dt_next_month)+"');\">"+
		"<img src=\"images/next.gif\" width=\"16\" height=\"16\" border=\"0\""+
		" alt=\"next month\"></a></td>\n</tr>\n"
	);

	var dt_current_day = new Date(dt_firstday);
	// print weekdays titles
	str_buffer += "<tr>\n";
	for (var n=0; n<7; n++)
		str_buffer += "	<td bgcolor=\"#87CEFA\">"+
		"<font color=\"white\" face=\"tahoma, verdana\" size=\"2\">"+
		week_days[(n_weekstart+n)%7]+"</font></td>\n";
	// print calendar table
	str_buffer += "</tr>\n";
	while (dt_current_day.getMonth() == dt_datetime.getMonth() ||
		dt_current_day.getMonth() == dt_firstday.getMonth()) {
		// print row heder
		str_buffer += "<tr>\n";
		for (var n_current_wday=0; n_current_wday<7; n_current_wday++) {
				if (dt_current_day.getDate() == dt_datetime.getDate() &&
					dt_current_day.getMonth() == dt_datetime.getMonth())
					// print current date
					str_buffer += "	<td bgcolor=\"#FFB6C1\" align=\"right\">";
				else if (dt_current_day.getDay() == 0 || dt_current_day.getDay() == 6)
					// weekend days
					str_buffer += "	<td bgcolor=\"#DBEAF5\" align=\"right\">";
				else
					// print working days of current month
					str_buffer += "	<td bgcolor=\"white\" align=\"right\">";

				if (dt_current_day.getMonth() == dt_datetime.getMonth())
					// print days of current month
					str_buffer += "<a href=\"javascript:window.opener."+str_target+
					".value='"+dt2dtstr(dt_current_day)+"';window.opener.document.myform.style.visibility='visible'; window.close();\">"+
					"<font color=\"black\" face=\"tahoma, verdana\" size=\"2\">";
				else 
					// print days of other months
					str_buffer += "<a href=\"javascript:window.opener."+str_target+
					".value='"+dt2dtstr(dt_current_day)+"'; window.close();\">"+
					"<font color=\"gray\" face=\"tahoma, verdana\" size=\"2\">";
				str_buffer += dt_current_day.getDate()+"</font></a></td>\n";
				dt_current_day.setDate(dt_current_day.getDate()+1);
		}
		// print row footer
		str_buffer += "</tr>\n";
	}
	// print calendar footer
	str_buffer +=
		"<form name=\"cal\">\n<tr><td colspan=\"7\" bgcolor=\"#87CEFA\">"+
		"<font color=\"White\" face=\"tahoma, verdana\" size=\"2\">"+
		"Time: <input type=\"text\" name=\"time\" value=\""+dt2tmstr(dt_datetime)+
		"\" size=\"8\" maxlength=\"8\"></font></td></tr>\n</form>\n" +
		"</table>\n" +
		"</tr>\n</td>\n</table>\n" +
		"</body>\n" +
		"</html>\n";

	var vWinCal = window.open("", "Calendar", 
		"width=200,height=250,status=no,resizable=yes,top=200,left=200");
	vWinCal.opener = self;
	var calc_doc = vWinCal.document;
	calc_doc.write (str_buffer);
	calc_doc.close();
}
// datetime parsing and formatting routimes. modify them if you wish other datetime format
function str2dt (str_datetime) {
	var re_date = /^(\d+)\-(\d+)\-(\d+)\s+(\d+)\:(\d+)\:(\d+)$/;
	if (!re_date.exec(str_datetime))
		return alert("Invalid Datetime format: "+ str_datetime);
	return (new Date (RegExp.$3, RegExp.$2-1, RegExp.$1, RegExp.$4, RegExp.$5, RegExp.$6));
}
function dt2dtstr (dt_datetime) {
	var auspuzzno = calcozpuzzno(dt_datetime.getFullYear(),dt_datetime.getMonth(),dt_datetime.getDate());

return (new String (
			weekday[dt_datetime.getDay()] + ", " + dt_datetime.getDate()+"/"+(dt_datetime.getMonth()+1)+"/"+dt_datetime.getFullYear()+" [" + puzznodescr(auspuzzno)));
}
function dt2tmstr (dt_datetime) {
	return (new String (
			dt_datetime.getHours()+":"+dt_datetime.getMinutes()+":"+dt_datetime.getSeconds()));
}


