/* $Id: //php/release/www/js/div_magic.js#6 $ */

/**
 * showDiv
 *
 * show the specified div
 */
function showDiv(divNameToShow)
{
  if (document.getElementById) {
    /**
     * DOM3 = IE5, NS6
     */
    document.getElementById(divNameToShow).style.visibility = 'visible';
    document.getElementById(divNameToShow).style.display    = 'block';
  }
  else {
    if (document.layers) {
      /**
       * NS4
       */
      eval('document.' + divNameToShow + '.visibility="visible"');
      eval('document.' + divNameToShow + '.display="block"');
    }
    else {
      /**
       * IE4
       */
      eval('document.all.' + divNameToShow + '.style.visibility="visible"');
      eval('document.all.' + divNameToShow + '.style.display="block"');
    }
  }
}

/**
 * showSpan
 *
 * show the specified span
 */
function showSpan(spanNameToShow)
{
  if (document.getElementById) {
    /**
     * DOM3 = IE5, NS6
     */
    document.getElementById(spanNameToShow).style.visibility = 'visible';
    document.getElementById(spanNameToShow).style.display    = 'inline';
  }
  else {
    if (document.layers) {
      /**
       * NS4
       */
      eval('document.' + spanNameToShow + '.visibility="visible"');
      eval('document.' + spanNameToShow + '.display="inline"');
    }
    else {
      /**
       * IE4
       */
      eval('document.all.' + spanNameToShow + '.style.visibility="visible"');
      eval('document.all.' + spanNameToShow + '.style.display="inline"');
    }
  }
}

/**
 * showTableRow
 *
 * show the specific table row
 */
function showTableRow(tableRowNameToShow) 
{
  if (document.getElementById) {
    /**
     * DOM3 = IE5, NS6
     */
    document.getElementById(tableRowNameToShow).style.visibility = 'visible';
    document.getElementById(tableRowNameToShow).style.display    = '';
  }
  else {
    if (document.layers) {
      /**
       * NS4
       */
      eval('document.' + tableRowNameToShow + '.visibility="visible"');
      eval('document.' + tableRowNameToShow + '.display=""');
    }
    else {
      /**
       * IE4
       */
      eval('document.all.' + tableRowNameToShow + '.style.visibility="visible"');
      eval('document.all.' + tableRowNameToShow + '.style.display=""');
    }
  }
}

/**
 * hideDiv
 *
 * show the specified div
 */
function hideDiv(divNameToShow,doNotCollapse)
{
  if (doNotCollapse) {
    disp = 'block';
  }
  else {
    disp = 'none';
  }

  if (document.getElementById) {
    /**
     * DOM3 = IE5, NS6
     */
    document.getElementById(divNameToShow).style.visibility = 'hidden';
    document.getElementById(divNameToShow).style.display    = disp;
  }
  else {
    if (document.layers) {
      /**
       * NS4
       */
      eval('document.' + divNameToShow + '.visibility="hidden"');
      eval('document.' + divNameToShow + '.display="' + disp + '"');
    }
    else {
      /**
       * IE4
       */
      eval('document.all.' + divNameToShow + '.style.visibility="hidden"');
      eval('document.all.' + divNameToShow + '.style.display="' + disp + '"');
    }
  }
}

/**
 * hideSpan
 *
 * hide the specified Span
 */
function hideSpan(spanNameToHide)
{

  if (document.getElementById) {
/**
     * DOM3 = IE5, NS6
     */
    document.getElementById(spanNameToHide).style.visibility = 'hidden';
    document.getElementById(spanNameToHide).style.display    = 'inline';
  }
  else {
    if (document.layers) {
      /**
       * NS4
       */
      eval('document.' + spanNameToHide + '.visibility="hidden"');
      eval('document.' + spanNameToHide + '.display="inline"');
    }
    else {
      /**
       * IE4
       */
      eval('document.all.' + spanNameToHide + '.style.visibility="hidden"');
      eval('document.all.' + spanNameToShow + '.style.display="inline"');
    }
  }
}


