function IsNumeric(sText)
{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;
   
   if (sText.length == 0) {
      return false;
   }
 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
   }


function CalculateSavings(SCForm)
{
    var result;
    var AnnualPieces;
    var TotalCurrentHours;
    var TotalGoalHours;
    var TotalCurrentWages;
    var TotalGoalWages;
    var ROI;
    var AnnualSavings;
	var MonthlySavings;
	var PricePerUnit;
	var TotalPrice;
	var MonthlyPayment;
	var MonthlyCashflow;
    if (IsNumeric(SCForm.AvgPcsPerWeek.value) && IsNumeric(SCForm.AvgHourlyWage.value) && IsNumeric(SCForm.CurrentPPH.value) && IsNumeric(SCForm.GoalPPH.value))
    {
        AnnualPieces = SCForm.AvgPcsPerWeek.value * 52;
        TotalCurrentHours = AnnualPieces / SCForm.CurrentPPH.value;
        TotalGoalHours = AnnualPieces / SCForm.GoalPPH.value;
        TotalCurrentWages = TotalCurrentHours * SCForm.AvgHourlyWage.value;
        TotalGoalWages = TotalGoalHours * SCForm.AvgHourlyWage.value;
        if (IsNumeric(SCForm.PayrollTaxPercent.value))
        {
            TotalCurrentWages = TotalCurrentWages * (1 + (SCForm.PayrollTaxPercent.value / 100));
            TotalGoalWages = TotalGoalWages * (1 + (SCForm.PayrollTaxPercent.value / 100));
        }
        AnnualSavings = TotalCurrentWages - TotalGoalWages;
        SCForm.AnnualSavings.value = '$' + AnnualSavings.toFixed(2);
		MonthlySavings = AnnualSavings / 12;
		SCForm.MonthlySavings.value = '$' + MonthlySavings.toFixed(2);
    }
    if (IsNumeric(SCForm.Stations.value))
	{
		TotalPrice = SCForm.Stations.value * SCForm.PriceEach.value;
		SCForm.TotalPrice.value = '$' + TotalPrice.toFixed(2);
		if (IsNumeric(SCForm.LeaseRate.value) && IsNumeric(SCForm.LeaseYears.value))
		{
			MonthlyPayment = calculatePayment(TotalPrice, SCForm.LeaseRate.value,SCForm.LeaseYears.value);
			SCForm.MonthlyPayment.value = '$' + MonthlyPayment.toFixed(2);
			MonthlyCashflow = MonthlySavings - MonthlyPayment;
			SCForm.MonthlyCashflow.value = '$' + MonthlyCashflow.toFixed(2);
		}
		if (IsNumeric(AnnualSavings))
		{
			if (AnnualSavings > 0)
			{
				ROI = TotalPrice / AnnualSavings
				SCForm.ROIYears.value = ROI.toFixed(2);
			}
		}
	}
    return result;
}

function CalculatePrice(SCForm)
{
	var PricePerUnit;
	if (SCForm.Stations.value <= 4)
	{
	  PricePerUnit = 1995;
	} else if (SCForm.Stations.value <= 8) {
	  PricePerUnit = 1795;
	} else {
	  PricePerUnit = 1595;
	}
	SCForm.PriceEach.value = PricePerUnit.toFixed(2);
}

function calculatePayment(pri, intRate, years) {
    // Get the user's input from the form. Assume it is all valid.
    // Convert interest from a percentage to a decimal, and convert from
    // an annual rate to a monthly rate. Convert payment period in years
    // to the number of monthly payments.
    var principal = pri;
    var interest = intRate / 100 / 12;
    var payments = years * 12;
    var Payment = 0;
    // Now compute the monthly payment figure, using esoteric math.
    var x = Math.pow(1 + interest, payments);
    var monthly = (principal*x*interest)/(x-1);

    // Check that the result is a finite number. If so, display the results
    if (!isNaN(monthly) && 
        (monthly != Number.POSITIVE_INFINITY) &&
        (monthly != Number.NEGATIVE_INFINITY)) {

        Payment = monthly;
        //document.loandata.total.value = round(monthly * payments);
        //document.loandata.totalinterest.value = 
        //    round((monthly * payments) - principal);
    }
    // Otherwise, the user's input was probably invalid, so don't
    // display anything.
    //else {
        //document.loandata.payment.value = "";
        //document.loandata.total.value = "";
        //document.loandata.totalinterest.value = "";
    //}
	return Payment;
}
