function checkInt(e) {
	if(isNaN(e.value)) {
		alert("Please enter numbers only");
		e.value = "";
	}
}

function initMe() {
	setCalendar();
	displayDate();
}

//*****************************************************************************
// Do not remove this notice.
//
// Copyright 2001 by Mike Hall.
// See http://www.brainjar.com for terms of use.
//*****************************************************************************

//============================================================================
// This code handles the calendar display and interaction.
//============================================================================

// Default target date to today.

var targetDate = new Date();
var firstAvailDate = new Date();
targetDate.setDate(firstAvailDate.getDate() + 21);
firstAvailDate.setDate(firstAvailDate.getDate() + 21);
firstAvailDate.setHours(0,0,0,0);


// Initialize the calendar display once the page loads.

//----------------------------------------------------------------------------
// This function updates the calendar display to reflect the current target
// date.
//----------------------------------------------------------------------------

function setCalendar(event) {

	var el, tableEl, rowEl, cellEl, linkEl;
	var tmpDate, tmpDate2;
	var i, j;

//	Force a redraw by hiding the entire page.

// 	document.body.style.display = "none";

//	Update month name.
	el = document.getElementById("calendarHeader").firstChild;
	el.nodeValue = targetDate.getMonthName() + "\u00a0" + targetDate.getFullYear();

	// Start with the first day of the month and go back if necessary to
	// the previous Sunday.
	tmpDate = new Date(Date.parse(targetDate));
	tmpDate.setDate(1);
	while (tmpDate.getDay() != 0) {
		tmpDate.addDays(-1);
	}

  // Go through each calendar day cell in the table and update it.

	tableEl = document.getElementById("calendar");
	for (i = 2; i <= 7; i++) {
		rowEl = tableEl.rows[i];

		// Hide row if it contains no dates in the current month.	
		tmpDate2 = new Date(Date.parse(tmpDate));
		tmpDate2.addDays(6);
		if (tmpDate.getMonth() != targetDate.getMonth() && tmpDate2.getMonth() != targetDate.getMonth()) {
			rowEl.className = "empty";
		} else {
			rowEl.className = "";
		}
		
		// Loop through a week.		
		for (j = 0; j < rowEl.cells.length; j++) {
			cellEl = rowEl.cells[j];
			linkEl = cellEl.firstChild;
		
			if (cellEl.oldClass == null) {
				cellEl.oldClass = cellEl.className;
			}

			if (tmpDate.getMonth() == targetDate.getMonth()) {
				linkEl.date = new Date(Date.parse(tmpDate));
				s = tmpDate.toString().split(" ");
				linkEl.title = s[0] + " " + s[1] + " " + s[2] + ", " + s[s.length - 1];
				
				//Set day
				linkEl.firstChild.nodeValue = tmpDate.getDate();
		
				if (j == 0 || j == 6) {
					cellEl.className = cellEl.oldClass + " weekend";
				} else {
					if(tmpDate >= firstAvailDate) {
						if (Date.parse(tmpDate) == Date.parse(targetDate)) {
							cellEl.className = cellEl.oldClass + " target";
						} else {
							cellEl.className = cellEl.oldClass;
						}
	
						linkEl.style.visibility = "";
					} else { 
						cellEl.className = cellEl.oldClass + " noclicky";
					}
				}
			} else {
				linkEl.style.visibility = "hidden";
			}
		
			// Highlight the current date.	

			
			// Go to the next day.
			tmpDate.addDays(1);
		}
	}
}

//----------------------------------------------------------------------------
// Event handlers for the calendar elements.
//----------------------------------------------------------------------------

function addMonths(event, n) {

  // Advance the calendar month and update the display.

  targetDate.addMonths(n);
  setCalendar(event);
}

function addYears(event, n) {

  // Advance the calendar year and update the display.

  targetDate.addYears(n);
  setCalendar(event);
}

