//---
function openNamedWin( url, name, width, height )
{
  var string = "width=";

  //---
  if ( width != null )
    string += width;
  else
    string += "600";

  if ( height != null )
  {
    string += ",height=";
    string += height;
  }

  if ( name == 'hlWin' )
    string += ",status=no,toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=yes";
  else if ( name == 'SearchNews' )
    string += ",status=no,toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=yes";
  else
    string += ",status=yes,toolbar=yes,menubar=yes,location=no,scrollbars=yes,resizable=yes";

  window.open( url, name, string );
}

function openPDFWin( url, name, width, height )
{
  var string = "width=";

  //---
  if ( width != null )
    string += width;
  else
    string += "785";

  if ( height != null )
  {
    string += ",height=";
    string += height;
  }

  if ( name == 'hlWin' )
    string += ",status=no,toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=yes";
  else if ( name == 'SearchNews' )
    string += ",status=no,toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=yes";
  else
    string += ",status=yes,toolbar=yes,menubar=yes,location=no,scrollbars=yes,resizable=yes";

  window.open( '/protected/AJAXPages/?'+url, 'infoWin', string );
}

//---
function openCenteredNamedWin()
{
  var allowScroll = false;
  var allowResize = false;
  var bringToFront = false;

  //---
  // get arguments this way since we want to do overloading but javascript can't
  //---
  var url = arguments[0];
  var name = arguments[1];
  var width = arguments[2];
  var height = arguments[3];

  //---
  // JD: 12/08/2006
  // Quick fix for IE7 issue:
  // Add 30 more pixels to the new window
  //---
  if ( extendPersonalizationWindow() == true )
    height += 30;

  if ( arguments.length > 4 )
    allowScroll = arguments[4];
  else
    allowScroll = false;

  if ( arguments.length > 5 )
    allowResize = arguments[5];
  else
    allowResize = false;

  if ( arguments.length > 6 )
    bringToFront = arguments[5];
  else
    bringToFront = false;

  var ht = screen.height;               // get user's screen height
  var wd = screen.width;                // get user's screen width
  var x = (screen.width - width) / 2;   // get center of screen minus popup width
  var y = (screen.height - height) / 2; // get center of screen minus popup height

  //---
  // set the options for the popup
  //---
  var pop = "width=" + width + ", height=" + height + ", ";

  if ( allowScroll )
    pop += "scrollbars=yes, ";
  else
    pop += "scrollbars=no, ";

  if ( allowResize )
    pop += "resizable=yes, ";
  else
    pop += "resizable=no, ";
 
  //pop += ("top=" + y + ", screenY=" + y + ", left=" + x + ", screenX=" + x + ", status=no, toolbar=no, menubar=no, location=no");
  pop += ("top=" + y + ", screenY=" + y + ", left=" + x + ", screenX=" + x + ", status=no, toolbar=no, menubar=yes, location=no");

  var win = window.open( url, name, pop ); // open popup

  if ( bringToFront == true )
    if ( win != null )
    	win.focus();
}

//---
function openCenteredFixedSizeWin( url, name, width, height )
{
  var ht = screen.height;               // get user's screen height
  var wd = screen.width;                // get user's screen width
  var x = (screen.width - width) / 2;   // get center of screen minus popup width
  var y = (screen.height - height) / 2; // get center of screen minus popup height

  // set the options for the popup
  var pop = "width=" + width + ", height=" + height + ", scrollbars=yes, resizable=no, top=" + y + ", screenY=" + y + ", left=" + x + ", screenX=" + x + ", status=no, toolbar=no, menubar=no, location=no";

  window.open( url, name, pop ); // open popup
}

//---
function openStoryWin( url, width )
{
  var undef;
  var widthStr = "width=", pop;
  var win;

  // set the options for the popup
  if ( (width != null) && (width != undef) )
    widthStr += width;
  else
    widthStr += "600";

  pop = widthStr + ", height=600, status=yes, toolbar=no, menubar=no, location=no, scrollbars=yes, resizable=no";

  if ( (win  = window.open( url, "storyWin", pop )) != null )
    win.focus();
}

//---
function openSearchWin( width, height, searchForm )
{
  var name = "SearchNews";
  var string = "width=";
  var opts = ",status=yes,toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=yes";
  var searchTxt = searchForm.ftsKWs.value;
  var url = "/IFRLuceneSearch/servlet/IFRLuceneSearch";

  //---
  url += '?simpleSearch=';
  url += searchTxt;

  //---
  if ( width != null )
    string += width;
  else
    string += "1000";

  if ( height != null )
  {
    string += ",height=";
    string += height;
  }
  string += opts;

  window.open( url, name, string );
    
  return false;
}

