/****************************************************************************************************
Author: brhurley@jamestower.com
Date: 06/08/2005
Purpose: Collection of functions commonly used by the Magrathea sites.
Dependencies: None
Revision History:
****************************************************************************************************/
// returns the requested number of characters from the left side of the requested string
function left(strValue, intCount)
{
	return strValue.substr(0, intCount);
}

// returns the requested number of characters from the right side of the requested string
function right(strValue, intCount)
{
	return strValue.substr(strValue.length - intCount);
}

// tests the requested variable for existance, returns true/false
function isDefined(objVariable)
{
	var blnDefined = true;
	try
	{
		if(typeof(objVariable) == "undefined")
		{
			blnDefined = false;
		}
	}
	catch(e)
	{
		blnDefined = false;
	}
	return blnDefined;
}

// updates the text displayed in an html element such as a div or span tag
function updateTextInElement(strElement, strText)
{
	try
	{
		objElement = document.getElementById(strElement);	
		if(objElement.hasChildNodes())
		{
			objElement.childNodes[0].nodeValue = strText;
		}
		else
		{
			objElement.appendChild(document.createTextNode(strText));
		}
	}
	catch(e)
	{
		// do nothing
	}
}

/**
 * Get the boolean value of a variable.  A numeric value greater than 0 is considered true, and a string
 * value of 'true' or 'yes' (case in-sensitive) is considered true.
 *
 * varValue: Variable to get the boolean equivilent to
 *
 * Return: True if the value can be converted to boolean true, false otherwise
 */
function getBoolean(varValue)
{
	var blnReturn = false;
	// if it is already a boolean value, just use it as is
	if(typeof(varValue) == "boolean")
	{
		blnReturn = varValue;
	}
	// else need to try to convert to boolean
	else
	{
		// We'll accept anything over 0 or any string form of 'true' or 'yes'
		if((varValue > 0) || (varValue.toUpperCase() == "TRUE") || (varValue.toUpperCase() == "YES"))
		{
			blnReturn = true;
		}
	}
	return blnReturn;
}

// gets an HTTPRequestObject to use for making AJAX requests
function getHTTPRequestObject()
{
	var objHTTPRequest;
    if(window.XMLHttpRequest) // Mozilla, Safari,...
    { 
        objHTTPRequest = new XMLHttpRequest();
        if(objHTTPRequest.overrideMimeType) 
        {
            objHTTPRequest.overrideMimeType('text/xml');
        }
    } 
    else if(window.ActiveXObject)  // IE
    {
        try 
        {
            objHTTPRequest = new ActiveXObject("Msxml2.XMLHTTP");
        } 
        catch (e) 
        {
            try 
            {
                objHTTPRequest = new ActiveXObject("Microsoft.XMLHTTP");
            } 
            catch (e) 
            {
            	objHTTPRequest = null;
            }
        }
    }
	return objHTTPRequest;
}
