//*****************************************************************************
//*
//*		UTILITY.JS
//*
//*		Javascript utility functions.
//*
//*		Requires:  browser_detect.js
//*
//*		(C)2004-2007 CarboniSoftware, LLC (except where noted)
//*
//*		If you understand this code, feel free to use, modify, or disect at will
//*		as there are no radically new or complicated ideas here.  If you don't
//*		understand this code, please leave as is with author reference.
//*
//*
//*****************************************************************************

var nav = (navigator.appName=="Netscape") ? true : false;
var mac = (navigator.appVersion.indexOf("Mac") != -1);
var ie = (document.all) ? true : false;

//*****************************************************************
//* Hashtable definitions
	Hashtable.prototype.hash = null;
	Hashtable.prototype.keys = null;
	Hashtable.prototype.location = null;

	function Hashtable()
	{
		this.hash = new Array();
		this.keys = new Array();

		this.location = 0;
	}

	Hashtable.prototype.get = function (key) {
		return this.hash[key];
	}

	Hashtable.prototype.put = function (key, value)
	{
		if (value == null)
			return null;

		if (this.hash[key] == null)
			this.keys[this.keys.length] = key;

		this.hash[key] = value;
	}
//*
//*****************************************************************

if(!document.getElementById || !document.childNodes || !document.createElement ) 
{
	alert('Your browser does not support W3C DOM functions.  You will not be able to use the features of this website correctly.  Please update your browser to a compatible version, or use a different compatible browser (example: IE 5.5+ or Netscape 6+).\n\n'+
	      'Browser information: ' + browser.name);
}

if (browser.isIE)
{
	if (!browser.isIE5up)
		alert('Your version of Internet Explorer: ' + browser.versionMajor + '.' + browser.versionMinor + ', cannot support the features of this website.  Please update Internet Explorer to at least version 5.5.');
}
else if (browser.isNS)
{
	if (!browser.isNS6up)
		alert('Your version of Netscape: ' + browser.versionMajor + '.' + browser.versionMinor + ', cannot support the features of this website.  Please update Netscape to at least version 6.0.');
}

function cursor_wait() 
{
	document.documentElement.className = 'waitCursor';
	//document.body.style.cursor = 'wait';
}

function cursor_clear() 
{
	document.documentElement.className = ''; 
	//document.body.style.cursor = 'default';
}

function setDocumentColor(color)
{
	document.body.style.backgroundColor = color;
}

function clearForm() 
{
	while (document.childNodes[0])
		document.removeChild(document.childNodes[0]);
}

function clearObject(objID)
{
	var obj = getObject(objID);
	
	if (obj)
	{
		while (obj.childNodes[0])
			obj.removeChild(obj.childNodes[0]);
	}
}

function leaveHTML(objectID)
{
	var obj = getObject(objectID);
	var iHtml = obj.innerHTML;
	
	document.body.innerHTML = iHtml;
	iHtml = document.body.outerHTML;
	
	var iHead = document.documentElement.innerHTML;
	document.write(iHead);
}

function leaveObject(objectID)
{
	var obj = getObject(objectID);
	
	if (obj)
	{
		// get the parent
		var parentObj = obj.parentNode;
		while (parentObj)
		{
			// remove all siblings
			var i=0;
			while (parentObj.childNodes[i])
			{
				if (parentObj.childNodes[i] == obj)
					i++
				else
					parentObj.removeChild(parentObj.childNodes[i]);
			}
			obj = parentObj;
			parentObj = parentObj.parentNode;
			
			if (parentObj == document)
				parentObj = null;
		}
	}
}

function setText(objID, text)
{
	clearObject(objID);
	
	var obj = getObject(objID);
	if (obj)
	{
		obj.innerHTML = text;
	}
}

function changeLocation(newLocation)
{
	//var locString = "location='"+newLocation+"'";
	//setTimeout(locString, 100);
	location=newLocation;
}

