var SECONDS_PER_MINUTE	= 60;
var SECONDS_PER_HOUR		= SECONDS_PER_MINUTE * 60;
var SECONDS_PER_DAY			= SECONDS_PER_HOUR   * 24;
var SECONDS_PER_MONTH		= SECONDS_PER_DAY    * 28;	// Define for shortest month
var SECONDS_PER_YEAR		= SECONDS_PER_DAY    * 365;	// Define for shortest year

var cookiePath = "/";
//var cookieDomain = ".DaysUntil.com";

//var cookieDomain = ".daysuntil.com";
//+++ 2nd copy of this in setTimeZone.js
var cookieVersion = 20;

var gNow   = new Date();
var gReloadHour;
var gReloadMinute;
var gReloadSecond;

			function getLocalTime(offsetGMT)
				{
				// Set the local time to the PHP timezone (in case one was selected and stored in a cookie)
				var local_mS = gNow.getTime();
				var offset_local_sec = gNow.getTimezoneOffset() * -60; // Convert to seconds and change sign to match PHP time zone convention
				var remote_mS = local_mS - (offset_local_sec - gOffsetRemoteSec)*1000;
				gNow.setTime(remote_mS);
				return gNow;
				}

			function updateTime()
				{
				if (this.DEBUG_ALERTS) alert ("Starting updateTime() function")

				// Get the current local time
				gNow=new Date();

				// When the time reaches the reload time, reload the page
				// +++ what if we miss a second???  Should we check for > the reload time ... how?
				if (gNow.getHours() == gReloadHour)
					if (gNow.getMinutes() == gReloadMinute)
						if (gNow.getSeconds() == gReloadSecond)
						  document.location.reload(true);

				// Get the local time
				getLocalTime();

				var nowTimeElement=document.getElementById("nowTime");
				var nowDateElement=document.getElementById("nowDate");

				nowTimeElement.innerHTML=getTimeString(gNow);
				//+++ don't updating date because we can't handle it
				//    for other than the gregorian calendar
				//    +++ put code back in to refresh the page at midnight instead
				//nowDateElement.innerHTML=getDateString(gNow);

				var t=setTimeout("updateTime()",1000);
				}

			function updateDates()
				{
				if (this.DEBUG_ALERTS) alert ("Starting updateDates() function")

				// Get the local time
				getLocalTime();


				var nextChange = finishUpdate();		// Update the rest of the dates

				var t=setTimeout("updateDates()",nextChange);
				}


//+++ since gNow is a global, why do we need to pass it into updateDate (now parameter)???
			function updateDate(date, id, now, resolution)
				{
				//ID_Labour-Day-NZ0
				date.calc(now);
				var element=document.getElementById(id);

				//document.title = "updateDate " + now;

				if (resolution == SECONDS_PER_DAY)
					{
					date.calcDeltas(ONE_DAY, 0);
					}
				if (resolution == SECONDS_PER_HOUR)
					{
					date.calcDeltas(ONE_HOUR, 0);
					}
				if (resolution == SECONDS_PER_MINUTE)
					{
					date.calcDeltas(ONE_MINUTE, 0);
					}
				if (resolution == 1)
					{
					date.calcDeltas(ONE_SECOND, 0);
					}
				var tempString = date.deltaString;

				if (date.type == ACTIVE)
					{
					if (date.endDateFlag)
						tempString = tempString + "left of";
					else
						tempString = tempString + "since";
					}
				else if (date.type == WAITING)
					{
					if (date.endDateFlag)
						tempString = tempString + "until the end of";
					else
						tempString = tempString + "until";
					}
				else if (date.type == COMPLETE)
					if (date.endDateFlag)
						tempString = tempString + "since the end of";
					else
						tempString = tempString + "since the start of";
				element.innerHTML = tempString;
			  }

//			function getMonthName(month)
//				{
//				var months=new Array("January","February","March","April","May","June","July","August","September","October","November","December")
//				return months[month]
//				}
			function getMonthName(date)
				{
				var months=new Array("January","February","March","April","May","June","July","August","September","October","November","December")
				var month = date.getMonth()
				return months[month]
				}
			
			function getMonthName2(month)
				{
				var months=new Array("January","February","March","April","May","June","July","August","September","October","November","December")
				return months[month]
				}

			function getDayName(date)
				{
				var days=new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday")
				var day = date.getDay()
				return days[day]
				}

			function addLeading(number, leadChar)
				{
				if (number > 9)
				  return number
				else
				  return leadChar+number
				}

			function getTimeString(date)
				{
				var $hours = date.getHours();
				var $ampm = "am";
				if ($hours > 12)
					{
					$hours = $hours - 12;
					$ampm = "pm";
					}
				if ($hours == 0)
					{
					$hours = 12;
					$ampm = "pm";
					}

				var $timeString =
					$hours   + ":"    +
					addLeading(date.getMinutes(), "0") + ":"    +
					addLeading(date.getSeconds(), "0") + " " + $ampm;

			    return $timeString
			    }

			function getDateString(date)
				{
				var datePostfix=new Array("0","st","nd","rd","th");

				// Get day of month, and index to postfix array
				var $dayOfMonth = date.getDate();
				var $postFixIndex = $dayOfMonth;
				if ($postFixIndex > 4)
					$postFixIndex = 4;

				var $dateString =
					getDayName(date)   + " "  +
					getMonthName(date) + " "  +
					$dayOfMonth + datePostfix[$postFixIndex] + ", " +
					date.getFullYear();

			    return $dateString
				}

			function getDateTimeString(date)
				{
				var datePostfix=new Array("0","st","nd","rd","th");

				// Get day of month, and index to postfix array
				var $dayOfMonth = date.getDate();
				var $postFixIndex = $dayOfMonth;
				if ($postFixIndex > 4)
					$postFixIndex = 4;

				var $hours = date.getHours();
				var $ampm = "am";
				if ($hours > 12)
					{
					$hours = $hours - 12;
					$ampm = "pm";
					}
				if ($hours == 0)
					{
					$hours = 12;
					$ampm = "pm";
					}

			    var $dateString =
					$hours   + ":"    +
					addLeading(date.getMinutes(), "0") + ":"    +
					addLeading(date.getSeconds(), "0") + " " + $ampm + " on " +
					getDayName(date)   + " "  +
					getMonthName(date) + " "  +
					$dayOfMonth + datePostfix[$postFixIndex] + ", " +
					date.getFullYear();

			    return $dateString
			    }

