function expandCollapse( ) {
	for (var i=0; i<expandCollapse.arguments.length-1; i++) {
		var element = document.getElementById(expandCollapse.arguments[i]);
		element.style.display = (element.style.display == "none") ? "block" : "none";
	}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

 

 function toggle(link,  Id) {
  var lText = link.innerHTML; var d = document.getElementById (Id); 
 if (d.style.display =='none') { link.innerHTML = 'close'; d.style.display = 'block'; }
 else { link.innerHTML = 'more'; d.style.display = 'none'; } }

 
function ChangeClass (obj, className)
{
	obj.className = className;
	return true;
}

function GoToURL(url)
{
	window.top.location = url;
	return true;
}

function whatIsInside (obj)
{
  var txt = '';
  for (var prop in obj) {
    txt += '(' + prop + ' | ' + obj [prop] + ') ';
  }
  return txt;
}

function whatIsInsideId (id)
{
	if (!id) return false;
	var obj = document.getElementById (id);
	if (!obj) return false;
	return whatIsInside (obj);
}

function toBool (boo) {
  if (boo) return true;
  else return false;
}

function isMozillaEngine ()
{
  // return ! toBool (document.all);
  return isGecko();
}

function isMozilla ()
{
  return isMozillaEngine() && ! isPhoenix();
}

function isPhoenix ()
{
  return toBool( navigator.userAgent .match ( /Firefox|Firebird|Phoenix/i ) );
}

function isOpera ()
{
  return toBool( navigator.userAgent .match ( /Opera/i ) );
}

function isGecko ()
{
	return toBool( navigator.userAgent .match ( /\) Gecko\// ) );
}

// TBD: recognize it better
function isMSIE ()
{
	return !isMozillaEngine() && !isOpera();
}

// Return a string representing revision if available.
function getRevision ()
{
	var reRevision = /rv:(\d+)\.(\d+)/;
	var mm = navigator.userAgent .match (reRevision);
	if ('undefined' == typeof mm || null == mm || 1 >= mm.length) {
		return null;
	}
	return {major: mm [1], minor: mm [2]};
}

// NOTE: returns true if buggy geometry (width in pixels etc).
// Q&D. Possible improvement: find which version are affected from a reliable source.
function isWrongGeom ()
{
	// SOME STATS
	
	// WRONG
	// Mozilla/5.0 (Windows; U; Windows NT 5.2; en-US; rv:1.6) Gecko/20040113
	// Mozilla/5.0 (Windows; U; Windows NT 5.2; en-US; rv:1.5) Gecko/20031007
	// Mozilla/5.0 (Windows; U; Windows NT 5.2; en-US; rv:1.4) Gecko/20030624 Netscape/7.1 (ax)
	
	// OK
	// Mozilla/5.0 (Windows; U; Windows NT 5.2; en-US; rv:1.7) Gecko/20040707 Firefox/0.8
	// Mozilla/5.0 (Windows; U; Windows NT 5.2; en-US; rv:1.7) Gecko/20040614 Firefox/0.8
	// Opera/7.50 (Windows NT 5.2; U) [en]
	// Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.1.4322)
	// Mozilla/5.0 (Windows; U; Windows NT 5.2; en-US; rv:1.7.2) Gecko/20040804 Netscape/7.2 (ax)

	// THE WORK

	// Only mozilla platform seems to be affected.
	if (! isGecko()) {
		return false;
	}

	// Only certain early versions seem to be affected.
	var revision = getRevision();
	if (! revision) {
		return true;
	}
	if (revision.major == '1' && revision.minor >= '7') {
		return false;
	}
	return true;
}

/* Place the item so that its top and bottom margins on the page look nice.
   Can position on top, in the middle, by the golden ratio (see code), or whatever.
 */
function place_the_item (itemName)
{
  var el_item = document.getElementById (itemName);
  if (!el_item) return true;

  var how2position_default= 'golden';
  var how2position = how2position_default;
  if (arguments.length > 1) {
	how2position = arguments [1];
  }
  switch (how2position) {
  case 'golden':
  case 'top':
  case 'topleft':
	break;
  default:
    how2position = how2position_default;
  }

  var itemtop_min;
  var itemleft_min;
  switch (how2position) {
  case 'golden':
  default:
	itemtop_min = 20;
	itemleft_min = 5;
	break;
  case 'top': case 'topleft':
	itemtop_min = 0;
	itemleft_min = 5;
	break;
  }
  /* Some versions of Mozilla do not know el_item.clientHeight, what gives? */
  
  if (isWrongGeom ()) {
	el_item.style.marginTop = itemtop_min;
	return true;
  }

  // el_item.style.top = (document.body.clientHeight - el_item.clientHeight -1) /2; /* half */
  // el_item.style.top = itemtop_min; /* top */
  // el_item.style.top = (document.body.clientHeight - el_item.clientHeight) * 0.382; /* golden */
  
  el_item.style.position = 'absolute';

  el_item.style.top = '800px';

  /* Use the proper parameters */
  if (el_item.clientWidth && el_item.clientWidth > 0) {
	el_width = el_item.clientWidth;
  } else if (el_item.width && el_item.width > 0) {
	el_width = el_item.width;
  } else {
	el_width = 0; // or null?
  }
  if (document.body.clientWidth && document.body.clientWidth > 0) {
	doc_width = document.body.clientWidth;
  } else if (document.width && document.width > 0) {
	doc_width = document.width;
  } else {
	doc_width = 0; // or null?
  }
  
  if (el_item.clientHeight && el_item.clientHeight > 0) {
	el_height = el_item.clientHeight;
  } else if (el_item.height && el_item.height > 0) {
	el_height = el_item.height;
  } else {
	el_height = 0; // or null?
  }
  
  /* Horizontal position */
  switch (how2position) {

  case 'topleft':
	el_item.style.left = 1;
	break;
  
  case 'top': case 'golden': default:
	el_item.style.left = (doc_width - el_width -1) /2; /* centered */
	//el_item.style.left = el_item.style.left + 'px';
	  
	el_item.style.left = parseInt (el_item.style.left);
	if ( parseInt (el_item.style.left) < parseInt (itemleft_min) ) {
		el_item.style.left = parseInt (itemleft_min);
	}
  }
  
  /* Vertical position */
  switch (how2position) {
  
  case 'golden': default:
	el_item.style.top = (document.body.clientHeight - el_height) * 0.382; /* golden */

	el_item.style.top = parseInt (el_item.style.top);
	if ( parseInt (el_item.style.top) < parseInt (itemtop_min) ) {
		el_item.style.top = parseInt (itemtop_min);
	}
	
	break;
  case 'top': case 'topleft':
	el_item.style.top = 0;

	break;  
  } // switch how2position
  
  return true;
}

function getElementPositionById (elemID) {
	if (!elemID) {
		return getElementPosition (null);
	}
    var elem = document.getElementById (elemID);
    return getElementPosition (elem);
}

function getElementPosition (elem) {
	if (!elem) {
		return {left:null, top:null};
	}
    var offsetLeft = 0;
    var offsetTop = 0;
    while (elem) {
        offsetLeft += elem.offsetLeft;
        offsetTop += elem.offsetTop;
        elem = elem.offsetParent;
    }    
    return {left:offsetLeft, top:offsetTop};
}

function getWindowGeom () {
	var doc_width, doc_height;
	
	if (document.body.clientWidth && document.body.clientWidth > 0) {
		doc_width = document.body.clientWidth;
	} else if (document.width && document.width > 0) {
		doc_width = document.width;
	} else {
		doc_width = 0; // or null?
	}

	if (document.body.clientHeight && document.body.clientHeight > 0) {
		doc_height = document.body.clientHeight;
	} else if (document.height && document.height > 0) {
		doc_height = document.height;
	} else {
		doc_height = 0; // or null?
	}

	return {width: doc_width, height: doc_height}
}

function getElementGeom (el) {
	if (!el) {
		return {left:null, top:null};
	}

	var el_width, el_height;
	
	if (el.clientWidth && el.clientWidth > 0) {
		el_width = el.clientWidth;
	} else if (el.width && el.width > 0) {
		el_width = el.width;
	} else {
		el_width = 0; // or null?
	}

	if (el.clientHeight && el.clientHeight > 0) {
		el_height = el.clientHeight;
	} else if (el.height && el.height > 0) {
		el_height = el.height;
	} else {
		el_height = 0; // or null?
	}

	return {width: el_width, height: el_height};
}

function getElementGeomById (elemID) 
{
	if (!elemID) {
		return getElementGeom (null);
	}
    var elem = document.getElementById (elemID);
    return getElementGeom (elem);
}

function OpenPopupWindow (url, title, width, height) {
	return window.open (url, title,
		'toolbar=no,location=no,status=no,menubar=yes,scrollbars=yes,resizable=no,' 
		+ 'width=' + width + ',height=' + height + ',left=20,top=20'
	);
}

// Concatenate parameters as arrays.
// Replacement for Array.concat()
function unite ()
{
  var ar = new Array ();
  var ii, aa;
  for (ii = 0; ii < arguments.length; ++ii) {
    switch (typeof arguments [ii]) {
    case 'object':
      for (aa = 0; aa < arguments [ii].length; ++aa) {
        ar [ar.length] = arguments [ii][aa];
      }
      break;
    case 'string': case 'number':
      ar [ar.length] = arguments [ii];
      break;
    default:
      // undefined, function... what else?
      break;
    }
  }
  return ar;
}

// Get the element referred to either by reference or by its id.
// Null if none.
function getElement (elOrId)
{
	if ('undefined' == typeof elOrId || !elOrId) return null;
	if ('object' == typeof elOrId) return elOrId;
	if ('string' == typeof elOrId) return document.getElementById (elOrId);
	if ('function' == typeof elOrId) return elOrId (this); // hmm
	return null;
}

// Return if el is in array ar.
function isIn (el, ar)
{
	if (!el || !ar) return false;
	var ii;
	for (ii = 0; ii < ar.length; ++ii) {
		if (el == ar [ii]) return true;
	}
	return false;
}

roundNumber = function (nn) {
	return (nn < 0.) ? parseInt (nn -0.5) : parseInt (nn +0.5);
}

// Input a (float) number, output the dollars and cents amount
// in format dddddd.cc (exactly 2 digits after the point).
formatDollars = function (dd) {
	var ff = parseFloat (dd); // No parseDouble is avaiable
	ff = 100. * ff; // Cents
	ff = roundNumber (ff); // Round
	ff = ff.toString ();
	
	var lenlen = ff .length ;
	if (lenlen > 2) {
		ff = ff.substring (0, lenlen - 2) +'.' + ff.substring (lenlen - 2, lenlen);
	} else if (2 == lenlen) {
		ff = '0.'+ ff;
	} else {
		ff = '0.0'+ ff;
	}
	
	return (ff);
};
