/*
//////////////////////////////////////////////////////////////////////////////////////////////////////////
Template:	dollarFormat.js
Location:	/includes/js/
Developer:	Sherry Liu
Date:		June 13, 2002

Purpose:	dollar format a interger.

Notes:		in this script, I also put a replace function. this function is used when calculating
			total dollars in the contract page. we need this function a make any dollar foramtted string
			back to an interger.

Revisions:
Name			Date		Issue	Details
---- 			----		-----	------- 
//////////////////////////////////////////////////////////////////////////////////////////////////////////
*/
	
	function replace(string,text,by) {
		// Replaces text with by in string
		    var strLength = string.length, txtLength = text.length;
		    if ((strLength == 0) || (txtLength == 0)) return string;
		
		    var i = string.indexOf(text);
		    if ((!i) && (text != string.substring(0,txtLength))) return string;
		    if (i == -1) return string;
		
		    var newstr = string.substring(0,i) + by;
		
		    if (i+txtLength < strLength)
		        newstr += replace(string.substring(i+txtLength,strLength),text,by);
		
		    return newstr;
		}

		
		function outputMoney(number) {
			if(isNaN(number))
			{
				alert("Please enter a valid dollar amount.\nPlease exclude commas when entering dollars, they will be entered for you.");				
				return outputDollars("0") + outputCents("0");
			}
			else
	    		return outputDollars(Math.floor(number-0) + '') + outputCents(number - 0);
		}
				
		function outputDollars(number) {
		    if (number.length <= 3)
		        return (number == '' ? '0' : number);
		    else {
		        var mod = number.length%3;
		        var output = (mod == 0 ? '' : (number.substring(0,mod)));
		        for (i=0 ; i < Math.floor(number.length/3) ; i++) {
		            if ((mod ==0) && (i ==0))
		                output+= number.substring(mod+3*i,mod+3*i+3);
		            else
		                output+= ',' + number.substring(mod+3*i,mod+3*i+3);
		        }
		        return (output);
		    }
		}
		
		function outputCents(amount) {
		    amount = Math.round( ( (amount) - Math.floor(amount) ) *100);
		    return (amount < 10 ? '.0' + amount : '.' + amount);
		}