﻿// Scripts used by the CSA Secure Cost Calculator 
// Copyright 2007 BluTrend LLC.

//Requires the Common.js file also be loaded on the same page using this script file.
//Gets Trim, IsInt, IsNumber, IsMoney, RemoveCommas from that file.

//Color settings
var bgOk = 'white';
var bgSelected = 'yellow';
var bgEr = 'orange';

//Internal Variables
var unitCount = 0;
var rate = .14;
var billPmtFee = 0;
var vcrFee = 0;
var perBillMgmtCost = 0;
var mktIncentive = 0;
var capRate = 6;

function Validate(currentStep)
{
	var hasError = false;
	
	//Clear the variables
	unitCount = 0;
	
	// --==-- UNIT COUNT
	if (Trim(form.Units.value) == "")
	{
		if (currentStep > 1)
		{
			HighlightError(form.Units, "UnitsMsg", "&nbsp;Required&nbsp;", hasError);
			hasError = true;
		}
		else
		{
			HighlightClear(form.Units, "UnitsMsg", (currentStep == 1));
		}
	}
	else if (!IsInt(form.Units.value))
	{
		HighlightError(form.Units, "UnitsMsg", "&nbsp;1 - 100000&nbsp;", hasError);
		hasError = true;
	}
	else if (RemoveCommas(form.Units.value) < 1 || RemoveCommas(form.Units.value) > 100000)
	{
		HighlightError(form.Units, "UnitsMsg", "&nbsp;1 - 100,000&nbsp;", hasError);
		hasError = true;
	}
	else
	{
		HighlightClear(form.Units, "UnitsMsg", (currentStep == 1));
		unitCount = parseInt(RemoveCommas(form.Units.value));
		form.Units.value = unitCount;
	}
	
	// --==-- ELECTRICITY RATE
	if (Trim(form.Rate.value) == "")
	{
		if (currentStep > 2)
		{
			HighlightError(form.Rate, "RateMsg", "&nbsp;Required&nbsp;", hasError);
			hasError = true;
		}
		else
			HighlightClear(form.Rate, "RateMsg", (currentStep == 2));
	}
	else if (!IsMoney(form.Rate.value))
	{
		HighlightError(form.Rate, "RateMsg", "&nbsp;0.001 - 5.000&nbsp;", hasError);
		hasError = true;
	}
	else if (parseFloat(form.Rate.value) < 0.001 || parseFloat(form.Rate.value) > 5)
	{
		HighlightError(form.Rate, "RateMsg", "&nbsp;0.001 - 5.000&nbsp;", hasError);
		hasError = true;
	}
	else
	{
		HighlightClear(form.Rate, "RateMsg", (currentStep == 2));
		rate = parseFloat(form.Rate.value);
	}
	
	if (form.OutsourcesBillPmt_0.checked) //Yes, they use bill payment outsourcing
	{
		//Clear highlighting in the Internal Bill Management section
		HighlightClear(form.PerBillMgmtCost, "PerBillMgmtCostMsg", false);
	
		// --==-- VACANT ELECTRIC BILL PAYMENT FEE
		if (Trim(form.BillPmtFee.value) == "")
		{
			HighlightClear(form.BillPmtFee, "BillPmtFeeMsg", (currentStep == 3));
			billPmtFee = 0;
			form.BillPmtFee.value = billPmtFee;	
		}
		else if (!IsMoney(form.BillPmtFee.value))
		{
			HighlightError(form.BillPmtFee, "BillPmtFeeMsg", "&nbsp;0 - 100&nbsp;", hasError);
			hasError = true;
		}
		else if (parseFloat(form.BillPmtFee.value) < 0 || parseFloat(form.BillPmtFee.value) > 100)
		{
			HighlightError(form.BillPmtFee, "BillPmtFeeMsg", "&nbsp;0 - 100&nbsp;", hasError);
			hasError = true;
		}
		else
		{
			HighlightClear(form.BillPmtFee, "BillPmtFeeMsg", (currentStep == 3));
			billPmtFee = parseFloat(form.BillPmtFee.value);
		}
		
		// --==-- VCR PROCESSING FEE
		if (Trim(form.VCRFee.value) == "")
		{
			HighlightClear(form.VCRFee, "VCRFeeMsg", (currentStep == 4));
			vcrFee = 0;
			form.VCRFee.value = vcrFee;	
		}
		else if (!IsMoney(form.VCRFee.value))
		{
			HighlightError(form.VCRFee, "VCRFeeMsg", "&nbsp;0 - 100&nbsp;", hasError);
			hasError = true;
		}
		else if (parseFloat(form.VCRFee.value) < 0 || parseFloat(form.VCRFee.value) > 100)
		{
			HighlightError(form.VCRFee, "VCRFeeMsg", "&nbsp;0 - 100&nbsp;", hasError);
			hasError = true;
		}
		else
		{
			HighlightClear(form.VCRFee, "VCRFeeMsg", (currentStep == 4));
			vcrFee = parseFloat(form.VCRFee.value);
		}
	}
	else
	{
		//Clear highlighting in the bill payment outsourcing section
		HighlightClear(form.BillPmtFee, "BillPmtFeeMsg", false);
		HighlightClear(form.VCRFee, "VCRFeeMsg", false);
		
	
		// --==-- BILL PROCESSING COST (done internally)
		if (Trim(form.PerBillMgmtCost.value) == "")
		{
			HighlightClear(form.PerBillMgmtCost, "PerBillMgmtCostMsg", (currentStep == 5));
			perBillMgmtCost = 0;
			form.VCRFee.value = perBillMgmtCost;	
		}
		else if (!IsMoney(form.PerBillMgmtCost.value))
		{
			HighlightError(form.PerBillMgmtCost, "PerBillMgmtCostMsg", "&nbsp;0 - 100&nbsp;", hasError);
			hasError = true;
		}
		else if (parseFloat(form.PerBillMgmtCost.value) < 0 || parseFloat(form.PerBillMgmtCost.value) > 100)
		{
			HighlightError(form.PerBillMgmtCost, "PerBillMgmtCostMsg", "&nbsp;0 - 100&nbsp;", hasError);
			hasError = true;
		}
		else
		{
			HighlightClear(form.PerBillMgmtCost, "PerBillMgmtCostMsg", (currentStep == 5));
			perBillMgmtCost = parseFloat(form.PerBillMgmtCost.value);
		}
	}
	
	// --==-- MARKETING AGREEMENT
	if (form.MarketingAgreement_0.checked)  //Yes they have a marketing agreement
	{
		if (Trim(form.MarketingIncentive.value) == "")
		{
			HighlightClear(form.MarketingIncentive, "MarketingIncentiveMsg", (currentStep == 6));
			mktIncentive = 0;
			form.MarketingIncentive.value = mktIncentive;	
		}
		else if (!IsMoney(form.MarketingIncentive.value))
		{
			HighlightError(form.MarketingIncentive, "MarketingIncentiveMsg", "&nbsp;0 - 200&nbsp;", hasError);
			hasError = true;
		}
		else if (parseFloat(form.MarketingIncentive.value) < 0 || parseFloat(form.MarketingIncentive.value) > 200)
		{
			HighlightError(form.MarketingIncentive, "MarketingIncentiveMsg", "&nbsp;0 - 200&nbsp;", hasError);
			hasError = true;
		}
		else
		{
			HighlightClear(form.MarketingIncentive, "MarketingIncentiveMsg", (currentStep == 6));
			mktIncentive = parseFloat(form.MarketingIncentive.value);
		}
	}
	else
	{
		HighlightClear(form.MarketingIncentive, "MarketingIncentiveMsg", false);
		mktIncentive = 0;
	}
	
	// --==-- MARKET CAP RATE
	if (Trim(form.CapRate.value) == "")
	{
		HighlightClear(form.CapRate, "CapRateMsg", (currentStep == 7));
		capRate = 0;
		form.CapRate.value = capRate;	
	}
	else if (!IsNumber(form.CapRate.value))
	{
		HighlightError(form.CapRate, "CapRateMsg", "&nbsp;0 - 100&nbsp;", hasError);
		hasError = true;
	}
	else if (parseFloat(form.CapRate.value) < 0 || parseFloat(form.CapRate.value) > 100)
	{
		HighlightError(form.CapRate, "CapRateMsg", "&nbsp;0 - 100&nbsp;", hasError);
		hasError = true;
	}
	else
	{
		HighlightClear(form.CapRate, "CapRateMsg", (currentStep == 7));
		capRate = parseFloat(form.CapRate.value);
	}

	return !hasError;
}

