// New Leaf debt calculator programmed by Code Mechanic (http://www.codemechanic.com)
// All Rights Reserved
// Last updated 03/19/05

// This function replaces bogus characters in a given string
function replaceChars(string) {
	//match against bad character equivalents in URL search string
	string = string.replace(/\%+\w{2}/g,"");
	//match against bad literal characters
	string = string.replace(/[^0-9|\.]/g,"");
	return string;
}

// This function prepopulates the debt calculator form, assuming the total debt value is passed in the URL
// It is called from the body onLoad event in includes/body_cm.inc
function fillForm() {
	var debthandoff = replaceChars(location.search.substr(location.search.indexOf("?")+7));
	document.debt_calculator.tdebt.value=debthandoff;
	if (location.search) {
		etdvalidate(document.debt_calculator);
	}
	else {
		document.debt_calculator.tdebt.value = "10000";
	}
}

function clearvalues(form){
	//reset form values
	form.tdebt.value = '';
	form.payment_rate.value = '1';
	form.monthly.value = '';
	form.months.value = '';
	form.cost.value = '';
	form.tdebt.focus();
}

function etdvalidate(form){
	
	//setup variables
	var strString = form.tdebt.value;
	var strValid = '0123456789.';
	
	//validation logic
	if (strString.length == 0){
		//alert('Please enter an estimated total debt.');
		clearvalues(form);
		return false;
		}
	if (strString < 7500){
		alert('Please enter an estimated total debt larger than $7500.');
		clearvalues(form);
		return false;
		}
	for (i=0; i < strString.length; i++){		
		var x = strString.charAt(i);
		if (strValid.indexOf(x,0) != -1){
		}
		else {
			alert ('With regard to your estimated total debt, please enter numeric values only.\nDollar signs, letters, commas, and periods are not accepted.');
			clearvalues(form);
			return false;
		}
	}
}

function computetotals(form){

	//setup variables
	var totaldebt = form.tdebt.value;
	var monthlypayment;
	var monthlypayment2 = Math.round(.0180 * totaldebt);
	var monthlypayment3 = Math.round(.0225 * totaldebt);
	var monthlypayment4 = Math.round(.0275 * totaldebt);
	var monthlypayment5 = Math.round(.0325 * totaldebt);

	//validation logic
	if ( form.tdebt.value == '') {
		alert ('Please enter an estimated total debt before choosing your monthly payment level.');
		form.payment_rate.value = '1';
		form.monthly.value = '';
		form.months.value = '';
		form.cost.value = '';
		form.tdebt.focus();
		return false;
	}
	if ( form.payment_rate.value == '1') {
		form.payment_rate.value = '1';
		form.monthly.value = '';
		form.months.value = '';
		form.cost.value = '';
		return false;
	}

	//establish monthly payment level
	if (form.payment_rate.value == '2' ){
		monthlypayment = monthlypayment2;
	}
	if (form.payment_rate.value == '3' ){
		monthlypayment = monthlypayment3;
	}
	if (form.payment_rate.value == '4' ){
		monthlypayment = monthlypayment4;
	}
	if (form.payment_rate.value == '5' ){
		monthlypayment = monthlypayment5;
	}
	
	//math
	var setupfees = Math.round((.06 * totaldebt) * 100) / 100;
	var settlementcreditors = Math.round((.35 * totaldebt) * 1000) / 1000;
	var settlementnewleaf = Math.round(((totaldebt - settlementcreditors) * .25) * 1000) / 1000;
	var monthscompletion = Math.round(((setupfees + settlementcreditors  + settlementnewleaf) / (monthlypayment - 25)) * 100) / 100;
	var monthlymaintenance = Math.round(Math.round(monthscompletion) * 25);
	var clientcost = (setupfees + monthlymaintenance + settlementcreditors  + settlementnewleaf);
	var clientcostpercentage = (clientcost / totaldebt);
	
	var calcforexcesstemp = Math.round((monthscompletion - Math.round(monthscompletion)) * 100) / 100;
	var calcforexcess = 0;
	if ( calcforexcesstemp > 0 ){
		calcforexcess = 1 - calcforexcesstemp;
	}
	else {
		calcforexcess = 0 - calcforexcesstemp;
	}
	
	//kept for reference to the next statement following these comments
	//var calcforexcess = 1 - Math.abs(Math.round((monthscompletion - Math.round(monthscompletion)) * 100) / 100);	
	var remainderbeforemaint = Math.round((calcforexcess * monthlypayment) * 100) / 100;
	var totalmaint = Math.round((monthlymaintenance - remainderbeforemaint) * 100) / 100;
	var additionalmonths = Math.round((totalmaint / monthlypayment) * 100) / 100;
	
	var programlength = Math.round(additionalmonths + monthscompletion);
	var costestimate = programlength * monthlypayment;
	
	//send output to form
	form.monthly.value = '$' + monthlypayment;
	form.months.value = Math.abs(programlength);
	form.cost.value = '$' + Math.abs(costestimate);	
	
	//report generation
	/*
	alert (
		   'LOGIC REPORT (this is for testing puposes only)\n' +
		   '----------------------------------------------------\n\n' +
		   'Estimated Total Debt: ' + totaldebt + '\n' +
		   'Select payment: ' + monthlypayment + '\n\n' +
		   'Payment Option 1 = ' + monthlypayment2 + '\n' +
		   'Payment Option 2 = ' + monthlypayment3 + '\n' +
		   'Payment Option 3 = ' + monthlypayment4 + '\n' +
		   'Payment Option 4 = ' + monthlypayment5 + '\n\n' +
		   'Set-up fees - total = ' + setupfees + '\n' +
		   'Estimated settlement amount to creditors = ' + settlementcreditors + '\n' +
		   'Estimated settlement fees to New Leaf = ' + settlementnewleaf + '\n' +
		   'Months 'til completion = ' + monthscompletion + '\n' +
		   'Monthly maintenance - total = ' + monthlymaintenance + '\n' +
		   'Total cost to client = ' + clientcost + '\n' +
		   'Client cost ratio = ' + clientcostpercentage + '\n\n' +
		   'Calc for excess = ' + calcforexcess + '\n' +
		   'Remainder before maint. Fee calc. = ' + remainderbeforemaint + '\n' +
		   'Total maint fee less acct remainder = ' + totalmaint + '\n' +
		   'Add'l mos to pay maint. Fee = ' + additionalmonths + '\n\n' +
		   'Using New Leaf's program, we estimate about: ' + programlength + ' months until debt freedom!' + '\n' +
		   'Estimated cost of the New Leaf Debt Settlement program: ' + costestimate);
	*/
}