function row_change( frm, scenario, amount ){
	var amount_paid = 0;
	//calculate each row in the scenario
	for( var i = 1; i <= amount; i++ ){
		//Get Year
		eval( "var year = getValueYear( frm, 'sc" + scenario + "_" + i +
			"_year' );" );
		//Get Balance
		eval( "var balance = getValueFloat( frm, 'sc" + scenario + "_" + i + 
			"_balance' );" );
		//Get Rate
		eval( "var rate = getValueFloat( frm, 'sc" + scenario + "_" + i +
			"_rate' );" );
		//Get Term
		eval( "var term = getValueFloat( frm, 'sc" + scenario + "_" + i +
			"_term' );" );
		var years;
		if( i == amount ){
			years = term
		} else {
			eval( "var years = getValueYear( frm, 'sc" + scenario + "_" + 
			(i + 1) + "_year' ) - year;" );
			if( years > term ){
				years = term;
			}
		}
		var months = term * 12;
		var monthly_rate = rate / 12 / 100;
		
		//Calculate Monthly Payment
		var monthly_payment = get_monthly_payment( balance, months,
			monthly_rate );
		//Calculate Amount Paid
		amount_paid += calc_amount_paid( term, years, monthly_payment );
		
		//Calculate Amount Remaining
		var remaining_balance = calc_remaining_balance( balance, term, 
			monthly_rate, years, monthly_payment );
		
		//Set Values
		eval( "setValueCurrency( frm, 'sc" + scenario + "_" + i + 
			"_payment', monthly_payment );" );
		eval( "setValueCurrency( frm, 'sc" + scenario + "_" + i + 
			"_paid', amount_paid );" );
		eval( "setValueYear( frm, 'sc" + scenario + "_" + i + 
			"_end_year', year + years );" );
		eval( "setValueCurrency( frm, 'sc" + scenario + "_" + i + 
			"_remain', remaining_balance );" );			
		if( i != amount ){
			eval( "setValueCurrency( frm, 'sc" + scenario + "_" + (i + 1) + 
				"_balance', remaining_balance );" );
		}
			
	}
}

function get_monthly_payment( balance, months, monthly_rate){
	return balance * ( monthly_rate / 
					   (1 - Math.pow( 1 + monthly_rate, - months) ) );
}

function calc_amount_paid( term, years, monthly_payment ){
	if( years > term ){
		years = term;
	}
	return monthly_payment * years * 12;
}

function calc_remaining_balance( balance, term, monthly_rate, years, monthly_payment ){
	if( years > term ){
		years = term;
	}
								   
	var remaining_balance = balance;
	
	for( var i = 0; i < years * 12; i++ ){
		remaining_balance -= monthly_payment - 
							 (remaining_balance * monthly_rate);
	} 
	
	if( remaining_balance < 0 ){
		remaining_balance = 0;
	}
	return remaining_balance;
}

//Gets a value from a form field and cleans it up into a floating point number
//
//Params:
//form - the form the field is in
//field - the name of the field to grab the info from
function getValueFloat( form, field ){
	var value = eval( "form." + field + ".value" );
	
	//remove non-digit non decimal characters
	value = value.replace( /[^-\d.]/g, "" );
	
	//check to see if we have a valid number
	var pattern = /^-?\d*(\.\d+)?$/;
	if( !pattern.test( value ) ){
		alert( "Not a valid number!" );
		return null;
	} else if( value == "" ){
		return null;
	}
	
	return parseFloat( value );
}

//Gets a value from a form field and cleans it up into a floating point number
//
//Params:
//form - the form the field is in
//field - the name of the field to grab the info from
function getValueYear( form, field ){
	var value = eval( "form." + field + ".value" );
	
	//check to see if we have a valid number
	var pattern = /^\d{4}$/;
	if( !pattern.test( value ) ){
		alert( "Not a valid year!" );
		return null;
	} else if( value == "" ){
		return null;
	}
	
	return parseFloat( value );
}

//Set floating point value into a form field with a US dollar format
//
//Params:
//form - the form the field is in
//field - the name of the field to put the value into
//value - the value to put in the field
function setValueCurrency( form, field, value ){
	if( isNaN( value ) || value == "Infinity" ){
		value = 0;
	}
	
	//Round number
	value *= 100;
	value = Math.round(value);
	value /= 100;
	
	//Add commas
	value = value.toString();
	var pos = value.indexOf(".");
	if( pos == -1 ){
		pos = value.length;
	}
	
	// To prevent commas being placed after a -
	var stop = 0;
	if( value.indexOf("-") != -1 ){
		stop = 1;
	}
	
	for( var i = pos - 3; i > stop; i -= 3 ){
		value = value.substr(0, i) + "," + value.substr(i);
	}
	
	//Make sure we have 2 decimal places
	pos = value.indexOf(".");
	if( pos == -1 ){
		value += ".00";
	} else if( pos == value.length - 2 ){
		value += "0";
	}
	
	//Set field
	eval( "form." + field + ".value = \"$\" +" + "value" );
}

function setValueYear( form, field, value ){
	//Set field
	eval( "form." + field + ".value = value" );
}