/**
 * hideDiv
 *
 * hide the specified table row.
 */
function hideTableRow(tableRowNameToHide, doNotCollapse)
{
  if (doNotCollapse) {
    disp = '';
  }
  else {
    disp = 'none';
  }

  if (document.getElementById) {
    /**
     * DOM3 = IE5, NS6
     */
    document.getElementById(tableRowNameToHide).style.visibility = 'hidden';
    document.getElementById(tableRowNameToHide).style.display    = disp;
  }
  else {
    if (document.layers) {
      /**
       * NS4
       */
      eval('document.' + tableRowNameToHide + '.visibility="hidden"');
      eval('document.' + tableRowNameToHide + '.display="' + disp + '"');
    }
    else {
      /**
       * IE4
       */
      eval('document.all.' + tableRowNameToHide + '.style.visibility="hidden"');
      eval('document.all.' + tableRowNameToHide + '.style.display="' + disp + '"');
    }
  }
}


/**
 * hideDivs
 *
 * hides the specified ids
 */
function hideDivs()
{
  args = hideDivs.arguments;
  for (id = 0; id < args.length; id++) {
    if (!document.getElementById(args[id])) {
      //alert(args[id]);
      continue;
    }
    document.getElementById(args[id]).style.visibility = 'hidden';
    document.getElementById(args[id]).style.display    = 'none';
  }

  return args.length;
}


/**
 * unhideDiv
 *
 * Shows the first parameter, and hides the following params
 */
function unhideDiv(divNameToShow)
{
  if (document.getElementById) {
    /**
     * DOM3 = IE5, NS6
     */

    /**
     * Show the requested div
     */
    document.getElementById(divNameToShow).style.visibility = 'visible';
    document.getElementById(divNameToShow).style.display    = 'block';

    /**
     * Hide the rest of the divs
     * Start at 1 because 0 is for the divNameToShow
     */
    for (id = 1; id < unhideDiv.arguments.length; id++) {
      if (unhideDiv.arguments[id] != divNameToShow) {
        document.getElementById(unhideDiv.arguments[id]).style.visibility = 'hidden';
        document.getElementById(unhideDiv.arguments[id]).style.display    = 'none';
      }
    }
  }
  else {
    if (document.layers) {
      /**
       * NS4
       */
      eval('document.' + divNameToShow + '.visibility="visible"');
      eval('document.' + divNameToShow + '.display="block"');
      /**
       * Hide the rest of the divs
       * Start at 2 because 0 is for the function name and 1 for the divNameToShow
       */
      for (id = 2; id < unhideDiv.arguments.length; id++) {
        if (unhideDiv.arguments[id] != divNameToShow) {
          eval('document.' + unhideDiv.arguments[id] + '.visibility="hidden"');
          eval('document.' + unhideDiv.arguments[id] + '.display="none"');
        }
      }
    }
    else {
      /**
       * IE4
       */
      eval('document.all.' + divNameToShow + '.style.visibility="visible"');
      eval('document.all.' + divNameToShow + '.style.display="block"');
      /**
       * Hide the rest of the divs
       * Start at 1 because 0 is for the divNameToShow
       */
      for (id = 1; id < unhideDiv.arguments.length; id++) {
        if (unhideDiv.arguments[id] != divNameToShow) {
          eval('document.all.' + unhideDiv.arguments[id] + '.style.visibility="hidden"');
          eval('document.all.' + unhideDiv.arguments[id] + '.style.display="none"');
        }
      }
    }
  }
}

/**
 * switchVisibility
 *
 * Swaps the visibility of the specified DIV/LAYER.
 */
function switchVisibility(id, initialState)
{
  var state;
  if (document.getElementById) { // DOM3 = IE5, NS6
    state = document.getElementById(id).style.visibility;

    switch (state) {
      case 'visible':
        hideDiv(id);
        break;

      case 'hidden':
        showDiv(id);
        break;

      default:
        switch (initialState) {
          case 'visible':
            hideDiv(id);
            break;

          case 'hidden':
            showDiv(id);
            break;
        }
    }
  }
  else {
    if (document.layers) { // NS4
      eval('state = document.' + id + '.visibility');
    }
    else { // IE4
      eval('state = document.all.' + id + '.style.visibility');
    }
    if (state == "visible") {
      hideDiv(id);
    }
    else {
      showDiv(id);
    }
  }
}


