/// <summary>
/// Preloads the image specified by url in the background.
/// </summary>
function PreloadImage(url)
{
	if( document.images )
	{
		var a = document.preloadedImages;
		if( a == null )
		{
			a = new Array();
			//alert(a.length);
			document.preloadedImages = a;
		}

		var img = new Image();
		img.src = url;
		
		a[a.length] = img;
	}
}

/// <summary>
/// Swaps the current image for the one specified by url.
/// </summary>
function SwapImage(img, newUrl)
{
	img.orgSrc = img.src;
	img.src = newUrl;
}

/// <summary>
/// Restores the image previously swapped out by SwapImage.
/// </summary>
function SwapRestore(img)
{
	if( img.orgSrc != null )
	{
		img.src = img.orgSrc;
		img.orgSrc = null;
	}
}

/// <summary>
/// When attached to the 'onkeypress' event, restricts the input to numbers only.
/// </summary>
function NumbersOnly(evt)
{
	if( !evt ) evt = window.event;
	if( !evt ) return;
	
	var code = (evt.charCode ? evt.charCode : (evt.which ? evt.which : evt.keyCode));
	if( code > 0x20 && (code < 0x30 || code > 0x39) )
	{
		evt.returnValue = false;
		if( evt.preventDefault ) evt.preventDefault();
	}
}

/// <summary>
/// Returns the passed value into a dollar amount in the format '$0.00'.
/// </summary>
function dollarize(value)
{
	return "$" + format(value, 2);
}

/// <summary>
/// Returns the value of an expression as a string with the specified number decimal placces.
/// </summary>
function format(expr, decplaces)
{
	// raise incoming value by power of 10 times the
	// number of decimal places; round to an integer; convert to string
	var str = "" + Math.round(eval(expr) * Math.pow(10, decplaces));

	// pad small value strings with zeros to the left of rounded number
	while (str.length <= decplaces)
	{
		str = "0" + str;
	}
	
	// establish location of decimal point
	var decpoint = str.length - decplaces
	
	// assemble final result from: (a) the string up to the position of
	// the decimal point; (b) the decimal point; and (c) the balance
	// of the string. Return finished product.
	return str.substring(0, decpoint) + "." + str.substring(decpoint, str.length);
}

function OpenWindowAndFocus(name, features, url) 
{
	// get a handle to the target window (or open a new one if none exists)
	var win = window.open('', name, features);
	
	// unload the existing page if not a new window
	// note: we are doing this because calling focus on the window in IE 
	//       fails when a plugin is loaded (Excel, PDF, etc.)
	if (win.location.href != 'about:blank')
	{
		win = window.open('about:blank', name, features);
	}
	
	// load our target page and set the focus
	win = window.open(url, name, features); 
	win.focus();
}