function getStyleObject(objectId) 
{
    if(document.getElementById && document.getElementById(objectId)) {
	return document.getElementById(objectId).style;
    } else if (document.all && document.all(objectId)) {
	return document.all(objectId).style;
    } else if (document.layers && document.layers[objectId]) {
	return document.layers[objectId];
    } else {
	return false;
    }
} 

function getObject(objectId)
{
    if(document.getElementById && document.getElementById(objectId)) {
	return document.getElementById(objectId);
    } else if (document.all && document.all(objectId)) {
	return document.all(objectId);
    } else if (document.layers) {
	return eval('document.'+objectId);
    } else {
	return false;
    }
}

function changeObjectVisibility(objectId, newVisibility) 
{
    var styleObject = getStyleObject(objectId);
    if(styleObject) 
    {
		styleObject.visibility = newVisibility;
		return true;
    } 
    else 
    {
		alert('no style: ' + objectId);
		return false;
    }
}

function moveObject(objectId, newXCoordinate, newYCoordinate) 
{
    var styleObject = getStyleObject(objectId);
    if(styleObject) 
    {
	 styleObject.position = 'absolute';
	 styleObject.left = newXCoordinate;
	 styleObject.top = newYCoordinate;
	 return true;
    } else {
	return false;
    }
} 

function getElementPosition(elemID) 
{
    var offsetTrail = document.getElementById(elemID);
    var offsetLeft = 0;
    var offsetTop = 0;
    while (offsetTrail) {
        offsetLeft += offsetTrail.offsetLeft;
        offsetTop += offsetTrail.offsetTop;
        offsetTrail = offsetTrail.offsetParent;
    }
    if (navigator.userAgent.indexOf("Mac") != -1 && 
        typeof document.body.leftMargin != "undefined") {
        offsetLeft += document.body.leftMargin;
        offsetTop += document.body.topMargin;
    }
    return {left:offsetLeft, top:offsetTop};
}

function findPos (objName)
{
	var oElmnt = getObject(objName);
	var loopE = "oElmnt";
	var XY = new Array;
	XY['locationX']=0;
	XY['locationY']=0;
	while((eval(loopE+".tagName")).toLowerCase()!="body"){
		loopE += ".offsetParent";
		XY['locationX'] += eval(loopE+".offsetLeft");
		XY['locationY'] += eval(loopE+".offsetTop");
	}
	return XY
}

function pause(numberMillis) 
{
    var now = new Date();
    var exitTime = now.getTime() + numberMillis;
    while (true) {
        now = new Date();
        if (now.getTime() > exitTime)
            return;
    }
}

function pointInElement(x, y, elementID)
{
	var obj = getObject(elementID);
	if (obj)
	{
		var objPos = getElementPosition(elementID);
		var aX = objPos.left;
		var aY = objPos.top;
		var aW = obj.offsetWidth;
		var aH = obj.offsetHeight;
		
		if ((x >= aX) && (x <= (aX+aW)) &&
			(y >= aY) && (y <= (aY+aH)))
			return true;
		else
			return false;
	}
	
	return false;
}

function hLineInElement(x1, x2, y, elementID)
{
	var obj = getObject(elementID);
	if (obj)
	{
		var objPos = getElementPosition(elementID);
		var aX = objPos.left-3;  // add a little more for borders
		var aY = objPos.top-3;
		var aW = obj.offsetWidth+6;
		var aH = obj.offsetHeight+6;
		
		if ( (((x1 <= aX) && (x2 >= aX)) || 
			 ((x1 <= (aX+aW)) && (x2 >= (aX+aW)))) &&
			 ((y >= aY) && (y <= (aY+aH))) )
		{
			return true;
		}
		else
			return false;
	}
	
	return false;
}