/**
 * moveDiv
 *
 * Move the div to the specified x and y
 */
function moveDiv(divId,x,y)
{
  if (document.getElementById && document.getElementById(divId)) {
    divObj = document.getElementById(divId);

    // x is defined
    if (!isNaN(x)) {

      if (!isNaN(divObj.style.left)) {
        divObj.style.left = x;
      }
      else if (!isNaN(divObj.style.pixelLeft)) {
        divObj.style.pixelLeft = x;
      }
      else if (!isNaN(divObj.left)) {
        divObj.left = x;
      }
    }

    // y is defined
    if (!isNaN(y)) {
      if (!isNaN(divObj.style.top)) {
        divObj.style.top = y;
      }
      else if (!isNaN(divObj.style.pixelTop)) {
        divObj.style.pixelTop = y;
      }
      else if (!isNaN(divObj.top)) {
        divObj.top = y;
      }
    }
  }
  else if (document.layers) {
    eval('document.' + id + '.left = ' + x);
    eval('document.' + id + '.top = ' + y);
  }
  else if (document.all) {
    eval('document.all.' + id + '.style.left = ' + x);
    eval('document.all.' + id + '.style.top  = ' + x);
  }
}


/**
 * moveDivToCenter
 *
 * Moves a div to the center of the screen
 */
function moveDivToCenter(id,itemH,itemW)
{
  height  = getBrowserHeight();
  width   = getBrowserWidth();

  if (height <= 0 || width <= 0) {
    return;
  }

  centerH = Math.floor((height-itemH) / 2);
  centerW = Math.floor((width-itemW)  / 2);

  moveDiv(id,centerW,centerH)
}

function getBrowserHeight()
{
  if (typeof(window.innerHeight) == 'number') {
    return window.innerHeight;
  }

  if (document.documentElement && document.documentElement.clientHeight) {
    //IE 6+ in 'standards compliant mode'
    return document.documentElement.clientHeight;
  }

  if (document.body && document.body.clientHeight) {
    //IE 4 compatible
    return document.body.clientHeight;
  }

  return 0;
}

function getBrowserWidth()
{
  if (typeof( window.innerWidth ) == 'number') {
    //Non-IE
    return window.innerWidth;
  }

  if (document.documentElement && document.documentElement.clientWidth) {
    //IE 6+ in 'standards compliant mode'
    return document.documentElement.clientWidth;
  }

  if (document.body && document.body.clientWidth) {
    //IE 4 compatible
    return document.body.clientWidth;
  }

  return 0;
}

// Functions for IE to get position of an object
function getPageOffsetLeft (el) {
	var ol=el.offsetLeft;
	while ((el=el.offsetParent) != null) { ol += el.offsetLeft; }
	return ol;
}
function getPageOffsetTop (el) {
	var ot=el.offsetTop;
	while((el=el.offsetParent) != null) { ot += el.offsetTop; }
	return ot;
}

function getCssObjXY(objID)
{
  // This function will return an Object with x and y properties
	var useWindow=false;
	var coordinates=new Object();
	var x=0,y=0;
	// Browser capability sniffing
	var use_gebi=false, use_css=false, use_layers=false;
	if (document.getElementById) { use_gebi=true; }
	else if (document.all) { use_css=true; }
	else if (document.layers) { use_layers=true; }
	// Logic to find position
 	if (use_gebi && document.all) {
		x=getPageOffsetLeft(document.all[objID]);
		y=getPageOffsetTop(document.all[objID]);
		}
	else if (use_gebi) {
		var o=document.getElementById(objID);
		x=getPageOffsetLeft(o);
		y=getPageOffsetTop(o);
		}
 	else if (use_css) {
		x=getPageOffsetLeft(document.all[objID]);
		y=getPageOffsetTop(document.all[objID]);
		}
	else if (use_layers) {
		var found=0;
		for (var i=0; i<document.anchors.length; i++) {
			if (document.anchors[i].name==objID) { found=1; break; }
			}
		if (found==0) {
			coordinates.x=0; coordinates.y=0; return coordinates;
			}
		x=document.anchors[i].x;
		y=document.anchors[i].y;
		}
	else {
		coordinates.x=0; coordinates.y=0; return coordinates;
		}
	coordinates.x=x;
	coordinates.y=y;
	return coordinates;
}

