var calTodaysDate = new Date();
var allowBefore = true;
var allowAfter = true;

function initializeCalendar(elementDate, elementMonth, elementYear, selectedDate, setAllowBefore, setAllowAfter) {
  allowBefore = setAllowBefore;
  allowAfter = setAllowAfter;
  if (setAllowBefore) {
    elementYear.options[selectedDate.getFullYear() - 2000].selected = true;
  } else {
    elementYear.options[0].selected = true;
  }
  populateMonthField(elementDate, elementMonth, elementYear);
  if (setAllowBefore) {
    elementMonth.options[selectedDate.getMonth()].selected = true;
  } else {
    elementMonth.options[0].selected = true;
  }
  populateDateField(elementDate, elementMonth, elementYear);
  if (setAllowBefore) {
    elementDate.options[selectedDate.getDate() - 1].selected = true;
  } else {
    elementDate.options[0].selected = true;
  }
}

function populateMonthField(elementDate, elementMonth, elementYear) {
  var currentSelection = elementMonth.options[elementMonth.selectedIndex].value;

  // Get rid of the previous items in the list
  var listLength = elementMonth.length;
  for (var i = 0; i < listLength; i++) {
    elementMonth.options[0] = null;
  }
  if (calTodaysDate.getFullYear() == elementYear.options[elementYear.selectedIndex].text) {
    // Only show months to today if it is the current year
    if (allowBefore) {
      for (var i = 0; i <= calTodaysDate.getMonth(); i++) {
        elementMonth.options[i] = new Option(i);
        setMonthTextAndValue(elementMonth, i + 1, i);
      }
    }
    if (allowAfter) {
      for (var i = calTodaysDate.getMonth(); i < 12; i++) {
        var index = i - calTodaysDate.getMonth();
        elementMonth.options[index] = new Option(index);
        setMonthTextAndValue(elementMonth, i + 1, index);
      }
    }
  } else {
    for (var i = 0; i < 12; i++) {
      elementMonth.options[i] = new Option(i);
      setMonthTextAndValue(elementMonth, i + 1, i);
    }
  }
  var selected = false;
  for (var i = 0; i < elementMonth.length; i++) {
    if (elementMonth.options[i].value == currentSelection) {
      elementMonth.options[i].selected = true;
      selected = true;
      break;
    }
  }
  if (!selected) {
    elementMonth.options[0].selected = true;
  }
  populateDateField(elementDate, elementMonth, elementYear);
}

function setMonthTextAndValue(elementMonth, whichOne, index) {
  var monthString;
  switch (whichOne) {
    case 1 : monthString = "January";
             break;
    case 2 : monthString = "February";
             break;
    case 3 : monthString = "March";
             break;
    case 4 : monthString = "April";
             break;
    case 5 : monthString = "May";
             break;
    case 6 : monthString = "June";
             break;
    case 7 : monthString = "July";
             break;
    case 8 : monthString = "August";
             break;
    case 9 : monthString = "September";
             break;
    case 10: monthString = "October";
             break;
    case 11: monthString = "November";
             break;
    case 12: monthString = "December";
             break;
  }
  elementMonth.options[index].text = monthString;
  elementMonth.options[index].value = whichOne;
}

