var gNow = new Date(g_today);
var g_popup;
var bColor = "#999999"; // bordercolor variable for Netscape compatibility

isNetscape = (navigator.appName.indexOf("Netscape") != -1) ? true : false;
isIE = (navigator.appName.indexOf("Microsoft") != -1) ? true : false;

function call_date(main_form, calendar_date)
{
  var form_day;
  var form_month;
  var form_year;
    
  form_day = main_form.day;
  form_month = main_form.month;
  form_year = main_form.year;
    
  var vLastDay = JSCalendar.get_daysofmonth(calendar_date.getMonth(), calendar_date.getYear());

  while(form_day.options.length > 20) 
  {
    form_day.options[20] = null;
  }

  for (var i = 0; i < form_month.options.length; i++) 
  {
    if(i == calendar_date.getMonth()) 
    {
      form_month.options[i].selected = true;
    }
  }
    
  for (var i = 20; i < vLastDay; i++) 
  {
    form_day.options[i] = new Option(i+1, i+1);
  }

  for (var i = 0; i < vLastDay; i++) 
  {
    if (i+1 == calendar_date.getDate()) 
    {
      form_day.options[i].selected = true;
    }
  }

  if ((calendar_date.getMonth() < g_today.getMonth()) && (calendar_date.getFullYear() <= g_today.getFullYear())) 
  {
    form_year.value = g_today.getFullYear() + 1;
  } 
  else
  {
    if ((calendar_date.getMonth() >= g_today.getMonth()) && (calendar_date.getFullYear() > g_today.getFullYear())) 
    {
      form_year.value = g_today.getFullYear();
    } 
    else
    { 
      form_year.value = calendar_date.getFullYear();
    }
  }

  ChangeMonth();
}

function check_date(main_form, calendar_date) 
{
  if (calendar_date == 'null') 
  {
    selectedYearValue = parseInt(main_form.year.value,10);
    selectedDayValue = parseInt(main_form.day.options[main_form.day.selectedIndex].text,10);
    selectedMonValue = parseInt(main_form.month.options[main_form.month.selectedIndex].value,10);
  } 
  else
  {
    theDate = new Date(calendar_date);
    selectedMonValue = theDate.getMonth();
    selectedDayValue = theDate.getDate(); 
    selectedYearValue = theDate.getFullYear();
  }
  
  var new_date = new Date(selectedYearValue, selectedMonValue, selectedDayValue);

  call_date(main_form, new_date);
}

JSCalendar.Months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
JSCalendar.DOMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
JSCalendar.lDOMonth = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

function JSCalendar(p_month, p_year)
{
  if ((p_month == null) && (p_year == null))  
  {
    return;
  }

  if (p_month == null) 
  {
    this.gMonthName = null;
    this.gMonth = null;
    this.gYearly = true;
  }
  else
  {
    this.gMonthName = JSCalendar.get_month(p_month);
    this.gMonth = new Number(p_month);
    this.gYearly = false;
  }

  this.gYear = p_year;
}

JSCalendar.get_month = calendar_get_month;
JSCalendar.get_daysofmonth = calendar_get_daysofmonth;
JSCalendar.calc_month_year = calendar_calc_month_year;

function calendar_get_month(monthNo) 
{
  return JSCalendar.Months[monthNo];
}

function calendar_get_daysofmonth(monthNo, p_year) 
{
  if((p_year % 4) == 0)
  {
    if((p_year % 100) == 0 && (p_year % 400) != 0)
	{
      return JSCalendar.DOMonth[monthNo];
	}

    return JSCalendar.lDOMonth[monthNo];
  } 
  else 
  {
    return JSCalendar.DOMonth[monthNo];
  }
}

function calendar_calc_month_year(p_Month, p_Year, incr) 
{
  var ret_arr = new Array();
    
  if (incr == -1)
  {
    if (p_Month == 0)
	{
      ret_arr[0] = 11;
      ret_arr[1] = parseInt(p_Year) - 1;
    } 
	else
	{
      ret_arr[0] = parseInt(p_Month) - 1;
      ret_arr[1] = parseInt(p_Year);
    }
  } 
  else if (incr == 1) 
  {
    if (p_Month == 11) 
	{
      ret_arr[0] = 0;
      ret_arr[1] = parseInt(p_Year) + 1;
    } 
	else
	{
      ret_arr[0] = parseInt(p_Month) + 1;
      ret_arr[1] = parseInt(p_Year);
    }
  }
  
  return ret_arr;
}

