/*

  JavaScript Display Events Calendar by Andrew Savka.
  email: a_sawka@yahoo.com

*/


var ASEventCalendarDateFormat = "mm/dd/yyyy";
var ASEventCalendarImageURL = "adminimages/calendar.gif";

var ARR_AS_EVENT_CALENDARS = new Array();

/* 

  Calendar Type can be: 
    1. "List"
    2. "SmallCalendar"
    3. "LargeCalendar"
    
  autohide 
    flag if set to true means that calendar will be shown only 
    when user place mouse over element referenced by control parameter.
  
  control
    - For autohide = true: Element under which calendar will dropdown. (mostly image or text element).
    - For autohide = false: Element which will be used as container (parent element) for calendar.

*/

function ASEventCalendar(ID,control,eventsArray,calendarType,autohide,defaultDate,dx,dy)
{
  this.ID = ID;
  this.control = control;  
  this.eventsArray = eventsArray;
  this.calendarType = (calendarType == "indefined" ? "SmallCalendar" : calendarType);
  this.autohide = (autohide == "undefined" ? false : autohide);
  this.calendarShown = false;
  this.cursorOnCalendarBody = false;
  this.cursorOncalendarHeader = false;
  this.cursorEventCellID = null;
  this.EventMenuContainer = null;
  this.EventMenuShown = false;
  this.GeneratedEventMenuID = null;
  this.OnShowEventRequest = null;

  this.cursorOnEventMenu = false;
    
  if(this.autohide)
  {
    this.control.onmouseover = new Function("window.ARR_AS_EVENT_CALENDARS['"+this.ID+"'].OverCalendarHeader();");
    this.control.onmouseout = new Function("onmouseout","window.ARR_AS_EVENT_CALENDARS['"+this.ID+"'].OutCalendarHeader();");
    this.control.onclick = new Function("onclick","window.ARR_AS_EVENT_CALENDARS['"+this.ID+"'].CalendarShowHide();");    
  }
  
  ARR_AS_EVENT_CALENDARS[this.ID] = this;
  
  this.defaultDate = (defaultDate == null ? new Date() : defaultDate);
  this.bodyObject = document.getElementById("calendarBody"); 
  this.currentDate = null;
  
  this.DayPosition = -1;
  this.MonthPosition = -1;
  this.YearPosition = -1;
  
  this.MonthSelect = null;
  this.MonthSelectContainer = null;
  this.YearControl = null;
  
  this.Container = null;
  if(this.autohide)
  {
    this.Container = document.createElement("div");      
    this.Container.onmouseover = new Function("window.ARR_AS_EVENT_CALENDARS['"+this.ID+"'].OverCalendarBody();");
    this.Container.onmouseout = new Function("window.ARR_AS_EVENT_CALENDARS['"+this.ID+"'].OutCalendarBody();");
  }
  else
  {
    this.Container = this.control;
  }
  
  this.CalendarBody = document.createElement("div");
  this.CalendarBody.id = this.ID + "_ASEventCalendarMonthBody";
  this.CalendarHeadBody = document.createElement("div");  
  this.CalendarBody.setAttribute("class","ASEventCalendarCbody");

  for(var idxPosition = 0; idxPosition < 3; idxPosition++)
  {
    if(ASEventCalendarDateFormat.split('/')[idxPosition] == "dd")
    {
      this.DayPosition = idxPosition;
    } 
    else if(ASEventCalendarDateFormat.split('/')[idxPosition] == "mm")
    {
      this.MonthPosition = idxPosition;  
    }
    else if(ASEventCalendarDateFormat.split('/')[idxPosition] == "yyyy")
    {
      this.YearPosition = idxPosition;
    }
  }
  
  if(this.DayPosition == -1 || this.MonthPosition == -1 || this.YearPosition == -1)
  {
    alert("Wrong date format specified for ASEventCalendar.");
    return null;
  }
  
  this.currentDate = this.defaultDate;

  if(this.autohide)
  {
    dx = (dx == null ? 0 : dx);
    dy = (dy == null ? 24 : dy);  
    this.PosX = getElementLeft(control.id) + dx;
    this.PosY = getElementTop(control.id) + dy;
    this.Container.id = this.ID + "_ASEventCalendar";
    this.Container.style.left = this.PosX + 'px';
    this.Container.style.top = this.PosY + 'px';
    this.Container.style.position = "absolute";
    this.Container.style.display = "none";    
    document.body.appendChild(this.Container);  
  }
  
  this.EventMenuContainer = document.createElement("div");
  this.EventMenuContainer.id = this.ID + "_ASEventCalendar";
  this.EventMenuContainer.style.left = 0;
  this.EventMenuContainer.style.top = 0;
  this.EventMenuContainer.style.position = "absolute";
  this.EventMenuContainer.style.display = "none";    
  this.EventMenuContainer.onmouseover = new Function("window.ARR_AS_EVENT_CALENDARS['"+this.ID+"'].EventMenuOver();");
  this.EventMenuContainer.onmouseout = new Function("window.ARR_AS_EVENT_CALENDARS['"+this.ID+"'].EventMenuOut();");
  
  document.body.appendChild(this.EventMenuContainer);  
  
  
  
  this.Generate();
  
  this.MonthSelect = new MonthSelect(this.ID+"_ASEventCalendar_MonthSelect",this.ID+"_ASEventCalendar_MonthSelectContainer",this.currentDate.getMonth());  
  this.MonthSelect.redraw();    
  this.MonthSelect.onMonthChanged = new Function("window.ARR_AS_EVENT_CALENDARS['"+this.ID+"'].UpdateMonth();");
  
  return this;
}


