function getPositionY (element) 
{ 
  var positionY = 0; 
  while (element != null) 
  { 
	positionY += element.offsetTop; 
	element = element.offsetParent; 
  } 
  return positionY; 
} 

function getPositionX (element) 
{ 
  var positionX = 0; 
  while (element != null) 
  { 
	positionX += element.offsetLeft; 
	element = element.offsetParent; 
  } 
  return positionX; 
}

function cargar_popup(width,height, bgcolor) 
{
	if	(LeerCookie('home_popup') == null)
	{
		clearInterval(intervalo_popup);
		intervalo_popup = setInterval("cerrar_popup();", t_popup);
		
		var options = {};
		options.bgcolor = bgcolor;
		options.zindex=999999998;
		if (bgcolor!= null)
			grayOut(true,options);
		initPopUp(width, height, bgcolor);

		//Creamos cookie
		crearCookieSeg('home_popup', 0, 1, 'dia');
	}
	else
	{
		document.getElementById('popup_banner_principal').style.display = 'none';
		visita = parseInt(LeerCookie('home_popup')) + 1;
		crearCookieSeg('home_popup', visita, 1, 'dia');
	}
}
		
function cerrar_popup()
{	
	if (document.getElementById('darkenScreenObject'))
		grayOut(false);			
	closePopUp("popup_banner_principal");
	clearInterval(intervalo_popup);
}

var isPopUpVisible = false;
var isIE6 = /msie|MSIE 6/.test(navigator.userAgent);
var isIE7 = /msie|MSIE 7/.test(navigator.userAgent);
var popupToKill;
var channelPrefix = "0";

function getPopupScrolledTop()
{
	scrolledTop = document.documentElement.scrollTop;	
	return scrolledTop;
}

function delayedClose()
{
	document.getElementById(popupToKill).parentNode.removeChild(document.getElementById(popupToKill))
}

function isFF2()
{
	if (/Firefox[\/\s](\d+\.\d+)/.test(navigator.userAgent)){ //test for Firefox/x.x or Firefox x.x (ignoring remaining digits);
		 var ffversion=new Number(RegExp.$1) // capture x.x portion and store as a number
		 if (ffversion>=3)
		  return false;
		 else if (ffversion>=2)
		  return true;
	}
	return false;
}

function calculatePageDimensions() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  var dimensions = {}
  dimensions.width = myWidth;
  dimensions.height = myHeight;
  return dimensions;  
}

function calculateVerticalCenter(width)
{	var dimensions = calculatePageDimensions();
	var pageWidth = dimensions.width;
	var center = pageWidth / 2 - width / 2;
	return center;
}

function calculateHorizontalCenter(height)
{	var dimensions = calculatePageDimensions();
	var pageHeight = dimensions.height;
	var center = pageHeight / 2 - height / 2;
	return center;
}

function ocultar_select() 
{ 
    var selects = document.getElementsByTagName('select');
    var inputs = document.getElementsByTagName('input');
	
	for(k=0;k<selects.length;k++)
	{
		var select = selects[k];
		if( (select.type== 'hidden') || !select.value) continue;
		if(isIE6)
			select.style.visibility='hidden';			
	}
}
																																																																	
function createPopUp(popUpCode)
{
	var div = document.createElement('div');
	div.innerHTML = popUpCode;
	document.body.appendChild(div.firstChild);
}
function closePopUp(popUpId)
{
	if (isIE6 || isIE7 ) 
	{	//IE is giving a null exception on calling this funtion from flash
		//it needs some time before deleting this div		
		document.getElementById(popUpId).style.display='none';
		popupToKill = popUpId;
		setTimeout("delayedClose()",150);		
	}
	else if(!isFF2())
	{
		document.getElementById(popUpId).parentNode.removeChild(document.getElementById(popUpId));
	}
	else
	{
		document.getElementById(popUpId).style.display='none';
		popupToKill = popUpId;
		setTimeout("delayedClose()",1500)
	}
	isPopUpVisible = false;
	if(isIE6)
	{
		var selects = document.getElementsByTagName('select');
	
		for(var k=0;k<selects.length;k++)
		{
			var select = selects[k];
			if( (select.type== 'hidden') || !select.value) continue;
			select.style.visibility='visible';
		}
	}
}

