/*
		dw_util.js
		utility functions (and 1 method for dynObj)
		version date: August 2002
		
		functions for getting window dimensions and scroll amount
		from http://13thparallel.com/?issue=2002.06&title=viewport
				
		This code is from Dynamic Web Coding 
    www.dyn-web.com 
    See Terms of Use at http://www.dyn-web.com/bus/terms.html
    Permission granted to use this code 
    as long as this entire notice is included.		
*/

// center in container (window or outer layer)
// NOTE: uses this.width/height properties of dynObj
// so make sure those have been obtained/set appropriately
// for type of content!!!!
dynObj.prototype.centerIn = function(outer) {
	var outWd, outHt, inWd, inHt, x, y;
	if (eval(outer)==window) {
		outWd=getWinWidth();
		outHt=getWinHeight();
	} else {
		outWd=outer.width;
		outHt=outer.height;
	}
	inWd=this.width;
	inHt=this.height;
	x=Math.round((outWd-inWd)/2);
	y=Math.round((outHt-inHt)/2);
	this.shiftTo(x,y);
}

// gets rendered height/width 
// for ns4, pass reference to layer. 
// for others, pass reference to layer or
// id of html element containing content (depends...)
function getWidth(obj,id) {
	var wd=0;
	if (document.getElementById||document.all) {
		var elem;
		if (id)	elem = (document.getElementById)? document.getElementById(id): document.all[id];
		else elem = obj;
		if (elem.offsetWidth) wd = elem.offsetWidth;
  } else if (obj.document) wd = obj.document.width;	// ns4
	return wd;
}

function getHeight(obj,id) {
	var ht=0;
	if (document.getElementById||document.all) {
    var elem;
		if (id)	elem = (document.getElementById)? document.getElementById(id): document.all[id];
		else elem = obj;
		if (elem.offsetHeight) ht = elem.offsetHeight;
  } else if (obj.document) ht = obj.document.height;	// ns4
	return ht;
}

// credit to http://www.13thparallel.com for the following 4 functions
// see http://13thparallel.com/?issue=2002.06&title=viewport
// returns width of window
function getWinWidth() {
	var winWd = 0;
	if (document.documentElement && document.documentElement.clientWidth) 
		winWd = document.documentElement.clientWidth;
	else if (document.body && document.body.clientWidth) 
		winWd = document.body.clientWidth;
	else if (document.body && document.body.offsetWidth) 
		winWd = document.body.offsetWidth; // ns6
	else if (window.innerWidth) winWd = window.innerWidth-18;
	return winWd;
}

// returns height of window
function getWinHeight() {
	var winHt = 0;
	if (window.innerHeight) winHt = window.innerHeight-18;
	else if (document.documentElement && document.documentElement.clientHeight) 
		winHt = document.documentElement.clientHeight;
	else if (document.body && document.body.clientHeight) 
		winHt = document.body.clientHeight;
	return winHt;
}	

// returns amount of vertical scroll
function getScrollY() {
	var scroll_y = 0;
	if (document.documentElement && document.documentElement.scrollTop)
		scroll_y = document.documentElement.scrollTop;
	else if (document.body && document.body.scrollTop) 
		scroll_y = document.body.scrollTop; 
	else if (window.pageYOffset)
		scroll_y = window.pageYOffset;
	else if (window.scrollY)
		scroll_y = window.scrollY;
	return scroll_y;
}

// returns amount of horizontal scroll
function getScrollX() {
	var scroll_x = 0;
	if (document.documentElement && document.documentElement.scrollLeft)
		scroll_x = document.documentElement.scrollLeft;
	else if (document.body && document.body.scrollLeft) 
		scroll_x = document.body.scrollLeft; 
	else if (window.pageXOffset)
		scroll_x = window.pageXOffset;
	else if (window.scrollX)
		scroll_x = window.scrollX;
	return scroll_x;
}


//---------------------------------------------------------------------------
// Utility routines used by various javascript functions to create new querys
// from existing urls.  If current browser url does not reflect current query,
// these functions are not useful (since they are not aware of the current
// application state.)  These functions are used sparingly in the reference
// implementation only in conjunction with modules that contain form element.
//---------------------------------------------------------------------------

function ConstructURL(url,removeterms,addterms) {
	var params = BuildURLArray(url);
	var newurl = "";
	for (var i=0; i<params.length; i++) {
		val = CheckArray(removeterms,params[i][0]);
		if (val == -1)
			newurl = newurl + "&" + params[i][0] + "=" + params[i][1];
	}
	for (var i=0; i<addterms.length; i++) {
		newurl = newurl + "&" + addterms[i];
	}
	if (newurl.length > 0) {
		newurl = newurl.substr(1);
		newurl = "?"+newurl;
	}
	else {
		newurl = "?N="+eneroot;
	}
	return newurl;
}

function BuildURLArray(oldurl) {
	var returnArray = new Array();
	var url;
	if (oldurl == "CURRENTURL")
		url = location.search;
	else if (oldurl == "BLANKURL") {
	}
	else {
		var tokens = oldurl.split("?");
		url = "?"+tokens[1];
	}
	if (url) {
		url = url.substr(1);
		var params = url.split("&");
		for (var i=0; i<params.length; i++) {
			var param = params[i].split("=");
			returnArray[i] = param;
		}
	}
	return returnArray;
}

function CheckArray(removeterms,checkterm) {
	for (var i=0; i<removeterms.length; i++) {
		if (removeterms[i] == checkterm)
			return 1;
	}
	return -1;
}

function GetValue(url, term) {
	var params = BuildURLArray(url);
	for (var i=0; i<params.length; i++) {
		if (params[i][0] == term) {
			return params[i][1];
		}
	}
	return -1;
}

// Client Side Browser Detection Code
var agt=navigator.userAgent.toLowerCase();
var is_mac    = (agt.indexOf("mac")!=-1);
var is_win	= ( (agt.indexOf("win")!=-1) || (agt.indexOf("16bit")!=-1) );
var is_major = parseInt(navigator.appVersion);
var msie_vers_start = agt.indexOf("msie")+5;
var msie_real_vers = parseFloat(agt.substring(msie_vers_start, msie_vers_start+3));
var is_linux = (agt.indexOf("inux")!=-1);
var is_nav  = ((agt.indexOf('mozilla')!=-1) && (agt.indexOf('spoofer')==-1) && (agt.indexOf('compatible') == -1) && (agt.indexOf('opera')==-1) && (agt.indexOf('webtv')==-1));
var is_nav6 = (is_nav && (agt.indexOf('netscape6') != -1));
var is_nav4up = (is_nav && (is_major >= 4));
var is_nav4 = (is_nav && (is_major == 4));
var is_ie   = (agt.indexOf("msie") != -1);
var is_ie3  = (is_ie && (is_major < 4));
var is_ie4  = (is_ie && (is_major == 4) && (msie_real_vers < 5));
var is_ie4up  = (is_ie  && (is_major >= 4));
var is_ie5up  = (is_ie  && !is_ie3 && !is_ie4);
