
/* ============================================================================
*  Makes the first letter of every word capitalized.
*  ========================================================================= */
String.prototype.ucfirst = function(){ return this.replace( /(^|\s)([a-z])/g , function(m,p1,p2){ return p1+p2.toUpperCase(); } ); };


/* ============================================================================
*  When moving text from an innerHTML, it does not decode special chars. This
*  function is to overcome that annoying 'feature'.
*  ========================================================================= */
String.prototype.decodeHTML = function() { return this.replace(/\&lt\;/g, '<').replace(/\&gt\;/g, '>').replace(/\&quot\;/g, '"').replace(/\&amp\;/g, '&').replace(/\&nbsp\;/g, ' '); }


/* ============================================================================
*  Trims off white space. Easy.
*  ========================================================================= */
String.prototype.trim = function() { return this.replace(/^\s*/, "").replace(/\s*$/, ""); }


/* ============================================================================
*  Searches an array to see if a given value exists.
*  NOTE: Adding prototype function to Arrays will impact for..in loops, which
*  will iterate including the prototype functions.
*  ========================================================================= */
//Array.prototype.exists = function(search)
//{
//	for (var i=0; i<this.length; i++) if (this[i] == search) return true;
//	return false;
//}


/* ============================================================================
*  Searches an array to see if a given value exists. Returns index. Returns -1
*  if a match is not found.
*  ========================================================================= */
function array_search(inArr, inSearch)
{
	if (inArr != null && inSearch != null && inSearch != "")
		for (var i = 0; i < inArr.length; i++)
			if (inArr[i] == inSearch)
				return i;
	return -1;
}


/* ============================================================================
*  To reset the state options back to default, or zeroed values.
*  ========================================================================= */
function resetSelection(selectElement, firstSelection)
{
	selectElement.options[0] = new Option();
	// selectObject.length = 1;	// Another way to get a new option;
	selectElement.options[0].value = "";

	if (firstSelection != null) selectElement.options[0].text = firstSelection;
	else selectElement.options[0].text = "Select One";

	selectElement.length = 1;	// Truncate old options.

	selectElement.selectedIndex = 0;
}


/* ============================================================================
*  Called by HTML, when creating a Select element that needs to populate the
*  option elements with the given string array. The array parameter is used to
*  create the Select Option elements. If the array is a simple array, it will
*  use each array element for both the Select text and Select value. However,
*  if the array element is another array, it will assume that the first element
*  of the child array is the Select text and the second element is the Select
*  value. If the useChildValAsKey is set to true, and there exists child arrays
*  in the array parameter, it will always use the first element of the child
*  array as both the Select text and Select value.
*  ========================================================================= */
function fillSelection(selectElement, inArray, useChildValAsText)
{
	//alert("Inside fillSelection.");

	if (useChildValAsText == null) useChildValAsText = false;

	// First clear out the old array
	// resetSelection(selectElement, null);
	var counter = 1;

	// Populate the selection options!
	for(var j = 0; j < inArray.length; j++)
	{
		selectElement.options[counter] = new Option();

		var value = inArray[j];

		if (inArray[j] instanceof Array)
		{
			if (useChildValAsText) selectElement.options[counter].text = inArray[j][1];
			else selectElement.options[counter].text = inArray[j][0];

			selectElement.options[counter].value = inArray[j][1];
		}
		else
		{
			selectElement.options[counter].text = inArray[j]
			selectElement.options[counter].value = inArray[j]
		}

		counter++;
	}
}


/* ============================================================================
*  To find and set the Select element to have a particular option selected.
*  ========================================================================= */
function selectOption(selectElement, selectOption)
{
	if (selectElement != null && selectElement.options != null && selectOption != null)
		for (var j = 0; j < selectElement.options.length; j++)
		{
			if (selectElement.options[j].text == selectOption)
			{
				selectElement.options[j].selected = true;
				break;
			}
		}
}


/* ============================================================================
*  To find and set the Select element to have a particular option selected.
*  ========================================================================= */
function selectOptionByValue(selectElement, selectOption)
{
	if (selectElement != null && selectElement.options != null && selectOption != null)
		for (var j = 0; j < selectElement.options.length; j++)
		{
			if (selectElement.options[j].value == selectOption)
			{
				selectElement.options[j].selected = true;
				break;
			}
		}
}

/* ============================================================================
*  Copy the selected value from one selection menu to another.
*  ========================================================================= */
function selectTransferSelection(oldSelect, newSelect)
{
	newSelect.selectedIndex = oldSelect.selectedIndex;
	//alert("Copying over newSelect.selectedIndex["+newSelect.selectedIndex+"]");
	if (!newSelect.options[newSelect.selectedIndex].selected) newSelect.options[newSelect.selectedIndex].selected=true;	// Odd, but sometimes this is needed, in addition to above.
}

/* ============================================================================
*  Sets a cookie
*  ========================================================================= */
function Set_Cookie( name, value, expires, path, domain, secure )
{
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );
	
	/*
	if the expires variable is set, make the correct
	expires time, the current script below will set
	it for x number of days, to make it for hours,
	delete * 24, for minutes, delete * 60 * 24
	*/
	if ( expires )
	{
	expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );
	
	document.cookie = name + "=" +escape( value ) +
	( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
	( ( path ) ? ";path=" + path : "" ) +
	( ( domain ) ? ";domain=" + domain : "" ) +
	( ( secure ) ? ";secure" : "" );
}

/* ============================================================================
*  Gets a cookie
*  ========================================================================= */
function Get_Cookie( check_name ) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f

	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );


		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}
