  
function Calendar(Year,Month,Day,Div,Field,Code) 
{ 
    // Start Configurable Date
    var ClassName = "CalendarStyle";
    var LeftArrow = "http://www.executivebeechcraft.com/images/calendar/leftarrow.png";
    var RightArrow = "http://www.executivebeechcraft.com/images/calendar/rightarrow.png";
  
    // Do not edit below      
    Calendar.MonthNames = new Array('JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC'); 
    this.oDate = new Date(); 
    this.Year = (Year == null) ? this.oDate.getFullYear() : Year; 
    this.Month = (Month == null) ? this.oDate.getMonth() : Month - 1;
    this.Day = (Day == null) ? this.oDate.getDate() : Day; 
    this.oDate = new Date(this.Year, this.Month, 1); 
    this.NextMonth = new Date(this.Year, this.Month + 1, 1); 
    this.WeekStart = this.oDate.getDay(); 
    this.MonthDays = Math.round((this.NextMonth.getTime() - this.oDate.getTime()) / 86400000) + 1;  
    
    this.ShowBackArrow = 0;
    this.theDate = new Date(); 
    this.theYear = theDate.getFullYear(); 
    this.theMonth = theDate.getMonth()+1;
    this.theDay = theDate.getDate(); 
       
    if(this.Year < this.theYear)
    ShowBackArrow++;    
    if(this.Year == this.theYear && this.Month < this.theMonth)
    ShowBackArrow++; 
    
    var EmptyCell = '<td class="emptycell">&nbsp;</td>';
    var EmptyRow = EmptyCell + EmptyCell + EmptyCell + EmptyCell + EmptyCell + EmptyCell + EmptyCell + '</tr>';
     
    this.HTML = '<table class="' + ClassName + '" cellspacing="1" cellpadding="0">'; 
    this.HTML += '<tr>';
    this.HTML += '<td colspan="7">';
    this.HTML += '<div class="monthcelldiv">';
    if(ShowBackArrow == 0 || Code == 0)    
    this.HTML += '<img onclick="GoToPrevMonth(Year, ' + (this.Month+1) + ',\'' + Div + '\',\'' + Field + '\', ' + Code + ')" class="left_arrow" src="' + LeftArrow + '">';
    this.HTML += '<img onclick="GoToNextMonth(Year, ' + (this.Month+1) + ',\'' + Div + '\',\'' + Field + '\', ' + Code + ')" class="right_arrow" src="' + RightArrow + '">';
    this.HTML += '<b class="calendar_month">' + Calendar.MonthNames[this.Month] + ' ' + this.Year + '</b>';  
    this.HTML += '</div>';   
    this.HTML += '</td>';
    this.HTML += '</tr>';
    this.HTML += '<tr class="daycell"><td>Su</td><td>Mo</td><td>Tu</td><td>We</td><td>Th</td><td>Fr</td><td>Sa</td></tr>'; 
    this.HTML += '<tr>'; 
    for(DayCounter = 0; DayCounter < this.WeekStart; DayCounter++) 
    { 
    this.HTML += EmptyCell; 
    } 
    for(DayCounter = 1; DayCounter < this.MonthDays; DayCounter++) 
    { 
    if((DayCounter + this.WeekStart) % 7 == 1) this.HTML += '<tr>'; 
    if(DayCounter == this.Day) 
    this.HTML += '<td class="todaycell" onclick="FillDate(Year, ' + (this.Month+1) + ', ' + DayCounter + ',\'' + Field + '\',\'' + Div + '\', ' + Code + ')">' + DayCounter + '</td>'; 
    else this.HTML += '<td class="whitecell" onclick="FillDate(Year, ' + (this.Month+1) + ', ' + DayCounter + ',\'' + Field + '\',\'' + Div + '\', ' + Code + ')">' + DayCounter + '</td>'; 
    if((DayCounter + this.WeekStart) % 7 == 0) this.HTML += '</tr>'; 
    } 
    for(j = (42 - (this.MonthDays + this.WeekStart)), DayCounter = 0; DayCounter <= j; DayCounter++) 
    {
    this.HTML += EmptyCell; 
    if((j - DayCounter) % 7 == 0) this.HTML += '</tr>'; 
    } 
    this.HTML += '</table>'; 
    this.HTML = this.HTML.replace('<tr><tr>','<tr>'); 
    this.HTML = this.HTML.replace(EmptyRow,''); 
    this.HTML = this.HTML.replace(EmptyRow,''); 

    return this.HTML;   
} 

function FillDate(Year,Month,Day,Field,Div,Code){
    var SelectedDate = Year+'-'+Month+'-'+Day;
    var IsDateValid = 0;
    
    var theDate = new Date(); 
    var theYear = theDate.getFullYear(); 
    var theMonth = theDate.getMonth()+1;
    var theDay = theDate.getDate();
    
    if(Year < theYear)
    IsDateValid++;   
    if(Year == theYear && Month < theMonth)
    IsDateValid++;   
    if(Year == theYear && Month == theMonth && Day < theDay)
    IsDateValid++;
    
    if(Code == 0)
    IsDateValid = 0;
    
    if(IsDateValid == 0){
    document.getElementById(Field).value = SelectedDate;
    document.getElementById(Div).innerHTML = "";
    }
    
    if(IsDateValid > 0)
    alert('Please Enter a Valid Date');
    
}

function ShowCalendar(Div,Field,Code){
    var content = Calendar(null,null,null,Div,Field,Code);
    document.getElementById(Div).innerHTML = content;
}

function closeCalendar(Div){
    document.getElementById(Div).innerHTML = "";
}

function GoToPrevMonth(Year,Month,Div,Field,Code){
  if(Month == 1){
  var M = 12;
  var Y = Year - 1;
  }
  if(Month > 1){
  var M = Month - 1;
  var Y = Year;
  }

  var theDate = new Date(); 
  var theYear = theDate.getFullYear(); 
  var theMonth = theDate.getMonth()+1;
  var theDay = theDate.getDate(); 

  if(Y == theYear && M == theMonth)
  var content = Calendar(Y,M,theDay,Div,Field,Code);
  else
  var content = Calendar(Y,M,0,Div,Field,Code);

  document.getElementById(Div).innerHTML = content;
}

function GoToNextMonth(Year,Month,Div,Field,Code){
  if(Month == 12){
  var M = 1;
  var Y = Year + 1;
  }
  if(Month < 12){
  var M = Month + 1;
  var Y = Year;
  }

  var theDate = new Date(); 
  var theYear = theDate.getFullYear(); 
  var theMonth = theDate.getMonth()+1;
  var theDay = theDate.getDate();

  if(Y == theYear && M == theMonth)
  var content = Calendar(Y,M,theDay,Div,Field,Code);
  else
  var content = Calendar(Y,M,0,Div,Field,Code); 

  document.getElementById(Div).innerHTML = content;
}
