/** 
 * Scripts for creating popup iframes on page by mousing over non-link objects, eg., frame submit button
 * Fn show_hide_box_obj is called from an event when mousing over an object
 * and creates on the fly div element containing an iframe element
 * each successive call to show_hide_box_obj toggles display of the element on or off
 * Fn move_box_obj sets position of the div element relative to the calling object
 * To implement, insert event in object element eg., 
 * onMouseOver="return show_hide_box_obj('terms_warning.php',authnet_sub_but,180,120,'2px solid orange','no',-195,-75)"
 * onMouseOut="return show_hide_box_obj('terms_warning.php',authnet_sub_but,180,120,'2px solid orange','no',-195,-75)"
 */
function move_box_obj (an, box, offsetleft, offsettop) {
  var cleft = offsetleft;
  var ctop = offsettop;
  var obj = an;
  while (obj.offsetParent) {
    cleft += obj.offsetLeft;
    ctop += obj.offsetTop;
    obj = obj.offsetParent;
  }
  box.style.left = cleft + 'px';
// replace 0 with an integer to offset the vertical position 
  ctop += an.offsetHeight + 0;
  if (document.body.currentStyle &&
    document.body.currentStyle['marginTop']) {
    ctop += parseInt(
      document.body.currentStyle['marginTop']);
  }
  box.style.top = ctop + 'px';
}
// 1st var is the url to display in box, 2nd var is the name attribute of the calling element 
// eg., onMouseOver="return ('terms_warning.php',authnet_sub_but,180,120,'2px solid orange','no',-195,-75)"
function show_hide_box_obj (an, object, width, height, borderStyle, scroll, shiftleft, shifttop) {
  var href = an;
  var boxdiv = document.getElementById(href);
  var obj = object;
  var scr = scroll;
// if boxdiv (var representing div element) exists already, display it
  if (boxdiv != null) {
    if (boxdiv.style.display=='none') {
      move_box_obj(obj, boxdiv, shiftleft, shifttop);
      boxdiv.style.display='block';
    } else
      boxdiv.style.display='none';
    return false;
  }
// create boxdiv (var representing div element) containing iframe, and display it
  boxdiv = document.createElement('div');
  boxdiv.setAttribute('id', href);
  boxdiv.style.display = 'block';
  boxdiv.style.position = 'absolute';
  boxdiv.style.width = width + 'px';
  boxdiv.style.height = height + 'px';
  boxdiv.style.border = borderStyle;
  boxdiv.style.backgroundColor = '#fff';

  var contents = document.createElement('iframe');
  contents.scrolling = scr;
  contents.frameBorder = '0';
  contents.style.width = width + 'px';
  contents.style.height = height + 'px';
  contents.src = href;

  boxdiv.appendChild(contents);
  document.body.appendChild(boxdiv);
  move_box_obj(obj, boxdiv, shiftleft, shifttop);

  return false;
}

/** 
 * Scripts for creating popup iframes on page by mousing over anchor links
 * Requires an anchor tag link <a> and an existing empty <div> element within the page
 * When the link is moused over, the div element appears as a pop-up box, and an iframe is created on the fly,
 * and remains as long as the mouse is over the box
 * To prevent the link from being clicked, set the shifttop to a negative value to conceal the link under the box
 * Example implementation:
 * <a href="file_to_display_in_box" onMouseOver="return show_hide_box(this,'div_element_id',304,346,'2px solid orange','no',-145,-5)" onMouseOut="hide('div_element_id')">click here</a>
<div id="div_element_id" style="display:none;" onMouseOver="show('div_element_id')" onMouseOut="hide('div_element_id')"></div>
 * 
 * Fn move_box sets position of the div element relative to the calling link
 */
function move_box (an, box, offsetleft, offsettop) {
  var cleft = offsetleft;
  var ctop = offsettop;
  var obj = an;
  while (obj.offsetParent) {
    cleft += obj.offsetLeft;
    ctop += obj.offsetTop;
    obj = obj.offsetParent;
  }
  box.style.left = cleft + 'px';
// replace 0 with an integer to offset the vertical position 
  ctop += an.offsetHeight + 0;
  if (document.body.currentStyle &&
    document.body.currentStyle['marginTop']) {
    ctop += parseInt(
      document.body.currentStyle['marginTop']);
  }
  box.style.top = ctop + 'px';
}

