////////////////////////////////////////////////////////////////////
// COPYRIGHT 2004 - 2 REAL STUDIOS INC./////////////////////////////
// This file and its all its content are intellectual properties////
// of 2 Real Studios Inc. and are not to be copied from or used ////
// without explicit authorization from 2 Real Studios Incorporated./
////////////////////////////////////////////////////////////////////
/*
  Shared miscalleneous functions.
*/



/////////////////////////////////////////////////////////////////////////// GLOBAL VARIABLES
///////////////////////////////////// SETTINGS
var g_bDebugMode					= true;	// Defines the state of the "Dump" function

///////////////////////////////////// GLOBAL OBJECTS
var g_eDump							= null;	// Text-area that in which the function 'Dump' outputs text



/////////////////////////////////////////////////////////////////////////// UTILITIES
function ConsoleEval()
{
	// Evaluates entered text in console
	
	if (!g_bDebugMode || !g_eDump) return;
	var nKey = window.event.keyCode;
	
	switch (nKey)
	{
		case 13:
			if (!window.event.shiftKey)
			{
				try { eval(EDump.value.substring(EDump.value.lastIndexOf("\n"), EDump.value.length)); }
				catch (oException) { Dump("\nExecution of line failed: " + oException); }
			}
			break;
		default:
			break;
	}
	window.event.cancelBubble = true;
}

function ChangeBrightness(nColor, nRatio)
{
	// Accepts a 24 bit RGB 'nColor' and darkens or brigthens it by 'nRatio'
	//***JPWEAK: Will change the color's tone if brigtening causes one or more of the RGB value to max out at 255
	var nColorOut = 0;
	for (var nBitShift = 0; nBitShift < 24; nBitShift += 8)
	{
		var nColorElement = (nColor & (255 << nBitShift)) >> nBitShift;
		nColorElement *= nRatio;
		if (nColorElement > 255) nColorElement = 255;
		nColorOut |= nColorElement << nBitShift;
	}
	return nColorOut;
}

function CreateElement(oParent, sTag, sID, sClass, sStyle, sProps, sInnerText, bNotUnselectable)
{
	// Function responsible for creating HTML elements from JScript
	// Build HTML string
	var sHTML = '<'+ sTag;
	if (sID) sHTML += ' Id="' + sID + '"';
	if (sClass) sHTML += ' Class="' + sClass + '"';
	if (sStyle) sHTML += ' Style="' + sStyle + '"';
	if (sProps) sHTML += ' ' + sProps;
	if (!bNotUnselectable)
	{
		if (sTag != "TABLE" && sTag != "TBODY" && sTag != "TR" && sTag != "SCRIPT" && sTag != "INPUT" && sTag != "BR") sHTML += " Unselectable=On";
	}
	sHTML += '>';
	
	// Create Element
	var e = oParent.document.createElement(sHTML);
	if (e == null) throw 'Error in CreateElement() while attempting to create tag "' + sHTML + '"';
	if (sInnerText) e.innerText = sInnerText;
	var eCreated = oParent.appendChild(e);
	if (eCreated == null) throw 'Error in CreateElement()while attempting to create tag "' + sHTML + '".  Element could not be inserted into parent.';

	return eCreated;
}

function Dump(sString)
{
	// Outputs string to target element(g_eDump)
	
	Debug.write(sString + "\n");
	if (!g_bDebugMode || !g_eDump) return;
	g_eDump.value += sString + "\n";
	g_eDump.scrollTop += 1000;
}

function FindParentTag(eChild, sTagName, sClassName, sAttribute)
{
	// Goes up the element parent chain until it encounters an element with the requested tab name
	
	var eCurrent = eChild;
	if (typeof(sClassName) == "undefined") sClassName = "";
	while (eCurrent.tagName != sTagName || eCurrent.className != sClassName)
	{
		eCurrent = eCurrent.parentElement;
		if (eCurrent == null || typeof(eCurrent) != "object" || eCurrent.tagName == "HTML") return null;
	}
	if (eCurrent && typeof(sAttribute) != "undefined") return eCurrent.getAttribute(sAttribute);
	return eCurrent;
}

function GetParent(oObject, nLevel)
{
	// Returns the parent at a specified level in the tree
	
	var oParent = oObject.parentElement;
	for (var nIndex = 0; nIndex < nLevel; nIndex++)
	{
		oParent = oParent.parentElement;
	}
	return oParent;
}

function GetChildren(oObject, nLevel)
{
	// Returns the child at a specified level in the tree
	
	var oChild = oObject.firstChild;
	for (var nIndex = 0; nIndex < nLevel; nIndex++)
	{
		oChild = oChild.firstChild;
	}
	return oChild;
}

function FindClassInstance(sClassName, eChild)
{
	// Iterates through parent chain until it finds an attribute 'sClassName'
	var eCurrent = eChild;
	try {
		while (eCurrent != null && typeof(eCurrent) == "object" && eCurrent.tagName != "HTML")
		{
			var oObject = eCurrent.getAttribute(sClassName);
			if (oObject) return oObject;
			eCurrent = eCurrent.parentElement;
		}
	}
	catch(sString) {}
	return null;		// Not found
}

function Load(sFileName)
{
	// Adds the specified Script to the page
	CreateElement(window.document.documentElement.firstChild, "SCRIPT", null, null, null, "Language='JavaScript' Src='" + sFileName + "'");
}

function Select(oObject)
{
	// Standard selection / unselection algorithm.  Assumes selection-capable objects support Select() and Unselect()
	if (g_oSelObject == oObject)
		return;

	if (g_oSelObject && g_oSelObject.Unselect)	g_oSelObject.Unselect();

	g_oSelObject = oObject;

	if (g_oSelObject && g_oSelObject.Select)	g_oSelObject.Select();
}

function ShowStatus(sString)
{
	// Display text on status bar
	window.status = sString;
}

function UnselectObjects()
{
	// Unselect All Objects
	for (var nObject = 0; nObject < g_aObjects.length; nObject++)
	{
		if (g_aObjects[nObject].Unselect) g_aObjects[nObject].Unselect();
	}
}

function WriteHTMLToFile(sFilePath, oElement)
{
	var oFSO = new ActiveXObject("Scripting.FileSystemObject");
	var oFile = oFSO.CreateTextFile(sFilePath, true);
	try {
		if (oElement)
		{
			oFile.WriteLine("<" + oElement.tagName + ">");
			oFile.WriteLine(oElement.innerHTML);
			oFile.Write("</" + oElement.tagName + ">");
		}
		else
		{
			oFile.WriteLine("<HTML>");
			oFile.WriteLine(window.document.documentElement.innerHTML);
			oFile.Write("</HTML>");
			oFile.Close();		
		}
	}
	catch(oException)
	{
		Dump("Writing HTML to file failed: '" + oException + "'.");
	}
}