/*********************************************************************
 *
 * The following routines support date-picker popups
 *
 *********************************************************************/	
	
var calendar, popup, calendarContainer, activeInput, isVisible;

function ToggleCalendar(e, inputId, calendarId)
{
    ShowCalendar(inputId, calendarId);

    e.cancelBubble = true;
}

function ShowCalendar(inputId)
{
    if (!calendar)
    {
        if (typeof(calendarId)!="undefined")
        {
            calendar = calendarId;
        }
        else
        {
            return;
        }
    }
    
    if (!popup)
        popup = new RadCalendar.Popup();
        
    if (!calendarContainer)
        calendarContainer = document.getElementById("calendarContainer");
        
    if (activeInput
        && activeInput.id == inputId
        && popup.IsVisible())
    {
        popup.Hide();
    }            
    else
    {        
        activeInput = YAHOO.util.Dom.get(inputId);
        var x = YAHOO.util.Dom.getX(activeInput);
        var y = YAHOO.util.Dom.getY(activeInput);
        
        var clientHeight = YAHOO.util.Dom.getViewportHeight();
        var clientWidth = YAHOO.util.Dom.getViewportWidth();
        
        // Set the width and height of the calendar
			var calWidth = 235;
			var calHeight = 235;
			
			if ((x + calWidth) > clientWidth)
				x = (clientWidth - calWidth);
				
			if ((y + activeInput.offsetHeight + calHeight) > clientHeight)
				y = (y - calHeight);
			else
				y += activeInput.offsetHeight;
        
        popup.Show(x, y, calendarContainer);
    }
}

function HideCalendar()
{
	if (popup && popup.IsVisible())
		popup.Hide();
}	

if (window.attachEvent)
{
	window.attachEvent("onresize", HideCalendar);
	window.attachEvent("onscroll", HideCalendar);
}

if (window.addEventListener)
{
	window.addEventListener("resize", HideCalendar, true);
	window.addEventListener("scroll", HideCalendar, true);
}

window.scroll = HideCalendar;    