ASEventCalendar.prototype.Generate = function()
{
  var mTable = document.createElement("table");
  mTable.setAttribute("class","ASEventCalendarContainer");
  
  var mR1 = document.createElement("tr");
  var mC1 = document.createElement("td");  
  mC1.setAttribute("class","ASEventCalendarHeadCell");
  mC1.setAttribute("align","center");
  
//  mC1.setAttribute("width","100%");      
  mC1.appendChild(this.CalendarHeadBody);  
  mR1.appendChild(mC1); 
  mTable.appendChild(mR1);
  
  var mR2 = document.createElement("tr");
  var mC2 = document.createElement("td");
  mC2.setAttribute("class","ASEventCalendarBodyCell");
  mC2.setAttribute("width","100%");  
  mC2.appendChild(this.CalendarBody);
  mR2.appendChild(mC2);
  mTable.appendChild(mR2);

  this.Container.appendChild(mTable);  
  this.RefreshHeader();
  this.RefreshMonthBody();
  this.Container.innerHTML += "";
  
}

ASEventCalendar.prototype.RefreshHeader = function()
{
  this.CalendarHeadBody.innerHTML = "";
  this.CalendarHeadBody.appendChild(this.RenderHeader());
}

ASEventCalendar.prototype.RefreshMonthBody = function()
{
  var lObj = document.getElementById(this.ID + "_ASEventCalendarMonthBody");
  lObj.innerHTML = "";
  if(this.calendarType == "List")
  {
    lObj.appendChild(this.RenderMonthListTable());
  }
  else
  {
    lObj.appendChild(this.RenderMonthTable());  
  }
  lObj.innerHTML += "";  
}

ASEventCalendar.prototype.RenderHeader = function()
{
  var hTable = document.createElement("table");
  hTable.setAttribute("class","ASEventCalendarHeaderTable");
  hTable.setAttribute("cellspacing","0");
  hTable.setAttribute("cellpadding","0");
   
  var hRow = document.createElement("tr");
  
  hCell = document.createElement("td");
  hCell.setAttribute("class","ASEventCalendarHeaderButton");
  hCell.appendChild(document.createTextNode("<"));  
  hCell.setAttribute("onmouseover","this.className = 'ASEventCalendarHeaderButtonHover'");
  hCell.setAttribute("onmouseout","this.className = 'ASEventCalendarHeaderButton'");  
  hCell.setAttribute("onclick","window.ARR_AS_EVENT_CALENDARS['"+this.ID+"'].PrevMonth()");
  hRow.appendChild(hCell);
  
  var hCell = document.createElement("td");  
  hCell.setAttribute("class","ASEventCalendarHeaderCell");
  hCell.setAttribute("id",this.ID+"_ASEventCalendar_MonthSelectContainer");
  this.MonthSelectContainer = hCell;
  hRow.appendChild(hCell);

  hCell = document.createElement("td");
  hCell.setAttribute("class","ASEventCalendarHeaderButton");
  hCell.appendChild(document.createTextNode(">"));  
  hCell.setAttribute("onmouseover","this.className = 'ASEventCalendarHeaderButtonHover'");
  hCell.setAttribute("onmouseout","this.className = 'ASEventCalendarHeaderButton'");  
  hCell.setAttribute("onclick","window.ARR_AS_EVENT_CALENDARS['"+this.ID+"'].NextMonth()");  
  hRow.appendChild(hCell);


  hCell = document.createElement("td");
  hCell.setAttribute("class","ASEventCalendarHeaderSpacer");
  hCell.appendChild(document.createTextNode(" "));
  hRow.appendChild(hCell);


  hCell = document.createElement("td");
  hCell.setAttribute("class","ASEventCalendarHeaderButton");
  hCell.appendChild(document.createTextNode("<"));  
  hCell.setAttribute("onmouseover","this.className = 'ASEventCalendarHeaderButtonHover'");
  hCell.setAttribute("onmouseout","this.className = 'ASEventCalendarHeaderButton'");  
  hCell.setAttribute("onclick","window.ARR_AS_EVENT_CALENDARS['"+this.ID+"'].PrevYear()");
  hRow.appendChild(hCell);

  hCell = document.createElement("td");
  hCell.setAttribute("class","ASEventCalendarHeaderCell");  
  var YearControl = document.createElement("input");
  YearControl.setAttribute("type","text");
  YearControl.setAttribute("class","ASEventCalendarYearControl");
  YearControl.setAttribute("value",this.currentDate.getFullYear());
  YearControl.setAttribute("onblur","window.ARR_AS_EVENT_CALENDARS['"+this.ID+"'].YearChanged()");
  YearControl.setAttribute("id",this.ID+"_ASEventCalendarYearControl");
  this.YearControl = YearControl;  
  hCell.appendChild(YearControl);
  hRow.appendChild(hCell);  

  hCell = document.createElement("td");
  hCell.setAttribute("class","ASEventCalendarHeaderButton");
  hCell.appendChild(document.createTextNode(">"));  
  hCell.setAttribute("onmouseover","this.className = 'ASEventCalendarHeaderButtonHover'");
  hCell.setAttribute("onmouseout","this.className = 'ASEventCalendarHeaderButton'");  
  hCell.setAttribute("onclick","window.ARR_AS_EVENT_CALENDARS['"+this.ID+"'].NextYear()");  
  hRow.appendChild(hCell);
        
  hCell = document.createElement("td");
  hCell.setAttribute("class","ASEventCalendarHeaderSpacer2");
  hCell.appendChild(document.createTextNode(" "));
  hRow.appendChild(hCell);
        
  hTable.appendChild(hRow);
  
  return hTable;
}

