/*
________________________________________
¤¤¤ Common: Template byBrick ¤¤¤
ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ
*/

/*
 * runOnLoad.js: portable registration for onload event handlers.
 * 
 * This module defines a single runOnLoad() function for portably registering
 * functions that can be safely invoked only when the document is fully loaded
 * and the DOM is available.
 *
 * Functions registered with runOnLoad() will not be passed any arguments when
 * invoked. They will not be invoked as a method of any meaningful object, and
 * the this keyword should not be used.  Functions registered with runOnLoad()
 * will be invoked in the order in which they were registered.  There is no
 * way to deregister a function once it has been passed to runOnLoad().
 *
 * In old browsers that do not support addEventListener() or attachEvent(),
 * this function relies on the DOM Level 0 window.onload property and will not
 * work correctly when used in documents that set the onload attribute
 * of their <body> or <frameset> tags.
 */
function runOnLoad(f) {
    if (runOnLoad.loaded) f();    // If already loaded, just invoke f() now.
    else runOnLoad.funcs.push(f); // Otherwise, store it for later
}

runOnLoad.funcs = []; // The array of functions to call when the document loads
runOnLoad.loaded = false; // The functions have not been run yet.

// Run all registered functions in the order in which they were registered.
// It is safe to call runOnLoad.run() more than once: invocations after the
// first do nothing. It is safe for an initialization function to call
// runOnLoad() to register another function.
runOnLoad.run = function() {
    if (runOnLoad.loaded) return;  // If we've already run, do nothing

    for(var i = 0; i < runOnLoad.funcs.length; i++) {
        try { runOnLoad.funcs[i](); }
        catch(e) { /* An exception in one function shouldn't stop the rest */ }
    }
    
    runOnLoad.loaded = true; // Remember that we've already run once.
    delete runOnLoad.funcs;  // But don't remember the functions themselves.
    delete runOnLoad.run;    // And forget about this function too!
};

// Register runOnLoad.run() as the onload event handler for the window
if (window.addEventListener)
    window.addEventListener("load", runOnLoad.run, false);
else if (window.attachEvent) window.attachEvent("onload", runOnLoad.run);
else window.onload = runOnLoad.run;




runOnLoad(function(){ //fix for FireFox; when <img-tags doesn't have width and height set the "$(document).ready(function(){" will run before the images are loaded
	
	/*if($("div.???").size() > 0){ 
		setTimeout('fixHtml()', 1000);
	}
	else {*/
		fixHtml();
	//}
	
});	


function fixHtml() {
	$(this).fixBoxAlert();
	$(this).fixF2kLists();
	$(this).addImageStyles();
	$(this).fixPuffBoxes();
	$(this).fixSubNav();	//always last
}

