var downPaymentOptionLabelText = "{0}% down";

function updateDownPaymentSelectBox(hDownPaymentSelect, hHomeValueField) {
	var form = hDownPaymentSelect.form;
	var lastValue = getFormFieldValue(hDownPaymentSelect);
	var homeValue = getInteger(getFormFieldValue(hHomeValueField));
	var showDollarValue = (homeValue != 0) ? true : false;

	var opts = hDownPaymentSelect.options;
	var tmpLabel;
	var tmpDollarValue;
	for (var i=0; i < opts.length; i++) {
		if ((opts[i].value == "") || isNaN(opts[i].value)) { continue; }
		tmpLabel = downPaymentOptionLabelText.replace("{0}", opts[i].value);
		if (showDollarValue) {
			tmpDollarValue = new String(calculateDownPaymentValue(parseInt(opts[i].value), homeValue));
			tmpLabel = "$" + addCommasToNumString(tmpDollarValue) + " (" + tmpLabel + ")";
		}
		
		hDownPaymentSelect.options[i].text = tmpLabel;
	}

	var msieFix = "fixDownPaymentBoxForMSIE('" + form.name + "', '" + hDownPaymentSelect.name + "', '" + lastValue + "');";
	setTimeout(msieFix, 50);
	
	form.down_payment_other.value = homeValue;
	
}


function calculateDownPaymentValue(percent, price) {
	return Math.ceil(price * (percent / 100));
}


function fixDownPaymentBoxForMSIE(formName, fieldName, lastValue) {
	var select = document.forms[formName].elements[fieldName];
 	if (select.selectedIndex == -1) {
 	    if (isValueInSelectbox(select, lastValue)) {
 	        select.value = lastValue;
 	    } else {
 	        select.value = "";
 	    }
 	}
}

function doDownPaymentSelectBoxLogic(hSelectbox) {
	var form = hSelectbox.form;
	toggleOtherField(hSelectbox, "otherDownPaymentBlock");
	if (hSelectbox.value == "other") {
		form.down_payment_other.value = "";
		form.DOWN_PMT.value = "";
	} else {
		if (form.down_payment_other.value != 0) {
			form.DOWN_PMT.value = calculateDownPaymentValue(parseInt(hSelectbox.value), form.down_payment_other.value);
		} else {
			form.DOWN_PMT.value = calculateDownPaymentValue(parseInt(hSelectbox.value), form.EST_VAL.value);
	}
}
}