ASEventCalendar.prototype.RenderMonthListTable = function()
{
  var lTable = document.createElement("table");
  var PrevRowExists = false;
  lTable.setAttribute("class","ASEventCalendarListTable");
  
  for(var idx = 1; idx < this.currentDate.getDaysInMonth(); idx++)
  {
  
    var eYear = this.currentDate.getFullYear();
    var eDay = String(idx);
    var eMonth = String(this.currentDate.getMonth()+1);  
    eDay = (eDay.length == 1 ? "0"+eDay : eDay);
    eMonth = (eMonth.length == 1 ? "0"+eMonth : eMonth);  
    var dateKey = eYear+"_"+eMonth+"_"+eDay;
  
    if(this.eventsArray[dateKey] != null)
    {
      
      if(PrevRowExists)
      {
        var pRow = document.createElement("tr");
        var pCell = document.createElement("td");
//        pCell.setAttribute("class","ASEventCalendarListBorderRow");
        pCell.setAttribute("colspan","2");
        var pHr = document.createElement("hr");
        pHr.setAttribute("size","1");
        pHr.setAttribute("noshade","");
        pHr.setAttribute("color","#888888");
        pCell.appendChild(pHr);
        pRow.appendChild(pCell);
        lTable.appendChild(pRow);
      }
      else
      {
        PrevRowExists  = true;
      }
    
    
      var lRow = document.createElement("tr");

      lRow.setAttribute("class","ASEventCalendarListDayRow");

      var lCell = document.createElement("td");
      lCell.setAttribute("class","ASEventCalendarListDateCell");
      if(this.eventsArray[dateKey].length > 1)
      {
        lCell.setAttribute("rowspan",this.eventsArray[dateKey].length);
      }
      lCell.appendChild(document.createTextNode(this.eventsArray[dateKey][0].Date));
      lRow.appendChild(lCell);
      lCell = document.createElement("td");
      lCell.setAttribute("class","ASEventCalendarListEventCell");
      lLink = document.createElement("a");
      lLink.setAttribute("class","ASEventCalendarMenuItemLink");
      lLink.setAttribute("href","#");
      lLink.setAttribute("onclick","window.ARR_AS_EVENT_CALENDARS['"+this.ID+"'].EventLinkClick('"+dateKey+"',"+this.eventsArray[dateKey][0].ID+"); return false;");
      lLink.appendChild(document.createTextNode(this.eventsArray[dateKey][0].ShortDescription));
      lCell.appendChild(lLink);
      lRow.appendChild(lCell);
      lTable.appendChild(lRow);
      
      for(var idxEvent = 1; idxEvent < this.eventsArray[dateKey].length;idxEvent++)
      {
        var lRow = document.createElement("tr");
        lCell = document.createElement("td");
        lCell.setAttribute("class","ASEventCalendarListEventCell");
        lLink = document.createElement("a");
        lLink.setAttribute("class","ASEventCalendarMenuItemLink");
        lLink.setAttribute("href","#");        
        lLink.setAttribute("onclick","window.ARR_AS_EVENT_CALENDARS['"+this.ID+"'].EventLinkClick('"+dateKey+"',"+this.eventsArray[dateKey][idxEvent].ID+"); return false;");
        lLink.appendChild(document.createTextNode(this.eventsArray[dateKey][idxEvent].ShortDescription));
        lCell.appendChild(lLink);
        lRow.appendChild(lCell);
        lTable.appendChild(lRow);        
      }      
    }        
  }
  return lTable;
}


ASEventCalendar.prototype.RenderMonthTable = function()
{
  
  var WeekCount = Math.floor(this.currentDate.getDaysInMonth()/7);    
  var FirstDay = new Date(this.currentDate.getFullYear(),this.currentDate.getMonth(),1);
  var FirstDayWeekday = FirstDay.getDay();
  
  if((this.currentDate.getDaysInMonth() % 7) > 0 || (this.currentDate.getDaysInMonth() % 7) == 0 && FirstDayWeekday >0)
  {
    WeekCount ++;  
  }
  
  if(FirstDayWeekday + (this.currentDate.getDaysInMonth() % 7) > 7 )
  {
    WeekCount ++;
  }  
  
  var monthTable = document.createElement("table");
  if(this.calendarType == "LargeCalendar")
  {
    monthTable.setAttribute("class","ASEventCalendarMonthTableLarge");  
  }
  else
  {
    monthTable.setAttribute("class","ASEventCalendarMonthTable");  
  }
  
  monthTable.appendChild(this.RenderWeekHeadersRow());
  
  
  for(var idxWeek = 0; idxWeek < WeekCount; idxWeek++)
  {
    monthTable.appendChild(this.RenderWeekRow(idxWeek))
  }
  
  return monthTable;
}