//fix puff boxes
jQuery.fn.fixPuffBoxes = function()
{

	var boxCss = "table.boxPuff2Green"
	if($(boxCss+":visible").size() > 0){
		var iSeparatorCellWidth = -1;
		$(boxCss+":visible").each(function(){
			var iWidth = $(this).width()-0;	//fix -4px
			var iNrOfCells = $(this).children("tbody").children("tr").children("td").size();
			if(iNrOfCells > 1){
				
				var iCnt = 0;
				var iCellHorizontalPadding = -1;
				
				//add separator cells
				var aCellWidth = new Array();
				var i=0;
				var bHardcodedCellWidthExist = false;
				var iTotHarcodedWidth = 0;
				var iNrOfHardcodedWidthCells = 0;
				$(this).children("tbody").children("tr").children("td").each(function(){
					aCellWidth[i] = -1;
					if($(this).attr("width")){
						if($(this).attr("width").indexOf("%") > -1) {
							//% not supported
						}
						else {
							aCellWidth[i] = $(this).attr("width");
							iTotHarcodedWidth += parseInt(aCellWidth[i]);
							bHardcodedCellWidthExist = true;
							iNrOfHardcodedWidthCells++;
						}
					}
					i++;
					iCnt++;
					if(iCnt < iNrOfCells){
						$(this).after('<td class="separator">&nbsp;</td>');
					}
					else {
						iCellHorizontalPadding = parseInt($(this).css("padding-left").replace("px", "")) + parseInt($(this).css("padding-right").replace("px", ""));
					}
				});
				
				var iNrOfSeparatorCells = iNrOfCells-1;
				if(iSeparatorCellWidth < 0){
					iSeparatorCellWidth = $(boxCss+":visible td.separator").width();
				}
				var iCellWidth = parseInt((iWidth - (iNrOfCells*iCellHorizontalPadding) - (iNrOfSeparatorCells*iSeparatorCellWidth))/iNrOfCells);
				iCnt=0;
				
				var iCellWidthNotHardcode = 0;
				if(bHardcodedCellWidthExist){
					var iTotDynamicCellWidth = parseInt(iWidth - iTotHarcodedWidth - (iCellHorizontalPadding*(iNrOfCells-iNrOfHardcodedWidthCells)) - (iNrOfSeparatorCells*iSeparatorCellWidth));
					iDynamicCellWidth =  parseInt(iTotDynamicCellWidth/(iNrOfCells-iNrOfHardcodedWidthCells));
					for(i=0; i < aCellWidth.length; i++){
						if(aCellWidth[i] < 0){
							aCellWidth[i] = iDynamicCellWidth;
						}
						else {
							aCellWidth[i] -= iCellHorizontalPadding;
						}
					}
				}
				
				var iTempWidth = 0;
				i=0;
				$(this).children("tbody").children("tr").children("td").each(function(){	//"td:not(.separator)"
					if($(this).attr("class") == "separator"){
						iTempWidth += $(this).width();
					}
					else {
						//fix last cell if width has been rounded incorrect
						if(i == aCellWidth.length-1){
							iCellWidth = iWidth - iTempWidth - iCellHorizontalPadding
							aCellWidth[i] = iCellWidth;
						}
					
						if(bHardcodedCellWidthExist && iDynamicCellWidth > 0){
							$(this).width(aCellWidth[i]);
						}
						else{
							$(this).width(iCellWidth);
						}
						i++;
						iTempWidth += $(this).width()+iCellHorizontalPadding;
					}
				});
				
				/*
				var msg = "";
				$(this).children("tbody").children("tr").children("td").each(function(){
					msg += $(this).width() + parseInt($(this).css("padding-left").replace("px", "")) + parseInt($(this).css("padding-right").replace("px", "")) + "\n";
				});
				alert(msg);
				*/
			}
		});
	}

}

//fix sub menu height - for background to reach the footer
jQuery.fn.fixSubNav = function()
{
	if($("#menu_vert").size() > 0) {
		if($("#mainbottom").offset() && $("#menu_vert").offset()){
			if($("#menyVertBottom").offset()){
				$("#menu_vert").css("height", $("#menyVertBottom").offset().top - $("#menu_vert").offset().top + 15);	//reset menu height
			}
			$("#menu_vert").css("height", $("#mainbottom").offset().top - $("#menu_vert").offset().top);
		}
	}
}


//fix alert box width
jQuery.fn.fixBoxAlert = function()
{
	if($(".boxAlert").size() > 0){
		
		$(".boxAlert").each(function(){
			var iBoxWidth = $(this).width();
			var iTextContinerWidth = $(this).children(".icon").children("div").width();
			if(iTextContinerWidth) {
				var iHorizontalPadding = (iBoxWidth - iTextContinerWidth)/2;
				$(this).css("padding-left", iHorizontalPadding + "px");
				//$(this).css("padding-right", iHorizontalPadding + "px");
			}
		});
	}
}

