// Global constants to determine behavior

  MaxFee = 25000;
  MinFee = 995;

// Parallel Arrays - correlate these values with the dropdown list

  a_Term = new Array();
    a_Term[0] = 12;
    a_Term[1] = 24;
    a_Term[2] = 48;
	a_Term[3] = 12;
    a_Term[4] = 18;
    a_Term[5] = 24;

  a_Rate = new Array();
    a_Rate[0] = 0;
    a_Rate[1] = 0;
    a_Rate[2] = 0;
	a_Rate[3] = 11.99;
    a_Rate[4] = 11.99;
    a_Rate[5] = 11.99;

function initFields(form) {

// Set the Procedure Fee Field
  form.result0.value = "";
  form.result1.value = "";
  form.result2.value = "";
  form.result3.value = "";
  form.result4.value = "";
  form.result5.value = "";
}


function checkfee(fee, months) {

  if (isNaN(fee)) {
    alert("Procedure Fee must be only numbers.");
    return false;}

  else if (fee > MaxFee) {
    alert("Procedure Fee must be less than $" + MaxFee);
    return false;}

  else if (fee < MinFee) {
    alert("Procedure Fee must be greater than $" + MinFee);
    return false;}

  /*else if ((fee < 1500) & (months > 18)) {
    return false;}

  else if ((fee < 1900) & (months > 36)) {
    return false;}

  else if ((fee < 2900) & (months > 48)) {
    return false;}
*/
  else {
    return true;
  }
}

function calculate(form) {

	var newfee, monthindex, monthend;
	var sum, power, ratio, res;
	var fld;

	fee = form.fee.value;

	if( !checkfee(fee, 0) )
		return false;

	/*
	if (fee < 1500)
	{
		a_Rate[0] = 12.99;
		document.image0.src='../images/18_1299.gif';
		document.image1.src='../images/24.gif';
		document.image2.src='../images/48.gif';
	}
	else
	{
		a_Rate[0] = 9.99;
		document.image0.src='../images/18_999.gif';
		document.image1.src='../images/24_1099.gif';
		document.image2.src='../images/48_1299.gif';
	}
	*/
	if (form.name == "RateCalculator1"){
		monthindex = 0;
		monthend = 1
	} else {
		monthindex = 3
		monthend = 5
	}

	for(monthindex; monthindex <= monthend; monthindex++)
	{
		if( !checkfee(fee, a_Term[monthindex]) )
		{
			fld = eval("document." + form.name + ".result" + monthindex + ";");
			fld.value = "N/A";
			continue;
		}

		monthlyrate = parseFloat((a_Rate[monthindex] /1200) + 1);

		newfee = parseInt(fee) + (a_Rate[monthindex] / 100 / 365) * parseInt(fee) * 30;

		power = 1.0;
		sum   = 0.0;

		for (var i = 0; i < a_Term[monthindex]; i++)
		{
			power  = power * monthlyrate;
			sum   += power;
		}

		power = power * monthlyrate;
		ratio = power / sum;

		//document.RateCalculator.result.value = "$" + Math.round(newfee * ratio * 100)/100 + " /month";
		fld = eval("document." + form.name + ".result" + monthindex + ";");
		fld.value = "$" + Math.ceil((newfee * ratio * 100)/100);
	}

	return true;
}