ASEventCalendar.prototype.RenderWeekHeadersRow = function()
{

  var WeekdayHeaders = new Array("Su","Mo","Tu","We","Th","Fr","Sa");

  var headersRow = document.createElement("tr");
  headersRow.setAttribute("class","WeekHeadersRow");
  
  for(idxWeekday = 0; idxWeekday < 7; idxWeekday ++)
  {
    var hCell = document.createElement("td");
    if(this.calendarType == "LargeCalendar")
    {
      hCell.setAttribute("class","WeekHeaderCellLarge");
    }
    else
    {
      hCell.setAttribute("class","WeekHeaderCell");
    }
    
    hCell.appendChild(document.createTextNode(WeekdayHeaders[idxWeekday]));
    headersRow.appendChild(hCell);
  }
  
  return headersRow;  
}

ASEventCalendar.prototype.RenderWeekRow = function(weekIdx)
{

  var FirstDay = new Date(this.currentDate.getFullYear(),this.currentDate.getMonth(),1);
  var FirstDayWeekday = FirstDay.getDay();
   
  
  var startDay = 0-FirstDayWeekday+weekIdx*7;
  
  var weekRow = document.createElement("tr");
  weekRow.setAttribute("class","WeekRow");
  
  
  for(var currentDay = startDay; currentDay < startDay+7; currentDay++)
  {
    if(currentDay >= 0 && currentDay < this.currentDate.getDaysInMonth())
    {
      weekRow.appendChild(this.RenderDayCell(currentDay));
    }
    else
    {
      weekRow.appendChild(this.RenderEmptyDayCell());
    }
  }
  
  return weekRow;
}

ASEventCalendar.prototype.RenderDayCell = function(day)
{
  var dayDate = new Date(this.currentDate.getFullYear(),this.currentDate.getMonth(),day+1);
  
  var eYear = this.currentDate.getFullYear();
  var eDay = String(day+1);
  var eMonth = String(this.currentDate.getMonth()+1);  
  eDay = (eDay.length == 1 ? "0"+eDay : eDay);
  eMonth = (eMonth.length == 1 ? "0"+eMonth : eMonth);  
  var dateKey = eYear+"_"+eMonth+"_"+eDay;
  
  var wkDay = dayDate.getDay();
  var cellClass = "DayCell"+wkDay;
  var dayCell = document.createElement("td");
  dayCell.id = this.ID + "_DayCell_" + dateKey;
  
  
  if(this.eventsArray[dateKey] != null)
  {
  
    var cellClass = "DayCellWithEvent";
    var cellClassOver = "DayCellWithEventw";
    if(this.calendarType == "LargeCalendar")
    {
      cellClass = "DayCellWithEventLarge";
      cellClassOver = "DayCellWithEventwLarge";    
    }
  
    dayCell.setAttribute("class",cellClass);
    dayCell.setAttribute("onmouseover","this.className = '"+cellClassOver+"';window.ARR_AS_EVENT_CALENDARS['"+this.ID+"'].EventDateCellOver('"+dateKey+"')");
    dayCell.setAttribute("onmouseout","this.className = '"+cellClass+"';window.ARR_AS_EVENT_CALENDARS['"+this.ID+"'].EventDateCellOut('"+dateKey+"')");      
    var CurrentEvent = this.eventsArray[dateKey][0];
//    dayCell.setAttribute("onclick","window.ARR_AS_EVENT_CALENDARS['"+this.ID+"'].EventDateCellClick('"+dateKey+"')");      
    dayCell.setAttribute("onclick","window.ARR_AS_EVENT_CALENDARS['"+this.ID+"'].EventLinkClick('"+dateKey+"',"+CurrentEvent.ID+")");    
  }
  else
  {
  
    var cellClassOver = cellClass + "w";
    if(this.calendarType == "LargeCalendar")
    {
      cellClass += "Large";
      cellClassOver += "Large";    
    }
      
    dayCell.setAttribute("class",cellClass);
    dayCell.setAttribute("onmouseover","this.className = '"+cellClassOver+"'");
    dayCell.setAttribute("onmouseout","this.className = '"+cellClass+"'");  
  }
      
  dayCell.appendChild(document.createTextNode(day+1));  
  return dayCell;
}

ASEventCalendar.prototype.RenderEmptyDayCell = function()
{
  var dayCell = document.createElement("td");
  dayCell.setAttribute("class","EmptyDayCell");
  dayCell.appendChild(document.createTextNode(" "));
  
  return dayCell;  
}

