/*
	General_it29.js 
	This file contains general javascript functions and should be included in every page.
*/

function SelectText(textbox) {
    if (textbox.createTextRange) {
        var oRange = textbox.createTextRange();
        oRange.moveStart("character", 0);
        oRange.moveEnd("character", textbox.value.length);
        oRange.select();
    }
    else if (textbox.setSelectionRange) {
        textbox.setSelectionRange(0, textbox.value.length);
    }
}

function bookmarksite(title, url,message){
	if (document.all)
		window.external.AddFavorite(url, title);
	else if (window.sidebar)
		alert(message);
}

function dosearch(url, obj)
{
  window.location = url;
}

function getElementsByClass(className) { 
     var arrResult = new Array(); 
     var arrTmp = new Array(); 
     arrTmp = document.getElementsByTagName("*"); 
     j=0;      
     for (i=0; i < arrTmp.length; i++) 
     { 
         if (arrTmp[i].className==className) 
         {           
           arrResult[j]=arrTmp[i]; 
						j++; 
         } 
      } 
     return arrResult; 
   } 

function NewWindow(theURL, winName, options)
{
	newwindow = window.open(theURL,winName,options);
	newwindow.opener = this;
	newwindow.focus();
}

function Vote(poll, option)
{
	var url = GetPollResultsURL()+'?poll='+ poll +'&option='+option;	
	showContentPopUp(url);
}

/*
	Toggles the visibility of list items in a list.
		- toggleItem: the <li> element which triggers the function
		- expandedText: the text of the toggleItem when the list is expanded
		- collapsedText: the text of the toggleItem when the list is collapsed
		- threshold: toggling will only happen for list items at a position above the threshold
*/
function ToggleListVisibility(listToToggle, threshold)
{
	var ListElement;
	var IsExpanded;
									
	// Get the containing <ul> element
	ListElement = listToToggle;
	// Get the current state
	IsExpanded = ListElement.className.indexOf(' exp') > -1;			
	// Add or remove "exp", depending on the current state
	ListElement.className = IsExpanded ? ListElement.className.replace(' exp','') : ListElement.className + ' exp';
				
	for (i=threshold;i <ListElement.children.length ;i++)
	{				
		if (ListElement.children[i].className.indexOf('expand') > -1)
		{
			ListElement.children[i].style.display = IsExpanded ? "block" : "none";
		}
		else if (ListElement.children[i].className.indexOf('collapse') > -1)
		{
			ListElement.children[i].style.display = IsExpanded ? "none" : "block";
		}		
		else ListElement.children[i].style.display = IsExpanded ? "none" : "block";
	}
}

/*
 show popup with content
*/
function showContentPopUp(theURL)
{
	width = 540;
	height = 460;
	winName="";
	options = 'toolbar=no,menubar=no,location=no,directories=no,status=no,scrollbars=yes,resizable=yes,copyhistory=no';
		
	options += ',width=' + width + ',height=' + height;
	if (window.screen)
	{
		windowLeft = (screen.availWidth - width)/2;
		windowTop = (screen.availHeight - height)/2;
		options += ',left=' + windowLeft + ',top=' + windowTop;
	}
	newwindow = window.open(theURL,winName,options);
	newwindow.opener = this;
	newwindow.focus();
}

function showContentPopUpExtended(theURL, width, height)
{
	winName = "";
	options = 'toolbar=no,menubar=no,location=no,directories=no,status=no,scrollbars=yes,resizable=yes,copyhistory=no';
		
	options += ',width=' + width + ',height=' + height;
	if (window.screen)
	{
		windowLeft = (screen.availWidth - width)/2;
		windowTop = (screen.availHeight - height)/2;
		options += ',left=' + windowLeft + ',top=' + windowTop;
	}
	newwindow = window.open(theURL,winName,options);
	newwindow.opener = this;
	newwindow.focus();
}

