/*

  JavaScript calendar by Andrew Savka.
  email: a_sawka@yahoo.com

*/


var ASDateFormat = "mm/dd/yyyy";
var ASCalendarImageURL = "adminimages/calendar.gif";

var ARR_AS_CALENDARS = new Array();

function ASCalendar(control,defaultDate,dx,dy)
{
  this.control = control;
  this.calendarShown = false;
  this.cursorOnCalendarBody = false;
  this.cursorOncalendarHeader = false;
  
  var controlParent = control.parentNode;
  var imageElement = document.createElement("img");
  var imageContainer = document.createElement("div");
  imageElement.setAttribute("src",ASCalendarImageURL);    
  imageElement.setAttribute("class","ASCalendarImage"); 
  
  imageElement.onmouseover = new Function("window.ARR_AS_CALENDARS['"+this.control.id+"'].OverCalendarHeader();");
  imageContainer.onmouseout = new Function("onmouseout","window.ARR_AS_CALENDARS['"+this.control.id+"'].OutCalendarHeader();");
  imageContainer.onclick = new Function("onclick","window.ARR_AS_CALENDARS['"+this.control.id+"'].CalendarShowHide();");    

  imageContainer.setAttribute("class","ASCalendarImageContainer");
  imageContainer.appendChild(imageElement);

  if(control.nextSibling != null && control.nextSibling != 'undefined')
  {
    controlParent.insertBefore(imageContainer,control.nextSibling)
  }
  else
  {
    controlParent.appendChild(imageContainer);
  }

  
  ARR_AS_CALENDARS[this.control.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 = document.createElement("div");      
  this.Container.onmouseover = new Function("window.ARR_AS_CALENDARS['"+this.control.id+"'].OverCalendarBody();");
  this.Container.onmouseout = new Function("window.ARR_AS_CALENDARS['"+this.control.id+"'].OutCalendarBody();");
  
  this.CalendarBody = document.createElement("div");
  this.CalendarBody.id = this.control.id + "_ASCalendarMonthBody";
  this.CalendarHeadBody = document.createElement("div");
  
  this.CalendarBody.setAttribute("class","ASCalendarCbody");

  for(var idxPosition = 0; idxPosition < 3; idxPosition++)
  {
    if(ASDateFormat.split('/')[idxPosition] == "dd")
    {
      this.DayPosition = idxPosition;
    } 
    else if(ASDateFormat.split('/')[idxPosition] == "mm")
    {
      this.MonthPosition = idxPosition;  
    }
    else if(ASDateFormat.split('/')[idxPosition] == "yyyy")
    {
      this.YearPosition = idxPosition;
    }
  }
  
  if(this.DayPosition == -1 || this.MonthPosition == -1 || this.YearPosition == -1)
  {
    alert("Wrong date format specified for ASCalendar.");
    return null;
  }

  var ControlHasCorrectDate = false;  
  if(this.control.value != "" && this.control.value.split('/').length == 3)
  {
    var Day = parseInt(this.control.value.split('/')[this.DayPosition]);
    var Month = parseInt(this.control.value.split('/')[this.MonthPosition]);
    var Year = parseInt(this.control.value.split('/')[this.YearPosition]);
        
    if(Month > 0 && Month < 13 && Day > 0 && Day <= daysInMonth(Month,Year))
    {
      ControlHasCorrectDate = true;
      this.currentDate = new Date(Year,Month-1,Day);
    }    
  }
  
  if(!ControlHasCorrectDate)
  {
    this.currentDate = this.defaultDate;
  }


  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.control.id + "_ASCalendar";
  this.Container.style.left = this.PosX;
  this.Container.style.top = this.PosY;
  this.Container.style.position = "absolute";
  this.Container.style.display = "none";
  
  document.body.appendChild(this.Container);  
  
  this.Generate();
  
  this.MonthSelect = new MonthSelect(this.control.id+"_ASCalendar_MonthSelect",this.control.id+"_ASCalendar_MonthSelectContainer",this.currentDate.getMonth());  
  this.MonthSelect.redraw();    
  this.MonthSelect.onMonthChanged = new Function("window.ARR_AS_CALENDARS['"+this.control.id+"'].UpdateMonth();");
  
  return this;
}


ASCalendar.prototype.Generate = function()
{
  var mTable = document.createElement("table");
  mTable.setAttribute("class","ASCalendarContainer");
  
  var mR1 = document.createElement("tr");
  var mC1 = document.createElement("td");  
  mC1.setAttribute("class","ASCalendarHeadCell");
  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","ASCalendarBodyCell");
  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 += "";
  
}

ASCalendar.prototype.RefreshHeader = function()
{
  this.CalendarHeadBody.innerHTML = "";
  this.CalendarHeadBody.appendChild(this.RenderHeader());
}

ASCalendar.prototype.RefreshMonthBody = function()
{
  var lObj = document.getElementById(this.control.id + "_ASCalendarMonthBody");
  lObj.innerHTML = "";
  lObj.appendChild(this.RenderMonthTable());
  lObj.innerHTML += "";  
}

ASCalendar.prototype.RenderHeader = function()
{
  var hTable = document.createElement("table");
  hTable.setAttribute("class","ASCalendarHeaderTable");
  hTable.setAttribute("cellspacing","0");
  hTable.setAttribute("cellpadding","0");
   
  var hRow = document.createElement("tr");
  var hCell = document.createElement("td");  
  hCell.setAttribute("class","ASCalendarHeaderCell");
  hCell.setAttribute("id",this.control.id+"_ASCalendar_MonthSelectContainer");
  this.MonthSelectContainer = hCell;
  hRow.appendChild(hCell);

  hCell = document.createElement("td");
  hCell.setAttribute("class","ASCalendarHeaderButton");
  hCell.appendChild(document.createTextNode("<"));  
  hCell.setAttribute("onmouseover","this.className = 'ASCalendarHeaderButtonHover'");
  hCell.setAttribute("onmouseout","this.className = 'ASCalendarHeaderButton'");  
  hCell.setAttribute("onclick","window.ARR_AS_CALENDARS['"+this.control.id+"'].PrevMonth()");
  hRow.appendChild(hCell);

  hCell = document.createElement("td");
  hCell.setAttribute("class","ASCalendarHeaderButton");
  hCell.appendChild(document.createTextNode(">"));  
  hCell.setAttribute("onmouseover","this.className = 'ASCalendarHeaderButtonHover'");
  hCell.setAttribute("onmouseout","this.className = 'ASCalendarHeaderButton'");  
  hCell.setAttribute("onclick","window.ARR_AS_CALENDARS['"+this.control.id+"'].NextMonth()");  
  hRow.appendChild(hCell);

  hCell = document.createElement("td");
  hCell.setAttribute("class","ASCalendarHeaderSpacer");
  hCell.appendChild(document.createTextNode(""));
  hRow.appendChild(hCell);

  hCell = document.createElement("td");
  hCell.setAttribute("class","ASCalendarHeaderCell");  
  var YearControl = document.createElement("input");
  YearControl.setAttribute("type","text");
  YearControl.setAttribute("class","ASCalendarYearControl");
  YearControl.setAttribute("value",this.currentDate.getFullYear());
  YearControl.setAttribute("onblur","window.ARR_AS_CALENDARS['"+this.control.id+"'].YearChanged()");
  YearControl.setAttribute("id",this.control.id+"_ASCalendarYearControl");
  this.YearControl = YearControl;  
  hCell.appendChild(YearControl);
  hRow.appendChild(hCell);
  
  hCell = document.createElement("td");
  hCell.setAttribute("class","ASCalendarHeaderButton");
  hCell.appendChild(document.createTextNode("<"));  
  hCell.setAttribute("onmouseover","this.className = 'ASCalendarHeaderButtonHover'");
  hCell.setAttribute("onmouseout","this.className = 'ASCalendarHeaderButton'");  
  hCell.setAttribute("onclick","window.ARR_AS_CALENDARS['"+this.control.id+"'].PrevYear()");
  hRow.appendChild(hCell);

  hCell = document.createElement("td");
  hCell.setAttribute("class","ASCalendarHeaderButton");
  hCell.appendChild(document.createTextNode(">"));  
  hCell.setAttribute("onmouseover","this.className = 'ASCalendarHeaderButtonHover'");
  hCell.setAttribute("onmouseout","this.className = 'ASCalendarHeaderButton'");  
  hCell.setAttribute("onclick","window.ARR_AS_CALENDARS['"+this.control.id+"'].NextYear()");  
  hRow.appendChild(hCell);
        
  hTable.appendChild(hRow);
  
  return hTable;
}

ASCalendar.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");
  monthTable.setAttribute("class","MonthTable");
  
  monthTable.appendChild(this.RenderWeekHeadersRow());
  
  
  for(var idxWeek = 0; idxWeek < WeekCount; idxWeek++)
  {
    monthTable.appendChild(this.RenderWeekRow(idxWeek))
  }
  
  return monthTable;
}