// This is for compatibility with Navigator 3, we have to create and discard one object before the prototype object exists.
new JSCalendar();

JSCalendar.prototype.show = function() 
{
  var vCode = "";
  
  g_popup.document.open();

  // Show navigation buttons
  var prevMMYYYY = JSCalendar.calc_month_year(this.gMonth, this.gYear, -1);
  var prevMM = prevMMYYYY[0];
  var prevYYYY = prevMMYYYY[1];

  var nextMMYYYY = JSCalendar.calc_month_year(this.gMonth, this.gYear, 1);
  var nextMM = nextMMYYYY[0];
  var nextYYYY = nextMMYYYY[1];

  // Setup the page...
  this.wwrite("<html>");
  this.wwrite("<head><title>Calendar</title>");
    
  // write out style rules
  this.wwrite(this.createCSS());
  this.wwrite("</head>");

  this.wwrite("<body onblur=\"self.focus()\">");

  // create calendar header
  this.wwrite("<table width=\"100%\" border=\"0\" cellspacing=\"1\" cellpadding=\"3\">");
  this.wwrite("<tr><td align=\"center\"><b>To choose a due date, click on a date below.</b></td></tr></table>");
        
  // Create the master table that contains all the tables making up the calendar. 
  this.wwrite("<table width=\"100%\" border=\"0\" cellspacing=\"2\" cellpadding=\"0\">");
  this.wwrite("<tr><td colspan=\"3\">");
    
  // create the table that contains the buttons to next and previous months and years.    
  this.wwrite("<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"2\">");
  this.wwrite("<tr>");
        
  //create hyperlink to the previous month button
  this.wwrite("<td>");
  this.wwrite("&lt;&#160;<a href=\"" + "javascript:window.opener.create_calendar(" + "'" + prevMM + "', '" + prevYYYY + "');" + "\" class=\"nav\">Previous</a></td>");
    
  //create hyperlink to the next month button
  this.wwrite("<td align=\"right\">");
  this.wwrite("<a href=\"" + "javascript:window.opener.create_calendar(" + "'" + nextMM + "', '" + nextYYYY + "');" + "\" class=\"nav\">Next</a>&#160;&gt;</td>");
        
  this.wwrite("</tr>");
  this.wwrite("</table></td></tr>");
  this.wwrite("<tr>");
  this.wwrite("<td align=\"center\" valign=\"top\">");
    
  for (z = 0; z < 2; z++) 
  {
    this.gMonthName = JSCalendar.Months[this.gMonth];
        
    // Create the string with the month name and the year
    this.wwrite("<!-- Begin Table with Month/Year string -->");
    this.wwrite("<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"3\">");
    this.wwrite("<tr>");
    this.wwrite("<td class=\"calMonth\" align=\"center\">");
    this.wwrite("<b>" + this.gMonthName + " " + this.gYear + "</b>");
    this.wwrite("</tr>");
    this.wwrite("</table>");
    this.wwrite("<!-- End Table with Month/Year string -->");
        
    // Get the complete calendar code for the month..
    vCode = this.getMonthlyCalendarCode();
    this.wwrite(vCode);

    this.wwrite("</td>");
    if (z == 0) this.wwrite("<td width=\"10\">&#160;</td><td align=\"center\" valign=\"top\">");
    if (this.gMonth == 11) 
	{
      this.gMonth = 0;
      this.gYear++;
    } 
	else
	{
	   this.gMonth++;
	}
  }
    
  this.wwrite("</tr></table>");
    
  this.wwrite("</body></html>");
    
  g_popup.document.close();
}

JSCalendar.prototype.getMonthlyCalendarCode = function() 
{
  var vCode = "";
  var vHeader_Code = "";
  var vData_Code = "";
    
  vCode += "<table width=\"100%\" cellspacing=\"1\" cellpadding=\"3\" class=\"MonthTableBorder\" ";
  if (isNetscape) 
  {
    vCode += "border=\"1\" bordercolor=\"" + bColor + "\"";
  }
  
  vCode += ">";
  vHeader_Code = this.cal_header();
  vData_Code = this.cal_data();
  vCode += vHeader_Code + vData_Code;
  vCode += "</table>";
  vCode += "\n";
        
  return vCode;
}

