// Copyright 1995-2008 (C) Otto de Voogd - http://www.7is7.com/
// Original URL: http://www.7is7.com/otto/datediff.html
//
// ALL RIGHTS RESERVED

// Date Functions for weekday.html and datediff.html

var greg=1582;

function calcWeekDay(form) {
	var jd;
	var wd;

	// parse input
	yy = parseInt(form.year.value,10)
	if (!isNaN(yy)) {
		form.year.value=yy
	}

	mm = parseInt(form.month.value,10)
	if (!isNaN(mm)) {
		form.month.value=mm
	}

	dd = parseInt(form.day.value,10)
	if (!isNaN(dd)) {
		form.day.value=dd
	}

	hh = parseInt(form.hour.value,10)
	if (!isNaN(hh)) {
		form.hour.value=hh
	}

	// check year
	/*
	if (yy<1) {
		form.leap.value="n/a"
		form.result.value="undefined"
		alert("This algorithm does not work for B.C. dates!")
		form.year.focus()
		form.year.select()
		return 0
	}
	*/

	if (yy>greg) {
		ly=(!(yy%4)-!(yy%100)+!(yy%400))
	} else {
		ly=!(yy%4)
	}

	// check month
	if (mm<1||mm>12) {
		form.result.value="error"
		alert("Month must be a value between 1 and 12!")
		form.month.focus()
		form.month.select()
		return 0
	}

	if ( (dd<1) || 
	     (dd>31) ||
	     ((mm==4||mm==6||mm==9||mm==11)&&dd>30) ||
	     (mm==2&&dd>(28+ly)) ) {
		form.result.value="error"
		alert("Date is not valid for given month!")
		form.day.focus()
		form.day.select()
		return 0
	}

	if (hh<0||hh>23) {
		form.result.value="error"
		alert("Hour must be a value between 0 and 23!")
		form.hour.focus()
		form.hour.select()
		return 0
	}

	// Gregorian changes

	yys = yy + mm/100 + dd/10000

	// days skipped
	// TODO: Japan introduced the Gregorian calendar in 1873
	// but had an entirely different system before that.
	if (
		(
			// Catholic Nations with the Pope
			greg==1582 && yys > 1582.1004 && yys < 1582.1015
		) || (
			// Catholics 2 France, Lorraine
			greg==1582.2 && yys > 1582.1209 && yys < 1582.1220
		) || (
			// Catholics 3 Holland, Brabant, Flanders
			greg==1582.3 && yys > 1582.1221 && yys < 1583.0101
		) || (
			// Prussia
			greg==1610 && yys > 1610.0822 && yys < 1610.0902
		) || (
			// Protestant Germany, Denmark and Norway
			greg==1700 && yys > 1700.0218 && yys < 1700.0301
		) || (
			// British Empire
			greg==1752 && yys > 1752.0902 && yys < 1752.0914
		) || (
			// Sweden
			greg==1753 && yys > 1753.0217 && yys < 1753.0301
		) || (
			// Alaska after purchase from Russia
			// coincided with a shift of the dateline.
			greg==1867 && yys > 1867.1006 && yys < 1867.1018
		) || (
			// Russia after revolution
			greg==1918 && yys > 1918.0131 && yys < 1918.0214
		)
	) {
		form.leap.value="n/a"
		form.result.value="undefined"
		alert("The given date was skipped during the introduction of the Gregorian calendar!")
		form.day.focus()
		form.day.select()
		return 0
	}

	// Let the calculating begin
	if (mm<3) {
		mm += 12
		yy -= 1
	}

	// Gregorian correction
	if (
		(
			greg==1582 && yys >= 1582.1015
		) || (
			greg==1582.2 && yys >= 1582.1220
		) || (
			greg==1582.3 && yys >= 1583.0101
		) || (
			greg==1610 && yys >= 1610.0902
		) || (
			greg==1700 && yys >= 1700.0301
		) || (
			greg==1752 && yys >= 1752.0914
		) || (
			greg==1753 && yys >= 1753.0301
		) || (
			greg==1867 && yys >= 1867.1018
		) || (
			greg==1918 && yys >= 1918.0214
		)
	) {
		gc=2-Math.floor(0.01*yy)+Math.floor(0.0025*yy)
	} else {
		gc=0
	}

	jd=Math.floor(365.25*yy)+Math.floor(30.6001*(mm+1))+dd+1720994.5+gc+(hh/24)

	wd=Math.floor(jd+0.5-(7*Math.floor((jd+0.5)/7)))

	msg = ''

	if (wd==0) {msg = 'Monday'}
	if (wd==1) {msg = 'Tuesday'}
	if (wd==2) {msg = 'Wednesday'}
	if (wd==3) {msg = 'Thursday'}
	if (wd==4) {msg = 'Friday'}
	if (wd==5) {msg = 'Saturday'}
	if (wd==6) {msg = 'Sunday'}
	
	if (!isNaN(yy)) {
		form.leap.value=(ly==1)?"yes":"no"
	}

	form.result.value=msg
	if (!isNaN(jd)) {
		form.julian.value=Math.floor(jd*100+0.5)/100
	}

	// An approximation of lunar age (reality is more complex):
	if (typeof(form.lunarage)!="undefined") {
		var latmp = (jd - 2415020.75933)/29.53058868;
		la = Math.floor((latmp - Math.floor(latmp))*100+0.5);
		if (!isNaN(la)) {
			form.lunarage.value=la+"%";
		}
	}

	return(jd);
}

// TIAF!
