/*
function startup() {
      m_idx = this.document.cal.m;
      y_idx = this.document.cal.y;
}

function getSelectedMonth() {
        return m_idx.selectedIndex;
}
function getSelectedYear() {
        return thisyear+y_idx.selectedIndex;
}
*/
function montharray(m0, m1, m2, m3, m4, m5, m6, m7, m8, m9, m10, m11) {
   this[0] = m0; this[1] = m1; this[2] = m2;
   this[3] = m3; this[4] = m4; this[5] = m5;
   this[6] = m6; this[7] = m7; this[8] = m8;
   this[9] = m9; this[10] = m10; this[11] = m11;
}
function MakeArray(n) {
  this.length = n; return this;
}
var Days = new MakeArray(7);
var Months = new MakeArray(12);
var showmonth, showyear, showdate, showtime;
Days[1]="Sunday"; Days[2]="Monday"; Days[3]="Tuesday";   Days[4]="Wednesday";
Days[5]="Thursday"; Days[6]="Friday"; Days[7]="Saturday";
Months[1]="January"; Months[2]="February"; Months[3]="March";   Months[4]="April";
Months[5]="May"; Months[6]="June"; Months[7]="July";   Months[8]="August";
Months[9]="September"; Months[10]="October"; Months[11]="November";
Months[12]="December";
function initialize()
{
   var background = "#FFFFFF", satcol = "#8E9FD9", suncol = "#FF0000";
   showmonth = getSelectedMonth();
   showyear = getSelectedYear();
   today = new Date();
   firstDay = new Date(showyear, showmonth, 1);
   var monthDays = new montharray(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
   year = firstDay.getYear();
   if(year<1900) year+=1900;
   if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) {
      monthDays[1] = 29;
   }
   nDays = monthDays[firstDay.getMonth()];
   startDay = firstDay.getDay();
   checkDate = today.getDate();
   if (checkDate>nDays) checkDate = nDays;
   var text = "<form name=cal>";
   text = "<table border=0 cellpadding=1><tr><th>Su</th><th>Mo</th><th>Tu</th><th>We</th><th>Th</th><th>Fr</th><th>Sa</th>";
   text += "</tr><tr>";
   column = 0;
   for (i=0; i<startDay; i++) {
      text += "<td><br></td>";
      column++;
   }
   for (i=1; i<=nDays; i++) {
      text += "<td>";
      text += "<center><font size=-1><a class=cali href='javascript:void(null)' onClick='showdate=" + i + ";writetodatestr();'>";
      if (column == 0) text += "<font color=" + suncol + ">";
      else if (column == 6) text += "<font color=" + satcol + ">";
      text += i;
      if (column == 6||column == 0) text += "</font>";
      if (i==checkDate) { showdate = i; }
      text += "</a></font></center>";
      text += "</td>";
      if(i>=nDays) {
        while(column<6) {
            text += "<td><br></td>";
            column++;
        }
      }
      column++;
      if (column == 7) {
         text += "</tr>\n<tr>";
         column = 0;
      }
   }
   text += "</tr></table>Today is " + getDate(today) + "</div><br>";
   //document.write(text);
    document.getElementById('calen').innerHTML=text;
/*
   text = "<br><font size=-2><b>and the time on that day</b></font><br><table border=0 cellpadding=1><tr>";
   k = 0;
   checkHour = today.getHours()+1;
   if (checkHour>23) checkHour = 23;
   for (i=0; i<24; i++) {
       for (j=0; j<60; j+=60) {
          if (i==0 && j==0) text += "<td><font size=-2><b>Early<br>Morning</b></font></td>";
          else if (i==6 && j==0) text += "</tr>\n<tr><td><font size=-2><b>Morning</b></font></td>";
          else if (i==12 && j==0) text += "</tr>\n<tr><td><font size=-2><b>Afternoon</b></font></td>";
          else if (i==18 && j==0) text += "</tr>\n<tr><td><font size=-2><b>Evening</b></font></td>";
          text += "<td align=center><font size=-2><a href='javascript:void(null)' onClick='showtime=" + k + "'>";
          if (i<10) text += "0";
          text += i + ":";
          if (j<10) text += "0";
          text += j;
          if (k==checkHour) { showtime = k; }
          text += "</a></font></td>";
          k++;
       }
 }
   text += "</tr></table></center><center><font size=-1><br>Click <input type=button value='Done' onClick=calculate()></font></center></form>";
   document.write(text);
*/
}
function getMonth(theDate) {
    var year = theDate.getYear();
    if(year<1900) year+=1900;
    return Months[theDate.getMonth()+1] + " " + year;
}
function getDate(theDate) {
    var year = theDate.getYear();
    if(year<1900) year+=1900;
    return Months[theDate.getMonth()+1] + " " + theDate.getDate() + ", " + year;
}
function addto(field, myvalue) {
mybox=parent.opener.document.cf.elements[field];
if (mybox.value=="") { comma=""; } else { comma=","; }
mybox.value += comma + myvalue;
}
function calculate() {
    var current = new Date();
    var newdate = new Date(showyear, showmonth, showdate, showtime);
    var mindiff =  Math.round((newdate-current)/(1000*60));
    if (mindiff<0) {
        alert("The time that you have selected is in past.");
        return;
    }
    if (mindiff<10) {
        alert("The time that you have selected is too early for reminder to be set.");
        return;
    }
    var toset;
    var hourdiff = Math.round(mindiff/60);
    if (hourdiff>23) {
        var daydiff = Math.round(hourdiff/24);
        if (daydiff>30) toset = daydiff + "d";
        else toset = hourdiff + "h";
    }
    else toset = mindiff + "m";
    if (document.cal.whom[0].checked) toset += "@remindme.com";
    else toset += "@remindyou.com";
    if (document.cal.whom[0].checked) addto("to", toset);
    else addto("cc", toset);
}

function writetodatestr(){
	datestr = showdate + '/' + document.getElementById('mon').options[document.getElementById('mon').selectedIndex].text + '/' + document.getElementById('yea').options[document.getElementById('yea').selectedIndex].text;
	document.frm.dot.value=datestr;
}
