addEventHandler('load', window_load, window);

autoHideHorizontalScrollbar();

function window_load()
{
    // TODO: not sure why we need this ... it was from the India developers
    // fixLayout();
    
//    var lnkPrintPage = document.getElementById('linkPrintPage');
//    addEventHandler('click', PrintPage, lnkPrintPage);
}

function PrintPage()
{
    window.print();
}

function autoHideHorizontalScrollbar()
{   
    // hide the horizontal scrollbar, unless screen resolution is less than the width of the content
    if (document.body.style)
    {
        var winSize = GetWindowSize();
        
        if (winSize.width < 975)
            document.documentElement.style.overflowX = 'scroll';
        else
            document.documentElement.style.overflowX = 'hidden';
    }
}

/* TODO: I don't think this is being used (it could be for IE 6, maybe?)
function fixLayout()
{
    var box1 = document.getElementById('column');
    var box2 = document.getElementById('content');

    if (box1 && box2)
	{
		if ((box2.offsetHeight - 232) >= box1.offsetHeight)
		    box1.style.height = box2.offsetHeight + 'px';
	}
}
*/

function LoadPopup(elemId, contentPage, title)
{
    var popup   = document.getElementById(elemId);
    var content = document.getElementById('PopupContent');
    
    while (content.hasChildNodes()) 
    {
        content.removeChild(content.firstChild);
    }
    
    var iFrame = document.createElement('iframe');

    // src="IframeContent/Microsoft_Gold_Certified_Partner.aspx" scrolling="auto" frameborder="0"
    iFrame.setAttribute('scrolling', 'auto');
    iFrame.setAttribute('frameborder', '0');
    iFrame.setAttribute('style', 'width:92%; padding: 5px; margin: 5px;');
    
    if (!iFrame)
    {
        alert('error loading popup');
        return;
    }
    
    iFrame.src = contentPage;
    
    content.appendChild(iFrame);
    
    ShowPopup(elemId, 500, 400);
}

function ShowPopup(elemId, width, height)
{
    var popup = document.getElementById(elemId);
    var bg    = document.getElementById('PopupBackground');
    
    if (popup)  popup.style.display = 'none';
    if (bg)     bg.style.display   = 'none';
    
    var content = document.getElementById('PopupContent');
    
    var winSize = GetWindowSize();
    var offset  = GetScrollPosition();
    
    var xPos = (winSize.width / 2)  - (width / 2);
    var yPos = (winSize.height / 2) - (height / 2) + offset.y - 20;
    
    if (yPos < offset.y) yPos = offset + 20;
    
    // set background position
    if (bg)
    {
        bg.style.top     = offset.y + 'px';
        bg.style.left    = offset.x + 'px';
        // bg.style.display = 'block';
    }
    
    // set popup position
    if (popup)
    {   
        popup.style.top     = yPos + 'px';
        popup.style.left    = xPos + 'px';
        popup.style.width   = width  + 'px';
        popup.style.display = 'block';
    }
    
    if (content)  
    {
        content.style.height = height + 'px';
    }
}

function HidePopup(elemId)
{
    var elem = document.getElementById(elemId);
    var bg   = document.getElementById('PopupBackground');
 
    if (elem)  elem.style.display = 'none';
    if (bg)    bg.style.display   = 'none';
}

function GetWindowSize()
{
    var width  = '1024';
    var height = '768';
    
    var winSize = new Size(width, height);
    
    if (document.documentElement && document.documentElement.clientWidth)
    {
	    winSize.width  = document.documentElement.clientWidth;
	    winSize.height = document.documentElement.clientHeight;
    }
    else if (document.body)
    {
	    winSize.width  = document.body.clientWidth;
	    winSize.height = document.body.clientHeight;
    }
    else if (self.innerWidth)
    {
	    winSize.width  = self.innerWidth;
	    winSize.height = self.innerHeight;
    }
    
    if (winSize.height < 1)  winSize.height = 1;
    if (winSize.width < 1)   winSize.width  = 1;
    
    return winSize;
}

function GetScrollPosition()
{
    var scrollPos = new Position(0, 0);
    
    if (document.documentElement && document.documentElement.scrollTop)
    {
        scrollPos.x = document.documentElement.scrollLeft;
        scrollPos.y = document.documentElement.scrollTop;
        
        // alert(document.documentElement.scrollLeft + ", " + document.documentElement.scrollTop);
    }
    else if (document.body && document.body.scrollTop)
    {
        scrollPos.x = document.body.scrollLeft;
        scrollPos.y = document.body.scrollTop;
    }
    else if (window.pageXOffset)
    {
        scrollPos.x = window.pageXOffset;
        scrollPos.y = window.pageYOffset;
    }
    
    // alert("Pos: " + scrollPos.x + ", " + scrollPos.y);
    
    return scrollPos;
}

function Size(width, height)
{
    this.width  = width;
    this.height = height;
}

function Position(xPos, yPos)
{
    this.x = xPos;
    this.y = yPos;
}