// Common Calculator Functions

/*COMMENT-----------------------------------------------------------------------------
FUNCTION:		formatDollar
DESCRIPTION	
		Takes a numeric value and flag indicating if a $ is needed and formats for US dollars
PARAMETERS
	Val							The value to be formatted
	IncludeDollarSign			boolean value where true indicates that result should have a $
RETURN VALUES
	Formatted value
-----------------------------------------------------------------------------COMMENT*/	
function formatDollar (Val, IncludeDollarSign)
	{
		Val= "" + Val

		if (Val.indexOf (".", 0)!=-1)
		{
			Dollars = Val.substring(0, Val.indexOf (".", 0));
			Cents = Val.substring(Val.indexOf (".", 0)+1, Val.indexOf (".", 0)+3);
			if (Cents.length==0) 
			{	
			Cents="00";
			}
			if (Cents.length==1)
			{
			Cents=Cents+"0";
			}
		}else{
			Dollars = Val;
			Cents = "00";
		}
		OutString="";
		len=Dollars.length;
		if (len>=3) 
		{
			while (len>0)
			{
				TempString=Dollars.substring(len-3, len)
				if (TempString.length==3)
				{
					OutString=","+TempString+OutString
					len=len-3;
				}else{
					OutString=TempString+OutString
					len=0
				}
			}
			if (OutString.substring(0, 1)==",") 
			{
				Dollars=OutString.substring (1, OutString.length)
			}else{
				Dollars=OutString
			}
		} 
		if (IncludeDollarSign)
		{
			FormattedVal = "$" +Dollars + "." + Cents;
		}else{
			FormattedVal = Dollars + "." + Cents;
		}
		return FormattedVal
}



/*COMMENT-----------------------------------------------------------------------------
FUNCTION:		replace
DESCRIPTION	
		Takes a string and replaces any occurrence of string 'p' with string 'rep'
PARAMETERS
	str							The string to be searched
	pat							The search pattern
	rep							The replacement string
RETURN VALUES
	Formatted value
-----------------------------------------------------------------------------COMMENT*/				
			function replace (str, pat, rep) {
				var r = '';
				var p;
				while ((p = str.indexOf(pat)) != -1) {
					r += str.substring (0, p) + rep;
					str = str.substring (p + pat.length);
				}
				r += str;  
				return r;
			}


			function isCurrency(val, source)
				{	
					val = replace(val,'$','');
					val = replace(val,',','');
					
					if(isNaN(val)) 
						{
						alert("The value you have entered is not a valid currency value.");
						source.value=0;
						source.blur();
						source.focus();
						}
				}


			function isPercent(val, source)
				{	
					val = replace(val,'%','');
					
					if(isNaN(val)) 
						{
						alert("The value you have entered is not a valid value.");
						source.value=0;
						source.blur();
						source.focus();
						}
				}
				

			function isNumeric(val, source)
				{						
					if(isNaN(val)) 
						{
						alert("The value you have entered is not a valid numeric value.");
						source.value=0;
						source.blur();
						source.focus();
						}
				}