JSCalendar.prototype.wwrite = function(wtext) 
{
  g_popup.document.writeln(wtext);
}

JSCalendar.prototype.wwriteA = function(wtext) 
{
  g_popup.document.write(wtext);
}

// New function added 09/18/2001 bmartinez
// Not exactly the prettiest way to control color/fonts,
// but it's still a lot cleaner than what we were doing.
JSCalendar.prototype.createCSS = function()
{
  var vCode = "";
    
  vCode += "<style type=\"text/css\">\n";
  vCode += "body { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px; color: #333333; background-color: #ffffff; margin: 0px }\n";
  vCode += "table { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px; color: #333333 }\n";
  vCode += "td { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px; color: #333333 }\n";
  vCode += "b { font-weight: bold }\n";
  vCode += "a:link { color: #003399; text-decoration: underline; font-weight: bold }\n";
  vCode += "a:visited { color: #003399; text-decoration: underline; font-weight: bold }\n";
  vCode += "a:active { color: #003399; text-decoration: underline; font-weight: bold }\n";
  vCode += "a:hover { color: #0000FF; text-decoration: underline; font-weight: bold }\n";
  vCode += ".header-tool { background-color: #003399; font-size: 12px; color: #ffffff; font-weight: bold }\n";
  vCode += ".MonthTableBorder { background-color: #dddddd }\n";
  vCode += ".MonthTableHeader { color: #DDDDDD; background-color: #888888 }\n";
  vCode += ".calBody { background-color: #eeeeee }\n";
  vCode += ".calMonth { font-size: 12px }\n";
  vCode += "a.DateSelect:link { color: #001177; text-decoration: none; font-weight: bold }\n";
  vCode += "a.DateSelect:visited { color: #001177; text-decoration: none; font-weight: bold }\n";
  vCode += "a.DateSelect:active { color: #001177; text-decoration: none; font-weight: bold }\n";
  vCode += "a.DateSelect:hover { color: #0000FF; text-decoration: underline; font-weight: bold }\n";
  vCode += "a.nav:link { color: #003399; text-decoration: underline; font-weight: bold }\n";
  vCode += "a.nav:visited { color: #003399; text-decoration: underline; font-weight: bold }\n";
  vCode += "a.nav:active { color: #003399; text-decoration: underline; font-weight: bold }\n";
  vCode += "a.nav:hover { color: #0000FF; text-decoration: underline; font-weight: bold }\n";
  vCode += ".invalid { color: #cccccc }\n";
  vCode += ".Today { color: #ff3333 }\n";
  vCode += "</style>\n";
    
  return vCode;
}

JSCalendar.prototype.cal_header = function() 
{
  var vCode = "";
    
  vCode += "<tr>\n";
  vCode += "<td width=\"14%\" align=\"center\" class=\"MonthTableHeader\">Sun</td>\n";
  vCode += "<td width=\"14%\" align=\"center\" class=\"MonthTableHeader\">Mon</td>\n";
  vCode += "<td width=\"14%\" align=\"center\" class=\"MonthTableHeader\">Tue</td>\n";
  vCode += "<td width=\"14%\" align=\"center\" class=\"MonthTableHeader\">Wed</td>\n";
  vCode += "<td width=\"14%\" align=\"center\" class=\"MonthTableHeader\">Thu</td>\n";
  vCode += "<td width=\"14%\" align=\"center\" class=\"MonthTableHeader\">Fri</td>\n";
  vCode += "<td width=\"14%\" align=\"center\" class=\"MonthTableHeader\">Sat</td>\n";
  vCode += "</tr>\n";
    
  return vCode;
}