//fix lists in fastportal-pages
jQuery.fn.fixF2kLists = function()
{
	
	
	
	//change class for the cell that holds the separator class
	$(".tdListHeader span.separator").each(function(){
		$(this).parent().attr("class", "tdListHeaderSeparator");
		$(this).hide();
	}); 
	$(".tdListRow span.separator").each(function(){
		$(this).parent().attr("class", "tdListRowSeparator");
		$(this).hide();
	});
	
	
	//add header width separator
	var iSeparatorCellNo = 0;
	var i = 0;
	$("#AD_SEARCH_AND_FIND_LIST th").each(function(){
		i++;
		if($(this).children(".separator").size() > 0){
			iSeparatorCellNo = i;
		}
	});
	if(iSeparatorCellNo > 0){
		var lblFind = $("#AD_SEARCH_AND_FIND_LIST #AD_SEARCH_LABEL").attr("text");
		var lblSearch = $("#AD_SEARCH_AND_FIND_LIST #AD_FIND_LABEL").attr("text");
		var html = "";
		html += '<thead>';
		html += '<tr>';
		html +=  '<th class="tdListHeaderTitle" colspan="' + (iSeparatorCellNo-1) + '">' + lblFind + '</th>';
		html +=  '<th class="tdListHeaderSeparator"></th>';
		html +=  '<th></th>';
		html +=  '<th class="tdListHeaderTitle" colspan="' + (i-iSeparatorCellNo-1) + '">' + lblSearch + '</th>';
		html += '</tr>';
		html += '</thead>';
		$("#AD_SEARCH_AND_FIND_LIST thead").before(html);
	}
}


//iframe height fix
jQuery.fn.fixIfameHeight = function()
{
  //IE fix
  /*if($("#mdMainContent").size() > 0){
    $("#mdMainContent").width(($("#mdMainContent").width()-2)+'px');
  } 
  if($(".mdMainContent").size() > 0){
    var mainWidth = $(".mdMainContent").width()-2;
    $(".mdMainContent").width(mainWidth+'px');
  } */
 
  
	// Safari is no good.
	if (jQuery.browser.safari) //jQuery.browser.msie)
	{
		// Set specific variable to represent all iframe tags.
		var iFrames = document.getElementsByTagName('iframe');
		
		// Resize heights.
		function iResize()
		{
			for (var i = 0; i < iFrames.length; i++)
			{
				iFrames[i].style.height = iFrames[i].contentWindow.document.body.offsetHeight + 'px';
				
				//alert(iFrames[i].style.height)
			}
		}

		// Start timer when loaded.
		$('iframe').load(function()
			{
				setTimeout(iResize, 0);
			}
		);

		// For Safari to realize iframes loaded.
		for (var i = 0; i < iFrames.length; i++)
		{
			var iSource = iFrames[i].src;
			iFrames[i].src = '';
			iFrames[i].src = iSource;
		}
		$(this).fixSubNav();
	}
	else
	{
		// For other good browsers.
		$('iframe').load(function()
			{
				this.style.height = (this.contentWindow.document.body.offsetHeight) + 'px';
				$(this).fixSubNav();
			}
		);
	}
}

