// ------------------------------------------------------------
// Called from the onload-event on the body-tag
// ------------------------------------------------------------
function pageLoaded()
{
}
adaptMainDivs();

// ------------------------------------------------------------
// Changes the height of the main divs of the page so
// they are the same height.
// ------------------------------------------------------------
function adaptMainDivs()
{
    var mainDiv = document.getElementById('maininnerdiv');
    var leftDiv = document.getElementById('leftareadiv');

    if (mainDiv && leftDiv)
    {        
        // Min-height
        var height = 350;
        
        // Main bottom margin
        var mainBottomMargin = 7;
        
        // Find the highest div
        if (leftDiv.clientHeight > height) height = leftDiv.clientHeight;
        if (mainDiv.clientHeight > height) height = mainDiv.clientHeight + mainBottomMargin;
        
        // Set new height
        leftDiv.style.height = height + 'px';
        mainDiv.style.height = (height-mainBottomMargin) + 'px';
    }
}

// ------------------------------------------------------------
// Builds an html-page for printing
// ------------------------------------------------------------
function printPage(pageName, appRoot) 
{
	if (!window.print)
	{
		window.status = 'No print';
		return;
	}

	// Get the main content area and other stuff we need
	var contentdiv		= document.getElementById('contentdiv');
	var footerdiv		= document.getElementById('sitefooterdiv');
	var rightareadiv	= document.getElementById('rightareadiv');

	if (contentdiv)
	{
        var logoHtml = '<img src="' + appRoot + 'images/eskilstunanu_nedtill.gif" style="padding-left: 20px;" alt="" />'
		var contentHtml = contentdiv.innerHTML;
		contentHtml += ('<div style="width: 180px; padding: 20px 0 0 34px">' +  rightareadiv.innerHTML + '</div>');
        var footerHtml = footerdiv ? '<hr style="margin-top: 20px"><div style="padding: 0 30px 0 30px">' + footerdiv.innerHTML + '</div>': '';

		var beginHtml = 
		      '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">' +
		      '<html>' +
			  '<head>' +
			  '<link rel="stylesheet" type="text/css" href="' + appRoot + 'styles/structure.css">' +
			  '<link rel="stylesheet" type="text/css" href="' + appRoot + 'styles/main.css">' +
			  '<link rel="stylesheet" type="text/css" href="' + appRoot + 'styles/units.css">' +
			  '<style> body { background-color: #fff } .PrintExclude { visibility: hidden; position: absolute; top: 0px; height: 0px } </style>' +
			  '<title>Eskilstuna.nu - ' + pageName + '</title>' +
			  '</head>' +
			  '<body style="margin: 10px 0px 10px 0px">';

		var endHtml = '</body></html>';

		var printWin = window.open('about:blank','','width=700,height=600,scrollbars=yes,toolbar=yes');
		printWin.document.open();
		printWin.document.write(beginHtml + 
		                        logoHtml +
		                        contentHtml + 
		                        footerHtml + 
		                        endHtml);
		printWin.document.close();

		printWin.print();
		printWin.close();
	}
}

// ------------------------------------------------------------
// This function will fire a click event on the specified control when the 
// enter key is pressed in a text field. Attach this function to the 
// onkeypress-event on the text field like this:
// <input type="text" onkeypress="return fireClickOnEnter(event, 'IdOfControlToFireClickOn');">
// ------------------------------------------------------------
function fireClickOnEnter(evt, controlId)
{
    var control = document.getElementById(controlId);
    var keyCode = (typeof window.event == 'object') ? window.event.keyCode : evt.keyCode;

    // If enter is pressed -> fire click-event on the control
    if (control && (keyCode == 13))
    {
        control.focus();
        control.click();
        return false;
    }
    else
    {
        return true;
    }
}
