// DOLIST:
// juliaanse variant mogelijk maken
// julgreg switchdate implementeren, zodat standaard voor 15 Okt 1582 de juliaanse wordt getoond
// the "mode" moet kunnen zijn: "julian, "gregorian", "auto"  of "yyyy-mm-dd", in laatste geval
// is dat een alternatieve switchdate (de eerste Greg datum opgeven), "auto" switcht op 1582-10-15
// dan tekst in gregcal.htm aanpassen omtrent de calendar button die dan niet meer altijd greg is
// e.e.a. kan worden geimplementeerd als properties van het document.all.pickadate object
// zoals b.v. julgregmode reeds geimplementeerd

// =================================================================================================
// YOU must rewrite the function datepicked(year,month,day) to handle the date that was picked up
// from the calendar; the one below is just a default:
// -------------------------------------------------------------------------------------------------

function datepicked (year, month, day)
{
	alert((new Date(Date.UTC(year,month-1,day))).toUTCString().split(" ").slice(0,4).join(" "))
}

// =================================================================================================
// invoke the next function somewhere in the BODY of the HTML document before invoking pickadate:
// -------------------------------------------------------------------------------------------------

function initpickadate()
{
	document.write ('<span id=pickadate julgregmode="auto" style="position:absolute;left:10;top:10;z-index:99;visibility:hidden"></span>')

	// in both "Auto" functions below:
	// mode = "julian", "gregorian", "auto" or "yyyymmdd" of first Greg date, default = "auto"
	// (only first letters "a", "j", and "g" are significant, e.g. "anything" is equivalent to "auto")

	Date.defaultFirstGregDate = 15821015

	Date.UTCParseJulian = function (y,m,d)
	{	var M = m+1, Y = (M < 3 ? y-1 : y)
		var J = 1721117 + Y*365 + Math.floor(Y/4) + Math.round(((M+9)%12)*30.6) + d
		return (J - 2440588) * 86400000
	}

	Date.UTCParseAuto = function (y,m,d,mode)
	{	var K = (arguments.length < 4 ? "" : (""+mode).toLowerCase() ); if (K == "") K = "a"
		var C = "ajg".indexOf(K.charAt(0))
		if (C < 1) {C = ( (y*100 + m+1)*100 + d < (C < 0 ? parseInt(K) : Date.defaultFirstGregDate) ? 1 : 2) }
		return (C > 1 ? Date.UTC (y,m,d) : Date.UTCParseJulian (y,m,d) )
	}

	Date.prototype.getUTCJulianDate = function ()
	{	var K, Y, M, D = 2440588 + Math.floor(this.valueOf()/86400000) - 1
		D -= 1721117		; K = Math.floor(D/1461)
		D -= K*1461		; Y = Math.min(3,Math.floor(D/365))
		D -= Y*365		; Y += K*4
		M = Math.floor(D/30.6)	; D -= Math.round(M*30.6) - 1
		M = (M + 2) % 12	; if (M < 2) Y++
		K = M % 5		; if (M > 0 && (K == 0 || K == 3) && D == 31) { D = 1; M++}
		return [Y,M,D,false]
	}

	Date.prototype.getUTCGregorianDate = function() {return [this.getUTCFullYear(),this.getUTCMonth(),this.getUTCDate(),true] }

	Date.prototype.getUTCAutoDate = function (mode)
	{	var Y = this.getUTCFullYear(), M = this.getUTCMonth(), D = this.getUTCDate()
		var K = (arguments.length < 1 ? "" : (""+mode).toLowerCase() ); if (K == "") K = "a"
		var C = "ajg".indexOf(K.charAt(0))
		if (C < 1) {C = ( (Y*100 + M+1)*100 + D < (C < 0 ? parseInt(K) : Date.defaultFirstGregDate) ? 1 : 2) }
		return (C > 1 ? this.getUTCGregorianDate() : this.getUTCJulianDate())
	}
}

// =================================================================================================
// next function displays a small calendar from which a date can be picked.
// month and year optionally specify which month is initially displayed, default is taken from
// system date. the optional "day" argument specifies a day to be highlighted on first display.
// -------------------------------------------------------------------------------------------------

