
function calcBalance(balance,amount,display,clean){
	intBalance = parseNumber($F(balance),2);
	intAmount = parseNumber($F(amount),2);
	
	if(intAmount > intBalance){
		$(display).innerHTML = 'Not enough funds!';
	} else {
	
		$(display).innerHTML = '$' + CommaFormatted(formatNumber((intBalance - intAmount),2));
	}	
	
	if(clean == 1) {$(amount).value = '$' + CommaFormatted(formatNumber(intAmount,2));}
}

//------------------------------------------------------------------------
// returns a number including up to the specified number
// of decimal places. Will not add decimal places
function parseNumber(strVal, decimals)
{
	if (decimals == 0) {
		return formatNumber(strVal, 0) * 1;
	} else {
		return formatNumber(strVal, decimals) * 1;
	}
}
String.prototype.parseNumber = new Function("dec", "return parseNumber(this, dec);");

//------------------------------------------------------------------------
// returns a string representation of a number, including the
// number of decimal places specified. It will include extra
// zero's at the end of the number if necessary.
// It also tries to be smart by removing all non numberical 
// characters except a leading minus sign and the first decimal
// in the string
// ex: formatNumber("82374.556", 5) //returns "82374.55600" 
// ex: formatNumber("82374.556", 5) //returns "82374.55600" 
// ex: formatNumber("a3fe7f4d.5u5c6w", 5) //returns "374.55600"
function formatNumber(strVal, decimalPlaces)
{
	strVal = strVal + '';
	
	//var res = Number(strVal);
	//if (res == NaN) {
		var parsedNo = "";
		for(var n = 0; n < strVal.length; n++)
		{
			var i = strVal.substring(n, n+1);
			
			// only add a - if it's the first character
			if (parsedNo.length == 0 && i == "-") 
			{
				parsedNo += i;
			
			}// only add one period
			else if (i == "." && parsedNo.indexOf(".") == -1)
			{
				parsedNo += i;
			
			} // add any other numerical character
			else if(i=="0"||i=="1"||i=="2"||i=="3"||i=="4"||i=="5"||i=="6"||i=="7"||i=="8"||i=="9")
			{
				parsedNo += i;
			}
		}
	//} else {
	//	parsedNo = strVal;
	//	alert(parsedNo);
	//}
	
	var num;
	if(parsedNo == ""){ 
		parsedNo = "0";
	}

	// no decimal places
	if (decimalPlaces == 0)
	{
		return Math.round(parsedNo);
	
	}// return as is without trimming or adding
	else if (decimalPlaces == undefined || decimalPlaces == -1)
	{
		return parsedNo;
	
	} // need to modify the decimal values
	else
	{
		var pieces = parsedNo.split(".");
		
		// there are no decimal digits - so add them all
		if (pieces.length == 1)
		{
			var pad = "";
			for (var i = 0; i < decimalPlaces; i++)
			{
				pad += "0";
			}
			return pieces[0] + '.' + pad;
		
		} // the decimal digits are longer than we want
		else if (String(pieces[1]).length > decimalPlaces)
		{
			return formatNumber(round(pieces[0] + '.' 
			+ pieces[1], decimalPlaces), decimalPlaces);

		} // the decimal digits are shorter than we want	
		else if (decimalPlaces > String(pieces[1]).length)
		{
			var shortBy = decimalPlaces - pieces[1].length;
			var pad = '';
			for (var i = 0; i < shortBy; i++)
			{
				pad += "0";
			}
			return String(pieces[0]) + '.' + String(pieces[1]) + pad;

		} else
		{
			return parsedNo;
		}
	
	}

}

// Adding parseNumber to the String object
String.prototype.parseNumber = new Function("decimals", "return formatNumber(this, decimals)");

//------------------------------------------------------------------------
// Rounds any type of number, not just integers like the Math.round function.
// You pass it a raw number, the number of decimal places you want and it will
// perform standard rounding. 
// Formula taken from: 
// http://www.mediacollege.com/internet/javascript/number/round.html
// I do not understand the formula - but then again I didn't take the time
// to try to understand it. All I know is that it passed my tests, so I
// am using it.
function round(p_strNum, p_intDecPlaces) {
	return Math.round(p_strNum * Math.pow(10, p_intDecPlaces))
	   / Math.pow(10, p_intDecPlaces);
}

//------------------------------------------------------------------------
// Strips non numerical characters and returns a 
// valid integer (positive onle)
function parseInt(stringNo)
{
	var parsedNo = "";
	for(var n=0; n<stringNo.length; n++){
		var i = stringNo.substring(n,n+1);
		if(i=="1"||i=="2"||i=="3"||i=="4"||i=="5"||i=="6"||i=="7"||i=="8"||i=="9"||i=="0"){
			parsedNo += i;
		}
	}
	if(parsedNo == ""){ 
		return 0;
	}else{
		return parsedNo * 1;
	}
}
String.prototype.parseInt = new Function("return parseInt(this)");

//// Comma formatted for currency
function CommaFormatted(amount)
{
    amount = amount + '';
    //alert('amount:' + amount);
	var delimiter = ","; // replace comma if desired
	var a = amount.split('.',2);
	var d = a[1];
	var i = parseInt(a[0]);
	if(isNaN(i)) { return ''; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	var n = new String(i);
	var a = [];
	while(n.length > 3)
	{
		var nn = n.substr(n.length-3);
		a.unshift(nn);
		n = n.substr(0,n.length-3);
	}
	if(n.length > 0) { a.unshift(n); }
	n = a.join(delimiter);
	//alert(d);
	if( d == undefined) {amount = n;}
	else {
		if(d.length < 1) { amount = n; }
		else { amount = n + '.' + d; }
	}
		amount = minus + amount;
	return amount;
}

//------------------------------------------------------------------------
// Clears the value of a field (passed by name) if it is 
// not a date - fairly primitive
function setDate(strField)
{
	if (!isDate($F(strField))) 
	{
		$(strField).value = '';
	}
	
	if(!CleanDate($F(strField)))
	{
		$(strField).value = '';
	}
	//alert(document.getElementById(strField).value);
}

function CleanDate(p_strDate) {
	
	try {
		var dtDate = new Date(Date.parse(p_strDate));
	} catch(ex) {
		return false;
	}
	
	if (isNaN(dtDate)) {
		return false;
	}
	
	var intDay = dtDate.getDate();
	var intMonth = dtDate.getMonth() + 1;
	var intYear = dtDate.getFullYear();
	
	if (isNaN(intDay)) {
		return false;
	}
	if (isNaN(intMonth)) {
		return false;
	}
	if (isNaN(intYear)) {
		return false;
	}
	if (intYear < 2000) {
		return false;
	}
	
	return intMonth + '/' + intDay + '/' + intYear;

}

//------------------------------------------------------------------------
// Returns true if the input is a valid date, false otherwise
function isDate(DateStringToCheck)
{
	return !isNaN(new Date(DateStringToCheck));
}