//fix pictures (rounded and borded layer)
jQuery.fn.addImageStyles = function()
{
	
	if($("img.pic1, img.pic, img.img").size() > 0) {
	
		$("img.pic1,img.pic, img.img").each(function(){
			//change class if image is aligned
			if($(this).attr("align")) {
				var align = $(this).attr("align");
				switch(align.toLowerCase()) {
					case "right":
						$(this).attr("class", $(this).attr("class") + "AlignRight");
						break;
					case "left":
						$(this).attr("class", $(this).attr("class") + "AlignLeft");
						break;
				}
			}
		});
	}
	
  if($("img.pic2").size() > 0) {
    
    $("img.pic2").each(function(){
      var iBorderSize = 15;
      var iImgWidth = $(this).width();
      var iImgHeight = $(this).height();
      var iImgPaddingHorizontal = parseInt($(this).css("padding-left").replace("px", "")) + parseInt($(this).css("padding-right").replace("px", ""));
      var iImgPaddingVertical = parseInt($(this).css("padding-top").replace("px", "")) + parseInt($(this).css("padding-bottom").replace("px", ""));
      var iMiddleHeight = iImgHeight - (iBorderSize * 2);
      var iImgTotWidth = iImgWidth + iImgPaddingHorizontal;
			
      //alert("pic size (WxH): " + imgWidth + " x " + imgHeight)
			
			
			var cssPadding = "";
			var cssPaddingLeft = 0;
			var cssMargin = "";
			var cssMarginLeft = 0;
			if($(this).attr("align")) {
				var align = $(this).attr("align");
				var iContentWidth = $(this).parent().width();
				var iContentPaddingHorizontal = parseInt($(this).parent().css("padding-left").replace("px", "")) + parseInt($(this).parent().css("padding-right").replace("px", ""));
				iContentWidth -= iContentPaddingHorizontal;
				switch(align.toLowerCase()) {
					case "right":
						//$(this).removeAttr("align")
						cssPaddingLeft = iContentWidth - iImgTotWidth;
						
						//change class if image is aligned
						$(this).attr("class", $(this).attr("class") + "AlignRight");
						break;
					case "left":
						//change class if image is aligned
						$(this).attr("class", $(this).attr("class") + "AlignLeft");
						
						if(jQuery.browser.msie){
							var iImgMarginRight = parseInt($(this).css("margin-right").replace("px", ""));
							cssMarginLeft = (-1)*(iImgTotWidth+iImgMarginRight);
						}
						
				}
				
				if(cssPaddingLeft != 0){
					cssPadding = "padding-left:" + cssPaddingLeft + "px;";
				}
				if(cssMarginLeft != 0){
					cssMargin = "margin-left:" + cssMarginLeft + "px;";
				}
			}
      
      var html = "";
      html += '<div style="clear"></div>';
      html += '<div class="pic_fancy" style="width:' + iImgTotWidth + 'px; ' + cssPadding + cssMargin + '">'; //height:' + (iImgHeight + iImgPaddingVertical) + 'px;
      html +=  '<div class="pic_top">';
      html +=   '<div class="pic_top_left"></div>';
      html +=   '<div class="pic_top_right"></div>';
      html +=  '</div>';
      html +=  '<div class="pic_middle" style="height:' + (iMiddleHeight + iImgPaddingVertical) + 'px;">';
      html +=   '<div class="pic_middle_left"></div>';
      html +=   '<div class="pic_middle_right"></div>';
      html +=  '</div>';
      html +=  '<div class="pic_bottom">';
      html +=   '<div class="pic_bottom_left"></div>';
      html +=   '<div class="pic_bottom_right"></div>';
      html +=  '</div>';
      html += '</div>';
      
      $(this).before(html);
      
    });
  }
}



$(document).ready(function(){
	//style fix
	if(jQuery.browser.safari) {
		$("#content").attr("class", $("#content").attr("class") + " safari");
		$("#page").attr("class", $("#page").attr("class") + " safari");
		$("#startpage").attr("class", $("#startpage").attr("class") + " safari");
		$("table.search .btn_default").parent("td").width("35px");
	}
	/*
	$.browser.browser.addSelectors("#content");
	$.browser.addSelectors("#page");
	$.browser.addSelectors("#startpage");
	*/
	
	$(".faq a").click(function(){
		$(".faq .faq_answer").hide();
		$(this).next().show();
		$(this).fixSubNav();
	});
	
	//add input button image end
	$("input.btn_default, input.fbsubmit, input.fbsubmit_prev, input.fbsubmit_next").after('<div class="btn_default_end"></div><div class="clear"></div>');
	$("input.btn_small").after('<div class="btn_small_end"></div><div class="clear"></div>');
	//add width to input button container to make sure that the button image end doesn't wrap
	if($("#startpage input.btn_default").parent(".btnContainer").size() < 1){	//not on startpage
		$("input.btn_default").parent(".btnContainer").each(function(){
			var iBtnWidth = $(this).width();
			if($(this).children(".btn_default_end").size() > 0){
				iBtnWidth += $(this).children(".btn_default_end").width();
				$(this).css("width",  iBtnWidth + 'px');
			}
		});
	}
	
	
	//create returnlink
	if(document.location.href.indexOf("addreturnlink") > -1){
		if($(".mainouterhtml").size() > 0) {
			var url = GetUrlParam("cmsreturnurl");
			if(url.length > 0){
				$(".mainouterhtml:last").after('<br/><a href="' + url + '">&laquo; Tillbaka</a>');
			}
		}
	}
	
	
	//fix iframe height
	if($("#cmsContentIframe").size() > 0){
		$(this).fixIfameHeight();
	}
	
	
	//if forced template is set; add forcedTempladeId to all links that doesnt have target set
	var strFindParam = "forcedTemplateId";
	var strForcedTemplateId = GetUrlParam(strFindParam);
	if(strForcedTemplateId.length > 0){
		var extraParams = strFindParam + "=" + strForcedTemplateId;

		//add forcedTempladeId to all links that doesnt have target set
		$("a").each(function(){
			var bTargetExist = false;
			var strTarget = ""; 
			if($(this).attr("target")){
				strTarget = $(this).attr("target");
				if(strTarget.length > 0){
					bTargetExist = true;
				}
			}
			
			if(bTargetExist){
				switch(strTarget){
					case "_blank":
						break;
				}
			}
			else {
				if($(this).attr("href")){
					var url = $(this).attr("href");
					if(url.indexOf("index.php") > -1){
						if(url.indexOf("index.php?") > -1){
							url += "&" + extraParams;
						}
						else {
							url += "?" + extraParams;
						}
						$(this).attr("href", url);
					}
				}
			}
		});
	}
	
});


