﻿// ---------------
// 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';
		}
	}
}

// ------------------------------------------------------------
// 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].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;
    }
}

// ------------------------------------------------------------
// 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;
    }
}

// ------------------------------------------------------------
// 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;
}



// ---------------
// Returns the scroll offset
// ---------------
function getScrollOffset()
{
    scrollOffset = {};
    if (window.pageYOffset)// all except Explorer
    { 
        scrollOffset.x = window.pageXOffset;
        scrollOffset.y = window.pageYOffset;
    }
    else if (document.documentElement && document.documentElement.scrollTop) // Explorer 6 Strict
    {
        scrollOffset.x = document.documentElement.scrollLeft;
        scrollOffset.y = document.documentElement.scrollTop;
    }
    else if (document.body) // all other Explorers
    {
        scrollOffset.x = document.body.scrollLeft;
        scrollOffset.y = document.body.scrollTop;
    }
    return scrollOffset;
}


function getWindowHeight() {
	var windowHeight = 0;
	if (typeof(window.innerHeight) == 'number') {
		windowHeight = window.innerHeight;
	}
	else {
		if (document.documentElement && document.documentElement.clientHeight) {
			windowHeight = document.documentElement.clientHeight;
		}
		else {
			if (document.body && document.body.clientHeight) {
				windowHeight = document.body.clientHeight;
			}
		}
	}
	return windowHeight;
}



// ---------------
// Returns the size of the viewport.
// ---------------
function getViewportSize()
{
    size = {};
    if (window.innerHeight)
    {
    	size.width  = window.innerWidth;
    	size.height = window.innerHeight;
    }
    else if (document.documentElement && document.documentElement.clientHeight)
    {
    	size.width  = document.documentElement.clientWidth;
    	size.height = document.documentElement.clientHeight;
    }
    else if (document.body)
    {
    	size.width  = document.body.clientWidth;
    	size.height = document.body.clientHeight;
    }
    return size;
}

function setHeightFromDiv( setImgID, fromDivID )
{
    var setImg = document.getElementById(setImgID);
    var fromDiv = document.getElementById(fromDivID);
    
    if( setImg && fromDiv )
    {
        setImg.style.height = fromDiv.clientHeight + "px";
        setImg.height = fromDiv.clientHeight + "px";
        
    }

}


function tipAFriend(url)
{
    window.open(url, '', 'width=620,height=500,scrollbars=yes,toolbar=yes');
}


// Search for divs with right classname 
function getDivsByClassName(needle) 
{ 
    var toproot = document.getElementById('MainMenu')

//   var my_array = document.getElementsByTagName("div"); 

   var my_array = toproot.getElementsByTagName("div"); 


   
   var retvalue = new Array(); 
   var i; 
   var j; 

   for (i=0,j=0;i<my_array.length;i++) { 
      var c = " " + my_array[i].className + " "; 
      if (c.indexOf(" " + needle + " ") != -1) retvalue[j++] = my_array[i]; 
    } 
    return retvalue; 
} 


//// Search for divs with right classname 
//function getDivsByClassName(needle, searchfromroot) 
//{ 
//    var toproot = document.getElementById(searchfromroot)
//    if(toproot)
//    {
//        var my_array = toproot.getElementsByTagName("div"); 
//       
//        var retvalue = new Array(); 
//        var i; 
//        var j; 

//        for (i=0,j=0;i<my_array.length;i++) { 
//            var c = " " + my_array[i].className + " "; 
//            if (c.indexOf(" " + needle + " ") != -1) retvalue[j++] = my_array[i]; 
//        } 
//        return retvalue; 
//    }
//    return null;
//} 


// ---------------
// Hides all windowed controls in browsers that
// can't display layers on top of them.
// ---------------
function hideWindowedControls()
{
	if ((BrowserDetect.browser == 'Explorer') && (BrowserDetect.version < 7))
	{
	    // Hides all listboxes
		for (var i = 0; i < document.getElementsByTagName('select').length; i++)
		{
			document.getElementsByTagName('select')[i].style.visibility = 'hidden';
		}
	}
}

// ---------------
// Shows all windowed controls.
// ---------------
function showWindowedControls()
{
	if ((BrowserDetect.browser) == 'Explorer' && (BrowserDetect.version < 7))
	{
	    // Show all listboxes
		for (var i = 0; i < document.getElementsByTagName('select').length; i++)
	    {
		    document.getElementsByTagName('select')[i].style.visibility = 'visible';
	    }
    }
}