ASEventCalendar.prototype.SelectDate = function(day)
{
  var DateParts = new Array();
  DateParts[this.DayPosition] = day;
  DateParts[this.MonthPosition] = this.currentDate.getMonth()+1;
  DateParts[this.YearPosition] = this.currentDate.getFullYear();  
  var SelectedDate = DateParts.join('/');  
  this.control.value = SelectedDate;
  this.CalendarShowHide();
}

ASEventCalendar.prototype.NextMonth = function()
{
  var currentMonth = this.currentDate.getMonth();
  currentMonth ++;
  if(currentMonth > 11)
  {
    currentMonth = 0;
    this.MonthSelect.currentMonth = currentMonth;  
    this.MonthSelect.redraw();    
    this.currentDate.setMonth(currentMonth);
    this.NextYear();
  }
  else
  {
    this.MonthSelect.currentMonth = currentMonth;
    this.MonthSelect.redraw();
    this.currentDate.setMonth(currentMonth);        
    this.RefreshMonthBody();
  }
}

ASEventCalendar.prototype.PrevMonth = function()
{
  var currentMonth = this.currentDate.getMonth();
  currentMonth --;
  if(currentMonth < 0)
  {
    currentMonth = 11;
    this.MonthSelect.currentMonth = currentMonth;  
    this.MonthSelect.redraw();    
    this.currentDate.setMonth(currentMonth);
    this.PrevYear();
  }
  else
  {
    this.MonthSelect.currentMonth = currentMonth;
    this.MonthSelect.redraw();
    this.currentDate.setMonth(currentMonth);    
    this.RefreshMonthBody();    
  }
}

ASEventCalendar.prototype.NextYear = function()
{
  var currentYear = this.currentDate.getFullYear();
  currentYear ++;
  document.getElementById(this.ID+"_ASEventCalendarYearControl").value = currentYear;
  this.currentDate.setFullYear(currentYear);
  this.RefreshMonthBody();
}

ASEventCalendar.prototype.PrevYear = function()
{
  var currentYear = this.currentDate.getFullYear();
  currentYear --;
  document.getElementById(this.ID+"_ASEventCalendarYearControl").value = currentYear;
  this.currentDate.setFullYear(currentYear);
  this.RefreshMonthBody();
}

ASEventCalendar.prototype.UpdateMonth = function()
{
  this.currentDate.setMonth(this.MonthSelect.currentMonth);
  this.RefreshMonthBody();
}


ASEventCalendar.prototype.YearChanged = function()
{
  var Year = document.getElementById(this.ID+"_ASEventCalendarYearControl").value;
  if(/^[0-9]{1,4}$/.test(Year))
  {
    this.currentDate.setFullYear(Year);
    this.RefreshMonthBody();
  }
}

ASEventCalendar.prototype.HideCalendar = function()
{
  if(!this.cursorOnCalendarBody && !this.cursorOnCalendarHeader && this.calendarShown && !this.MonthSelect.cursorOnMenuBody && !this.cursorOnEventMenu)
  {
    this.CalendarShowHide();
  }
}

ASEventCalendar.prototype.ShowCalendar = function()
{
  if((this.cursorOnCalendarBody || this.cursorOnCalendarHeader) && !this.calendarShown)
  {
    this.CalendarShowHide();    
  }
}

ASEventCalendar.prototype.DelayedHideCalendar = function()
{
  window.setTimeout("ARR_AS_EVENT_CALENDARS['"+this.ID+"'].HideCalendar()",500);
}

ASEventCalendar.prototype.DelayedShowCalendar = function()
{
  window.setTimeout("ARR_AS_EVENT_CALENDARS['"+this.ID+"'].ShowCalendar()",500);
}

ASEventCalendar.prototype.OverCalendarBody = function()
{
  this.cursorOnCalendarBody = true;
}

ASEventCalendar.prototype.OutCalendarBody = function()
{
  this.cursorOnCalendarBody = false;
  this.DelayedHideCalendar();
}

ASEventCalendar.prototype.OverCalendarHeader = function()
{
  this.DelayedShowCalendar();
  this.cursorOnCalendarHeader = true;  
}

ASEventCalendar.prototype.OutCalendarHeader = function()
{
  this.cursorOnCalendarHeader = false;
  this.DelayedHideCalendar();
}

ASEventCalendar.prototype.CalendarShowHide = function()
{
  
  var menuObj = this.Container;

  if(this.calendarShown)
  {
    menuObj.style.display = "none";
  }
  else  
  {          
    menuObj.style.display = "block";    
  }
  
  this.calendarShown = !this.calendarShown;

}






ASEventCalendar.prototype.EventDateCellOver = function(DateKey)
{
  this.cursorEventCellID = DateKey;
  this.DelayedShowEventMenu(DateKey);
}

ASEventCalendar.prototype.EventDateCellOut = function(DateKey)
{
  this.cursorEventCellID = null;
  this.DelayedHideEventMenu(DateKey);  
}

ASEventCalendar.prototype.EventDateCellClick = function(DateKey)
{
  this.cursorEventCellID = DateKey;  
  this.ShowEventMenu(DateKey);
}



ASEventCalendar.prototype.EventMenuOver = function()
{
  this.cursorOnEventMenu = true;  
}

