function stripcommas(x) {  //strip commas from string
    str = x;
    var re = /,/g
    str = str.replace(re,"")
    return str;
  } 

  function currency(anynum) { 
	neg_flag = 0                                          //initialize the flag for a negative number 
	if (anynum < 0) {neg_flag = 1}                        //if negative, retval gets brackets later on

	big_string = ""+(Math.round(100*(Math.abs(anynum))))  //rounding the absolute value times 100
	biglen = big_string.length                            //how the string gets handled depends on its length
	if (biglen == 0)                   //null
		{retval = "0.00"} 
	else if (biglen == 1)              //1 to 9 (.01 to .09 cents)
		{retval = "0.0"+big_string}
	else if (biglen == 2)              //10 to 99 (.10 to .99 cents)
		{retval = "0."+big_string}
	else  {                               //all cases above 100 ($1.00)
        //The substring method returns all characters in the string
        // starting with and including the the first argument,
        // up to but not including the second argument.  
		hundredths_digit = big_string.substring(biglen-1,biglen)  
		tenths_digit = big_string.substring(biglen-2,biglen-1)    
		integer_digits = big_string.substring(0,biglen-2)
		var re = /(-?\d+)(\d{3})/
		while (re.test(integer_digits))  {
			integer_digits = integer_digits.replace(re, "$1,$2")
		}
		retval = integer_digits + "." + tenths_digit + hundredths_digit
	}  
    if (neg_flag == 1) {retval = "("+retval+")"}
    return retval
  }


  function CalculatePayment(FutureValue, Months, InterestRate) {
    var pv = 0;    // present value
    var fv = 0;    // future value
    var np = 0;    // number of payment periods
    var pmt = 0;   // amount of each periodic payment
    var ir = 0;    // interest rate on the account per period
    var f = 0;     // factor
    var s = "0";   // string
    var d = 0;     // decimal place

    if (InterestRate==0 || Months==0 || FutureValue==0) {
      alert("Please fill in all of the required fields.")
      return ""
    }
    else {
	  pv = 0;

          fv = stripcommas(FutureValue);  	  
	  if (isNaN(fv)) {
	    alert("Savings goal is not a valid number.");
	    return ""
	  }  	  
	  fv = parseFloat(fv);
	  
	  np = stripcommas(Months);
	  if (isNaN(np)) {
	    alert("Number of months is not a valid number.");
	    return ""
	  }  
	  np =  parseFloat(np);
	  	  
	  ir = stripcommas(InterestRate);
	  if (isNaN(ir)) {
	    alert("Interest rate is not a valid number.");
	    return ""
	  }  
	  ir = (parseFloat(ir)/100)/12;	  
	  
	  f = Math.pow(1 + ir,np);
	  pmt = -((ir * (fv + (f * pv)))/(-1 + f));
	  pmt = Math.abs(pmt);   //don't want to display a negative number, even though that is proper result
	  s = currency(pmt);
	  return s;
    }
  }


  function CalculateFutureValue(Payment, Months, InterestRate) {
    var pv = 0;    // present value
    var fv = 0;    // future value
    var np = 0;    // number of payment periods
    var pmt = 0;   // amount of each periodic payment
    var ir = 0;    // interest rate on the account per period
    var f = 0;     // factor
    var s = "0";   // string
    var d = 0;     // decimal place

    if (InterestRate==0 || Months==0 || Payment==0) {
      alert("Please fill in all of the required fields.")
      return ""
    }
    else {
	  pv = 0;
	  
          pmt = stripcommas(Payment);
	  if (isNaN(pmt)) {
	    alert("Monthly deposit is not a valid number.");
	    return ""
	  }  	 
	  pmt = parseFloat(pmt);	 
   	  
	  np = stripcommas(Months);
	  if (isNaN(np)) {
	    alert("Number of months is not a valid number.");
	    return ""
	  }  	  
	  np = parseFloat(np);  
	  
	  ir = InterestRate;
	  if (isNaN(ir)) {
	    alert("Interest rate is not a valid number.");
	    return ""
	  }  	  
	  ir = (parseFloat(ir)/100)/12;  
	    	  
	  f = Math.pow(1 + ir,np);
	  fv = -((-pmt + (f * pmt) + (ir * f * pv))/ir);	  
	  fv = Math.abs(fv);    //don't want to display a negative number, even though that is proper result
	  s = currency(fv);
	  return s;
    }
  }