//tab handler (with corresponding tab containers)
$(document).ready(function(){
		
	var DEFAULT_TAB = 0;	//0 based
	
	$(".tabContainer").hide();	//hide all; done by css
	$(".tabContainer:eq("+DEFAULT_TAB+")").show();
	activateTab($(".tabmenu a:eq(" + DEFAULT_TAB + ")").attr("id"));
	
	$(".tabmenu a").click(function(){
		
		var tabId = $(this).attr("id");
		activateTab(tabId);
		var currentIndex = $(".tabmenu a").index(this);
		$(".tabContainer").hide();	//hide all

		var contentId = $(".tabContainer:eq("+ currentIndex +")").attr("id");
		$("#"+contentId).show();
		
		
		if($("iframe").size() > 0){
			//iframe exist
			
			if($("#"+contentId + " #f2kContentIframe").size() > 0){
				LoadIFrame($("#"+contentId + " #f2kContentIframe"), $("#"+contentId + " #f2kContentIframe").attr("url"));
			}
			else{
				LoadIFrame(null, ""); //empty all iframes
			}
		}
		
	});
	
	
	function activateTab(tabId){
		$(".tabmenu a").removeAttr("class");
		$("#" + tabId).attr("class", "active");
	}
	
	var strPrevLoadedUrl = "";
	function LoadIFrame(objIframe, url){
		
		var bLoadIframe = false;
		if(objIframe){
			if(url.lenght > 0){
				bLoadIframe = true;
			}
		}
		if(bLoadIframe) {
			$(objIframe).attr("src", url);
		}
		else {
			if(strPrevLoadedUrl != url){
				$("iframe").attr("src", url);
			}
		}
		$(this).fixIfameHeight();
		strPrevLoadedUrl = url;
		
	}
});
//END: tab handler