ASCalendar.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");
    hCell.setAttribute("class","WeekHeaderCell");
    hCell.appendChild(document.createTextNode(WeekdayHeaders[idxWeekday]));
    headersRow.appendChild(hCell);
  }
  
  return headersRow;  
}

ASCalendar.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;
}

ASCalendar.prototype.RenderDayCell = function(day)
{
  var dayDate = new Date(this.currentDate.getFullYear(),this.currentDate.getMonth(),day+1);
  var wkDay = dayDate.getDay();
  var cellClass = "DayCell"+wkDay;
  
  var dayCell = document.createElement("td");
  dayCell.setAttribute("class",cellClass);
  dayCell.setAttribute("onmouseover","this.className = '"+cellClass+"w'");
  dayCell.setAttribute("onmouseout","this.className = '"+cellClass+"'");  
  dayCell.setAttribute("onclick","window.ARR_AS_CALENDARS['"+this.control.id+"'].SelectDate("+(day+1)+")");
  dayCell.appendChild(document.createTextNode(day+1));
  
  return dayCell;
}

ASCalendar.prototype.RenderEmptyDayCell = function()
{
  var dayCell = document.createElement("td");
  dayCell.setAttribute("class","EmptyDayCell");
  dayCell.appendChild(document.createTextNode(" "));
  
  return dayCell;  
}