JSCalendar.prototype.cal_data = function() 
{
  var vDate = new Date(g_today);
  vDate.setDate(1);
  
  vDate.setMonth(this.gMonth);
  vDate.setFullYear(this.gYear);
  
  var rightNow = new Date(g_today);
  var msDay = 86400000;
  var vFirstDay=vDate.getDay();
  var vDay=1;
  var vLastDay=JSCalendar.get_daysofmonth(this.gMonth, this.gYear);
  
  var vOnLastDay=0;
  var vCode = "";

  vCode += "<tr>\n";
  for (i = 0; i < vFirstDay; i++) 
  {
    vCode += "<td width=\"14%\" class=\"calBody\">&#160;</td>\n";
  }
    
  // Write rest of the 1st week
  for (j = vFirstDay; j < 7; j++) 
  {
    vCode += "<td width=\"14%\" align=\"right\" class=\"calBody\">";
    var checkDay = new Date(this.gYear, this.gMonth, vDay);
      
    if((checkDay - rightNow) / msDay >= 0)
	{
      vCode += "<a href=\"#\" class=\"DateSelect\" ";

      vCode += "onClick=\"self.opener.check_date(self.opener.main_form, '" + this.format_data(vDay);
      vCode += "');" + " window.close();\">" + this.format_day(vDay) + "</a>";
    } 
	else
	{
      vCode += "<span class=\"invalid\">" + this.format_day(vDay) + "</span>";
    }
    
	vCode += "</td>\n"; 
    vDay++;
  }
  vCode += "</tr>\n";
    
  // Write the rest of the weeks
  for (k = 2; k < 7; k++) 
  {
    vCode += "<tr>\n";
    for (j = 0; j < 7; j++)
	{
      vCode += "<td width=\"14%\" align=\"right\" class=\"calBody\">";
      var checkDay = new Date(this.gYear, this.gMonth, vDay);
          
      if ((checkDay - rightNow) / msDay > -1)
	  {
        vCode += "<a href=\"#\" class=\"DateSelect\" ";
        vCode += "onClick=\"self.opener.check_date(self.opener.main_form, '"+this.format_data(vDay)+"');" + " window.close();\">" + this.format_day(vDay) + "</a>";
      } 
	  else
	  {
        vCode += "<span class=\"invalid\">" + this.format_day(vDay) + "</span>";
      }
            
      vCode += "</td>\n"; 
      vDay++;
                 
      if (vDay > vLastDay) 
	  {
        vOnLastDay = 1;
        break;
      }
    }
      
    if (j == 6)
	{
      vCode += "</tr>\n";
	}

    if (vOnLastDay == 1)
	{
      break;
	}
  }
  
  // Fill up the rest of last week with proper blanks, so that we get proper square blocks
  for (m = 1; m < (7 - j); m++) 
  {
    vCode += "<td width=\"14%\" class=\"calBody\">&#160;</td>\n";
  }
  
  vCode += "</tr>";
    
  return vCode;
}

JSCalendar.prototype.format_day = function(vday) 
{
  var vNowDay = gNow.getDate();
  var vNowMonth = gNow.getMonth();
  var vNowYear = gNow.getFullYear();

  if (vday == vNowDay && this.gMonth == vNowMonth && this.gYear == vNowYear) 
  {
    return ("<span class=\"Today\"><b>" + vday + "</b></span>");
  } 
  else
  {
    return (vday);
  }
}

JSCalendar.prototype.format_data = function(p_day) 
{
  var vData;
  var vMonth = this.gMonth + 1;
  vMonth = (vMonth.toString().length < 2) ? "0" + vMonth : vMonth;
  var vMon = JSCalendar.get_month(this.gMonth).substr(0,3).toUpperCase();
  var vFMon = JSCalendar.get_month(this.gMonth).toUpperCase();
  var vY4 = new String(this.gYear);
  var vDD = (p_day.toString().length < 2) ? "0" + p_day : p_day;

  vData = vMonth + "\/" + vDD + "\/" + vY4;
  
  return vData;
}

function create_calendar(p_month, p_year) 
{
  g_calendar = new JSCalendar(p_month, p_year);

  g_calendar.show();
}

function show_calendar(p_month, p_year) 
{
  if(isNetscape) 
  {
    g_popup = window.open("", "Calendar", "width=500,height=245,status=no,resizable=yes,top=200,left=200");
  }
  else
  {
    g_popup = window.open("", "Calendar", "width=500,height=240,status=no,resizable=yes,top=200,left=200");
  }
        
  g_popup.opener = self;

  create_calendar(p_month, p_year);
}
