function PaymentCalculator(){
	this.formRef;
	this.price = null;
	this.loanTerm = null;
	this.interestRate = null;
	this.downPayment = null;
}
PaymentCalculator.prototype.init = function(formRef, price, loanTerm, interestRate, downPayment){
	this.formRef = formRef;
	this.price = this.stripBadChars(price);
	this.loanTerm = loanTerm;
	this.interestRate = interestRate;
	this.downPayment = this.downPayment;
	this.populateFields();
}
PaymentCalculator.prototype.populateFields = function(){
	this.setTextInput("price", this.price);
	this.setSelectMenu("loanTerm", this.loanTerm);
	this.setTextInput("interestRate", this.interestRate);
	this.setTextInput("downPayment", this.downPayment);
}
PaymentCalculator.prototype.calculateMonthlyPayment = function(){
	this.cleanData();
	if(this.validateFields()){
		var totalCost;
		var payment;
	
	    totalCost = (this.price - this.downPayment);
		if (this.interestRate == 0) {
			payment = totalCost / this.loanTerm;
		} else {
			var monthlyRate = (this.interestRate / 100) / 12; 
			var pow = 1;
			for (var i=0; i<this.loanTerm; i++) {
				pow = pow * (1 + monthlyRate);
			}
			payment = (totalCost * pow * monthlyRate) / (pow - 1);
		}
		payment = parseInt(payment);
		payment = getFormattedPrice(payment);
		document.getElementById("monthlyPaymentValue").innerHTML = payment;
	}
}
PaymentCalculator.prototype.cleanData = function(){
	this.price = this.stripBadChars(this.formRef["price"].value);
	this.loanTerm = getSelected(this.formRef["loanTerm"]).value;
	this.interestRate = this.stripBadChars(this.formRef["interestRate"].value);
	this.downPayment = this.stripBadChars(this.formRef["downPayment"].value);
	this.populateFields();
}
PaymentCalculator.prototype.validateFields = function(){
	clearForm(this.formRef);
	var errorFields = new Array();
	//check all required fields are filled in
	errorFields = getMissingReqs(getRequiredFields(this.formRef));
	allFields = createFieldsHash(this.formRef["all_fields"], "|");
	if (!this.executeResult(errorFields, getErrorMessage(ERROR_ENTERREQUIREDDATA + "\n\n", errorFields))) return false;
	//if down payment is too large return an error
	if (this.downPayment > this.price){
		errorFields.push(this.formRef["price"]);
		errorFields.push(this.formRef["downPayment"]);
		allFields = createFieldsHash(this.formRef["all_fields"], "|");
		if (!this.executeResult(errorFields, ERROR_DOWNPAYMENTTOOLARGE)) return false;
	}
	//if any of the fields is not valid return an error
	if (!this.isValidNum(this.price)) errorFields.push(this.formRef["price"]);
	if (!this.isValidNum(this.interestRate)) errorFields.push(this.formRef["interestRate"]);
	if (!this.isValidNum(this.downPayment)) errorFields.push(this.formRef["downPayment"]);
	allFields = createFieldsHash(this.formRef["all_fields"], "|");
	return this.executeResult(errorFields, getErrorMessage(ERROR_ENTERVALIDDATA + "\n\n", errorFields));
}
PaymentCalculator.prototype.executeResult = function(badFields, message) {
	var returnValue = true;
	if ((arguments.length > 1) && (badFields.length)) {
		returnValue = false;
		changeFieldColor(badFields, yellow);
		alert(message);
	}
	allFields = null;
	return returnValue;
}
PaymentCalculator.prototype.stripBadChars = function(inputData) {
	var num = String(inputData);
	//remove any characters except numbers and period
	var onlyNumbersRE = new RegExp("[^0-9\\.]*", "gi"); 
	num = num.replace(onlyNumbersRE, "");
	if (num != ""){
		return Number(num);
	} else {
		return null;
	}	
}
PaymentCalculator.prototype.isValidNum = function(num){
	if (num == null) return false;
	num = Number(num);
	if (num < 9999999 && num >= 0) return true;
	else return false
}
PaymentCalculator.prototype.setSelectMenu = function(fieldName, fieldValue){
	if (this.formRef[fieldName] != null && fieldValue != null){
		var selectMenu = this.formRef[fieldName];
		var selectedValue = fieldValue;
		for (var i=0; i<selectMenu.options.length; i++){
			if (selectMenu.options[i].value == selectedValue){
				selectMenu.selectedIndex = i;
				break;
			}
		}
	}
}
PaymentCalculator.prototype.setTextInput = function(fieldName, fieldValue){
	if (this.formRef[fieldName] != null && fieldValue != null){
		this.formRef[fieldName].value = fieldValue;
	}
}