function FixStartPuff(){
	var FIX_LOGIN_PUFF = false;
	var FIX_LOGIN_PUFF_HEADER = false;
	var msg = '';
	var iHeight, iExtraHeight;
	var iB2BHeight = 0;	//table border to border height
	var iB2BHeightHeader = 0;	//border to border header height
	
	//box fancy
	var iBoxTopSpan = 5;	//the top shadow height
	var iBoxBottomSpan = 5;	//the bottom shadow height
	var iBoxHeightHeader = 0;
	var iBoxHeightFooter = 0;
	var iBoxB2BHeightFooter = 0;
	var iBoxHeight = 0;
	var iBoxVerticalPaddingHeader = parseInt($("table.boxFancy td.boxTop").css("padding-top").replace("px", "")) + parseInt($("table.boxFancy td.boxTop").css("padding-bottom").replace("px", ""));
	var iBoxVerticalPaddingFooter = parseInt($("table.boxFancy td.boxBottom").css("padding-top").replace("px", "")) + parseInt($("table.boxFancy td.boxBottom").css("padding-bottom").replace("px", ""));
	var iBoxVerticalPaddingMain = parseInt($("table.boxFancy td.boxMain").css("padding-top").replace("px", "")) + parseInt($("table.boxFancy td.boxMain").css("padding-bottom").replace("px", ""));
	if($("table.boxFancy").size() > 0){
		//header height
		iBoxHeightHeader = $("table.boxFancy").children("tbody").children("tr").children("td.boxTop").height() + iBoxVerticalPaddingHeader;
		iB2BHeightHeader = iBoxHeightHeader - iBoxTopSpan;
		
		//footer height
		iBoxHeightFooter = $("table.boxFancy").children("tbody").children("tr").children("td.boxBottom").height() + iBoxVerticalPaddingFooter;
		iBoxB2BHeightFooter = iBoxHeightFooter - iBoxBottomSpan;
		
		//table height
		iBoxHeight = $("table.boxFancy").height();
		iB2BHeight = iBoxHeight - iBoxTopSpan - iBoxBottomSpan;
	}
	msg += 'BOX:\nHeader: ' + iBoxHeightHeader + '\nFooter Height: ' + iBoxHeightFooter + '\nTot Height: ' + iBoxHeight + '\n';
	msg += 'Border To Border Header Height: ' + iB2BHeightHeader + '\n';
	msg += 'Border To Border Height: ' + iB2BHeight + '\n';


	//puff
	var iPuffBottomSpan = 6;	//the bottom shadow height
	var iPuffHeightHeader = 0;
	var iPuffHeightFooter = 0;
	var iPuffB2BHeightFooter = 0;
	var iPuffHeightMain = 0;
	var iPuffHeight = 0;
	var iPuffVerticalPaddingHeader = parseInt($("table.boxPuffGrey td.boxPuffTop").css("padding-top").replace("px", "")) + parseInt($("table.boxPuffGrey td.boxPuffTop").css("padding-bottom").replace("px", ""));
	iPuffVerticalPaddingHeader += 2;	//addition for top- and bottom border
	
	var iPuffVerticalPaddingMain = parseInt($("table.boxPuffGrey td.boxPuffMain").css("padding-top").replace("px", "")) + parseInt($("table.boxPuffGrey td.boxPuffMain").css("padding-bottom").replace("px", ""));
	if($("table.boxPuffGrey").size() > 0){
	
		$("table.boxPuffGrey").each(function(){
			iHeight = $(this).children("tbody").children("tr").children("td.boxPuffTop").height() + iPuffVerticalPaddingHeader;
			if(iHeight > iPuffHeightHeader){ iPuffHeightHeader = iHeight; }
			if(iHeight > iB2BHeightHeader){ iB2BHeightHeader = iHeight; }
			
			iHeight = $(this).children("tbody").children("tr").children("td.boxPuffMain").height() + iPuffVerticalPaddingMain;
			if(iHeight > iPuffHeightMain){ iPuffHeightMain = iHeight; }
			
			iHeight = $(this).children("tbody").children("tr").children("td.boxPuffBottom").height();
			if(iHeight > iPuffHeightFooter){ iPuffHeightFooter = iHeight; }
			iPuffB2BHeightFooter = iPuffHeightFooter - iPuffBottomSpan;
		});
		
		iHeight = iB2BHeightHeader + iPuffHeightMain + iPuffHeightFooter;	
		if(iHeight > iPuffHeight){ iPuffHeight = iHeight; }
		if((iHeight - iPuffBottomSpan) > iB2BHeight){ iB2BHeight = iHeight - iPuffBottomSpan; }
	}
	iBoxHeight = iB2BHeight + iBoxTopSpan + iBoxBottomSpan;
	msg += '\nPUFF:\nHeader: ' + iPuffHeightHeader + '\nFooter Height: ' + iPuffHeightFooter + '\nTot Height: ' + iPuffHeight + '\n';
	msg += '\nMax Border To Border Header Height: ' + iB2BHeightHeader + '\n';
	msg += 'Max Border To Border Height: ' + iB2BHeight;

	//set header heights	
	if(iB2BHeightHeader > 0){
		//puff
		iHeight = iB2BHeightHeader;
		if(jQuery.browser.msie){ iHeight -= iPuffVerticalPaddingHeader; }
		$("table.boxPuffGrey td.boxPuffTop").css("height", iHeight + "px");
		
		//box
		iHeight = iB2BHeightHeader + iBoxTopSpan;
		if(jQuery.browser.msie){ iHeight -= iBoxVerticalPaddingHeader; }
		if(FIX_LOGIN_PUFF || FIX_LOGIN_PUFF_HEADER) {
			$("table.boxFancy td.boxTop").css("height", iHeight + "px");
		}
	}
	
	//set main heights	
	if(iB2BHeight > 0){
		//puff
		iHeight = iB2BHeight - iB2BHeightHeader - iPuffB2BHeightFooter; 
		if(jQuery.browser.msie){ iHeight -= iPuffVerticalPaddingMain; }
		$("table.boxPuffGrey td.boxPuffMain").css("height", iHeight + "px");
		
		//box
		iHeight = iB2BHeight - iB2BHeightHeader - iBoxB2BHeightFooter;
		if(jQuery.browser.msie){ iHeight -= iBoxVerticalPaddingMain; }
		if(FIX_LOGIN_PUFF) {
			$("table.boxFancy td.boxMain").css("height", iHeight + "px");
		}
	}
	

	//alert(msg);
}