ASCalendar.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();
}

ASCalendar.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();
  }
}

ASCalendar.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();    
  }
}

ASCalendar.prototype.NextYear = function()
{
  var currentYear = this.currentDate.getFullYear();
  currentYear ++;
  document.getElementById(this.control.id+"_ASCalendarYearControl").value = currentYear;
  this.currentDate.setFullYear(currentYear);
  this.RefreshMonthBody();
}

ASCalendar.prototype.PrevYear = function()
{
  var currentYear = this.currentDate.getFullYear();
  currentYear --;
  document.getElementById(this.control.id+"_ASCalendarYearControl").value = currentYear;
  this.currentDate.setFullYear(currentYear);
  this.RefreshMonthBody();
}

ASCalendar.prototype.UpdateMonth = function()
{
  this.currentDate.setMonth(this.MonthSelect.currentMonth);
  this.RefreshMonthBody();
}


ASCalendar.prototype.YearChanged = function()
{
  var Year = document.getElementById(this.control.id+"_ASCalendarYearControl").value;
  if(/^[0-9]{1,4}$/.test(Year))
  {
    this.currentDate.setFullYear(Year);
    this.RefreshMonthBody();
  }
}

ASCalendar.prototype.HideCalendar = function()
{
  if(!this.cursorOnCalendarBody && !this.cursorOnCalendarHeader && this.calendarShown && !this.MonthSelect.cursorOnMenuBody)
  {
    this.CalendarShowHide();
  }
}

ASCalendar.prototype.ShowCalendar = function()
{
  if((this.cursorOnCalendarBody || this.cursorOnCalendarHeader) && !this.calendarShown)
  {
    this.CalendarShowHide();    
  }
}

ASCalendar.prototype.DelayedHideCalendar = function()
{
  window.setTimeout("ARR_AS_CALENDARS['"+this.control.id+"'].HideCalendar()",500);
}

ASCalendar.prototype.DelayedShowCalendar = function()
{
  window.setTimeout("ARR_AS_CALENDARS['"+this.control.id+"'].ShowCalendar()",500);
}

ASCalendar.prototype.OverCalendarBody = function()
{
  this.cursorOnCalendarBody = true;
}

ASCalendar.prototype.OutCalendarBody = function()
{
  this.cursorOnCalendarBody = false;
  this.DelayedHideCalendar();
}

ASCalendar.prototype.OverCalendarHeader = function()
{
  this.DelayedShowCalendar();
  this.cursorOnCalendarHeader = true;  
}

ASCalendar.prototype.OutCalendarHeader = function()
{
  this.cursorOnCalendarHeader = false;
  this.DelayedHideCalendar();
}

ASCalendar.prototype.CalendarShowHide = function()
{
  
  var menuObj = this.Container;

  if(this.calendarShown)
  {
    menuObj.style.display = "none";
  }
  else  
  {          
    menuObj.style.display = "block";    
  }
  
  this.calendarShown = !this.calendarShown;

}


/*
===============================================================================
       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;
}


/*
===============================================================================
              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];
  }
  
}


