// we don't need this - it has now been DEPRECATED - miks - 08/2007 


// JAVASCRIPT MICRO-API
// these scripts are intended to provide x-browser methods for returning or setting property values for html elements 
// sources are indicated in comments before each script if not authored in-house
// compiler/editor: 	Mike Sadka
// created: 			June 2004

// ----- DETECT STANDARDS-COMPLIANT BROWSERS -----
var W3CDOM = (document.createElement && document.getElementsByTagName);
// IDENTIFY BROWSER, VERSION AND OS
// FROM: http://www.quirksmode.org/js/detect.html
// I don't like browser detection, but some browsers cannot be reliably excluded by object detection, and need to be (eg - Mac IE)
// this script may look like overkill, but is quite ingenious (see url above) and reliable
var detect = navigator.userAgent.toLowerCase();
var OS,browser,version,total,thestring;
if (checkIt('konqueror')) {
	browser = "Konqueror";
	OS = "Linux";
} 
else if (checkIt('safari')) browser = "Safari"
else if (checkIt('omniweb')) browser = "OmniWeb"
else if (checkIt('opera')) browser = "Opera"
else if (checkIt('webtv')) browser = "WebTV";
else if (checkIt('icab')) browser = "iCab"
else if (checkIt('msie')) browser = "Internet Explorer"
else if (!checkIt('compatible')) {
	browser = "Netscape Navigator"
	version = detect.charAt(8);
} 
else browser = "An unknown browser";
if (!version) version = detect.charAt(place + thestring.length);
if (!OS) {
	if (checkIt('linux')) OS = "Linux";
	else if (checkIt('x11')) OS = "Unix";
	else if (checkIt('mac')) OS = "Mac"
	else if (checkIt('win')) OS = "Windows"
	else OS = "an unknown operating system";
}
function checkIt(string) {
	place = detect.indexOf(string) + 1;
	thestring = string;
	return place;
}
// prevent Mac IE from running scripts
if((browser == 'Internet Explorer') && (OS == 'Mac'))  W3CDOM = false;

// ----- END DETECT STANDARDS-COMPLIANT BROWSERS -----



// ----- CHECK IF OBJECT EXISTS ----- 
function exists(obj) {
// obj argument can be object reference (eg - document.forms[0]) or ID string
	if (typeof obj == 'string') {
		if (document.getElementById(obj) == null) {
			return false;
		} else {
			return true;
		}
	} else {
		if (typeof obj == 'undefined') {
			return false;
		} else {
			return true;
		}
	}
}

// ----- HIDE / SHOW OBJECT ----- 
function hide(obj) {
	document.getElementById(obj).style.visibility = 'hidden';
}
function show(obj) {
	document.getElementById(obj).style.visibility = 'visible';
}


// ----- GET X-BROWSER REFERENCE TO OBJECT ----- 
// !!! PROBABLY REDUNDANT AS NOT NEEDED IN STANDARDS-COMPLIANT BROWSERS !!!
// FROM: http://www.quirksmode.org/js/dhtmloptions.html 
// 	- an expanded version which can deal with NN4x layers is also available at the same URL
// 	- should be rendered redundant when we no longer need to support IE5 - miks
// USAGE: var x = new getObj('layername');
// 	- x.style.PROPERTYNAME [= VALUE]
// 	- x.obj.PROPERTYNAME [= VALUE]
//	- x.obj.getXPos()
// EXPLANATION: the function creates a new JavaScript object x. It goes through all possible DOMs and if it finds the one that the browser supports it sets this.obj and this.style. The this keyword makes sure that the two properties obj and style are added to the new object x.  Style properties can be read or set using the syntax 'x.style.PROPERTYNAME = VALUE'.  Object properties can be read or set  using 'x.obj.PROPERTYNAME = VALUE'.  
//	- NOTE that the object created by the function is NOT an object reference but a container for the specified objects properties and methods (see http://www.quirksmode.org/js/dhtmloptions.html). Becuase the scripts which return position info will not accept a reference of this kind, they are attached to the getObj object as methods getXPos and getYPos.
// possibly attach top and left functions as methods here, then don't have prob that cannot pass a new getObj to those functions - DONE

function getObj(name) 
// name argument is string representing element ID
{
  // identify DOM, and set references accordingly
  if (document.getElementById)
  {
  	this.obj = document.getElementById(name);
	this.style = document.getElementById(name).style;
  }
  else if (document.all)
  {
	this.obj = document.all[name];
	this.style = document.all[name].style;
  }
  else if (document.layers)
  {	// we could drop this...? - miks
   	this.obj = document.layers[name];
   	this.style = document.layers[name];
  }
  // attach methods to getObj object
  this.obj.getXPos = DL_GetElementLeft;
  this.obj.getYPos = DL_GetElementTop;
}

// ----- GET TOP AND LEFT POSITIONS OF ELEMENT ----- 
// FROM: http://www.webreference.com/dhtml/diner/realpos2/2.html
// 	- these functions should work EXCEPT where elements are nested within tables (which we intend not to do)
// 	- expanded versions which will place elements accurately even within nested tables, in both IE and mozilla-based browsers, are available from: http://www.webreference.com/dhtml/diner/realpos2/10.html 
// EXPLANATION: function can be called explicitly in the usual way or attached to elements as a method
// USEAGE:
// As a METHOD:
//	elementReference.getTrueXPosition = DL_GetElementLeft;
//	elementReference.getTrueYPosition = DL_GetElementTop;
//	var nMyElementsTrueXPosition = elementReference.getTrueXPosition();
//	var nMyElementsTrueYPosition = elementReference.getTrueYPosition();
// As a FUNCTION:
//	var nMyElementsTrueXPosition = DL_GetElementLeft(elementReference);
//	var nMyElementsTrueYPosition = DL_GetElementTop(elementReference);