// ---------------
// Opens a dialog. The first parameter should be the dialog name.
// ---------------
function openDialog()
{
    //    gCounter =gCounter+1;

	var dialogName	= arguments[0];
	var dialog		= document.getElementById('DialogContainer');
	var hiddenField	= document.getElementById(gDialogHiddenFieldId);	

//    try
//    {
//        if(dialogName = "TipFriend")
//        {
//            s.prop9 = "Tipsa van";
//        }
//        if(dialogName = "ObjectIntrestRegistration")
//        {
//            s.prop9 = "Anmal intresse";
//        }
//    }catch(err){}
	if (dialog && hiddenField)
	{
		dialog.className  = 'Dialog ' + dialogName;
		hiddenField.value = '';
		// Make a delimited string with the dialogname and parameters
		
		for (var i = 0; i < arguments.length; i++)
		{
			hiddenField.value += arguments[i];
			if (i+1 < arguments.length) hiddenField.value += '|';
}

        __doPostBack(gDialogHiddenFieldId, '');
		
		hideWindowedControls();
		showDialogBackground();

	}
}

// ---------------
// Closes the dialog-layer.
// ---------------
function closeDialog()
{
//    gCounter =gCounter-1;
	var dialog		= document.getElementById('DialogContainer');
	var hiddenField	= document.getElementById(gDialogHiddenFieldId);
	if (dialog && hiddenField)
	{
		dialog.style.display = 'none';
		hiddenField.value	 = '';
		__doPostBack(gDialogHiddenFieldId,'');	
				
		hideDialogBackground();
		showWindowedControls();
	}
	var helpDialog		= document.getElementById('DialogHelpContainer');
	var helphiddenField	= document.getElementById(gHelpDialogHiddenFieldId);	
    if(helpDialog)
	{
	    helpDialog.style.display = 'none';
		helphiddenField.value	 = '';
		__doPostBack(gHelpDialogHiddenFieldId,'');	
	}
	
}


// ---------------
// Positions the dialog layer in the center of the screen.
// ---------------
function positionDialog()
{
	var dialog = document.getElementById('DialogContainer');
	if (dialog)
	{
		var viewSize   = getViewportSize();
		var scrollSize = getScrollOffset();
		dialog.style.display = 'block';
		
		// Center window on screen
		dialog.style.left = (scrollSize.x + (viewSize.width  - dialog.clientWidth)  / 2) + 'px';
				
		var marginTop = dialog.clientHeight>viewSize.height ? ((viewSize.height-dialog.clientHeight)/2):100;
        var top = (scrollSize.y + ((viewSize.height - dialog.clientHeight) / 2) - marginTop);
		if( top>scrollSize.y)
		    dialog.style.top  = top  + 'px';    
		 else
		    dialog.style.top  = scrollSize.y  + 'px';    		 		
	}
}

function positionHelpDialog() {
    var dialog = document.getElementById('DialogHelpContainer');
    if (dialog) {
        var viewSize = getViewportSize();
        var scrollSize = getScrollOffset();
        dialog.style.display = 'block';

        // Center window on screen
        dialog.style.left = (scrollSize.x + (viewSize.width - dialog.clientWidth) / 2) + 'px';

        var marginTop = dialog.clientHeight > viewSize.height ? ((viewSize.height - dialog.clientHeight) / 2) : 100;
        var top = (scrollSize.y + ((viewSize.height - dialog.clientHeight) / 2) - marginTop);
        if (top > scrollSize.y)
            dialog.style.top = top + 'px';
        else
            dialog.style.top = scrollSize.y + 'px';
    }
}

//// ---------------
//// Positions the dialog layer in the center of the screen.
//// ---------------
//function positionDialog()
//{
//	var dialog = document.getElementById('DialogContainer');
//	if (dialog)
//	{
//		var viewSize   = getViewportSize();
//		var scrollSize = getScrollOffset();
//		dialog.style.display = 'block';
//		
//		// Center window on screen
//		dialog.style.left = (scrollSize.x + (viewSize.width  - dialog.clientWidth)  / 2) + 'px';
//				
//		var marginTop = dialog.clientHeight>viewSize.height ? ((viewSize.height-dialog.clientHeight)/2):100;
//        var top = (scrollSize.y + ((viewSize.height - dialog.clientHeight) / 2) - marginTop);
//		if( top>scrollSize.y)
//		    dialog.style.top  = top  + 'px';    
//		 else
//		    dialog.style.top  = scrollSize.y  + 'px';    		 		
//	}
//}



