/*

  JavaScript calendar by Andrew Savka.
  email: a_sawka@yahoo.com

*/


var ASDateFormat = "mm/dd/yyyy";
var ASCalendarImageURL = "adminimages/calendar.gif";

var ARR_AS_POPUPMENUS = new Array();

function ASPopupMenu(control,arrMenuItems,dx,dy)
{
  this.control = control;
	this.arrMenuItems = arrMenuItems;
  this.calendarShown = false;
  this.cursorOnMenuBody = false;
  this.cursorOnMenuHeader = false;
    
  addListener(this.control,"mouseover",new Function("window.ARR_AS_POPUPMENUS['"+this.control.id+"'].OverMenuHeader();"));
  addListener(this.control,"mouseout",new Function("onmouseout","window.ARR_AS_POPUPMENUS['"+this.control.id+"'].OutMenuHeader();"));

//  addListener(this.control,"mouseover",function(){this.OverMenuHeader});
//  addListener(this.control,"mouseout",new Function("onmouseout","window.ARR_AS_POPUPMENUS['"+this.control.id+"'].OutMenuHeader();"));

  ARR_AS_POPUPMENUS[this.control.id] = this;
    
  this.Container = document.createElement("div");      
  this.Container.onmouseover = new Function("window.ARR_AS_POPUPMENUS['"+this.control.id+"'].OverMenuBody();");
  this.Container.onmouseout = new Function("window.ARR_AS_POPUPMENUS['"+this.control.id+"'].OutMenuBody();");
  
	
	
  this.MenuBody = document.createElement("div");
  this.MenuBody.id = this.control.id + "_ASPopupMenuBody";
  
  this.MenuBody.setAttribute("class","ASPopupMenuCbody");

  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 + "_ASPopupMenu";
  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();
  return this;
}


ASPopupMenu.prototype.Generate = function()
{
  var mTable = document.createElement("table");
  mTable.setAttribute("class","ASPopupMenuContainer");
  
  
  var mR2 = document.createElement("tr");
  var mC2 = document.createElement("td");
  mC2.setAttribute("class","ASPopupMenuBodyCell");
  mC2.setAttribute("width","100%");  
  mC2.appendChild(this.MenuBody);
  mR2.appendChild(mC2);
  mTable.appendChild(mR2);

  this.Container.appendChild(mTable);  
  this.RefreshMenuBody();
  this.Container.innerHTML += "";
  
}

ASPopupMenu.prototype.RefreshMenuBody = function()
{
  var lObj = document.getElementById(this.control.id + "_ASPopupMenuBody");
  lObj.innerHTML = "";
  lObj.appendChild(this.RenderMenuTable());
  lObj.innerHTML += "";  
}

ASPopupMenu.prototype.RenderMenuTable = function()
{
  
  var menuTable = document.createElement("table");
	menuTable.setAttribute("cellspacing","0");
  menuTable.setAttribute("class","MenuTable");    
  
  for(var idxMenu = 0; idxMenu < this.arrMenuItems.length; idxMenu++)
  {
    menuTable.appendChild(this.RenderMenuRow(idxMenu))
  }
  
  return menuTable;
}

ASPopupMenu.prototype.RenderMenuRow = function(menuIdx)
{

  var menuRow = document.createElement("tr");
  menuRow.setAttribute("class","ASPopupMenuRow");
  
	var menuCell = document.createElement("td");
	menuCell.setAttribute("class","ASPopupMenuCell");
	menuCell.setAttribute("onmouseover","this.className='ASPopupMenuCell_hover'");
	menuCell.setAttribute("onmouseout","this.className='ASPopupMenuCell'");
	menuCell.setAttribute("onclick","window.location.href='"+this.arrMenuItems[menuIdx]["url"]+"'");
	menuCell.appendChild(document.createTextNode(this.arrMenuItems[menuIdx]["caption"]));	
	
//	var menuLink = document.createElement("a");
//	menuLink.setAttribute("class","ASPopupMenuItemLink");
//	menuLink.setAttribute("href",this.arrMenuItems[menuIdx]["url"]);
//	menuLink.appendChild(document.createTextNode(this.arrMenuItems[menuIdx]["caption"]));
  
//	menuCell.appendChild(menuLink);
	menuRow.appendChild(menuCell);
	  
  return menuRow;
}



ASPopupMenu.prototype.HideMenu = function()
{
  if(!this.cursorOnMenuBody && !this.cursorOnMenuHeader && this.menuShown)
  {
    this.MenuShowHide();
  }
}

ASPopupMenu.prototype.ShowMenu = function()
{	
  if((this.cursorOnMenuBody || this.cursorOnMenuHeader) && !this.menuShown)
  {		
    this.MenuShowHide();    
  }
}

ASPopupMenu.prototype.DelayedHideMenu = function()
{
  window.setTimeout("ARR_AS_POPUPMENUS['"+this.control.id+"'].HideMenu()",200);
}

ASPopupMenu.prototype.DelayedShowMenu = function()
{
  window.setTimeout("ARR_AS_POPUPMENUS['"+this.control.id+"'].ShowMenu()",200);
}

ASPopupMenu.prototype.OverMenuBody = function()
{
  this.cursorOnMenuBody = true;
}

ASPopupMenu.prototype.OutMenuBody = function()
{
  this.cursorOnMenuBody = false;
  this.DelayedHideMenu();
}

ASPopupMenu.prototype.OverMenuHeader = function()
{
  this.DelayedShowMenu();
  this.cursorOnMenuHeader = true;  
}

ASPopupMenu.prototype.OutMenuHeader = function()
{
  this.cursorOnMenuHeader = false;
  this.DelayedHideMenu();
}

ASPopupMenu.prototype.MenuShowHide = function()
{

  var menuObj = this.Container;

  if(this.menuShown)
  {
    menuObj.style.display = "none";
  }
  else  
  {          
    menuObj.style.display = "block";    
		menuObj.style.left = this.PosX;
		menuObj.style.top = this.PosY;		
  }
  
  this.menuShown = !this.menuShown;

}

/*
===============================================================================
              Common Functions
===============================================================================
*/

function addListener(element, event, listener, bubble) {
  if(element.addEventListener) {
    if(typeof(bubble) == "undefined") bubble = false;
    element.addEventListener(event, listener, bubble);
  } else if(this.attachEvent) {
    element.attachEvent("on" + event, listener);
  }
}

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];
  }
  
}
