Months = Array("","January","February","March","April","May","June","July","August","September","October","November","December");

var beginCentury = 1900;
var endCentury = 2100;
var year;
var month;
var days;
var callerId;
var pageTitle;

function daysInMonth(year,month) {
       if ((month==1)||(month==3)||(month==5)||(month==7)||(month==8)||(month==10)||(month==12)) days=31
  else if ((month==4)||(month==6)||(month==9)||(month==11)) days=30
  else if ((((year % 100)==0) && ((year % 400)==0)) || (((year % 100)!=0) && ((year % 4)==0))) days = 29
  else days = 28;
  return days;
};


function createMonthSelect(currentMonth) {
  var selectMonth = "";
  selectMonth = "<select name='month' size='1' onChange='javascript:opener.popUpCal(self.document.Form1.year[self.document.Form1.year.selectedIndex].value,self.document.Form1.month[self.document.Form1.month.selectedIndex].value);'>\r\n";
  for (var i=1; i<=9; i++) {
    selectMonth = selectMonth + "  <option value='0" + i + "'";
    if (i == currentMonth) selectMonth = selectMonth + " selected";
    selectMonth = selectMonth + ">" + Months[i] + "</option>\r\n";
  }

  for (var i=10; i<=12; i++) {
    selectMonth = selectMonth + "  <option value='" + i + "'";
    if (i == currentMonth) selectMonth = selectMonth + " selected";
    selectMonth = selectMonth + ">" + Months[i] + "</option>\r\n";
  }
  

  selectMonth = selectMonth + "</select>\r\n";
  return selectMonth;
}


function createYearSelect(currentYear) {
  var selectedYear = "";
  selectedYear = "<select name='year' size='1' onChange='javascript:opener.popUpCal(self.document.Form1.year[self.document.Form1.year.selectedIndex].value,self.document.Form1.month[self.document.Form1.month.selectedIndex].value);'>\r\n";
  for (var i=beginCentury; i<=endCentury; i++) {
    selectedYear = selectedYear + "  <option value='" + i + "'";
    if (i == currentYear) selectedYear = selectedYear + " selected";
    selectedYear = selectedYear + ">" + i + "</option>\r\n";
  }
  selectedYear = selectedYear + "</select>";
  return selectedYear;
}

function createDaysTable(year,month) {
  var calTable = "<table border='0' cellpadding='2' cellspacing='0' bgcolor='#00D4D2'>\r\n  <tr>";
  var todayIs = new Date();
  todayIs.setYear(year);
  todayIs.setMonth(month-1);
  todayIs.setDate(1);
  ajust = todayIs.getDay();
  calTable = calTable + "\r\n    <td align='center'>Su</td><td align='center'>Mo</td><td align='center'>Tu</td><td align='center'>We</td><td align='center'>Th</td><td align='center'>Fr</td><td align='center'>Sa</td></div>\r\n  <tr>";
  for (var j=1; j<=ajust; j++) {
    calTable = calTable + "\r\n    <td></td>";
  }
  for (var i=1; i<10; i++) {
    calTable = calTable + "\r\n    <td"
    if ((i == dayToday()) && (month == monthToday()) && (year == yearToday())) calTable = calTable + " bgcolor='#00D4D2'";

    calTable = calTable + "><input type='button' value='0" + i + 
    "' onClick='javascript:opener.year=self.document.Form1.year[self.document.Form1.year.selectedIndex].value;" +
    "opener.month=self.document.Form1.month[self.document.Form1.month.selectedIndex].value; opener.day=\"0" + i + 
    "\";self.close();'></td>";
    
     if (((i+ajust) % 7)==0) calTable = calTable + "\r\n  </tr>\r\n\  <tr>";
  }

  for (var i=10; i<=daysInMonth(year,month); i++) {
    calTable = calTable + "\r\n    <td"
    if ((i == dayToday()) && (month == monthToday()) && (year == yearToday())) calTable = calTable + " bgcolor='#FF0000'";
    calTable = calTable + "><input type='button' value='" + i +"' onClick='javascript:opener.year=self.document.Form1.year[self.document.Form1.year.selectedIndex].value;opener.month=self.document.Form1.month[self.document.Form1.month.selectedIndex].value; opener.day=" + i + ";self.close();'></td>";
    if (((i+ajust) % 7)==0) calTable = calTable + "\r\n  </tr>\r\n\  <tr>";
  }
  calTable = calTable + "\r\n  </tr>\r\n</table>";
  return calTable;
}



function popUpCal(year,month) {
  var html = "";
  html = html + "<html>\r\n<head>\r\n  <title>" + pageTitle + "</title>\r\n</head>\r\n<body bgcolor='#00D4D2' onUnload='opener.formatDate();'>\r\n  <div align='center'>\r\n  <form name='Form1'>\r\n";
  html = html + createMonthSelect(month);
  html = html + createYearSelect(year);
  html = html + createDaysTable(year,month);
  html = html + "\r\n  </form>\r\n  </div>\r\n</body>\r\n</html>\r\n";
  calWindow = open("","calendar","width=300,height=250");
  calWindow.document.open();
  calWindow.document.writeln(html);
  calWindow.document.close();
  calWindow.focus();
}

function yearToday() {
  var dateVar = new Date();
  if (navigator.appName == "Netscape") return dateVar.getYear() + 1900
  else return dateVar.getYear();
}

function monthToday() {
  var dateVar = new Date();
  return dateVar.getMonth()+1;
}

function dayToday() {
  var dateVar = new Date();
  return dateVar.getDate();
}

function displayCalander(dateVar,dateField) {
  year = yearToday();
  month = monthToday();
  day = dayToday();
  callerId = dateVar;
  pageTitle = dateField;
  popUpCal(year,month);
}

function formatDate() {
  callerId.value = year + "-" + month + "-" + day;
}

