﻿// ---------------
// Toggles visibility of the specified element.
// ---------------
function toggleVisibility(elemId)
{
	var elem = document.getElementById(elemId);
	
	if (elem)
	{
		if (elem.style.display == 'none')
		{		
			elem.style.display = 'block';
		}
		else
		{			
			elem.style.display = 'none';
		}
	}
}

// ------------------------------------------------------------
// Returns the x coordinate of the specified object
// ------------------------------------------------------------
function findPosX(obj)
{
    var curleft = 0;
    if (obj.offsetParent)
    {
        while (obj.offsetParent)
        {
            curleft += obj.offsetLeft
            obj = obj.offsetParent;
        }
    }
    else if (obj.clientLeft)
    {
        curleft += obj.clientLeft;
    }
    return curleft;
}

// ------------------------------------------------------------
// Returns the y coordinate of the specified object
// ------------------------------------------------------------
function findPosY(obj)
{
    var curtop = 0;
    if (obj.offsetParent)
    {
        while (obj.offsetParent)
        {
            curtop += obj.offsetTop
            obj = obj.offsetParent;
        }
    }
    else if (obj.clientTop)
    {
        curtop += obj.clientTop;
    }
    return curtop;
}

// ------------------------------------------------------------
// Changes the height of a number of elements so they are the same
// height. The input param is an array with elements-id:s.
// ------------------------------------------------------------
function adaptElementHeight(divNameArr, minHeight)
{
	// This script messes up the page when it's displayed in EPi's
	// edit mode so we need this rather ugly work-around to prevent
	// the script from running when the page is viewed in edit mode.
	if (window.parent.name == 'EditPanel')
	{
		return; 
	}
	
    var height = minHeight;
    var divArr = Array();
    
    // Loop over all the divs to get the height if the highest
    for (i = 0; i < divNameArr.length; i++)
    {
        divArr[i] = document.getElementById(divNameArr[i]);
        
        if (divArr[i])
        {
			// We set the height to auto to erase any previously hardcoded pixel
			// values which will otherwise prevent this function from running correctly
			// more than one time. It's useful to be able to call this function
			// again when dynamic content is displayed on the page.
	        divArr[i].style.height = 'auto';
	        
	        if (divArr[i].clientHeight > height)
	        {
				height = divArr[i].clientHeight;
			}
        }
    }    

    // Set the height for all divs
    for (i = 0; i < divArr.length; i++)
    {
        if (divArr[i])
        {
            divArr[i].style.height = height + 'px';
        }
    }
}

// ------------------------------------------------------------
// Sets the height of all the children of the specified element
// to the height of the highest child.
// ------------------------------------------------------------
function adaptChildHeight(parentId)
{
	// This script messes up the page when it's displayed in EPi's
	// edit mode so we need this rather ugly work-around to prevent
	// the script from running when the page is viewed in edit mode.
	if (window.parent.name == 'EditPanel')
	{
		return;
	}

    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].className != 'clear')
            {
                if (parentElement.childNodes[i].className == 'puff-block-spacer-long') {
                    parentElement.childNodes[i].style.height = (maxHeight + 10) + 'px';
                } else {
                    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;
    }
}

// ------------------------------------------------------------
// This function will fire a postbackon 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 postbackOnEnter(event, 'IdOfControlToFirePostbackOn');">
// ------------------------------------------------------------
function postbackOnEnter(evt, controlId)
{
    var keyCode = (typeof window.event == 'object') ? window.event.keyCode : evt.keyCode;

    // If enter is pressed -> do a postback
    if (keyCode == 13)
    {
		__doPostBack(controlId,'');
		return false;
    }
    else
    {
        return true;
    }
}

function addLoadEvent(func) 
{
    var oldonload = window.onload;
    if (typeof window.onload != 'function') 
    {
        window.onload = func;
    } 
    else 
    {
        window.onload = function()
        {
            if (oldonload)
            {
                oldonload();
            }
            func();
        }
    }
}

var getFFVersion=navigator.userAgent.substring(navigator.userAgent.indexOf("Firefox")).split("/")[1]
            	    var FFextraHeight=parseFloat(getFFVersion)>=0.1? 16 : 0 //extra height in px to add to iframe in FireFox 1.0+ browsers

function resizeIframe(frameid)
{

    var currentfr=document.getElementById(frameid);
    if (currentfr && !window.opera)
    {
        currentfr.style.display="block";
        
        if (currentfr.contentDocument && currentfr.contentDocument.body && currentfr.contentDocument.body.offsetHeight) //ns6 syntax
            currentfr.height = currentfr.contentDocument.body.offsetHeight+FFextraHeight; 
        else if (currentfr.Document && currentfr.Document.body && currentfr.Document.body.scrollHeight) //ie5+ syntax
        {
            if (currentfr.Document.body.scrollHeight == 35)	//Avantime fix for hiding iframe if empty.
                currentfr.style.display="none";
            else
                currentfr.height = currentfr.Document.body.scrollHeight;
        }
        if (currentfr.addEventListener)
            currentfr.addEventListener("load", readjustIframe, false);
        else if (currentfr.attachEvent)
        {
            currentfr.detachEvent("onload", readjustIframe); // Bug fix line
            currentfr.attachEvent("onload", readjustIframe);
        }
        
    }
}


function readjustIframe(loadevt)
{
    var crossevt=(window.event)? event : loadevt
    var iframeroot=(crossevt.currentTarget)? crossevt.currentTarget : crossevt.srcElement
    if (iframeroot)
    resizeIframe(iframeroot.id);
}

function OpenAgree(pageId)
{
    var printWin = window.open('/Pages/Agree.aspx?pageid='+pageId,'','width=400,height=350,scrollbars=yes,toolbar=no');
}