/*
Redirect to page 'url'
for ie it takes the base url and strips of the page.
then it adds the original url because ie ignores the basetag in javascript
(tested in IE en FF)
*/
function redirect(url)
{
  var path = '';
  
  if (url.indexOf('http://') == -1)
  {
    //ie other browsers dont have the document.all element
    if(document.all)
    {
      var elements;
      elements = document.getElementsByTagName('BASE');
      
      if (elements != null && elements.length > 0)
      {
        path = elements[0].href;
        path = path.substr(0,path.lastIndexOf('/')+1);
      }
    }
    path += url;
    window.location.href = path;
  }
  else
  {
    window.location.href = url;
  }
}

//Redirect urls that are root relative
function redirectRootRelative(url) {
    window.location.href = url;
}

/*
	This function adds the specified event handler to the body onload event.
*/
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

/*
    This function changes the parent element of the element with id
    sourceElementId to the element with id newParentElementId.
*/
function changeElementParent(sourceElementId, newParentElementId) {
	var source;
	var parent;

	source = document.getElementById(sourceElementId);
	parent = document.getElementById(newParentElementId);
	if ((source != null) && (parent != null)) {
		// document.documentElement.insertBefore(source, parent);
		parent.insertBefore(source, null);
		// the following lines ensure that youtube videos are not disapearing in FF when changeElementParent is called
		var videoPlayer = document.getElementById('frameVideoPlayer');
		if (videoPlayer != null)
			videoPlayer.src = videoPlayer.src;
	}
	else {
		if (source != null) {
			source.innerHTML = "";
		}
	}
}

/* 
	for IE only we have a problem with the banners when there are no supplier links except for the vertical ones 
	- this is the code that solves it 
*/
$(function() {
	if ($.browser.msie)
		setTimeout('VerticalSuppliersRefresh(0)', 200);
});

function VerticalSuppliersRefresh(counter) {
	var lVerticalSuppliers = $('.horizSupp');
	if (lVerticalSuppliers.length == 0)
		return;
		
	lVerticalSuppliers.html(lVerticalSuppliers.html());
	if (counter < 20)
		setTimeout('VerticalSuppliersRefresh(' + ++counter + ')', 100);
}

function openSendAFriendPopup(url)
{
	showContentPopUp(url);
}

function ShowFloatingDivContent(divId, ev)
{
	ChangeFloatingDivContent(divId,true,ev);
}

function HideFloatingDivContent(divId,ev)
{
	ChangeFloatingDivContent(divId,false,ev);
}

var MouseX = 0;
var MouseY = 0;
// required for IE
var ScrollX = 0;
var ScrollY = 0;

function getMouseXY(ev)
{
	var IE = document.all?true:false;
		
	if (IE) 
	{
		// grab the x-y pos.s if browser is IE
		MouseX = event.clientX;
		MouseY = event.clientY;
		// get scroll position
		if (document.documentElement)
		{
			ScrollX = document.documentElement.scrollLeft;
		} else {
			ScrollX = document.body.scrollLeft;
		}
		MouseX += ScrollX;
		if (document.documentElement)
		{
			ScrollY = document.documentElement.scrollTop;
		} else {
			ScrollY = document.body.scrollTop;
		}
		MouseY += ScrollY;
	}
	else 
	{
		// grab the x-y pos.s if browser is NS
		MouseX = ev.pageX;
		MouseY = ev.pageY;
	}  
}

var WindowWidth = 0;
var WindowHeight = 0;

function getWindowWH()
{
	var IE = document.all?true:false;
		
	if (IE)
	{
		// grab the w-h if browser is IE
		WindowWidth = document.documentElement ? document.documentElement.clientWidth : document.body.offsetWidth;
		WindowHeight = document.documentElement ? document.documentElement.clientHeight : document.body.offsetHeight;
	}
	else 
	{  
		// grab the w-h if browser is NS
		WindowWidth = window.innerWidth - 20;
		WindowHeight = window.innerHeight - 20;
	}
}

