// JavaScript Document

// Check for W3C DOM support...
if (document.getElementById && document.createElement) {

  function getObj(name) {
    // Creates an object container for the .obj and .style properties.
    // 1. .obj, giving access to the actual HTML element. You have to use this property 
    //    to read out or set anything other than styles.
    // 2. .style, giving access to the styles of the HTML element. You have to use this 
    //    property to read out or set the styles of the element.
    // 
    // For Example:
    //   var x = new getObj('ElementName');
    //   alert(x.obj.id);
    //   x.style.top = '20px';
    // 
    if (document.getElementById)
    {
  	  this.obj = document.getElementById(name);
	  this.style = document.getElementById(name).style;
    }
  }

  function findObj(theObj, theDoc)
  // Example: obj = findObj("image1");
  {
    var p, i, foundObj;
  
    if(!theDoc) theDoc = document;
    if( (p = theObj.indexOf("?")) > 0 && parent.frames.length)
    {
      theDoc = parent.frames[theObj.substring(p+1)].document;
      theObj = theObj.substring(0,p);
    }
    if(!(foundObj = theDoc[theObj]) && theDoc.all) foundObj = theDoc.all[theObj];
    for (i=0; !foundObj && i < theDoc.forms.length; i++) 
      foundObj = theDoc.forms[i][theObj];
    for(i=0; !foundObj && theDoc.layers && i < theDoc.layers.length; i++) 
      foundObj = findObj(theObj,theDoc.layers[i].document);
    if(!foundObj && document.getElementById) foundObj = document.getElementById(theObj);
  
    return foundObj;
  }

  function findPosX(obj)
  {
    var curleft = 0;
    if (obj.offsetParent) {
      while (obj.offsetParent) {
        curleft += obj.offsetLeft;
        obj = obj.offsetParent;
      }
    }
    else if (obj.x)
      curleft += obj.x;
    return curleft;
  }

  function findPosY(obj)
  {
    var curtop = 0;
    if (obj.offsetParent) {
      while (obj.offsetParent) {
        curtop += obj.offsetTop;
        obj = obj.offsetParent;
      }
    }
    else if (obj.y)
      curtop += obj.y;
    return curtop;
  }

  function showHideLayers() {
    // * Dependencies * 
    // This function requires the following snippets:
    //   findObj
    //
    // Accepts a variable number of arguments, in triplets as follows:
    // arg 1: simple name of a layer object, such as "Layer1"
    // arg 2: ignored (for backward compatibility)
    // arg 3: 'hide' or 'show'
    // repeat...
    //
    // Example: showHideLayers(Layer1,'','show',Layer2,'','hide');
 
    var i, visStr, obj, args = showHideLayers.arguments;
    for (i=0; i<(args.length-2); i+=3) {
      if ((obj = findObj(args[i])) != null) {
        visStr = args[i+2];
        if (obj.style) {
          obj = obj.style;
          if(visStr == 'show') visStr = 'visible';
          else if(visStr == 'hide') visStr = 'hidden';
        }
        obj.visibility = visStr;
      }
    }
  }

  function isLayerVisible(theLayer) {
    // * Dependencies * 
    // This function requires the following snippets:
    //   findObj
    //
    // Example: isVisible = isLayerVisible('Layer1');
 
    var obj, visStr;
    if ((obj = findObj(theLayer)) != null) {
      if (obj.style)
        obj = obj.style;
      if (obj.visibility == 'show' || obj.visibility == 'visible')
        visStr = true;
      else if (obj.visibility == 'hide' || obj.visibility == 'hidden')
        visStr = false;
      return visStr;
    }
  }

  function layerVisibilityToggle(theLayer) {
    // * Dependencies * 
    // This function requires the following snippets:
    //   isLayerVisible, showHideLayers
    //
    // Example: layerVisibilityToggle('Layer1');

    if ( isLayerVisible(theLayer) ) {
      showHideLayers(theLayer,'','hide');
    }
    else {
      showHideLayers(theLayer,'','show');
    }
  }

  function layerToggle() {
    // * Dependencies * 
    // This function requires the following snippets:
    //   isLayerVisible, showHideLayers
    //
    // Accepts a variable number of arguments, as follows:
    // arg 1: simple name of a layer object, such as "Layer1" to turn on.
    // arg 2 through ...: simple name of a layer object, such as "Layer1" to turn off.
    //
    // Example: layerToggle('Layer1', 'Layer2', 'Layer3');

    var i, args = layerToggle.arguments;
    if ( ! isLayerVisible(args[0]) ) {
      var evalString = "showHideLayers(";
      for (i=1; i<(args.length); i++) {
        if (i > 1)
          evalString += ",";
        evalString += "'" + args[i] + "','','hide'";
      }
      if (i > 1)
        evalString += ",";
      evalString += "'" + args[0] + "','','show');";
      eval( evalString );
    }
  }

  function resizeLayers() {
    // * Dependencies * 
    // This function requires the following snippets:
    //   findObj
    //
    // Accepts a variable number of arguments, in triplets as follows:
    // arg 1: simple name of a layer object, such as "Layer1"
    // arg 2: width
    // arg 3: height
    // repeat...
    //
    // Example: resizeLayers(Layer1,'500','500',Layer2,'300','400');
 
    var i, height, width, obj, args = resizeLayers.arguments;
    for (i=0; i<(args.length-2); i+=3) {
      if ((obj = findObj(args[i])) != null) {
        width = args[i+1];
        height = args[i+2];
        if (obj.style)
          obj = obj.style;
        obj.height = height;
        obj.width = width;
      }
    }
  }

  function moveLayers()  {
    // * Dependencies * 
    // This function requires the following snippets:
    //   findObj
    //
    // Accepts a variable number of arguments, in triplets as follows:
    // arg 1: simple name of a layer object, such as "Layer1"
    // arg 2: position type: absolute or relative
    // arg 3: top
    // arg 4: left
    // repeat...
    //
    // Example: moveLayers(Layer1,'absolute','100','100',Layer2,'relative','200','200');
 
    var i, position, top, left, obj, args = moveLayers.arguments;
    for (i=0; i<(args.length-3); i+=4) {
      if ((obj = findObj(args[i])) != null) {
        position = args[i+1];
        top = args[i+2];
        left = args[i+3];
        if (position != "absolute" && position != "relative")
          position = "absolute";
        if (obj.style)
          obj = obj.style;
        obj.position = position;
        obj.top = top;
        obj.left = left;
      }
    }
  }

  function moveLayersRelativeTo() {
    // * Dependencies * 
    // This function requires the following snippets:
    //   findObj, findPosX, findPosY, moveLayers
    //
    // Accepts a variable number of arguments, in triplets as follows:
    // arg 1: simple name of a layer object to move, such as "Layer1"
    // arg 2: vertical edge of relative object: top or bottom
    // arg 3: vertical offset to relative object
    // arg 4: horizontal edge of relative object: left or right
    // arg 5: horizontal offset to relative object
    // arg 6: simple name of relative object, such as "Table1"
    // repeat...
    //
    // Example: moveLayersRelativeTo('Layer1','Top','0','Left','0','Table1');

    var i, verticalEdge, horizontalEdge, relObj, relObjTop, relObjBottom, relObjLeft, relObjRight, args = moveLayersRelativeTo.arguments;
    var evalString = "moveLayers(";
    for (i=0; i<(args.length-5); i+=6) {
      verticalEdge = args[i+1].toLowerCase();
      horizontalEdge = args[i+3].toLowerCase();
      if (verticalEdge != "top" && verticalEdge != "bottom" && horizontalEdge != "left" && horizontalEdge != "right")
        continue;
      if ((relObj = findObj(args[i+5])) != null) {
        relObjTop = findPosY(relObj);
        relObjLeft = findPosX(relObj);
        relObjBottom = parseInt(relObjTop) + parseInt(relObj.offsetHeight);
        relObjRight = parseInt(relObjLeft) + parseInt(relObj.offsetWidth);
        relObjBottomIE = parseInt(relObj.offsetHeight);
        relObjRightIE = parseInt(relObj.offsetWidth);
        if ( i > 0 && i < (args.length-5) )
          evalString += ",";
        if ( verticalEdge == "top" ) {
          if ( horizontalEdge == "left" )
            evalString += "'" + args[i] + "','absolute','" + (parseInt(relObjTop) + parseInt(args[i+2])) + "','" + (parseInt(relObjLeft) + parseInt(args[i+4])) + "'";
          else if ( horizontalEdge == "right" )
            evalString += "'" + args[i] + "','absolute','" + (parseInt(relObjTop) + parseInt(args[i+2])) + "','" + (parseInt(relObjRight) + parseInt(args[i+4])) + "'";
        }
        else if ( verticalEdge == "bottom" ) {
          if ( horizontalEdge == "left" )
            evalString += "'" + args[i] + "','absolute','" + (parseInt(relObjBottom) + parseInt(args[i+2])) + "','" + (parseInt(relObjLeft) + parseInt(args[i+4])) + "'";
          else if ( horizontalEdge == "right" )
            evalString += "'" + args[i] + "','absolute','" + (parseInt(relObjBottom) + parseInt(args[i+2])) + "','" + (parseInt(relObjRight) + parseInt(args[i+4])) + "'";
        }
      }
      else {
        continue;
      }
    }
    evalString += ");";
    eval( evalString );
  }

  function getCheckedValue(radioObj) {
    // return the value of the radio button that is checked
    // return an empty string if none are checked, or
    // there are no radio buttons
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
  }

  function setCheckedValue(radioObj, newValue) {
    // set the radio button with the given value as being checked
    // do nothing if there are no radio buttons
    // if the given value does not exist, all the radio buttons
    // are reset to unchecked
	if(!radioObj)
		return;
	var radioLength = radioObj.length;
	if(radioLength == undefined) {
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
	for(var i = 0; i < radioLength; i++) {
		radioObj[i].checked = false;
		if(radioObj[i].value == newValue.toString()) {
			radioObj[i].checked = true;
		}
	}
  }

}