function Recalculate(currentStep)
{
	var adjFactor = 0;
	var miCostPerUnit = 44;
	var moCostPerUnit = 23;
	var miTotalViolationCost = 0;
	var moTotalViolationCost = 0;
	var miViolatorCount = 0;
	var moViolatorCount = 0;
	var miViolatorMthCount = 0;
	var moViolatorMthCount = 0;
	var miBillPmtFee = 0;
	var moBillPmtFee = 0;
	var miVCRFee = 0;
	var moVCRFee = 0;
	var miProcessFee = 0;
	var moProcessFee = 0;
	var totalMktIncentive = 0;
	var totalCostWithVCR = 0;
	var totalCostWithoutVCR = 0;
	
	var miViolatorCountMultiplier = 0.0567;
	var moViolatorCountMultiplier = 0.07;
	var miViolatorMthMultiplier = 0.134940364;
	var moViolatorMthMultiplier = 0.144819413;

	if (!Validate(currentStep))
		return;
		
	//Reformat fields that need formatting
	switch (currentStep)
	{
		case 2:
			form.Units.value = AddCommas(unitCount);
			break;
		default:
			break;
	}
	
	//Calculate the Estimated Number of MI and MO Violators
	miViolatorCount = unitCount * miViolatorCountMultiplier;
	moViolatorCount = unitCount * moViolatorCountMultiplier;
	
	//Calculate the number of Move-In and Move-Out Violator months in a year using the Violator Multipliers
	miViolatorMthCount = unitCount * miViolatorMthMultiplier;
	moViolatorMthCount = unitCount * moViolatorMthMultiplier;
	
	//Calculate the $/Unit if the rate is not the default $0.14 or when it is going to back to default from something else
	if (rate != 0.14 || parseInt(form.TotalCostPerUnit.value) != 67)
	{
		adjFactor = rate / 0.14;
		miCostPerUnit = Math.round(adjFactor * miCostPerUnit);
		moCostPerUnit = Math.round(adjFactor * moCostPerUnit);
		
		form.MICostPerUnit.value = miCostPerUnit;
		form.MOCostPerUnit.value = moCostPerUnit;
		form.TotalCostPerUnit.value = miCostPerUnit + moCostPerUnit;
	}
	
	//Calculate the Violation Costs for all Dallas/Houston properties
	miTotalViolationCost = miCostPerUnit * unitCount;
	moTotalViolationCost = moCostPerUnit * unitCount;
	
	form.MIViolationCost.value = AddCommas(miTotalViolationCost);
	form.MOViolationCost.value = AddCommas(moTotalViolationCost);
	form.TotalViolationCost.value = AddCommas(miTotalViolationCost + moTotalViolationCost);
	
	//Process the Outsource billing section
	if (form.OutsourcesBillPmt_0.checked) //Uses outsource billing
	{
		//Calculate the Total Bill Payment Fee Per Year
		if (billPmtFee > 0)
		{
			miBillPmtFee = billPmtFee * miViolatorMthCount;
			moBillPmtFee = billPmtFee * moViolatorMthCount;
		}
		
		form.TotalBillPmtFee.value = AddCommas(Math.round(miBillPmtFee + moBillPmtFee));
		
		//Calculate the Total VCE Processing Fee Per Year
		if (vcrFee > 0)
		{
			miVCRFee = vcrFee * miViolatorMthCount;
			moVCRFee = vcrFee * moViolatorMthCount;
		}
		
		form.TotalVCRFee.value = AddCommas(Math.round(miVCRFee + moVCRFee));
		
		//Calculate the Total Processing Fee Per Year
		form.TotalProcessCost.value = AddCommas(Math.round(miBillPmtFee + moBillPmtFee + miVCRFee + moVCRFee));
	}
	else  //Don't use outsource billing
	{
		//Calculate the Total Per Bill Management Cost (when there is no 3rd party bill processor).
		if (perBillMgmtCost > 0)
		{
			miProcessFee = perBillMgmtCost * miViolatorMthCount;
			moProcessFee = perBillMgmtCost * moViolatorMthCount;
		}
		
		form.TotalPerBillMgmtCost.value = AddCommas(Math.round(miProcessFee + moProcessFee));
	}	
	
	//Calculate the Marketing Incentive
	totalMktIncentive = mktIncentive * miViolatorCount;  //Only uses MI (it is possible to get incentives on a resident who moved out, but not as easy as move-in so it is left out here

	form.MarketingIncentiveLosses.value = AddCommas(Math.round(totalMktIncentive));
	
	//Calculate the Total Savings (both with and without VCR)
	//With = H+M+Q OR H+O+Q
	//Without = F+J+Q OR F+O+Q
	if (form.OutsourcesBillPmt_0.checked)
	{
		totalCostWithVCR = moTotalViolationCost + (miBillPmtFee + moBillPmtFee + miVCRFee + moVCRFee) + totalMktIncentive;
		totalCostWithoutVCR = (miTotalViolationCost + moTotalViolationCost) + (miBillPmtFee + moBillPmtFee) + totalMktIncentive;
	}
	else
	{
		totalCostWithVCR = moTotalViolationCost + (miProcessFee + moProcessFee) + totalMktIncentive;
		totalCostWithoutVCR = (miTotalViolationCost + moTotalViolationCost) + (miProcessFee + moProcessFee) + totalMktIncentive;
	}
	
	form.SavingsWithVCR.value = AddCommas(Math.round(totalCostWithVCR));
	form.SavingsWithoutVCR.value = AddCommas(Math.round(totalCostWithoutVCR));
	
	//Calculate the Valuation Increase (with and without VCR)
	if (capRate == 0)
	{
		form.ValuationWithVCR.value = "";
		form.ValuationWithoutVCR.value = "";
	}
	else
	{
		capRate = capRate / 100;
		form.ValuationWithVCR.value = AddCommas(Math.round(totalCostWithVCR / capRate));
		form.ValuationWithoutVCR.value = AddCommas(Math.round(totalCostWithoutVCR / capRate));
	}
}