function closeMsg(objMsg){
  if(objMsg){ objMsg.style.display = 'none'; }
}

var newWin;
var previousURL;
var previousWinName
function OpenWindow(URL,Width,Height,scrollbars,winname,resizable){
	if(scrollbars == "yes" || scrollbars == "1"){
	  scrollbars = "yes"
  }
  else{
	  scrollbars = "no"
  }
  if(resizable == "yes" || resizable == "1"){
	  resizable = "yes"
  }
  else{
	  resizable = "no"
  }
  
  if (!newWin || newWin.closed || winname != previousWinName){
		var winParams = 'toolbar=0,scrollbars='+scrollbars+',status=no,resizable='+resizable;
		if(Width.length > 0 && !isNaN(Width) && Height.length > 0 && !isNaN(Height)){
			if(parseInt(Width) > 0 && parseInt(Height) > 0){
				winParams += ',width='+Width+',height='+Height;
			}
		}
		newWin = window.open(URL,winname,winParams);
			
		previousURL = URL;
		if (!newWin.opener){
			newWin.opener = window;
		}
	}
	else if(URL != previousURL){
		newWin.location.href = URL;
		newWin.focus();
		previousURL = URL;
	}
	else {
		newWin.focus();
	}
	
	previousWinName = winname
}


function getForm(){
	for(var i=0; i < document.forms.length; i++){
		if(document.forms[i]){
			if(document.forms[i].name.indexOf('_id') > -1){
				return document.forms[i];
			}
		}
	}
	
	return document.forms[0];
}

function getFirstChildByName(obj, childName){
	var objChildHierarchy = obj;
	for(var i=0; i < 10; i++){ //max depth 10
		objChildHierarchy = objChildHierarchy.firstChild;
		if(objChildHierarchy){
			if(objChildHierarchy.name && objChildHierarchy.name.indexOf(childName) > -1){
				return objChildHierarchy;
			}
		}
		else {
			break;
		}
	}
	
	return null;
}





function showAltTextF2K(objName, txt, bToggleHideShowSelecteBoxes){
	var objAltMsg = document.getElementById(objName);
	if(objAltMsg){
		objAltMsg.innerHTML = txt;
		objAltMsg.style.width = '200px';
		if(txt.length > 100){
			objAltMsg.style.width = '400px';
		}
		objAltMsg.style.display = "block";
		if(bToggleHideShowSelecteBoxes){
			toggleHideShowAllSelectBoxesF2K(false);
		}
	}
}


function hideAltTextF2K(objName){
	var objAltMsg = document.getElementById(objName);
	if(objAltMsg){
		objAltMsg.style.display = "none";
		toggleHideShowAllSelectBoxesF2K(true);
	}
}