function ChangeFloatingDivContent(divId, enabled, ev)
{
	var lDiv = document.getElementById(divId);
	var IE = document.all?true:false;
	var offsetParentX = -7;
	var offsetParentY = -13;
	
	if (lDiv != null)
	{
		if (enabled)
		{
			lDiv.style.display = "block";
			getMouseXY(ev);
			getWindowWH();
			if (lDiv.offsetParent != null)
			{
				// On some pages the div containing the tooltip is not positioned according to the
				// top left corner of the document but according to the offsetParent of the tooltip div 
				// and we have to position the tooltip according to that offsetParent
				offsetParentX = lDiv.offsetParent.offsetLeft - 7;
				offsetParentY = lDiv.offsetParent.offsetTop - 13;
			}
			if (!IE)
			{
				if (WindowWidth - MouseX <= lDiv.offsetWidth)
				{
					lDiv.style.left = (MouseX - lDiv.offsetWidth - offsetParentX) + "px";
				} else {
					lDiv.style.left = (MouseX - offsetParentX) + "px";
				}
				if (WindowHeight - MouseY <= lDiv.offsetHeight)
				{
					lDiv.style.top = (MouseY - lDiv.offsetHeight - offsetParentY) + "px";
				} else {
					lDiv.style.top = (MouseY - offsetParentY) + "px";
				}
			} else {
				if (WindowWidth - (MouseX - ScrollX) <= lDiv.offsetWidth)
				{
					lDiv.style.left = (MouseX - lDiv.offsetWidth - offsetParentX) + "px";
				} else {
					lDiv.style.left = (MouseX - offsetParentX) + "px";
				}
				if (WindowHeight - (MouseY - ScrollY) <= lDiv.offsetHeight)
				{
					lDiv.style.top = (MouseY - lDiv.offsetHeight - offsetParentY) + "px";
				} else {
					lDiv.style.top = (MouseY - offsetParentY) + "px";
				}
			}
		}
		else
		{
			lDiv.style.display = "none";
		}
	}
}

function SetCssClassActive(id, enabled)
{
	var lControl = document.getElementById(id);	
	
	if (lControl != null)
	{
		if (enabled)
		{
			/* Only add ' active' to the classname if it isn't already in the className */
			if (lControl.className.indexOf(' active') <= -1)
			{
				lControl.className += ' active'
			}			
		}
		else
		{
			lControl.className = lControl.className.replace(/\s*active/, '');
		}
	}
}

function SetVisible (elementId, visible)
{
	var div = document.getElementById(elementId);
	if (div != null)
	{
		if (visible)
			div.style.display = "block";
		else
			div.style.display = "none";
	}
}

function SetVisibleInternationalHeader (elementId, visible) {
    var IE6 = false/*@cc_on || @_jscript_version < 5.7@*/;
	var div = document.getElementById(elementId);
	var iframe = document.getElementById("IFrameInternational");
	var browserName = navigator.appName; 
	var browserVer = parseInt(navigator.appVersion);
	var divHeaderContent = document.getElementById("headerContent"); 
	var left = divHeaderContent.offsetWidth + divHeaderContent.offsetLeft;
	var top = 34;

	if ((typeof (isMultiLanguage) != 'undefined') && (isMultiLanguage == 'True'))
	    top = 52;
	else { } /*if ((document.all) && (browserVer <= 4))
	    // For IE6 we need different top settings
	    top = 46;*/
	
	if (div != null)
	{
		if (visible)
		{
		    document.body.appendChild(iframe);
		    document.body.appendChild(div);

		    div.style.display = "block";
		    // for div.offsetWidth to work, the div has to be visible
		    left = left - div.offsetWidth;
		    div.style.left = left + "px";
		    div.style.top = top + "px";

		    // Iframe behind international websites dropdown (makes the dropdown go in front of 
		    // a flash banner)
		    iframe.style.display = "block";
		    iframe.style.left = left + "px";
		    iframe.style.top = (top + 7) + "px";
		    if (IE6) {
		        iframe.style.height = (div.offsetHeight - 14) + "px";
		    } else {
		        iframe.style.height = (div.offsetHeight - 7) + "px";
		    }
		}
		else
		{
			div.style.display = "none";
			iframe.style.display = "none";
		}			
	}
}

function SetVisibleDelay (elementId, visible)
{
	var div = document.getElementById(elementId);
	setTimeout("",3000);
	if (div != null)
	{
		if (visible)
			div.style.display = "block";
		else
			div.style.display = "none";
	}

}

