var isIE6 = /msie|MSIE 6/.test(navigator.userAgent);
var isIE7 = /msie|MSIE 7/.test(navigator.userAgent);
var popupToKill;
var channelPrefix = "0";

function getPopupScrolledTop()
{
      var scrolledTop;

      scrolledTop = top.document.documentElement.scrollTop;
      if (scrolledTop == 0)
            scrolledTop = top.document.body.scrollTop;

      return scrolledTop;
}


function delayedClose()
{
	top.document.getElementById(popupToKill).parentNode.removeChild(top.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( top.window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = top.window.innerWidth;
    myHeight = top.window.innerHeight;
  } else if( top.document.documentElement && ( top.document.documentElement.clientWidth || top.document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = top.document.documentElement.clientWidth;
    myHeight = top.document.documentElement.clientHeight;
  } else if( top.document.body && ( top.document.body.clientWidth || top.document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = top.document.body.clientWidth;
    myHeight = top.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 getFlexApp(appName) {
  if (navigator.appName.indexOf ("Microsoft") !=-1) {
    return window[appName];
  }
  else {
    return document[appName];
	}
}																																																																		
function createPopUp(popUpCode)
{
	var div = top.document.createElement('div');
	div.innerHTML = popUpCode;
	top.document.body.appendChild(div.firstChild);
}

function closePopUp(popUpId)
{
      top.document.getElementById(popUpId).style.display='none';
      popupToKill = popUpId;
      delayedClose();   
	  mostrarSelect();
	  try{top.frames['iframe_datos'].mostrarSelect()} catch(e){}
}

function mostrarSelect()
{
	 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(dir,width,height)
{
	isPopUpVisible = true;
	
	var frameWidth = width;
	var frameHeight = height;
	var scrollOffset =  getPopupScrolledTop(frameHeight);
	var frameTop = calculateHorizontalCenter(frameHeight) + scrollOffset +"px";
	
	var DivHTML="<div id='div_grayout' style='position: absolute; top: "+frameTop+"; left:" + calculateVerticalCenter(frameWidth) + "px; padding:0px; border:0px; width: "+ frameWidth +"px; height: "+frameHeight+"px; z-index:999999999; background-color:#ffffff'>"
			//+	"<div class='cierre_grayout'><a href='javascript:closePopUpGrayOut()'><span class='cierre_grayout_texto'>Cerrar </span><img src='/on/io/es/c2c/hogar/greybox/w_close.gif'></a></div>"
			+ 	"<iframe name='iframe_grayout' id='iframe_grayout' frameborder='0' src='"+dir+"' style='width:100%;height:100%;' scrolling='no'>&nbsp;</iframe>"				
			+	"</div>";
	createPopUp(DivHTML);

	 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='hidden';
			}
	 }
}

function closePopUpGrayOut() 
{
	grayOut(false);			
	closePopUp("div_grayout");
}


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 || 80;
  var opaque = (opacity / 100);
  var bgcolor = options.bgcolor || '#585858';
  var dark=top.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 = top.document.getElementsByTagName("body")[0];
    var tnode = top.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=top.document.getElementById('darkenScreenObject');  // Get the object.
    window.onresize = resize;
  }
  if (vis) {
	var pageWidth;
	var pageHeight;
	if (top.document.documentElement)
	{
		pageHeight = (top.document.documentElement.scrollHeight)+'px';
		//pageWidth = (top.document.documentElement.scrollWidth)+'px';
		//if(isIE6) pageWidth = (top.document.documentElement.scrollWidth - 22)+'px';
	}
	else if( top.document.body && ( top.document.body.scrollHeight ) ) 
	{       
       pageHeight = top.document.body.scrollHeight+'px';
	   //pageWidth = (top.document.body.scrollWidth)+'px';
	   //if(isIE6) pageWidth = (top.document.body.scrollWidth - 22)+'px';
    } 
	else if( top.document.body.offsetHeight ) 
	{
      pageHeight = top.document.body.offsetHeight+'px';
	  //pageWidth = (top.document.body.offsetWidth)+'px';
	  //if(isIE6) pageWidth = (top.document.body.offsetWidth - 22)+'px';
    } 
	else {
       //pageWidth='100%';
       pageHeight='100%';
    }    
	 pageWidth='100%';

	if (top.document.documentElement && top.document.body)
	{
		if (top.document.body.scrollHeight > top.document.documentElement.scrollHeight)
		{
			pageHeight = (top.document.body.scrollHeight)+'px';
		}
	}
    
    //set the shader to cover the entire page and make it visible.
    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=top.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';*/

  var pageWidth;
  var pageHeight;
	if (top.document.documentElement)
	{
		pageHeight = (top.document.documentElement.scrollHeight)+'px';
		//pageWidth = (top.document.documentElement.scrollWidth)+'px';
		//if(isIE6) pageWidth = (top.document.documentElement.scrollWidth - 22)+'px';
	}
	else if( top.document.body && ( top.document.body.scrollHeight ) ) 
	{       
	   pageHeight = top.document.body.scrollHeight+'px';
	   //pageWidth = (top.document.body.scrollWidth)+'px';
	   //if(isIE6) pageWidth = (top.document.body.scrollWidth - 22)+'px';
	} 
	else if( top.document.body.offsetHeight ) 
	{
	  pageHeight = top.document.body.offsetHeight+'px';
	  //pageWidth = (top.document.body.offsetWidth)+'px';
	  //if(isIE6) pageWidth = (top.document.body.offsetWidth - 22)+'px';
	} 
	else {
	   //pageWidth='100%';
	   pageHeight='100%';
	}    
	 pageWidth='100%';

	if (top.document.documentElement && top.document.body)
	{
		if (top.document.body.scrollHeight > top.document.documentElement.scrollHeight)
		{
			pageHeight = (top.document.body.scrollHeight)+'px';
		}
	}

  dark.style.width= pageWidth;
  dark.style.height= pageHeight;
}

function GB_showCenter(dir,width,height) 
{
	var options = {};
	options.bgcolor = '#585858';
	options.zindex='99999999';
	grayOut(true,options);
	
	initPopUp(dir,width,height);
}