function populateDateField(elementDate, elementMonth, elementYear) {
  var currentSelection = elementDate.options[elementDate.selectedIndex].value;
  var beginDate = 0;
  var daysInMonth;

  if (calTodaysDate.getFullYear() == elementYear.options[elementYear.selectedIndex].text &&
     (calTodaysDate.getMonth() + 1) == elementMonth.options[elementMonth.selectedIndex].value) {
    // Only show days to today if it is the current year and current month selected
    if (allowBefore && !allowAfter) {
      daysInMonth = calTodaysDate.getDate();
    } else {
      beginDate = calTodaysDate.getDate() - 1;
      nextMonthFirstDay = new Date(elementYear.options[elementYear.selectedIndex].text, elementMonth.options[elementMonth.selectedIndex].value, 1);
      selectedMonthLastDay = new Date(nextMonthFirstDay - 86400000);
      daysInMonth = selectedMonthLastDay.getDate();
    }
  } else {
    // First day of the next month from the one chosen
    // This is accomplished because the month passed into the Date() function
    //     is zero ('0') based and if greater than 11 goes to the next year
    nextMonthFirstDay = new Date(elementYear.options[elementYear.selectedIndex].text, elementMonth.options[elementMonth.selectedIndex].value, 1);

    // selectedMonthLastDay is the last day of the month chosen
    //     (86400000 is the number of milliseconds in a day)
    selectedMonthLastDay = new Date(nextMonthFirstDay - 86400000);
    daysInMonth = selectedMonthLastDay.getDate();
  }
  // Get rid of the previous items in the list
  listLength = elementDate.length;
  for (var i = 0; i < listLength; i++) {
    elementDate.options[0] = null;
  }
  // Set the list to the numbers of days in the month selected
  var index = 0;
  for (var i = beginDate; i < daysInMonth; i++) {
    elementDate.options[index] = new Option(i + 1);
    elementDate.options[index].value = i + 1;
    index++;
  }
  var selected = false;
  for (var i = 0; i < elementDate.length; i++) {
    if (elementDate.options[i].value == currentSelection) {
      elementDate.options[i].selected = true;
      selected = true;
      break;
    }
  }
  if (!selected) {
    elementDate.options[0].selected = true;
  }
}

var calDate = null;
var calMonth = null;
var calYear = null;
var calWin = null;
var dontOpenAgain = false;

function openCalendar(elementDate, elementMonth, elementYear, e, allowBefore, allowAfter) {
  calDate = elementDate;
  calMonth = elementMonth;
  calYear = elementYear;

  w = screen.width - 350;
  h = screen.height - 220;
  x = e.screenX;
  y = e.screenY;
  if (x > w) {
    x = x - 350;
  }
  if (y > h) {
    y = y - 220;
  }
  var calendarUrl = "/include/calendar.jsp";
  if (calMonth != null && calYear != null) {
    calendarUrl = calendarUrl + "?y=" + calYear.options[calYear.selectedIndex].value + "&m=" + calMonth.options[calMonth.selectedIndex].value + "&b=" + allowBefore + "&a=" + allowAfter + "&z=1";
  }
  calWin = window.open(calendarUrl, 'Calendar', 'width=350,height=220,top=' + y + ',left=' + x);
}

function setDate(date, month, year) {
  calYear.options[year - calYear.options[0].value].selected = true;
  populateMonthField(calDate, calMonth, calYear);
  calMonth.options[month - calMonth.options[0].value].selected = true;
  populateDateField(calDate, calMonth, calYear);
  calDate.options[date - calDate.options[0].value].selected = true;
  calWin.close();
  calWin = null;
}

function openTextCalendar(elementText, e, allowBefore, allowAfter) {
  calDate = elementText;

  w = screen.width - 350;
  h = screen.height - 220;
  x = e.screenX;
  y = e.screenY;
  if (x > w) {
    x = x - 350;
  }
  if (y > h) {
    y = y - 220;
  }
  var month = calDate.value.substring(0, 2);
  var year = calDate.value.substring(6, 10);
  if (year < calTodaysDate.getFullYear() ||
     (year == calTodaysDate.getFullYear() && month < (calTodaysDate.getMonth() + 1))) {
    month = calTodaysDate.getMonth() + 1;
    year = calTodaysDate.getYear();
  }
  var calendarUrl = "/include/calendar.jsp?y=" + year + "&m=" + month + "&b=" + allowBefore + "&a=" + allowAfter + "&z=0";
  calWin = window.open(calendarUrl, 'Calendar', 'width=350,height=220,top=' + y + ',left=' + x);
}

function setTextDate(newDate) {
  calDate.value = newDate;
  calWin.close();
  calWin = null;
}

function closeCalendar() {
  if (calWin != null) {
    calWin.close();
    calWin = null;
  }
}