//---
/***
function openCenteredNamedWin( url, name, width, height )
{
  var ht = screen.height;               // get user's screen height
  var wd = screen.width;                // get user's screen width
  var x = (screen.width - width) / 2;   // get center of screen minus popup width
  var y = (screen.height - height) / 2; // get center of screen minus popup height

  // set the options for the popup
  var pop = "width=" + width + ",height=" + height + ",scrollbars=yes" + ",resizable=yes" + ",top=" + y + ",screenY=" + y + ",left=" +
	     x + ",screenX=" + x + "status=no,toolbar=no,menubar=no,location=no";

  window.open( url, name, pop ); // open popup
}
***/

var debugWindow;
function debug(msg)
{
  if (typeof debugWindow == "undefined" || debugWindow == null)
    debugWindow = window.open('', 'debugWindow', 'scrollbars=yes,width=300,height=400');

  debugWindow.document.writeln(msg + "<br>");
}

/*
 * This sets the position on the screen of event details popup
 * when a user mouses over the economic calendar events for the
 * day in the sidebar.
 */
function calendarPositionDetails(e)
{
	// ie uses window.event netscape will pass the event as e
	var e;
	if (!e)
		e = window.event;

	/*
	 * more fun browser differences
	 * IE keeps the target html element in e.srcElement
	 * netscape/firefox/mozilla keeps the target in e.target
	 */
	var target;
	if (e.target)
		target = e.target;
	else if (e.srcElement)
		target = e.srcElement;

	// only do this for A nodes
	if (target.nodeName.toUpperCase() != "A")
		return;

	// apparently safari needs this
	 if (target.nodeType && target.nodeType == 3)
		target = target.parentNode;

	// the span is the second child since the a text is first
	var spanElement = target.childNodes.item(1);
	if (spanElement != null) {
		var tableHeader = spanElement.getElementsByTagName("TH")[0];
		var headline = tableHeader.childNodes.item(0);
		var newWidth;
		if (headline.length > 28) {
			newWidth = 152 + 10 * (headline.length - 28);
		} else {
			newWidth = 152;
		}

		/*
		 * need to walk through all tr in the span and adjust
		 * widths based on whether there are two or three columns
		 * skip the first row, because it is the header
		 */
		var tableRows = spanElement.getElementsByTagName("TR");
		for (var i = 1; i < tableRows.length; i++) {
			var rowCells = tableRows[i].getElementsByTagName("TD");
			var cellWidth = "50%";
			if (rowCells.length == 3) {
				cellWidth = "33%";
				if (newWidth < 210) {
					newWidth = 210;
				}
			}

			for (var j = 0; j < rowCells.length; j++)
				rowCells[j].style.width = cellWidth;
		}

		/*
		 * firefox/mozilla needs the width
		 * dynamically set for long
		 * event names
		 */
/**		if (navigator.userAgent.toUpperCase().indexOf("MSIE") == -1)  **/ 
			spanElement.style.width = newWidth + "px";



		var windowHeight;
		if (window.innerHeight)
		{
			windowHeight = window.innerHeight;
		}
		else if (document.documentElement && document.documentElement.clientHeight)
		{
			windowHeight = document.documentElement.clientHeight;
		}
		else if (document.body)
		{
			windowHeight = document.body.clientHeight;
		}
	
		var x = 0;
		var y = 0;
		if (e.pageX || e.pageY)
		{
			x = e.pageX;
			y = e.pageY;
		} 
		else if (e.clientX || e.clientY)
		{
			x = e.clientX + document.body.scrollLeft;
			y = e.clientY + document.body.scrollTop;
		}

		x = x - newWidth - 10;
		if (x < 0)
			x = 0;

		if (160 + y > windowHeight + document.body.scrollTop)
			y = windowHeight - 160 + document.body.scrollTop;

                y =0;
                x = -110;     
		/**spanElement.style.top = y + "px"; **/
		spanElement.style.left =x + "px";

	}
}


/*
 * For page 17522: Display a deal's LEADS in a pop-up when 
 * the string is too long to fit in the alloted column size
 * on the screen
 */