function toggleHideShowAllSelectBoxesF2K(bShow){
	var appName = navigator.appName;
	if(appName.indexOf("Microsoft") > -1){
		var i = 0;
	  var selectObjects = document.getElementsByTagName("select")
	  if(selectObjects){
	  	for(var i=0; i < selectObjects.length; i++){
	  		if(selectObjects[i].type == "select-one"){
					if(bShow){  			
		  			selectObjects[i].style.visibility = "visible";
		  		}
		  		else {
		  			selectObjects[i].style.visibility = "hidden";
		  		}
	  		}
	  	}
	  }
  }
}

function showAltTextForceWidth(objName, txt, boxWidth){
	var objAltMsg = document.getElementById(objName);
	if(objAltMsg){
		if (txt.length > 0) {
			objAltMsg.innerHTML = txt;
		}
		objAltMsg.style.width = boxWidth.toString() + 'px';
		objAltMsg.style.display = "block";
	}
}

var strMaxWidth = 0;
function showAltText(objName, txt){
	var objAltMsg = document.getElementById(objName);
	if(objAltMsg){
		if (txt.length > 0) {
			objAltMsg.innerHTML = txt;
		}
		else {
		  txt = objAltMsg.innerHTML;
		}
		
		if(strMaxWidth.length > 0 && !isNaN(strMaxWidth)){
			objAltMsg.style.width = strMaxWidth + 'px';
		}
		else {
			objAltMsg.style.width = '200px';
			if(txt.length > 100){
				objAltMsg.style.width = '400px';
			}
		}
		objAltMsg.style.display = "block";
	}
}



function hideAltText(objName){
	var objAltMsg = document.getElementById(objName);
	if(objAltMsg){
		objAltMsg.style.display = "none";
	}
}

function listProperties(obj, strFind){
  var i = 0;
  var msg = "";
  for(itm in obj){
    if(i > 50){
      alert(msg);
      i = 0;
      msg = "";
    }	
		if(strFind.length > 0){
			if(itm.toLowerCase().indexOf(strFind.toLowerCase()) > -1){	//search for lower case part of item name
				msg += itm + "\n";	
				i++;
			}
		}
		else {
			msg += itm + "\n";	
			i++;
		}

  }
  alert(msg);
}


 var prevThemePic = -1;
 function ThemePic() {
	/* IMAGES IS LOADED ON defatul.htm */
	/*var aThemes = new Array(2);
            aThemes[0]= 'images/pic_page_start_theme1a.jpg';
            aThemes[1]= 'images/pic_page_start_theme1a.jpg';*/
    if(prevThemePic == -1){
		RndPic = Math.floor(Math.random()*aThemes.length);
	}
	else{
		RndPic = prevThemePic + 1;
		if(RndPic >= aThemes.length) { RndPic = 0;}
	}
	var objThemePicHolder = document.getElementById("themePicHolder")
	if (objThemePicHolder){
		objThemePicHolder.style.backgroundImage = "url(" + aThemes[RndPic] + ")";
	}
	prevThemePic = RndPic;
	
	if(iLoadNextPictureAfterSec > 0){
		LoadThemePic();
	}
}

function LoadThemePic() {
	setTimeout('ThemePic()', iLoadNextPictureAfterSec*1000);
}

function GetUrlParam( paramName )
{
	var oRegex = new RegExp( '[\?&]' + paramName + '=([^&]+)', 'i' ) ;
	var oMatch = oRegex.exec( window.location.search ) ;
	
	if ( oMatch && oMatch.length > 1 )
		return oMatch[1] ;
	else
		return '' ;
}

function unescapeXmlText(str){
	// ċ: unescape("%E5")
	// ä: unescape("%E4")
	// ö: unescape("%F6")
	// Ċ: unescape("%C5")
	// Ä: unescape("%C4")
	// Ö: unescape("%D6")
	
	str = str.replace(/&#229;/g, unescape("%E5"));
	str = str.replace(/&#228;/g, unescape("%E4"));
	str = str.replace(/&#246;/g, unescape("%F6"));
	str = str.replace(/&#197;/g, unescape("%C5"));
	str = str.replace(/&#196;/g, unescape("%C4"));
	str = str.replace(/&#214;/g, unescape("%D6"));
	return str;
}