function SetVisibleDelayInternationalHeader (elementId, visible)
{
	var div = document.getElementById(elementId);
	var iframe =document.getElementById("IFrameInternational");
	var browserName=navigator.appName; 
	var browserVer=parseInt(navigator.appVersion);
	
	setTimeout("",3000);
	if (div != null)
	{
		if (visible)
		{
			div.style.display = "block";
			iframe.style.display ="block";
		}
		else
		{
			div.style.display = "none";
			iframe.style.display="none";
		}
	}
}
function WebAdsClearCSS() 
{
    
    var headerBannerWebAds = document.getElementById("adscale:NjI0NDA");
    if (headerBannerWebAds != null)
    { headerBannerWebAds.style.margin = ""; }

    var leftBannerWebAds = document.getElementById("adscale: NWZiMDA");
    if (leftBannerWebAds != null)
    { leftBannerWebAds.style.margin = ""; }

    var weatherBannerWebAds = document.getElementById("adscale: NWZiNDA");
    if (weatherBannerWebAds != null)
    { weatherBannerWebAds.style.margin = ""; }
}

function base64Decode(data)
{
    data = data.replace(/[^a-z0-9\+\/=]/ig, '');
    if (typeof(atob) == 'function') return atob(data);
    var b64_map = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
    var byte1, byte2, byte3;
    var ch1, ch2, ch3, ch4;
    var result = new Array();
    var j=0;
    while ((data.length%4) != 0) {
        data += '=';
    }

    for (var i=0; i <data.length; i+=4) {
        ch1 = b64_map.indexOf(data.charAt(i));
        ch2 = b64_map.indexOf(data.charAt(i+1));
        ch3 = b64_map.indexOf(data.charAt(i+2));
        ch4 = b64_map.indexOf(data.charAt(i+3));

        byte1 = (ch1 << 2) | (ch2 >>4);
        byte2 = ((ch2 & 15) << 4) | (ch3 >> 2);
        byte3 = ((ch3 & 3) << 6) | ch4;

        if(true) result[j++] = String.fromCharCode(byte1);
        if (ch3 != 64) result[j++] = String.fromCharCode(byte2);
        if (ch4 != 64) result[j++] = String.fromCharCode(byte3);
    }

    return result.join('');
}

// the advertisements on the page are set the show visibility
function ShowFlashObjects(show) {
	if (!window.headerBannerID)
		return;

	// on all browsers
	// the advertisements currently available on the page			
	for (var i = 0; i < headerBannerID.length; i++) {
		var id1El = document.getElementById(headerBannerID[i]);
		if (id1El != null)
			id1El.style.visibility = show ? "visible" : "hidden";
	}
}

function ShowCountryFlashMap(show) {
	if (typeof (countryAreasFlash) != "undefined") {
		// countryAreasFlash - this is defined when the flash map is embedded
		var lCountryAreasFlash = document.getElementById(countryAreasFlash);
		if (lCountryAreasFlash != null) {
			lCountryAreasFlash.style.visibility = show ? "visible" : "hidden";
		}
	}
}

function ShowBanners(show) {
	if (typeof (headerBannerID) != "undefined") {
		if (headerBannerID) {
			for (i = 0; i < headerBannerID.length; i++) {
				var lBannerContainer = document.getElementById(headerBannerID[i]);
				if (lBannerContainer) {
					lBannerContainer.style.visibility = show ? "visible" : "hidden";
				}
			}
		}
	}
}

// Shows thew hidden refinements for a dimension
// Because the refinements are presented using <dl> and <dd> tags (which do not accept div elements inside on IE6 and IE7) an extra <dl> is used 
// for the hidden refinements. The dd elements inside it are than taken and placed in the parent <dl> of the "more ... " link
function ShowHiddenRefinements(hiddenRefinementsDlId, link) {
	var lDl = document.getElementById(hiddenRefinementsDlId);
	link.parentNode.style.display = "none";
	if (lDl) {
		link.parentNode.parentNode.innerHTML += $(lDl).children(":first").html();
	}
}

function ShowFrames(show) {
	var lFrames = document.getElementsByTagName("iframe");
	if (lFrames != null && lFrames != "undefined") {
		for (var i = 0; i < lFrames.length; i++) {
			var lOneFrame = lFrames[i];
			if (lOneFrame) {
				lOneFrame.style.visibility = show ? "visible" : "hidden";
			}
		}
	}
}