ASEventCalendar.prototype.EventMenuOut = function()
{
  this.cursorOnEventMenu = false;
  this.DelayedHideEventMenu(this.GeneratedEventMenuID);
  if(this.autohide)
  {
    this.DelayedHideCalendar();
  }
}



ASEventCalendar.prototype.DelayedHideEventMenu = function(DateKey)
{
  window.setTimeout("ARR_AS_EVENT_CALENDARS['"+this.ID+"'].HideEventMenu('"+DateKey+"')",200);
}

ASEventCalendar.prototype.DelayedShowEventMenu = function(DateKey)
{
  window.setTimeout("ARR_AS_EVENT_CALENDARS['"+this.ID+"'].ShowEventMenu('"+DateKey+"')",200);
}



ASEventCalendar.prototype.HideEventMenu = function(DateKey)
{
  if(this.cursorEventCellID != DateKey && !this.cursorOnEventMenu && this.EventMenuShown)
  {
    this.EventMenuShowHide(DateKey);
  }
}

ASEventCalendar.prototype.ShowEventMenu = function(DateKey)
{
  if((this.cursorEventCellID == DateKey || this.cursorOnEventMenu) && !this.EventMenuShown)
  {
    this.EventMenuShowHide(DateKey);
  }
}

ASEventCalendar.prototype.EventMenuShowHide = function(DateKey)
{
  
  var menuObj = this.EventMenuContainer;
  
  if(!this.EventMenuShown)
  {
    this.GeneratedEventMenuID = DateKey;
    this.GenarateEventMenu(DateKey);
    menuObj.style.display = "block";
  }
  else  
  {
    this.GeneratedEventMenuID = null;    
    menuObj.style.display = "none";    
  }
  
  this.EventMenuShown = !this.EventMenuShown;

}


ASEventCalendar.prototype.GenarateEventMenu = function(DateKey)
{

  var mTable = document.createElement("table");
  mTable.setAttribute("class","ASEventCalendarMenu");

  for(idx = 0; idx < this.eventsArray[DateKey].length; idx++)
  {
    var CurrentEvent = this.eventsArray[DateKey][idx];
    var mRow = document.createElement("tr");
    var mCell = document.createElement("td");
    mCell.setAttribute("class","ASEventCalendarMenuItem");
    mCell.setAttribute("nowrap","");
    var mLink = document.createElement("a");
    mLink.setAttribute("class","ASEventCalendarMenuItemLink");
    mLink.setAttribute("href","#");
    mLink.setAttribute("onclick","window.ARR_AS_EVENT_CALENDARS['"+this.ID+"'].EventLinkClick('"+DateKey+"',"+CurrentEvent.ID+"); return false;");
    mLink.appendChild(document.createTextNode(CurrentEvent.Date + " - " + CurrentEvent.ShortDescription));
    mCell.appendChild(mLink);
    mRow.appendChild(mCell);
    mTable.appendChild(mRow);
  }
  
  this.EventMenuContainer.innerHTML = "";
  this.EventMenuContainer.appendChild(mTable);
  this.EventMenuContainer.innerHTML += "";
  
  var PosX = getElementLeft(this.ID+"_DayCell_"+DateKey) + 20;
  var PosY = getElementTop(this.ID+"_DayCell_"+DateKey) + 5;
  
  this.EventMenuContainer.style.left = PosX;
  this.EventMenuContainer.style.top = PosY;  
}


ASEventCalendar.prototype.EventLinkClick = function(DateKey,EventID)
{
  if(this.OnShowEventRequest != null)
  {  
    var CurrentEvent = null;
    for(var idx = 0; idx < this.eventsArray[DateKey].length; idx++)
    {
      if(this.eventsArray[DateKey][idx].ID == EventID)
      {
        CurrentEvent = this.eventsArray[DateKey][idx];
        break;
      }
    }
    
    if(CurrentEvent != null)
    {    
      this.OnShowEventRequest(CurrentEvent,this);
    }
    
  }
}

/*
===============================================================================
       Month Select
===============================================================================
*/



var ARR_MONTH_NAMES = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
var CALENDAR_SELECTS = new Array();

MonthSelect.prototype.redrawHead = msRedrawHead;
MonthSelect.prototype.redrawBody = msRedrawBody;
MonthSelect.prototype.createMonthCell = msCreateMonthCell;
MonthSelect.prototype.redraw = msRedraw;
MonthSelect.prototype.hideMenu = msHideMenu;
MonthSelect.prototype.showMenu = msShowMenu;
MonthSelect.prototype.delayedHideMenu = msDelayedHideMenu;
MonthSelect.prototype.delayedShowMenu = msDelayedShowMenu;
MonthSelect.prototype.overMenuBody = msOverMenuBody;
MonthSelect.prototype.outMenuBody = msOutMenuBody;
MonthSelect.prototype.overMenuHeader = msOverMenuHeader;
MonthSelect.prototype.outMenuHeader = msOutMenuHeader;
MonthSelect.prototype.menuShowHide = msMenuShowHide;
MonthSelect.prototype.setCurrentMonth = msSetCurrentMonth;
MonthSelect.prototype.getMonth = msGetMonth;

