/* WB.JS
   Browser detection and size utilities.

    Version Information:
     20010426a = Initial production release.
	 20020520a = Redefined browser type identification to support Netscape 6, Netscape 4.7 and Internet Explorer 5 & 6.
	 20020523a = Re-added isIE and isNS for compatibility with other scripts.
	 20020530a = Modified the get size function due to Netscape 4.7 error.
	 20021122a = Added additional options to winSize.
	 20050420a = Added getCoords function.
	 20050420b = Added getObjRef function.
	 20050502a = Added display settings to toggleText function.
	 20060202a = Updated getSize function to support more recent IE versions.

Copyright © 2006, Land Information Access Association
*/

/* Initialize Variables */
var styleObj;
var conHide, conShow;
var winX, winY;
var isIE = 0;
var isNS = 0;

/* Get Browser Name and Version */
var browName = (navigator.appName);
var browVer = parseFloat(navigator.appVersion);
switch (browName) {
	case 'Microsoft Internet Explorer':
		isIE = 1;
	break
	case 'Netscape':
		isNS = 1;
	break
}
//alert(browName + browVer);

/* Assign Variables Based on Browser Capability */
function getStyleObj() {
	if (document.layers) {
		styleObj = 'document.~name~';
		conHide = 'hide';
		conShow = 'show';
	} else if (document.all) {
		styleObj = 'document.all.~name~.style';
		conHide = 'hidden';
		conShow = 'visible';
	} else if (document.getElementById) {
		styleObj = 'document.getElementById("~name~").style';
		conHide = 'hidden';
		conShow = 'visible';
	}
}

/* Get Object Reference by Name */
function getObjRef(objName) {
	if (document.layers) {
		return eval('document.' + objName);
	} else if (document.all) {
		return eval('document.all.' + objName);
	} else if (document.getElementById) {
		return eval('document.getElementById("' + objName + '")');
	}
}

/* Get Window Size */
function getSize() {
  winX = 0, winY = 0;
  if (typeof(window.innerWidth) == 'number') {
    //Non-IE
    winX = window.innerWidth;
    winY = window.innerHeight;
  } else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
    //IE 6+ in 'standards compliant mode'
    winX = document.documentElement.clientWidth;
    winY = document.documentElement.clientHeight;
  } else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
    //IE 4 compatible
    winX = document.body.clientWidth;
    winY = document.body.clientHeight;
  }
  //alert('Window Size: ' + winX + ',' + winY);
}

function showSize() {
	getSize();
	document.writeln("<input type='hidden' name='Width' value='" + winX + "'>");
	document.writeln("<input type='hidden' name='Height' value='" + winY + "'>");
	//var messAge = "Browser: " + browName + " " + browVer + ", Width: " + winX + ", Height: " + winY;
	//alert(messAge);
}

/* Get Window Size */
function winSize(getParent){
	//Use Parent Frame
	if (getParent == 1) {
		if (document.body.clientWidth) {
			winX = parent.document.body.clientWidth;
			winY = parent.document.body.clientHeight;
		}else if (isNS){
			winX = parent.window.innerWidth;
			winY = parent.window.innerHeight;
		}
	//Use Opener
	} else if (getParent == 2) {
		if (document.body.clientWidth) {
			winX = opener.document.body.clientWidth;
			winY = opener.document.body.clientHeight;
		}else if (isNS){
			winX = opener.innerWidth;
			winY = opener.innerHeight;
		}
	//Get maximum window size
	} else if (getParent == 3) {
		winX = screen.availWidth;
		winY = screen.availHeight;
	//Get current window size
	} else {
		getSize();
	}
	//var messAge = "Browser: " + browName + " " + browVer + ", Width: " + winX + ", Height: " + winY;
	//alert(messAge);
}

function toggleText(spanName, howTo) {
	getStyleObj();
	var theSpan = eval(styleObj.replace("~name~",spanName));
	if (theSpan) {
		var whatVis = theSpan.visibility
		//alert(whatVis);
		if (howTo == 0) {
			theSpan.visibility = conHide;
			if (theSpan.display) theSpan.display = 'none';
		} else if (howTo == 1) {
			theSpan.visibility = conShow;
			if (theSpan.display) theSpan.display = 'block';
		} else if (whatVis == conShow || whatVis == "" || whatVis == "inherit") {
			theSpan.visibility = conHide;
			if (theSpan.display) theSpan.display = 'none';
		} else {
			theSpan.visibility = conShow;
			if (theSpan.display) theSpan.display = 'block';
		}
	}
}

//getCoords returns an array of screen coordinates (relative to the BODY tag)
//for an object objRef in the format (Top, Right, Bottom, Left)
function getCoords(objRef) {
	var objParent = null;
	var intX = 0;
	var intY = 0;
	var intWidth = objRef.offsetWidth;
	var intHeight = objRef.offsetHeight;
	var objList;
    // Walk up the document tree until we find the BODY
    // and add the distance from the parent to our counter.
	do {
		intX += objRef.offsetLeft
		intY += objRef.offsetTop
		if (objRef.offsetParent) {
			objParent = objRef.offsetParent.tagName;
			objRef = objRef.offsetParent
		} else { objParent = 'BODY' }
	} while(objParent != 'BODY')
	return new Array(intY, intX + intWidth, intY + intHeight, intX);
}