function OutSourceBillPmt_Click()
{
	if (form.OutsourcesBillPmt_0.checked)
	{
		//Yes
		form.BillPmtFee.readOnly = false;
		form.VCRFee.readOnly = false;
		form.PerBillMgmtCost.readOnly = true;
		
		I1.disabled = I2.disabled = I3.disabled = I4.disabled = false;
		J1.disabled = J2.disabled = J3.disabled = J4.disabled = false;
		K1.disabled = K2.disabled = K3.disabled = K4.disabled = false;
		L1.disabled = L2.disabled = L3.disabled = L4.disabled = false;
		M1.disabled = M2.disabled = M3.disabled = M4.disabled = false;
		N1.disabled = N2.disabled = N3.disabled = N4.disabled = true;
		O1.disabled = O2.disabled = O3.disabled = O4.disabled = true;
		
		Recalculate(3);
		form.BillPmtFee.focus();
	}
	else
	{
		//No
		form.BillPmtFee.readOnly = true;
		form.VCRFee.readOnly = true;
		form.PerBillMgmtCost.readOnly = false;
		
		I1.disabled = I2.disabled = I3.disabled = I4.disabled = true;
		J1.disabled = J2.disabled = J3.disabled = J4.disabled = true;
		K1.disabled = K2.disabled = K3.disabled = K4.disabled = true;
		L1.disabled = L2.disabled = L3.disabled = L4.disabled = true;
		M1.disabled = M2.disabled = M3.disabled = M4.disabled = true;
		N1.disabled = N2.disabled = N3.disabled = N4.disabled = false;
		O1.disabled = O2.disabled = O3.disabled = O4.disabled = false;
		
		Recalculate(5);
		form.PerBillMgmtCost.focus();
	}
}