function MonthSelect(id,headContainerId,defaultMonth)
{

  this.id = id;  
  CALENDAR_SELECTS[this.id] = this;
  
  this.headContainer = document.getElementById(headContainerId);
  
  this.bodyContainer = document.createElement("div");  
  this.bodyContainer.className = "bContainer";
  this.bodyContainer.onmouseover = new Function("window.CALENDAR_SELECTS['"+this.id+"'].overMenuBody();");
  this.bodyContainer.onmouseout = new Function("window.CALENDAR_SELECTS['"+this.id+"'].outMenuBody();");
   
  this.bodyContainer.id = "body_"+headContainerId;    
  this.bodyContainer.style.position = "absolute";
  this.bodyContainer.zIndex = "7";
  this.bodyContainer.style.display = "none";
  this.bodyContainer.style.overflow = "auto";
  this.bodyContainer.style.height = "100px";
  this.bodyContainer.style.width = "94px";
  
  
  document.body.appendChild(this.bodyContainer);

  this.defaultMonth = defaultMonth;
  this.currentMonth = defaultMonth;
  this.showMonthCell = null;  
  this.menuShown = false;
  this.cursorOnMenuBody = false;
  this.cursorOnMenuHeader = false;
  this.onMonthChanged = null;
}

function msRedraw()
{
  this.redrawHead();
  this.redrawBody();
}

function msRedrawHead()
{
  var hTable = document.createElement("table");
  hTable.setAttribute("width","70");
  hTable.setAttribute("height","20");
  hTable.setAttribute("cellPadding","0");
  hTable.setAttribute("cellSpacing","0");  
  hTable.setAttribute("border","0");
  
  var hRow = document.createElement("tr");
  
  var hCell = document.createElement("td");
  hCell.setAttribute("width","70");
  hCell.setAttribute("height","20");
  hCell.setAttribute("class","MonthSelectHead");
  hCell.setAttribute("onMouseOver","this.className='MonthSelectHeadHover';window.CALENDAR_SELECTS['"+this.id+"'].overMenuHeader();");
  hCell.setAttribute("onMouseOut","this.className='MonthSelectHead';window.CALENDAR_SELECTS['"+this.id+"'].outMenuHeader();");
  hCell.setAttribute("onClick","window.CALENDAR_SELECTS['"+this.id+"'].menuShowHide();");    
  hCell.appendChild(document.createTextNode(ARR_MONTH_NAMES[this.currentMonth]));
                        
  hRow.appendChild(hCell);
  hTable.appendChild(hRow);  
  
  this.showColorCell = hCell;
  this.headContainer.innerHTML = "";
  this.headContainer.appendChild(hTable);
  this.headContainer.innerHTML += "";  
}

function msRedrawBody()
{
  var cTable = document.createElement("table");
  cTable.setAttribute("cellPadding","0");
  cTable.setAttribute("cellSpacing","1");
  cTable.setAttribute("class","monthTable");
  
//  cTable.setAttribute("onMouseOver","window."+this.id+".overMenuBody();");
//  cTable.setAttribute("onMouseOut","window."+this.id+".outMenuBody();");
      
  for(var monthIdx = 0; monthIdx < 12; monthIdx++)
  {    
    var cRow = document.createElement("tr");
    cTable.appendChild(cRow);
    cRow.appendChild(this.createMonthCell(monthIdx));
  }
    
  this.bodyContainer.innerHTML = "";
  this.bodyContainer.appendChild(cTable);
  this.bodyContainer.innerHTML += "";
  
  
}

function msCreateMonthCell(month)
{
  var cell = document.createElement("td");
  cell.setAttribute("class","monthCell");  
  cell.setAttribute("onMouseOver","this.className = 'monthCellHover';");
  cell.setAttribute("onMouseOut","this.className = 'monthCell';");    
  cell.setAttribute("onClick","window.CALENDAR_SELECTS['"+this.id+"'].setCurrentMonth("+month+");");

  cell.appendChild(document.createTextNode(ARR_MONTH_NAMES[month]));
  
  return cell;
}

function msSetCurrentMonth(month)
{

  this.currentMonth = month;
  this.redrawHead();
  this.cursorOnMenuBody = false;
  this.hideMenu();
  if(this.onMonthChanged != null)
  {
    this.onMonthChanged();
  }
}

function msHideMenu()
{
  if(!this.cursorOnMenuBody && !this.cursorOnMenuHeader && this.menuShown)
  {
    this.menuShowHide();
  }
}

function msShowMenu()
{
  if((this.cursorOnMenuBody || this.cursorOnMenuHeader) && !this.menuShown)
  {
    this.menuShowHide();    
  }
}

function msDelayedHideMenu()
{
  window.setTimeout("CALENDAR_SELECTS['"+this.id+"'].hideMenu()",500);
}

function msDelayedShowMenu()
{
  window.setTimeout("CALENDAR_SELECTS['"+this.id+"'].showMenu()",500);
}

function msOverMenuBody()
{
  this.cursorOnMenuBody = true;
}

function msOutMenuBody()
{
  this.cursorOnMenuBody = false;
  this.delayedHideMenu();
}