function vLineInElement(y1, y2, x, elementID)
{
	var obj = getObject(elementID);
	if (obj)
	{
		var objPos = getElementPosition(elementID);
		var aX = objPos.left-3;  // add a little more for borders
		var aY = objPos.top-3;
		var aW = obj.offsetWidth+6;
		var aH = obj.offsetHeight+6;

		if ( (((y1 <= aY) && (y2 >= aY)) || 
			 ((y1 <= (aY+aH)) && (y2 >= (aY+aH)))) &&
			 ((x >= aX) && (x <= (aX+aW))) )
		{
			return true;
		}
		else
			return false;
	}
	
	return false;
}

function trimString (str) 
{
	while (str.charAt(0) == ' ')
		str = str.substring(1);
	while (str.charAt(str.length - 1) == ' ')
		str = str.substring(0, str.length - 1);
	return str;
}

function copyElementValueToClipboard(elementID)
{
	var obj = getObject(elementID);
	if (obj)
	{
		copyToClipboard(obj.value);
	}
}

function copyToClipboard(text)
{
	if (window.clipboardData) 
	{
		window.clipboardData.setData("Text", text);
	}
	else if (window.netscape) 
	{ 
		netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
		var clip = Components.classes['@mozilla.org/widget/clipboard;1'].createInstance(Components.interfaces.nsIClipboard);
		if (!clip) return;
		
		var trans = Components.classes['@mozilla.org/widget/transferable;1'].createInstance(Components.interfaces.nsITransferable);
		if (!trans) return;

		trans.addDataFlavor('text/unicode');

		var str = new Object();
		var len = new Object();
		var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
		var copytext=text;
		str.data=copytext;
		trans.setTransferData("text/unicode",str,copytext.length*2);
		var clipid=Components.interfaces.nsIClipboard;
		if (!clip) return false;
		clip.setData(trans,null,clipid.kGlobalClipboard);
	}
	//document.ReportForm.CopyButton.value = "Copied.";				
	return false;
}

function hextorgb(n1,n2,n3,n4,n5,n6) 
{
	n1 = replacevals(n1);
	n2 = replacevals(n2);
	n3 = replacevals(n3);
	n4 = replacevals(n4);
	n5 = replacevals(n5);
	n6 = replacevals(n6);

	var returnval = ((16 * n1) + (1 * n2));
	var returnval1 = 16 * n3 + n4;
	var returnval2 = 16 * n5 + n6;

	return ((16 * n1) + (1 * n2))+","+((16 * n3) + (1 * n4))+","+((16 * n5) + (1 * n6));
}

function replacevals(n) 
{
	if (n == "a") { n = "10"; }
	if (n == "b") { n = "11"; }
	if (n == "c") { n = "12"; }
	if (n == "d") { n = "13"; }
	if (n == "e") { n = "14"; }
	if (n == "f") { n = "15"; }

	return n
}

function doBGFadeMem(elem,startRGB,endRGB,steps,intervals,powr) {
//BG Fader with Memory by www.hesido.com
	if (elem.bgFadeMemInt) window.clearInterval(elem.bgFadeMemInt);
	var actStep = 0;
	elem.bgFadeMemInt = window.setInterval(
		function() {
			elem.currentbgRGB = [
				easeInOut(startRGB[0],endRGB[0],steps,actStep,powr),
				easeInOut(startRGB[1],endRGB[1],steps,actStep,powr),
				easeInOut(startRGB[2],endRGB[2],steps,actStep,powr)
				];
			elem.style.backgroundColor = "rgb("+
				elem.currentbgRGB[0]+","+
				elem.currentbgRGB[1]+","+
				elem.currentbgRGB[2]+")";
			actStep++;
			if (actStep > steps) window.clearInterval(elem.bgFadeMemInt);
		}
		,intervals)
}

function easeInOut(minValue,maxValue,totalSteps,actualStep,powr) {
//Generic Animation Step Value Generator By www.hesido.com
	var delta = maxValue - minValue;
	var stepp = minValue+(Math.pow(((1 / totalSteps)*actualStep),powr)*delta);
	return Math.ceil(stepp)
}
