function HideContent( d ) {
    if ( d.length < 1 ) { return; }
    document.getElementById( d ).style.display = "none";
}
function ShowContent( d ) {
    if ( d.length < 1 ) { return; }
    document.getElementById( d ).style.display = "block";
}
function ReverseContentDisplay( d ) {
    if ( d.length < 1 ) { return; }
    if ( document.getElementById( d ).style.display == "none" ) { document.getElementById(d).style.display = "block"; }
    else { document.getElementById( d ).style.display = "none"; }
}

function ReplaceHTML( HTMLID, HTMLBuffer ) {
  //  alert('ReplaceHTML:   HTMLID =|' + HTMLID + '|   HTMLBuffer=|' + HTMLBuffer + '|');

  if (! HTMLID )
	alert( "ReplaceHTML: error - HTMLID should not be null" );
  document.getElementById( HTMLID ).innerHTML = HTMLBuffer;
}

function pauseRotation( pauseFlag ) {
  if ( pauseFlag.count != 0 ) {
    pauseFlag.count = 0; 
  } else {
    pauseFlag.count++;
  }
}

function RotateNewsItems( timeOutPeriod ) {
  if ( newsItems.length == 1 )
	return 1;					// only rotate if there is more than one item in the array
  if ( pauseNewsFlag.count ) {
    ShowNextItem( newsItems );
  }
  setTimeout ( "RotateNewsItems( " + timeOutPeriod + " )", timeOutPeriod );
}

function RotatePromoItems( timeOutPeriod ) {

  /*
  // HACK to make the animated gif item rotate for longer period than the other item
  // We want to have the timeOutPeriod be either 5 seconds or 13 seconds (note: times are measured in miliseconds)
  if (timeOutPeriod < 5000 && timeOutPeriod > 0) {
	// guarantee a starting point (this means at the first timeOutPeriod for the recursive call will be 13000)
	timeOutPeriod = 13000;
  } else if ( timeOutPeriod == 13000) {
	timeOutPeriod = 5000;
  } else if ( timeOutPeriod == 5000) {
	timeOutPeriod = 13000;
  }
  // ABOVE IS A HACK. We don't want to keep this forever
  */  
  if ( promoItems.length == 1 )
	return 1;					// only rotate if there is more than one item in the array

  if ( pausePromoFlag.count ) {
    ShowNextItem( promoItems );
  }
  setTimeout ( "RotatePromoItems( " + timeOutPeriod + " )", timeOutPeriod );
}

// gets the first item in the array   Displays it   Then puts it back on the end of the array
function ShowNextItem( items ) {
  var item = items.shift();	// get the first element of the array (this removes the item)
  //  console.log ('item.toSource()=' + item.toSource());
  ReplaceHTML( item.div, item.html );
  items.push( item );		// add the item to the end of the array
}

// gets the first item in the array   Displays it   Then puts it back on the front of the array
function ShowFirstItem( items ) {
  var item = items.shift();	// get the first element of the array (this removes the item)
  //  console.log ('item.toSource()=' + item.toSource());
  ReplaceHTML( item.div, item.html );
  items.unshift( item );		// add the item to the beginning of the array
}


var newsItems        = new Array();
var pauseNewsFlag    = { count: 1 }; // keeps track of whether the news items should rotate or not (starts unpaused). Has to be an object because
                                     // objects are passed by reference whereas variables are passed by value
var promoItems       = new Array();
var pausePromoFlag   = { count: 1 }; // keeps track of whether the promo items should rotate or not (starts unpaused). Has to be an object because
                                     // objects are passed by reference whereas variables are passed by value