function initPopUp(width, height, bgcolor)
{	
	if(!isPopUpVisible)
	{
		isPopUpVisible = true;

		ocultar_select();
		
		var divWidth = width;
		var divHeight = height;
		var scrollOffset =  getPopupScrolledTop(divHeight);
		var divTop = calculateHorizontalCenter(divHeight) + scrollOffset +"px";

		divpopup_centrado = document.getElementById('popup_centrado');
		divpopup_centrado.style.top=divTop;
		divpopup_centrado.style.left=calculateVerticalCenter(divWidth) + 'px';
		divpopup_centrado.style.display='';
	}
}

function grayOut(vis, options) {
  // Pass true to gray out screen, false to ungray
  // options are optional.  This is a JSON object with the following (optional) properties
  // opacity:0-100         // Lower number = less grayout higher = more of a blackout 
  // zindex: #             // HTML elements with a higher zindex appear on top of the gray out
  // bgcolor: (#xxxxxx)    // Standard RGB Hex color code
  // grayOut(true, {'zindex':'50', 'bgcolor':'#0000FF', 'opacity':'70'});
  // Because options is JSON opacity/zindex/bgcolor are all optional and can appear
  // in any order.  Pass only the properties you need to set.
  var options = options || {}; 
  var zindex = options.zindex || 50;
  var opacity = options.opacity || 70;
  var opaque = (opacity / 100);
  var bgcolor = options.bgcolor || '#000000';
  var dark=document.getElementById('darkenScreenObject');
  if (!dark) {
    // The dark layer doesn't exist, it's never been created.  So we'll
    // create it here and apply some basic styles.
    // If you are getting errors in IE see: http://support.microsoft.com/default.aspx/kb/927917
    var tbody = document.getElementsByTagName("body")[0];
    var tnode = document.createElement('div');           // Create the layer.
        tnode.style.position='absolute';                 // Position absolutely
        tnode.style.top='0px';                           // In the top
        tnode.style.left='0px';                          // Left corner of the page
        tnode.style.overflow='hidden';                   // Try to avoid making scroll bars            
        tnode.style.display='none';                      // Start out Hidden
        tnode.id='darkenScreenObject';                   // Name it so we can find it later		
	
    tbody.appendChild(tnode);                            // Add it to the web page
    dark=document.getElementById('darkenScreenObject');  // Get the object.
    window.onresize = resize;
  }
  if (vis) {
    // Calculate the page width and height 
    if( document.body && ( document.body.scrollWidth || document.body.scrollHeight ) ) {
        var pageWidth = document.body.scrollWidth+'px';
        var pageHeight = document.body.scrollHeight+'px';
    } else if( document.body.offsetWidth ) {
      var pageWidth = document.body.offsetWidth+'px';
      var pageHeight = document.body.offsetHeight+'px';
    } else {
       var pageWidth='100%';
       var pageHeight='100%';
    }   
    if(pageHeight == '0px')
	  pageHeight=document.documentElement.scrollHeight+'px';
    
    //set the shader to cover the entire page and make it visible.
	dark.style.position='absolute';
    dark.style.opacity=opaque;                      
    dark.style.MozOpacity=opaque;                   
    dark.style.filter='alpha(opacity='+opacity+')'; 
    dark.style.zIndex=zindex;        
    dark.style.backgroundColor=bgcolor;  
    dark.style.width= pageWidth;
    dark.style.height= pageHeight;
    dark.style.display='block';
  } else {
     dark.style.display='none';
  }
}

function resize()
{
  var dark=document.getElementById('darkenScreenObject');
  
  if( document.body && ( document.body.scrollWidth || document.body.scrollHeight ) ) {
        var pageWidth = document.body.scrollWidth+'px';
        var pageHeight = document.body.scrollHeight+'px';
    } else if( document.body.offsetWidth ) {
      var pageWidth = document.body.offsetWidth+'px';
      var pageHeight = document.body.offsetHeight+'px';
    } else {
       var pageWidth='100%';
       var pageHeight='100%';
    }
   
  if(pageHeight == '0px')
	  pageHeight=document.documentElement.scrollHeight+'px';
  dark.style.width= pageWidth;
  dark.style.height= pageHeight;
}