function moveDivToAnchor(divID,anchorID,offsetX,offsetY)
{
  if (document.getElementById) {
    if (document.getElementById(divID)) {
      var divObj = document.getElementById(divID);
    }
    if (document.getElementById(anchorID)) {
      var anchorObj = document.getElementById(anchorID);
    }
  }

  if (isNaN(offsetX)) {
    offsetX = 0;
  }

  if (isNaN(offsetY)) {
    offsetY = 0;
  }

  coords = getCssObjXY(anchorObj.id);
  anchorObjX = coords.x
  anchorObjY = coords.y

  moveDiv(divID,anchorObjX + offsetX, anchorObjY + offsetY);
}

var ns = (navigator.appName.indexOf("Netscape") != -1);
var d = document;
var px = document.layers ? "" : "px";

function floatDiv(id, sx, sy)
{
  var el=d.getElementById?d.getElementById(id):d.all?d.all[id]:d.layers[id];
	window[id + "_obj"] = el;
	if(d.layers)el.style=el;
	el.cx = el.sx = sx;el.cy = el.sy = sy;
	el.sP=function(x,y){this.style.left=x+px;this.style.top=y+px;};
	el.flt=function()
	{
		var pX, pY;
		pX = (this.sx >= 0) ? 0 : ns ? innerWidth : 
		document.documentElement && document.documentElement.clientWidth ? 
		document.documentElement.clientWidth : document.body.clientWidth;
		pY = ns ? pageYOffset : document.documentElement && document.documentElement.scrollTop ? 
		document.documentElement.scrollTop : document.body.scrollTop;
		if(this.sy<0) 
		pY += ns ? innerHeight : document.documentElement && document.documentElement.clientHeight ? 
		document.documentElement.clientHeight : document.body.clientHeight;
		this.cx += (pX + this.sx - this.cx)/8;this.cy += (pY + this.sy - this.cy)/8;
		this.sP(this.cx, this.cy);
		setTimeout(this.id + "_obj.flt()", 10);
	}
	return el;
}


var FLOAT_DIV_OPENED = false;
function floatDivUntilOpened(id, sx, sy)
{
  var el=d.getElementById?d.getElementById(id):d.all?d.all[id]:d.layers[id];
	window[id + "_obj"] = el;
	if(d.layers)el.style=el;
	el.cx = el.sx = sx;el.cy = el.sy = sy;
	el.sP=function(x,y){this.style.left=x+px;this.style.top=y+px;};
	el.flt=function()
	{
	  var pX, pY;
		pX = (this.sx >= 0) ? 0 : ns ? innerWidth : 
		document.documentElement && document.documentElement.clientWidth ? 
		document.documentElement.clientWidth : document.body.clientWidth;
		pY = ns ? pageYOffset : document.documentElement && document.documentElement.scrollTop ? 
		document.documentElement.scrollTop : document.body.scrollTop;
		if(this.sy<0) 
		pY += ns ? innerHeight : document.documentElement && document.documentElement.clientHeight ? 
		document.documentElement.clientHeight : document.body.clientHeight;
		this.cx += (pX + this.sx - this.cx)/8;this.cy += (pY + this.sy - this.cy)/8;
		if (!FLOAT_DIV_OPENED) {
	    this.sP(this.cx, this.cy);
	  }
		setTimeout(this.id + "_obj.flt()", 10);
	}
	return el;
}