// goes through all tags of type sTagType with sAttributeType="sAttributeVal" 
// These tags get their sAttributeToReplace attribute replaced with sAttributeValue.
function replaceAttributes( sTagType, sAttributeType, sAttributeVal, sAttributeToReplace, sAttributeToReplaceValue ) {

  //  alert( "REPLACEATTRIBUTES called" );

  // ERROR CHECKING due to IE 7 badness
  var badIEAttributes = { 'href': 1, 'src': 1, 'datetime': 1, 'style': 1, 'enctype': 1, 'value': 1, 'onload': 1, 'onunload': 1, 'dir': 1, 'rowspan': 1, 'colspan': 1, 'maxlength': 1, 'accesskey': 1, 'tabindex': 1, 'valign': 1, 'input': 1, 'longdesc': 1 };
  if (badIEAttributes[ sAttributeType ]) { alert ( "IE doesn't support finding the value of the " + sAttributeType + " attribute" ); return false; }
  
  /* This function uses helper functions. Here's an example:
	 // changes the background of an object
	 function changeBackGroundColor( obj, sColor ) {
	 obj.style.backgroundColor = sColor;
	 return false;
	 }
	 //
	 function changeInputEventsInit() {
	 // onMouseOver changes background to light yellow
	 // onMouseOut changes background to white
	 replaceAttributes( "input", "class", "frmTextBox", "onMouseOver", "changeBackGroundColor(this, '#FFFFD0'); return false;" );
	 replaceAttributes( "input", "class", "frmTextBox", "onMouseOut", "changeBackGroundColor(this, '#FFFFFF'); return false;" );
	 return false;
	 }

	 //Add the changeInputEventsInit() to the body onLoad event like this:
	 <body onLoad="changeInputEventsInit();">
	 OR do it in Javascript:
	 replaceObjectAttribute(document.body, "", "onload", "changeInputEventsInit();" );
  */
	if (!document.getElementsByTagName){ return; }

	// get all the tags
	var tagObjs = document.getElementsByTagName( sTagType );

	//	alert("replaceAttributes \n sTagType=" + sTagType + "\nsAttributeType=" + sAttributeType + "\nsAttributeVal=" + sAttributeVal + "\nsAttributeToReplace=" + sAttributeToReplace + "\nlength=" + tagObjs.length);

	// loop through all found tags
	for ( var i = 0; i < tagObjs.length; i++ ) {
		var tagObj = tagObjs[ i ];

	  if (i <= 0) {
		//		alert( tagObjs.toSource());
		//		alert("replaceAttributes \n sTagType=" + sTagType + "\nsAttributeType=" + sAttributeType + "\nsAttributeVal=" + sAttributeVal + "\nsAttributeToReplace=" + sAttributeToReplace + "\nlength=" + tagObjs.length);

		/*
		alert("OBJ#" + i + 
			  "\n\nREPLACEATTRIBUTES \n\nsTagType\t=\t" + sTagType + 
			  "\n\nsAttributeType\t=\t" + sAttributeType + 
			  "\n\nsAttributeVal\t=\t" + sAttributeVal + 
			  "\n\nsAttributeToReplace\t=\t " + sAttributeToReplace + 
			  "\n\n\nCOMPARISON VALS:\nsAttributeVal\t=\t" + sAttributeVal + 
			  "\n\ntagObj.getAttribute( sAttributeType )\t=\t" + tagObj.getAttribute( sAttributeType ) + 
			  "\n\ntagObj.attribute[ sAttributeType ]\t=\t" + tagObj.attributes[ sAttributeType ].value);
		*/
	  }

	  //		if ( sAttributeVal == "" || tagObj.getAttribute( sAttributeType ) == sAttributeVal ) {
	  //		if ( sAttributeVal == "" || tagObj.attributes[ sAttributeType ].value == sAttributeVal ) {
		if ( sAttributeVal == "" || tagObj.attributes[ sAttributeType ].value == sAttributeVal ) {
		  var tmp = tagObj.getAttribute( sAttributeToReplace )
			//			addEvent(tagObj, sAttributeToReplace, sAttributeToReplaceValue);

			//			document.getElementsByTagName (sTagType)[i][sAttributeToReplace] = sAttributeToReplaceValue;
			//			document.getElementsByTagName (sTagType)[i].onMouseOver = sAttributeToReplaceValue;
			//			document.getElementsByTagName (sTagType)[i].attributes[sAttributeToReplace].value = sAttributeToReplaceValue;
			//tagObj.attributes[sAttributeToReplace].value = sAttributeToReplaceValue;

			//			tagObj.setAttribute( sAttributeToReplace, sAttributeToReplaceValue );
			//		  tagObj[ sAttributeToReplace ] = sAttributeToReplaceValue;
			//		  tagObj.onmouseover = sAttributeToReplaceValue;
			//		  document.getElementsByTagName( sTagType ) [i][ sAttributeToReplace ] = sAttributeToReplaceValue;
		  if (i <= 2) {
			/*			alert( "ADDING JS CODE:\n\n tagObj\t=\t" + tagObj.toSource() +  */
			/*						alert ( +
				   "\n\ntagObj.getAttribute(" + sAttributeToReplace + ")\t=\t" + tagObj.getAttribute( sAttributeToReplace ) +
				   "\n\nsAttributeToReplace\t=\t" + sAttributeToReplace +
				   "\n\nvalue\t=\t" + sAttributeToReplaceValue
				   );
			*/
			//			alert("FOUND BEFORE=" + tmp + "\nAFTER replaceAttributes attributeVal=" + tagObj.getAttribute( sAttributeToReplace ));
		  }
	  }
	}
	return false;
}