// --- LEFT ---
function DL_GetElementLeft(eElement)
// eElement argument is object reference
{
    if (!eElement && this)                       // if argument is invalid
    {                                            // (not specified, is null or is 0)
        eElement = this;                         // and function is a method
    }                                            // identify the element as the method owner
    
    var nLeftPos = eElement.offsetLeft;          // initialize var to store calculations
    var eParElement = eElement.offsetParent;     // identify first offset parent element  
    while (eParElement != null)
    {                                            // move up through element hierarchy
        nLeftPos += eParElement.offsetLeft;      // appending left offset of each parent
        eParElement = eParElement.offsetParent;  // until no more offset parents exist
    }
    return nLeftPos;                             // return the number calculated
}
// --- TOP ---
function DL_GetElementTop(eElement)
// eElement argument is object reference
{
    if (!eElement && this)
    {
        eElement = this;
    }

    var nTopPos = eElement.offsetTop;
    var eParElement = eElement.offsetParent;
    while (eParElement != null)
    {
        nTopPos += eParElement.offsetTop;
        eParElement = eParElement.offsetParent;
        //alert(eElement.id + ' : ' + nTopPos);
    }
    return nTopPos;
}
// --- ALIASES FOR OBJECT POSITION SCRIPTS ---
// forceObjRef call allows elem argument to be either a string or an object reference
function getTop(elem) { return DL_GetElementTop(forceObjRef(elem)); }
function getLeft(elem) { return DL_GetElementLeft(forceObjRef(elem)); }

// ----- FORCE PASSED ELEMENT ID TO OBJECT REFERENCE ----- 
function forceObjRef(elem) {
// accepts a string or object reference - converts strings to object refs
// allows developers to pass strings or object refs to functions
	if (typeof elem == 'string') {
		// elem = new getObj(elem); - this would be more consistent but doesn't return a straight object reference
		elem = document.getElementById(elem);
	}
	return elem;
}

// ----- GET EVENT COORDINATES ----- 
// FROM: http://www.quirksmode.org/js/dhtmloptions.html 
function getEventCoords(e,coord)
{
	var posx = 0;
	var posy = 0;
	if (!e) var e = window.event;
	if (e.pageX || e.pageY)
	{
		posx = e.pageX;
		posy = e.pageY;
	}
	else if (e.clientX || e.clientY)
	{
		posx = e.clientX + document.body.scrollLeft;
		posy = e.clientY + document.body.scrollTop;
	}
	if (coord == 'x') return posx;
	if (coord == 'y') return posy;
}

// ----- SUPERIMPOSE ONE OBJECT ON ANOTHER ----- 
function superimpose(obj1, obj2) {
// obj1 and obj2 are strings repersenting div IDs
// obj2 will be superimposed over obj1

	// get superimposition coordinates
	targetObj = document.getElementById(obj1);
	topSPos = getTop(targetObj);
	leftSPos = getLeft(targetObj);
	
	// superimpose obj2 on obj1
	otherObj = document.getElementById(obj2);
	otherObj.style.top = topSPos + 'px';
	otherObj.style.left = leftSPos + 'px';
	
	//alert('top: ' + otherObj.style.top);
	//alert('left: ' + otherObj.style.left);
}

// ----- GET ELEMENTS BY CLASS NAME -----
// FROM: http://www.dynamicdrive.com/dynamicindex17/switchcontent_dev.htm
function getElementsByClassName(classname) {
	ccollect=new Array();
	var inc=0;
	var alltags = document.all? document.all : document.getElementsByTagName("*");
	for (i=0; i<alltags.length; i++){
		//if (alltags[i].className==classname) ccollect[inc++]=alltags[i];				// this only handles single classes, not space-seperated
		if (alltags[i].className != undefined) {
			if (alltags[i].className.indexOf(classname) > -1) ccollect[inc++]=alltags[i];	// but this works - miks (except in Opera...until added the test for undefined)
		}
	}
	return ccollect;
}

// ----- TRIM LEADING/TRAILING SPACES (adapt for other whitespace)  -----
function trim(strText) {
    // this will get rid of leading spaces
    while (strText.substring(0,1) == ' ') strText = strText.substring(1, strText.length);
    // this will get rid of trailing spaces
	while (strText.substring(strText.length-1,strText.length) == ' ') strText = strText.substring(0, strText.length-1);
	return strText;
}

// ----- PAUSE JAVASCRIPT EXECUTION -----
// not sure how useful this will be - but sometimes a delay can help fix buggy js behaviour
// script (C) 2003 Sean McManus
// FROM:  http://www.sean.co.uk
function pausecomp(millis) {
	d = new Date();		 	//today's date
	while (1) {
		mill = new Date() ;	// date now
		diff = mill-d; 		// difference in milliseconds
		if( diff > millis ) {break;}
	}
}



///////////////////////////////
// ALTERNATIVE COORDINATE FUNCTION - possibly not as reliable (needed addition of (objParent != 'HTML') to handle absolutely positioned divs (for which parent is not BODY, in IE at least) - miks
function getElementPos(elem,coord) {
  var objItem = document.getElementById(elem);
  var objParent = null;
  var intX = 0;
  var intY = 0;
  do
   { // Walk up our document tree until we find the body
     // and add the distance from the parent to our counter.
    intX += objItem.offsetLeft;
    intY += objItem.offsetTop;
    objParent = objItem.offsetParent.tagName;
    objItem = objItem.offsetParent;
   }
  while((objParent != 'BODY') && (objParent != 'HTML'));
  if (coord == 'x') return intX;
  if (coord == 'y') return intY;
}
///////////////////////////////