function dealLeadPositionDetails( e )
{
  // ie uses window.event netscape will pass the event as e
  var e;
  if (!e)
    e = window.event;

  /*
   * more fun browser differences
   * IE keeps the target html element in e.srcElement
   * netscape/firefox/mozilla keeps the target in e.target
   */
  var target;
  if (e.target)
    target = e.target;
  else if (e.srcElement)
    target = e.srcElement;

  // only do this for A nodes
  if (target.nodeName.toUpperCase() != "A")
    return;

  // apparently safari needs this
   if (target.nodeType && target.nodeType == 3)
    target = target.parentNode;

  // the span is the second child since the a text is first
  var spanElement = target.childNodes.item(1);
  if (spanElement != null) {
    var tableHeader = spanElement.getElementsByTagName("TH")[0];
    var headline = tableHeader.childNodes.item(0);
    var newWidth;
    if (headline.length > 28) {
      newWidth = 152 + 10 * (headline.length - 28);
    } else {
      newWidth = 152;
    }

    /*
     * need to walk through all tr in the span and adjust
     * widths based on whether there are two or three columns
     * skip the first row, because it is the header
     */
    var tableRows = spanElement.getElementsByTagName("TR");
    for (var i = 1; i < tableRows.length; i++) {
      var rowCells = tableRows[i].getElementsByTagName("TD");
      var cellWidth = "50%";
      if (rowCells.length == 3) {
        cellWidth = "33%";
        if (newWidth < 210) {
          newWidth = 210;
        }
      }

      for (var j = 0; j < rowCells.length; j++)
        rowCells[j].style.width = cellWidth;
    }

    /*
     * firefox/mozilla needs the width
     * dynamically set for long
     * event names
     */
    spanElement.style.width = newWidth + "px";
  
    var windowHeight;
    if (window.innerHeight)
    {
      windowHeight = window.innerHeight;
    }
    else if (document.documentElement && document.documentElement.clientHeight)
    {
      windowHeight = document.documentElement.clientHeight;
    }
    else if (document.body)
    {
      windowHeight = document.body.clientHeight;
    }
  
    var x = 0;
    var y = 0;
    if (e.pageX || e.pageY)
    {
      x = e.pageX;
      y = e.pageY;
    } 
    else if (e.clientX || e.clientY)
    {
      x = e.clientX + document.body.scrollLeft;
      y = e.clientY + document.body.scrollTop;
    }

    x = x - newWidth - 10;
    if (x < 0)
      x = 0;

    if (160 + y > windowHeight + document.body.scrollTop)
      y = windowHeight - 160 + document.body.scrollTop;

    y = y - 90;

    spanElement.style.top = y + "px";
    spanElement.style.left = x + "px";

    //---
    // Have some fun in IE: Display the postion on the screen
    //---
    /*
    var posText = "cy: " + e.clientY + "px, dy: " + y + "px";
    var id = getAttrValue( spanElement, "id" );
    var key = "YPOS_" + id;

    var posElem = document.getElementById( key );
    if ( posElem == null )
    {
      var table = spanElement.getElementsByTagName("TABLE")[0];
      if ( table != null )
      {
        var tbody = table.getElementsByTagName("TBODY")[0];
        if ( tbody != null )
        {
          var newText = document.createTextNode( posText );
	  if ( newText != null )
	  {
            var newTD = document.createElement( "TD" );
	    if ( newTD != null )
	    {
              newTD.setAttribute( "id", key );

              var newTR = document.createElement( "TR" );
              if ( newTR != null )
	      {
                newTD.appendChild( newText );
                newTR.appendChild( newTD );
  
                tbody.appendChild( newTR );
              }
            }
          }
        }
      }
    }
    else
      posElem.innerHTML = posText;
    */
  }
}


String.prototype.trim = function() {
a = this.replace(/^\s+/, '');
return a.replace(/\s+$/, '');
};

//---
function extendPersonalizationWindow()
{
  var addSpace = false;

  if ( navigator.appName == "Microsoft Internet Explorer" )
  {
    var appVerStr = "", ieVerStr = "", ieMajVerStr = "", ieMinVerStr = "";
    var pos, endPos, perPos;
    var ieMajVer = 0;
    var ieMinVer = 0;

    appVerStr = navigator.appVersion;
    if ( (pos = appVerStr.indexOf( "MSIE" )) > -1 )
    {
      endPos = appVerStr.indexOf( ";", pos );
      ieVerStr = appVerStr.substring( pos + 4, endPos ).trim();

      if ( (perPos = ieVerStr.indexOf( "." )) != -1 )
      {
        ieMajVerStr = ieVerStr.substring( 0, perPos );
        ieMinVerStr = ieVerStr.substring( perPos + 1 );

        ieMajVer = parseInt( ieMajVerStr );
        ieMinVer = parseInt( ieMinVerStr );
      }
      else
      {
        ieMajVer = parseInt( ieVerStr );
        ieMinVer = 0;
      }

      if ( ieMajVer >= 7 )
        addSpace = true;
    }
  }

  return addSpace;
}