function pickadate (month, year, day)
{
	var mm = ["January","February","March","April","May","June","July","August","September","October","November","December"]
	var dd = ["Mon","Tues","Wednes","Thurs","Fri","Satur","Sun"]
	var T  = new Date()
	var N  = arguments.length
	var Y  = (N > 1 ? 1*year      : T.getFullYear() )
	var M  = (N > 0 ? 1*month - 1 : T.getMonth()    )
	Y += Math.floor (M/12)
	M = (M % 12 + 12) % 12
	var A  = new Date (Date.UTCParseAuto (Y,M,1,document.all.pickadate.julgregmode) )
	var D = (A.getUTCDay() + 6) % 7
	var K = 24 * 60 * 60 * 1000
	var X = new Date (A.valueOf() - D*K)
	var x = "<table cellspacing=0 cellpadding=3 onclick='event.cancelBubble=true;return false;'>"
	x += "<tr><td bgcolor=ivory align=center>"
	x += "<table border=1 cellspacing=0 cellpadding=2 style='font:12pt Arial; color:darkblue' bgcolor=skyblue>"
	x += "<tr><td colspan=7>"
		x += "<table width=100% cellspacing=0 cellpadding=0 bgcolor=lightskyblue><tr>"
		x += "<td align=left>"
		x += "<input type=button value='=' style='font-weight:bold;font-size:8pt;height:16pt'"
		x += " title='current month'  onclick='setTimeout(\"pickadate()\",10);'>"
		x += "</td><td align=center style='font-size:14pt;color:darkblue'>"
		x += "<b>" + mm[M] + " " + Y + "</b></td>"
		x += "<td align=right>"
		x += "<input type=button value='X' style='font-weight:bold;font-size:8pt;height:16pt'"
		x += " title='close calendar' onclick='document.all.pickadate.style.visibility=\"hidden\"'>"
		x += "</td></tr></table>"
	x += "</td></tr><tr>"
	for (var D = 0; D < 7; D++)
	{	x += "<td align=center width=30 style='font:10pt Arial'"
		if (D > 4) x += " bgcolor=deepskyblue"
		x += "><b>" + dd[D].slice(0,3) + "</b></td>"
	}
	R = 0
	D = 0
	x += "</tr>"
	XX = X.getUTCAutoDate (document.all.pickadate.julgregmode)
	while (X < A || XX[0]*100 + XX[1] <= Y*100 + M || X.getDay() != 1)
	{	T = (new Date()).getUTCAutoDate (document.all.pickadate.julgregmode)
		XY = XX[0]; XM = XX[1]; XD = XX[2]
		var highlight = (N > 2 && XY == 1*year && XM == 1*month-1 && XD == 1*day)
		var y = [XY,XM+1,XD,XX[3]]
		var titletext = dd[D] + "day " + XD + " " + mm[XM] + " " + XY
		if (!XX[3] )
		{	titletext += " Julian\n(will be converted to "
			titletext += X.getUTCDate() + " " + mm[X.getUTCMonth()] + " " + X.getUTCFullYear()
			titletext += " Gregorian\nif you select it)"
		}
		y.push (XX[3] )
		if (D == 0) x += "<tr height=25pt>"
		x += "<td align=center"
		var bgcolor = (highlight ? "yellow" : "skyblue")
		if (!XX[3] )
		{	if (!highlight) bgcolor = (D > 4 ? "silver" : "lightgrey")
			x += " bgcolor=" + bgcolor
		} else if (D > 4)
		{	if (!highlight) bgcolor = "deepskyblue"
			x += " bgcolor=" + bgcolor
		} else if (highlight)
		{	x += " bgcolor=" + bgcolor
		}
		x += " style='cursor:hand"
		if (XM != M) x += "; font:8pt Arial; color:black"
		if (XY==T[0] && XM==T[1] && XD==T[2] ) x += "; font-weight:bold"
		x += "'"
		x += " title='" + titletext + "'"
		x += " onmouseover='this.style.backgroundColor=\"bisque\"'"
		x += " onmouseout='this.style.backgroundColor=\"" + bgcolor + "\"'"
		x += " onclick='document.all.pickadate.style.visibility=\"hidden\";"
		x += "datepicked(" + y + ")"
		x += "'>" + XD + "</td>"
		if (D == 6) {x += "</tr>"; R++}
		X = new Date (X.valueOf() + K)
		D = (D + 1) % 7
		XX = X.getUTCAutoDate (document.all.pickadate.julgregmode)
	}
	x += "<tr height=" + [177,152,127,102,77,52,25][R] + "pt><td colspan=7 valign=bottom>"
		x += "<table width=100% cellspacing=0 cellpadding=0 bgcolor=lightskyblue><tr>"
		x += "<td align=left>"
		x += "<input type=button value='<<<<' title='100 years backward' style='font-weight:bold;font-size:8pt;height:16pt'"
		x += " onclick='setTimeout(\"pickadate(" + (M+1) + "," + (Y-100) + ")\",10);'>"
		x += "<input type=button value='<<<'  title='10 years backward'  style='font-weight:bold;font-size:8pt;height:16pt'"
		x += " onclick='setTimeout(\"pickadate(" + (M+1) + "," + (Y- 10) + ")\",10);'>"
		x += "<input type=button value='<<'   title='1 year backward'    style='font-weight:bold;font-size:8pt;height:16pt'"
		x += " onclick='setTimeout(\"pickadate(" + (M+1) + "," + (Y-  1) + ")\",10);'>"
		x += "<input type=button value='<'    title='1 month backward'   style='font-weight:bold;font-size:8pt;height:16pt'"
		x += " onclick='setTimeout(\"pickadate(" + (M  ) + "," + (Y    ) + ")\",10);'>"
		x += "</td><td align=right>"
		x += "<input type=button value='>'    title='1 month forward'    style='font-weight:bold;font-size:8pt;height:16pt'"
		x += " onclick='setTimeout(\"pickadate(" + (M+2) + "," + (Y    ) + ")\",10);'>"
		x += "<input type=button value='>>'   title='1 year forward'     style='font-weight:bold;font-size:8pt;height:16pt'"
		x += " onclick='setTimeout(\"pickadate(" + (M+1) + "," + (Y+  1) + ")\",10);'>"
		x += "<input type=button value='>>>'  title='10 years forward'   style='font-weight:bold;font-size:8pt;height:16pt'"
		x += " onclick='setTimeout(\"pickadate(" + (M+1) + "," + (Y+ 10) + ")\",10);'>"
		x += "<input type=button value='>>>>' title='100 years forward'  style='font-weight:bold;font-size:8pt;height:16pt'"
		x += " onclick='setTimeout(\"pickadate(" + (M+1) + "," + (Y+100) + ")\",10);'>"
		x += "</td></tr></table>"
	x += "</td></tr></table>"
	x += "</td></tr></table>"
	with (document.all.pickadate)
	{	if (style.visibility != "visible")
		{	style.pixelLeft = document.body.scrollLeft + event.x - 10
			style.pixelTop  = document.body.scrollTop  + event.y - 10
		}
		innerHTML = x
		style.visibility = "visible"
	}
}
