/* ***************
*****************
**  UTILITIES  **
*****************
*************** */
/*
*  trim()
*    this adds the trim() function to all strings
*    when called, this uses the parent string as input
*  
*  return: (string) any white-space at the start or the end of the input string is removed
*                   and the inside of the string is returned
*/
String.prototype.trim = function() { return this.replace( /^\s+|\s+$/g, '' ); };

/*
*  randomNum( starting, ending )
*    generates a random number between the selected range
*  
*  starting: one of the range constraints [inclusive]
*  ending:   the other range constraint [inclusive]
*  
*  return:   a randome number between the range constraints [inclusive]
*/
function randomNum( starting, ending )
{
	var output = 0;

	if ( starting > ending )
	{
		var temp = starting;
		starting = ending;
		ending = temp;
	};

	output = Math.floor( ( ending - ( starting - 1 ) ) * Math.random() ) + starting;

	return output;
};

/*
*  GetHumanDate( dateFormat, year, month, date )
*    returns a string representing the input date in a human readable format
*  
*  dateFormat:   this can either be "long" or "short"
*                long:   January, February, etc...
*                short:  Jan, Feb, etc...
*  year:         year
*  month:        month (0 - 11)
*  date:         day of the month (1 - 31)
*  
*  return:       (string) date in a human readable format
*/
function GetHumanDate( dateFormat, year, month, date )
{
	var months_long = new Array( 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December' );
	var months_short = new Array( 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sept', 'Oct', 'Nov', 'Dec' );
	var output = '';
	var m = '';
	var d = '';

	if ( dateFormat.trim().toLowerCase() == 'short' )
	{
		m = months_short[month];
	}
	else
	{
		m = months_long[month];
	};

	switch( date )
	{
		case 1:
			d = date + 'st';
			break;
		case 21:
			d = date + 'st';
			break;
		case 31:
			d = date + 'st';
			break;
		case 2:
			d = date + 'nd';
			break;
		case 22:
			d = date + 'nd';
			break;
		case 3:
			d = date + 'rd';
			break;
		case 23:
			d = date + 'rd';
			break;
		default:
			d = date + 'th';
	};

	output = m + ' ' + d + ', ' + year;
	return output;
};

/*
*  GetLastModifiedDate()
*    returns the last date that the document was modified.
*    this is presented in a human readable format.
*  
*  return:   (string) last date that the document was modified
*/
function GetLastModifiedDate()
{
	var output = '';
	try
	{
		var datStr = '';
		var dateObj = new Date();
		try
		{
			datStr = document.lastModified;
		}
		catch (e1)
		{
			try
			{
				if ( dateObj.toLocaleDateString )
				{
					datStr = dateObj.toLocaleDateString( document.lastModified );
				}
				else
				{
					datStr = '';
				};
			}
			catch (e2)
			{
				datStr = '';
			};
		};
		var date = new Date( Date.parse( datStr ) );
		output = GetHumanDate( 'long', date.getFullYear(), date.getMonth(), date.getDate() );
	}
	catch (e3)
	{
		output = '';
	};
	return output;
};

/*
*  getPageSize()
*    gets the size of the page
*  
*  return  OBJECT( width, height )
*/
function getPageSize()
{
	var pageWidth = 0;
	var pageHeight = 0;
	var pageTop = 0;
	var pageLeft = 0;
	try
	{
		pageTop = document.body.scrollTop;
		pageLeft = document.body.scrollLeft;
	}
	catch (e)
	{
	};
	try
	{
		pageWidth = window.innerWidth;
		pageHeight = window.innerHeight;
	}
	catch (e1)
	{
	};
	if ( pageWidth == undefined )
	{
		try
		{
			pageWidth = document.body.clientWidth;
			pageHeight = document.body.clientHeight;
		}
		catch (e2)
		{
		};
		if ( pageWidth == undefined )
		{
			try
			{
				pageWidth = documentElement.clientWidth;
				pageHeight = documentElement.clientHeight;
			}
			catch (e3)
			{
			};
		};
	};
	var obj = new Object();
	obj.width = pageWidth;
	obj.height = pageHeight;
	obj.top = pageTop;
	obj.left = pageLeft;
	obj.bottom = pageTop + pageHeight;
	obj.right = pageLeft + pageWidth;
	return obj;
};

/*
*  GetAbsPosition( object )
*    Returns an object containing the (x, y) position of the input object along with the objects width/height
*  
*  object: the object whose location to be determined
*  return: an object with 4 properties (left [x], top [y], width, height)
*/
function GetAbsPosition( thing )
{
	var position = new Object;
	position.left = 0;
	position.top = 0;
	position.width = 0;
	position.height = 0;
	position.bottom = 0;
	position.right = 0;
	try
	{
		position.width = thing.offsetWidth;
		position.height = thing.offsetHeight;
	}
	catch (e1)
	{
		position.width = 0;
		position.height = 0;
	};
	if ( thing )
	{
		position.left = thing.offsetLeft;
		position.top = thing.offsetTop;
		if ( thing.offsetParent )
		{
			var parentpos = GetAbsPosition( thing.offsetParent );
			position.left += parentpos.left;
			position.top += parentpos.top;
		};
	};
	position.bottom = position.top + position.height;
	position.right = position.left + position.width;
	return position;
};
