Depval = 'Medium';
function checkNumberEntry(input, min, max, msg) {
var str = input.value;
if (str == null || input.length == 0) {
msg = msg + " is blank.  You must fill in this field to calculate.";
alert(msg);
return false;
}
for (var i = 0; i < str.length; i++) {
var curr_ch = str.substring(i, i + 1)
if ((curr_ch < "0" || curr_ch > "9") && curr_ch != '.') {
msg = msg + " should only contain digits.  You entered: " + input.value;
alert(msg);
return false;
}
}
var num = input.value;
if (num < min && (min != "none")) {
msg = msg + " that's too low: " + input.value + ".  You should only enter values greater than " + min + ".";
alert("You have entered a " + msg);
return false;
}
if  (num > max && (max != "none")) {
msg = msg + " that's high: " + input.value + ".  You should only enter values less than " + max + ".";
alert("You have entered a " + msg);
return false;
}
input.value = str;
return true;
}
function myRound(val) {
val = val * 100;
val = Math.round(val);
return val / 100;
}
function computeField(input) {
if (input.value != null && input.value.length != 0)
input.value = "" + eval(input.value);
compute(input.form);
}
function compute(form) {
var Price = form.price.value;
var Curyear = form.cur_year.value;
var Age = form.age.value;
if (!checkNumberEntry(form.price, 0, "none", "Price")) {
form.val.value = "Invalid";
return;
}
if (!checkNumberEntry(form.cur_year, 0, "none", "# years planning to own car")) {
form.val.value = "Invalid";
return;
}
if (!checkNumberEntry(form.age, 0, "none", "age of car at purchase")) {
form.val.value = "Invalid";
return;
}
var depreciation = 0;
if (Depval == "High") {
var dep_pct1 = 0.35;
var dep_pct2 = 0.18;
var dep_pct3 = 0.16;
var dep_pct4 = 0.20;
var dep_pct5 = 0.18;
} else if (Depval == "Medium") {
var dep_pct1 = 0.25;
var dep_pct2 = 0.12;
var dep_pct3 = 0.12;
var dep_pct4 = 0.14;
var dep_pct5 = 0.15;
} else if (Depval == "Low") {
var dep_pct1 = 0.16;
var dep_pct2 = 0.05;
var dep_pct3 = 0.08;
var dep_pct4 = 0.11;
var dep_pct5 = 0.12;
} else {
alert("Error.  You must select a depreciation value for your car.");
return;
}
var counter = Age;
while (counter < Curyear) {
if (counter == 0) depreciation += (dep_pct1 * Price);
if (counter == 1) depreciation += (dep_pct2 * (Price - depreciation));
if (counter == 2) depreciation += (dep_pct3 * (Price - depreciation));
if (counter == 3) depreciation += (dep_pct4 * (Price - depreciation));
if (counter >= 4) depreciation += (dep_pct5 * (Price - depreciation));
counter++;
}
form.depr.value = myRound(depreciation);
form.val.value = myRound(Price - depreciation);
}
function setDepval(string) {
Depval = string;
}
function clearForm(form) {
Depval = '';
form.depr.value = form.val.value = 0;
}