// ---------------
// Creates a semi-transparent div and places it over the entire screen.
// ---------------
//function showDialogBackground()
//{
//	var bgDiv = document.getElementById('DialogBG');
//	if (bgDiv)
//	{
//	    var height = document.getElementById('main').clientHeight + 40;
//		height = (height >= document.documentElement.clientHeight)? height: document.documentElement.clientHeight;
//		bgDiv.style.height	= height + 'px';
//		bgDiv.style.display	= 'block';

//		// IE6 and below needs a special class
//		if ((BrowserDetect.browser == 'Explorer') && (BrowserDetect.version < 7))
//		{
//			bgDiv.className = 'DialogBGIE6';
//		}	
//	}
//}
function showDialogBackground() {
    var bgDiv = document.getElementById('DialogBG');
    if (bgDiv) {
        bgDiv.style.height = '0px';
        var page = $(document.getElementById('page') ? document.getElementById('page') : document.getElementById('pageHemnet'));
        var height = $(document).height();
        // height = (height >= document.documentElement.clientHeight) ? height : document.documentElement.clientHeight;
        bgDiv.style.height = ($(document).height() + 100) + 'px';
        bgDiv.style.width = $(document).width() + 'px';
        bgDiv.style.display = 'block';

        // IE needs a special class
        if ((BrowserDetect.browser == 'Explorer')) {
            bgDiv.className = 'DialogBGIE6';
        }
    }
}

function pageHeight() 
{
    return window.innerHeight != null? 
        window.innerHeight : document.documentElement && document.documentElement.clientHeight?  
            document.documentElement.clientHeight : document.body != null? 
                document.body.clientHeight : null;
} 

// ---------------
// Hides the semi-transparent div
// ---------------
function hideDialogBackground()
{
	var bgDiv = document.getElementById('DialogBG');
	if (bgDiv)
	{
		bgDiv.style.display = 'none';
	}
}



//// ---------------
//// Creates a semi-transparent div and places it over the entire screen.
//// ---------------
//function showDialogBackground()
//{
//	var bgDiv = document.getElementById('DialogBG');
//	var dialogContainer = document.getElementById('DialogContainer');
//	if (bgDiv)
//	{
//		var viewSize   = getViewportSize();

//	    var height = 0; // bgDiv.clientHeight;
//	    if(height < document.getElementById('container').clientHeight) height = document.getElementById('container').clientHeight;
//	    
//	    if( height < viewSize.height ) height = viewSize.height;
//	    
//	    if(height < dialogContainer.clientHeight)
//    	    height = dialogContainer.clientHeight;
//	    
//	    
//	    height = height + scrollOffset.y;
//	    
//		bgDiv.style.height	= height + 'px';
//		bgDiv.style.display	= 'block';

//	}
//}

//// ---------------
//// Hides the semi-transparent div
//// ---------------
//function hideDialogBackground()
//{
//	var bgDiv = document.getElementById('DialogBG');
//	if (bgDiv)
//	{
//		bgDiv.style.display = 'none';
//	}
//}



//// ---------------
//// Positions the dialog layer in the center of the screen.
//// ---------------
//function positionDialog()
//{
//	var dialog = document.getElementById('DialogContainer');
//	if (dialog)
//	{
//		var viewSize   = getViewportSize();
//		var scrollSize = getScrollOffset();
//		dialog.style.display = 'block';
//		
//		// Center window on screen
//		dialog.style.left = (scrollSize.x + (viewSize.width  - dialog.clientWidth)  / 2) + 'px';
//				
//		var marginTop = dialog.clientHeight>viewSize.height ? ((viewSize.height-dialog.clientHeight)/2):100;
//        var top = (scrollSize.y + ((viewSize.height - dialog.clientHeight) / 2) - marginTop);
//		if( top>scrollSize.y)
//		    dialog.style.top  = top  + 'px';    
//		 else
//		    dialog.style.top  = scrollSize.y  + 'px';    		 		
//	}
//}


//// ---------------
//// Closes the dialog-layer.
//// ---------------
//function closeDialog()
//{
//	var dialog		= document.getElementById('DialogContainer');
//	var dialogBody  = document.getElementById('DialogBody');
//	if (dialog)
//	{
//		dialogOrig.innerHTML = dialogBody.innerHTML;

//		dialog.style.display = 'none';
//				
//		hideDialogBackground();
//		showWindowedControls();
//	}
//}



// 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);
//                    }