function MarketingAgreement_Click()
{
	if (form.MarketingAgreement_0.checked)
	{
		//Yes
		form.MarketingIncentive.readOnly = false;
		P1.disabled = P2.disabled = P3.disabled = P4.disabled = false;
		Q1.disabled = Q2.disabled = Q3.disabled = Q4.disabled = false;
	
		Recalculate(6);
	}
	else
	{
		//No
		form.MarketingIncentive.readOnly = true;
		P1.disabled = P2.disabled = P3.disabled = P4.disabled = true;
		Q1.disabled = Q2.disabled = Q3.disabled = Q4.disabled = true;
		
		Recalculate(7);
	}
}

function HighlightClear(ctrl, msgCtrlNm, hasFocus)
{
	var msgCtrl = document.getElementById(msgCtrlNm);

	if (hasFocus)
		ctrl.style.backgroundColor = bgSelected;
	else
		ctrl.style.backgroundColor = bgOk;
	
	if (msgCtrl != null)
	{
		msgCtrl.style.visibility = "hidden";
		msgCtrl.innerHTML = "";
	}
}

function HighlightError(ctrl, msgCtrlNm, msg, hasError)
{
	var msgCtrl = document.getElementById(msgCtrlNm);

	ctrl.style.backgroundColor = bgEr;
	
	if (msgCtrl != null)
	{
		msgCtrl.style.visibility = "visible";
		msgCtrl.innerHTML = msg;
	}
}