//===========================================================

			function enableButton(buttonId)
				{
			  //alert ("categoriesChanged() xxx");
				document.getElementById(buttonId).disabled=false;
				}
			function selectAll(elementArrayName)
				{
				var element=document.getElementsByName(elementArrayName);

				for (counter = 0; counter < element.length; counter++)
					element[counter].checked=true;
				}

			function clearAll(elementArrayName)
				{
				var element=document.getElementsByName(elementArrayName);

				for (counter = 0; counter < element.length; counter++)
					element[counter].checked=false;
				}

			// replace this with above ++++++
			function selectAllCategories()
				{
				var categoriesSelectElem=document.getElementsByName("categories[]");

				for (counter = 0; counter < categoriesSelectElem.length; counter++)
					categoriesSelectElem[counter].checked=true;

				document.getElementById("saveButton").disabled=false;
				}

			function clearAllCategories()
				{
				var categoriesSelectElem=document.getElementsByName("categories[]");

				for (counter = 0; counter < categoriesSelectElem.length; counter++)
					{
					categoriesSelectElem[counter].checked=false;
					}
				document.getElementById("saveButton").disabled=false;
				}

			function submitForm() //+++ is this used???
				{
				// Get an array of elments with the given name
				// "_x" is because in IE you can't name the variable the same as the ID name!!!
				category_form_x=document.getElementById("daysUntilForm");
				category_form_x.submit();
				}

function calendarTypeChanged()
	{
	var calendarType = document.getElementById("calendarTypeSelect").value;
	setCookie("dateFormat"+cookieVersion, calendarType, 365);
	document.location.reload(true);
	}

function calendar2ndTypeChanged()
	{
	var calendarType = document.getElementById("calendar2ndTypeSelect").value;
	setCookie("dateFormatSecondary"+cookieVersion, calendarType, 365);
	document.location.reload(true);
	}


function setCookie(c_name,value,expiredays)
	{
//	var exdate=new Date();
//	exdate.setDate(exdate.getDate() + expiredays);
//	gmt_expiration_time = time.gmtime(time.time() + (sess.session_timeout * 60))
//	expireDate = time.strftime("%a, %d-%b-%Y %H:%M:%S GMT", gmt_expiration_time);

	var cookieDomain = document.domain;

//	alert ("document.domain=" + cookieDomain);

		var date = new Date();
		date.setTime(date.getTime()+(expiredays*24*60*60*1000));
		document.cookie=c_name+ "=" + escape(value) +
	  ";path=" + cookiePath +
	  ";domain=" + cookieDomain +
		((expiredays==null) ? "" : ";expires="+date.toGMTString());


		//+++ expires is obsolete...start using "; max-age=" + (60*60*24*365)
		//    Safari doesn't support it yet
//    ((expiredays==null) ? "" : ";max-age="+expiredays*60*60*24);

//	  ((expiredays==null) ? "" : ";expires="+exdate);
//	xxxx=c_name+ "=" + escape(value) +
//	  ";path=" + cookiePath +
//	  ";domain=" + cookieDomain +
//	  ((expiredays==null) ? "" : ";max-age="+expiredays*60*60*24);
//	alert("setCookie()="+xxxx);
	}

// These function are used by the close category and date images to
// tell the form processor what to hide
function closeCategory(category)
	{
	document.getElementById("closeCategory").value = category;
	document.getElementById("daysUntilForm").submit();
	}
function closeDate(date)
	{
	document.getElementById("closeDate").value = date;
	document.getElementById("daysUntilForm").submit();
	}


//function calendarMonthChanged(link, selectId)
function calendarDateChanged(link, selectId)
	{
	var sep = '?';
	if (link.indexOf(sep) >= 0)
		sep = '&';
	location.href = link + sep + 'date=' + document.getElementById(selectId).value;
	return false;
	}

//function calendarYearChanged(link, selectId)
//	{
//	location.href = link + '?date=' + document.getElementById(selectId).value;
//	return false;
//	}
