// ------------------------------------------------------------
// Global variables
// ------------------------------------------------------------
var gArticleRows = new Array();


// ------------------------------------------------------------
// Called from the onload-event on the body-tag
// ------------------------------------------------------------
function pageLoaded()
{
    adaptMainDivs();
    adaptArticleRows()
}


// Makes sure the three main divs are the same height.
function adaptMainDivs()
{
    var leftDiv		   = document.getElementById('leftareadiv');
    var mainDiv		   = document.getElementById('mainareadiv');
    var rightDiv	   = document.getElementById('rightareadiv');
    var mainandleftdiv = document.getElementById('mainandleftdiv');

    // min-height
    var height = 400;
       
    // find the highest div
    if (leftDiv  && leftDiv.clientHeight  > height) height = leftDiv.clientHeight;
    if (mainDiv  && mainDiv.clientHeight  > height) height = mainDiv.clientHeight;
    if (rightDiv && rightDiv.clientHeight > height)	height = rightDiv.clientHeight;
    if (mainandleftdiv && mainandleftdiv.clientHeight > height) height = mainandleftdiv.clientHeight;

    // set new height
    if (leftDiv)  leftDiv.style.height  = height + 'px';
    if (mainDiv)  mainDiv.style.height  = height + 'px';
    if (rightDiv) rightDiv.style.height = height + 'px';
    if (mainandleftdiv) mainandleftdiv.style.height = height + 'px';
}






// ------------------------------------------------------------
// Adapts the height of the rows containing articles
// ------------------------------------------------------------
function adaptArticleRows()
{
    // Loop over all the article rows specified in the global array
    for (var i = 0; i < gArticleRows.length; i++)    
    {
        alignChildHeight(gArticleRows[i]);
    }
}

// ------------------------------------------------------------
// Sets the height of all the children of the specified element
// to the height of the highest child.
// ------------------------------------------------------------
function alignChildHeight(parentId)
{
    var parentElement = document.getElementById(parentId);
    if (parentElement)
    {
        var maxHeight = 0;

        // Loop over all children to find the maxHeight
        for (i = 0; i < parentElement.childNodes.length; i++)
        {
            if (parentElement.childNodes[i].clientHeight > maxHeight)
            {
                maxHeight = parentElement.childNodes[i].clientHeight;
            }
        }

        // Loop over all children and set the height
        for (i = 0; i < parentElement.childNodes.length; i++)
        {
            if (parentElement.childNodes[i].style && 
                (parentElement.childNodes[i].style.clear != 'both'))
            {
                parentElement.childNodes[i].style.height = maxHeight + 'px';
            }
        }
    }
}

// ------------------------------------------------------------
// 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;
    }
}









// ------------------------------------------------------------
// 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 breadcrumbdiv = document.getElementById('breadcrumbdiv');
	var contentdiv    = document.getElementById('mainareadiv');
	var footerdiv     = document.getElementById('footerdiv');

	if (contentdiv)
	{
//        var logoHtml = '<div style="float: left; padding-left: 20px;"><img src="' + appRoot + 'images/logo_printable.gif" alt="" /></div>'
//		var breadcrumbHtml = breadcrumbdiv ? breadcrumbdiv.innerHTML : '';
//        logoHtml += '<div style="float: right; padding-right: 20px;">' + breadcrumbHtml + '</div><hr style="clear: both;"></hr><br />';
		var contentHtml    = contentdiv.innerHTML;
        var footerHtml     = footerdiv     ? '<hr>' + footerdiv.innerHTML : '';

		var beginHtml = 
		      '<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> .PrintExclude { visibility: hidden; position: absolute; top: 0px; height: 0px }</style>' +
			  '<title>NORTHERN EUROPEAN PROPERTIES LTD - ' + pageName + '</title>' +
			  '</head>' +
			  '<body style="margin: 10px 0px 10px 0px; background-color: #ffffff;">';

		var endHtml = '</body></html>';

		var printWin = window.open('about:blank','','width=720,height=600,scrollbars=yes,toolbar=yes');
		printWin.document.open();
		printWin.document.write(beginHtml + 
//		                        logoHtml +
		                        contentHtml + 
		                        footerHtml + 
		                        endHtml);
		printWin.document.close();

		printWin.print();
	}
}


function openPrintWindow(url)
{
    w = window.open(url, 'PrintWindow', 'width=666,height=614,location=no,scrollbars=yes,menubar=no,toolbar=no,resizable=yes,status=yes');
    w.focus();
}

// ---------------
// Open SendTip-window
// ---------------
function TipPage(url)
{
    w = window.open(url, 'PrintOfficeWindow', 'width=340,height=600,location=no,scrollbars=yes,menubar=no,toolbar=no,resizable=no,status=yes');
    w.focus();
}