// 1st var should always be "this", eg., onClick="return show_hide_box(this,'div_element_id',304,346,'2px solid orange','no',-115,0)"
function show_hide_box (an, div_id, width, height, borderStyle, scroll, shiftleft, shifttop) {
  var href = an.href;
  var boxdiv = document.getElementById(div_id);
  var scr = scroll;
// if boxdiv (var representing div element) exists, display it; for browsers that conform to W3C DOM
  if (boxdiv != null)
  {
    if (boxdiv.style.display=='none') 
	{
	// if the iframe sub-element does not exist yet, create it
	var iframe_exists = document.getElementById(div_id + '_i');
	if (iframe_exists == null)
		{
		// create iframe sub-element within the existing div element
		var contents = document.createElement('iframe');
		contents.setAttribute('id', div_id + '_i');
		contents.scrolling = scr;
		contents.frameBorder = '0';
		contents.style.width = width + 'px';
		contents.style.height = height + 'px';
		contents.src = href;
		boxdiv.appendChild(contents);
		document.body.appendChild(boxdiv);
		}
	// set the div element parameters
	boxdiv.style.position = 'absolute';
	boxdiv.style.width = width + 'px';
	boxdiv.style.height = height + 'px';
	boxdiv.style.border = borderStyle;
	boxdiv.style.backgroundColor = '#fff';
	// position div element and display
	move_box(an, boxdiv, shiftleft, shifttop);
	boxdiv.style.display='block';
    }
	else
	{
	boxdiv.style.display='none';
    }
    return false;
  }
  return false;
}


function getStyleObject(objectId) {
  // cross-browser function to get an object's style object given its id
  if(document.getElementById && document.getElementById(objectId)) {
    // W3C DOM
    return document.getElementById(objectId).style;
  } else if (document.all && document.all(objectId)) {
    // MSIE 4 DOM
    return document.all(objectId).style;
  } else if (document.layers && document.layers[objectId]) {
    // NN 4 DOM.. note: this won't find nested layers
    return document.layers[objectId];
  } else {
    return false;
  }
}

/** 
 * Sets the style.display property of the given object to either display or
 * un-display the object.  Displaying the object means the object will take
 * up its allotted space in its container; undisplaying means it will take up
 * no space.
 */
function changeObjectDisplay(objectId, newDisplay) {
  // get a reference to the cross-browser style object and make sure the object exists
  var styleObject = getStyleObject(objectId);
  if(styleObject) {
    styleObject.display = newDisplay;
    return true;
  } else {
    // we couldn't find the object, so we can't change its visibility
    return false;
  }
}

/**
* Hide element.
*/
function hide(elemId) {
  changeObjectDisplay(elemId, 'none');
}

/**
* Show element.
*/
function show(elemId){
  changeObjectDisplay(elemId, '');
}

/** 
 * Toggles the style.display property of the given object to either display or
 * un-display the object. 
 * Example implementation:
 * <a href="#" onclick="toggleObjectDisplay('iwb_dest_checklist'); return false;" title="View and select individual properties to search">On or Off</a>
 */
function toggleObjectDisplay(objectId) {
  // get a reference to the cross-browser style object and make sure the object exists
  var styleObject = getStyleObject(objectId);
  if(styleObject) 
  {
    if(styleObject.display=='none') 
 	 {
	  styleObject.display = '';
	  return true;
	 }
	else
	 {
	  styleObject.display = 'none';
	  return true;
	 }
  } else {
    // we couldn't find the object, so we can't change its visibility
    return false;
  }
}

/** 
 * Toggles the images.src property of the images given by src_element
 * Always use "this" as the first variable
 * Example implementation:
 * <a href="#" onclick="toggleObjectDisplay('iwb_dest_checklist'); toggleImage(this,'toggle_hide_properties_link.gif','toggle_show_properties_link.gif'); return false;" title="View and select individual properties to search">On or Off</a>
 */
function toggleImage(src_element, imgSrc1, imgSrc2) {
  // get a reference to the image source object and make sure it exists
	image = src_element.getElementsByTagName('img')[0];
  if(image) 
  {
  	image.src = image.src.match(imgSrc1) ? imgSrc2 : imgSrc1;
		return true;
  } else {
    // we couldn't find the image, so we can't change its source
    return false;
  }
}

// a href="JavaScript:openWindow('height=475,width=700,menubar=1,resizable=yes,scrollbars=yes,location=no','url','windowname')"
function openWindowWithSpecs(url,windowname,specs)
{window.open(url, windowname, specs)}