function msOverMenuHeader()
{
  this.delayedShowMenu();
  this.cursorOnMenuHeader = true;  
}

function msOutMenuHeader()
{
  //alert("Header Out; Cursor On MenuBody:"+cursorOnMenuBody+"; Cursor On MenuHeader:"+cursorOnMenuHeader);
  this.cursorOnMenuHeader = false;
  this.delayedHideMenu();
}

function msMenuShowHide()
{

  var menuObj = this.bodyContainer;

  if(this.menuShown)
  {
    menuObj.style.display = "none";    
  }
  else  
  {
    var posX = getElementLeft(this.headContainer.id);
    var posY = getElementTop(this.headContainer.id);    
    
    posY += 21;
      
    menuObj.style.left = posX;
    menuObj.style.top = posY;
    menuObj.style.display = "block";    
  }
  
  this.menuShown = !this.menuShown;

}

function msGetMonth()
{
  return this.currentMonth;
}


/*
===============================================================================
              ASEvent Class (Just structure to hold event data.)
===============================================================================
*/

function ASEvent(EventID,EventDate,ShortDescription,LongDescription)
{
  this.ID = EventID;
  this.Date = EventDate;
  this.ShortDescription = ShortDescription;
  this.LongDescription = (LongDescription != "undefined" && LongDescription != "" ? LongDescription : ShortDescription);
  this.Details = "";
  return this;
}


/*
===============================================================================
              Object to perform event data request
===============================================================================
*/


EventRequestQueue = new Array();    

function EventDataRequest(ID,CallBackURL)
{
  this.ID = ID;
  this.ArrQueue = EventRequestQueue;
  this.Busy = 0;
  this.httpObj = getHTTPObject();
  this.CallBackURL = CallBackURL;
  this.OnDataReceived = null;
  this.ErrorFlag = false;
  this.EventData = null;
  this.ArrQueue[this.ID] = this;
  return this;
}

EventDataRequest.prototype.DoRequest = function()
{
  if(!this.Busy)
  {
    this.Busy = 1;
//      prompt("",this.CallBackURL+"?Id="+this.ID+"&"+this.ItemType+"="+ItemValue);
    this.httpObj.open("GET",this.CallBackURL+"?Id="+this.ID,true);
  	this.httpObj.onreadystatechange = new Function("top.EventRequestQueue[\""+this.ID+"\"].DataReceived();");
  	this.httpObj.send(null);
          
  }
}

EventDataRequest.prototype.DataReceived = function()
{
  if(this.httpObj.readyState == 4)
	{
                       
	  if(this.httpObj.status == 200)
		{		
					
		  var xmlDoc = this.httpObj.responseXML;
			var Result = xmlDoc.getElementsByTagName("result").item(0).firstChild.data;
      if(Result == "Error")
      {
        this.ErrorFlag = true;
      }
      this.EventData = Result;
		}
		else
		{
      this.ErrorFlag = true;
      this.EventData = "Error receiving data. HTTP Error Code: " + this.httpObj.status;
		}
		
		this.Busy = false;
    

    if(this.OnDataReceived != null)
    {    
      this.OnDataReceived(this);
    }
    		
	}		  						
  
}


/*
===============================================================================
              Common Functions
===============================================================================
*/

function getElementLeft(Elem) {
 	var elem = document.getElementById(Elem);
  xPos = elem.offsetLeft;  
  tempEl = elem.offsetParent;
  while (tempEl != null) {
  	xPos += tempEl.offsetLeft;
  	tempEl = tempEl.offsetParent;
  }
  return xPos;
}

function getElementTop(Elem) {
 	var elem = document.getElementById(Elem);
  yPos = elem.offsetTop;
  
  tempEl = elem.offsetParent;
  while (tempEl != null) {
  	yPos += tempEl.offsetTop;
  	tempEl = tempEl.offsetParent;
  }
  return yPos;
}

function daysInYear(year)
{
  if(year % 4 != 0)
  {
    return 365;
  }
  else if(((year % 100) != 0) || ((year % 400) != 0))
  {
    return 366
  }
  
  return 365
}

function isLeapYear(year)
{
  return daysInYear(year) == 366;
}

function daysInMonth(month,year)
{
  var DaysCount = new Array(31,28,31,30,31,30,31,31,30,31,30,31);  
  var Res = -1;
  
  if(this.isLeapYear(year) && month == 1)
  {
    Res =  29;
  }
  else
  {
    Res =  DaysCount[month];
  }

  return Res;
  
}


Date.prototype.daysInYear = function()
{

  var year = this.getFullYear();

  if(year % 4 != 0)
  {
    return 365;
  }
  else if(((year % 100) != 0) || ((year % 400) != 0))
  {
    return 366
  }
  
  return 365
}

Date.prototype.isLeapYear = function()
{ 
  return this.daysInYear() == 366;
}

Date.prototype.getDaysInMonth = function()
{

  var DaysCount = new Array(31,28,31,30,31,30,31,31,30,31,30,31);  
  var Month = this.getMonth();

  if(this.isLeapYear() && Month == 1)
  {
    return 29;
  }
  else
  {
    return DaysCount[Month];
  }
  
}


