// used for stripping invalid chars from any given string
function strMend(str, validchars)
{
	retStr = "";
	for (i=0; i < str.length; i++) 
	{
		thechar = str.charAt(i);
		if(validchars.indexOf(thechar) == -1)
		{
		}
		else
		{
			retStr += thechar;
		}
	}
	return retStr;
}

// formats the value into an integer (can actually do decimal places as well)
function formatNumber(val, fraction, places, allowNegatives, leaveBlank)
{
	var searchString = "01234567890.";
	if(allowNegatives)
	{
		searchString += "-";
	}
	var tt = strMend(""+val, searchString);
	var ss = "";
	var punct = false;
	var ok = false;
	var ch = '';
	// First Ensure there is only one minus sign at the beginning (if at all) and one punct.
	for(a = 0; a < tt.length; a++)
	{
		ch = tt.charAt(a);
		ok = true;
		if(ch == '-' && a != 0)
		{
			ok = false;
		}
		else
		if(ch == '.')
		{
			if(punct)
			{
				ok = false;
			}
			punct = true;
		}
		if(ok)
		{
			ss += ch;
		}
	}
	if(ss.length > 0)
	{
		if(fraction <= 0)
		{
			tt = Math.round(Number(ss));
		}
		else
		{
			tt = Math.round(Number(ss)*fraction)/fraction;
		}
	}
	else
	if (leaveBlank == 1)
	{
		return "";
	}
	tt += "";
	z = tt.indexOf(".",0);
	if(z != -1)
	{ // found a dec place
		c = 0;
		for(a = z; a < tt.length; a++)
		{
			c++;
		}
		for(a = c; a <= places; a++)
		{
			tt += "0";
		}
	}
	else
	{ // not found, so force
		if(tt == "")
		{
			tt = "0";
		}
		for(a = 0; a < places; a++)
		{
			if(a == 0)
			{
				tt += ".";
			}
			tt += "0";
		}
	}
	return tt;
}

// Get URL Parameter
function gup(name)
{  
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");  
	var regexS = "[\\?&]" + name + "=([^&#]*)";  
	var regex = new RegExp( regexS );  
	var results = regex.exec( window.location.href );  
	if( results == null )
	{
		return "";  
	}
	else
	{
		return unescape(results[1]);
	}
}

// Forced page load here instead
function loadMe(loc, url)
{
	eval(loc + ".location.href = '" + url + "'");
	return false;
}