// either accepts an object, or looks it up by the passed sID and then replaces the sElement with the sValue
function replaceObjectAttribute(Obj, sID, sAttribute, sValue) {

  if ( !Obj || typeof Obj != 'object' ) {
	Obj = document.getElementById( sID );
  }
  if ( Obj ) {
	// addEvent( Obj, sAttribute, sValue); 
 	Obj.setAttribute( sAttribute, sValue );
//	Obj[ sAttribute ].value = sValue;
// Obj.onLoad.value = sValue; 
//	alert ("onload=" + document.body.onLoad);
	return true;
  }
  return false;
}


function addEvent(obj, evType, fn){
 if (obj.addEventListener){
    obj.addEventListener(evType, fn, true);
    return true;
 } else if (obj.attachEvent){
    var r = obj.attachEvent("on"+evType, fn);
    return r;
 } else {
    return false;
 }
} 


// addEvent and removeEvent, designed by Aaron Moore
function addEvent1(element, listener, handler) {
  //  alert ("addEvent called\nelement\t=\t" + element + "\nlistener\t=\t" + listener + "\nhandler\t=\t" + handler)
	//if the system is not set up, set it up, and
	// store any outside script's event registration in the first handler slot
	if(typeof element[listener] != 'function' || 
	typeof element[listener + '_num'] == 'undefined'){
		element[listener + '_num'] = 0;
		if(typeof element[listener] == 'function'){
			element[listener + 0] = element[listener];
			element[listener + '_num']++;
		}
		element[listener] = function(e){
			var r = true;
			e = (e) ? e : window.event;
			for(var i = 0; i < element[listener + '_num']; i++)
				if(element[listener + i](e) === false) r = false;
			return r;
		}
	}
	//if handler is not already stored, assign it
	for(var i = 0; i < element[listener + '_num']; i++)
		if(element[listener + i] == handler) return;
	element[listener + element[listener + '_num']] = handler;
	element[listener + '_num']++;
}
function removeEvent(element, listener, handler)
{
	//if the system is not set up, or there are no handlers to remove, exit
	if(typeof element[listener] != 'function' || 
	typeof element[listener + '_num'] == 'undefined' ||
	element[listener + '_num'] == 0) return;
	//loop through handlers,
	//  if target handler is reached, begin overwriting each
	//  handler with the handler in front of it until one before the last
	var found = false;
	for(var i = 0; i < element[listener + '_num']; i++){
		if(!found)
			found = element[listener + i] == handler;
		if(found && (i+1) < element[listener + '_num'])
			element[listener + i] = element[listener + (i+1)];
	}
	//if handler was found, decrement the handler count
	if(found)
		element[listener + '_num']--;
}