function setTargetDate(event, link) {
	if (link.parentNode.className.indexOf("weekend") >= 0) {
		alert("Maintenance is scheduled Monday through Friday, excluding holidays.");
	} else if (link.parentNode.className.indexOf("noclicky") >= 0) {
		alert("Online maintenance scheduling must be made 3 weeks in advance or later");
	} else {
		if (link.date != null) {
			targetDate = new Date(Date.parse(link.date));
			setCalendar(event);
			displayDate(event);
		}
	}
}

function displayDate(event) {

	var mm, dd, yyyy;

	mm = String(targetDate.getMonth() + 1);
	while (mm.length < 2) {
		mm = "0" + mm;
	}

	dd = String(targetDate.getDate());
	while (dd.length < 2) {
		dd = "0" + dd;
	}
	yyyy = String(targetDate.getFullYear());
	while (yyyy.length < 4) {
		yyyy = "0" + yyyy;
	}

	document.getElementById("daten_day").value = dd;
	document.getElementById("daten_month").value = mm;
	document.getElementById("daten_year").value = yyyy;
}

function formatDate() {

  var mm, dd, yyyy;

  // Return the target date in "mm/dd/yyyy" format.

  mm = String(targetDate.getMonth() + 1);
  while (mm.length < 2)
    mm = "0" + mm;
  dd = String(targetDate.getDate());
  while (dd.length < 2)
    dd = "0" + dd;
  yyyy = String(targetDate.getFullYear());
  while (yyyy.length < 4)
    yyyy = "0" + yyyy;

  return mm + "/" + dd + "/" + yyyy;
}

//============================================================================
// Add new properties and methods to the Date object.
//============================================================================

// Properties

Date.prototype.monthNames = new Array("January", "February", "March", "April",
  "May", "June", "July", "August", "September", "October", "November",
  "December");
Date.prototype.savedDate  = null;

// Methods

Date.prototype.getMonthName = dateGetMonthName;
Date.prototype.getDays      = dateGetDays;
Date.prototype.addDays      = dateAddDays;
Date.prototype.addMonths    = dateAddMonths;
Date.prototype.addYears     = dateAddYears;

//----------------------------------------------------------------------------
// getMonthName(): Returns the name of the date's month.
//----------------------------------------------------------------------------

function dateGetMonthName() {

  return this.monthNames[this.getMonth()];
}

//----------------------------------------------------------------------------
// getDays(): Returns the number of days in the date's month.
//----------------------------------------------------------------------------

function dateGetDays() {

  var tmpDate, d, m;

  tmpDate = new Date(Date.parse(this));
  m = tmpDate.getMonth();
  d = 28;
  do {
    d++;
    tmpDate.setDate(d);
  } while (tmpDate.getMonth() == m);

  return d - 1;
}

//----------------------------------------------------------------------------
// addDays(n): Adds the specified number of days to the date.
//----------------------------------------------------------------------------

function dateAddDays(n) {

  // Add the specified number of days.

  this.setDate(this.getDate() + n);

  // Reset the new day of month.

  this.savedDate = this.getDate();
}

//----------------------------------------------------------------------------
// addMonths(n): Adds the specified number of months to the date, adjusting
// the day of the month if necessary.
//----------------------------------------------------------------------------

function dateAddMonths(n) {

  // Save the day of month if not already set.

  if (this.savedDate == null)
    this.savedDate = this.getDate();

  // Set the day of month to the first to avoid rolling.

  this.setDate(1);

  // Add the specified number of months.

  this.setMonth(this.getMonth() + n);

  // Restore the saved day of month, if possible.

  this.setDate(Math.min(this.savedDate, this.getDays()));
}

//----------------------------------------------------------------------------
// addYears(n): Adds the specified number of years to the date, adjusting the
// day of the month for leap years.
//----------------------------------------------------------------------------

function dateAddYears(n) {

  // Save the day of month if not already set.

  if (this.savedDate == null)
    this.savedDate = this.getDate();

  // Set the day of month to the first to avoid rolling.

  this.setDate(1);

  // Add the specified number of years.

  this.setFullYear(this.getFullYear() + n);

  // Restore the saved day of month, if possible.

  this.setDate(Math.min(this.savedDate, this.getDays()));
}
