/* Credit Card Calculations */

function calculatePayments(num) {
	objAmount = parseFloat(document.getElementById("calcAmount" + num).value);
	objAPR = parseFloat(document.getElementById("calcAPR" + num).value);
	objDaysMonth = parseFloat(document.getElementById("calcDaysMonth" + num).value);
	objDaysYear = parseFloat(document.getElementById("calcDaysYear" + num).value);
	
  objIntDay = document.getElementById("calcIntDay" + num);
	objIntMonth = document.getElementById("calcIntMonth" + num);
	objIntYear = document.getElementById("calcIntYear" + num);
  
 // Math.round(num * 100) / 100;
  
  objIntDay.value = Math.round(parseFloat(((objAPR/objDaysYear) * objAmount)/100) * 100) / 100;
  objIntMonth.value = Math.round(parseFloat((((objAPR/objDaysYear) * objAmount)/100) * objDaysMonth) * 100) / 100;
  objIntYear.value = Math.round(parseFloat((((objAPR/objDaysYear) * objAmount)/100) * objDaysYear) * 100) / 100; 
  
  if(objAPR != null && objAPR > 0) {
  	displayQualifyingOffer();
  }
}

function calculateTransferFee(num) {
	objAmount = parseFloat(document.getElementById("calcAmount" + num).value);
	objTransferRate = parseFloat(document.getElementById("calcTransferRate" + num).value);
	
    objIntTransferFee = document.getElementById("calcIntTransferFee" + num);
	objIntTransferCost = document.getElementById("calcIntTransferCost" + num);
  
	objIntTransferFee.value = Math.round(parseFloat((objAmount * objTransferRate)/100) * 100) / 100;
	objIntTransferCost.value = parseFloat(objAmount) + parseFloat(objIntTransferFee.value);
	
	if(objTransferRate != null && objTransferRate > 0) {
  		displayQualifyingOffer();
  	}
}

function calculatePayoff(num) {
	objAmount = document.getElementById("calcAmount" + num);
	objAPR = document.getElementById("calcAPR" + num);
	objMonthlyPayment = document.getElementById("calcMonthlyPayment" + num);
	objMonthsUntilPayoff = document.getElementById("calcMonthsUntilPayoff" + num);
	objYearsUntilPayoff = document.getElementById("calcYearsUntilPayoff" + num);

	floatAmount = parseFloat(objAmount.value);
	floatAPR = parseFloat(objAPR.value);
	floatMonthlyPayment = parseFloat(objMonthlyPayment.value);
	floatMonthsUntilPayoff = parseFloat(objMonthsUntilPayoff.value);
	
	objMonthsUntilPayoff.value = Math.ceil((floatAmount + (floatAmount*(floatAPR/100)))/floatMonthlyPayment);	
	objYearsUntilPayoff.value = Math.round((floatMonthsUntilPayoff / 12)*100)/100;
}

function calculateMonthlyPayment(num) {
	objAmount = document.getElementById("calcAmount" + num);
	objAPR = document.getElementById("calcAPR" + num);
	objMonthlyPayment = document.getElementById("calcMonthlyPayment" + num);
	objMonthsUntilPayoff = document.getElementById("calcMonthsUntilPayoff" + num);
	objYearsUntilPayoff = document.getElementById("calcYearsUntilPayoff" + num);

	floatAmount = parseFloat(objAmount.value);
	floatAPR = parseFloat(objAPR.value);
	floatMonthlyPayment = parseFloat(objMonthlyPayment.value);
	floatMonthsUntilPayoff = parseFloat(objMonthsUntilPayoff.value);
	
	objMonthlyPayment.value = (floatAmount + (floatAmount*(floatAPR/100)))/floatMonthsUntilPayoff;
	objYearsUntilPayoff.value = Math.round((floatMonthsUntilPayoff / 12)*100)/100;
}

function generateAvgInput(numDays) {
	objAvgDailyInputs = document.getElementById("avg-daily-inputs");
	objDailyAverageBalance = document.getElementById("txtDailyAverageBalance1");
	
	objDailyAverageBalance.value = 0;
	
	objAvgDailyInputs.innerHTML = "";	
	for(num=1;num<=numDays;num++) {
		objAvgDailyInputs.innerHTML += "<div style=\"padding:5px 0px 5px 0px;\">";
		objAvgDailyInputs.innerHTML += "<label class=\"avg-label\" style=\"display:inline-block;font-size:100%;width:100px;\">Day " + num + "</label> $&nbsp;<input name=\"calcDayBalance" + num + "\" id=\"calcDayBalance" + num + "\" type=\"text\" class=\"input-field\" onkeyup=\"javascript:calculateDailyAvgBalance('" + numDays +"')\" />";
		objAvgDailyInputs.innerHTML += "</div>";
	}
}

function calculateDailyAvgBalance(numDays) {
	objDailyAverageBalance = document.getElementById("txtDailyAverageBalance1");
	var floatTotal = 0;
	var floatDailyAverageBalance = 0;
	
	for(num=1;num<=numDays;num++) {
		objBalance = document.getElementById("calcDayBalance" + num);
		floatTotal += parseFloat(objBalance.value);
		floatDailyAverageBalance = floatTotal / numDays;
	}
	
	objDailyAverageBalance.value = Math.round(floatDailyAverageBalance*100) / 100;
}


/* Mortgage Calculations */
function calculateMonthlyMortgage() {
	//M = P [ (r/12)(1 + (r/12))^n ] / [ (1 + (r/12))n - 1]
	//M = P * ( J / (1 - (1 + J) ** -N))
	
	objPrinciple = parseFloat(document.getElementById("mortPrinciple").value);
	objDownPayment = parseFloat(document.getElementById("mortDownPayment").value);
	objInterestRate = parseFloat(document.getElementById("mortInterestRate").value);
	objMortgageTerm = parseFloat(document.getElementById("mortTerm").value);
	
	objFloatBorrow = objPrinciple - objDownPayment;
	objFloatMonthlyInterest = objInterestRate / (12 * 100);
	objFloatMonths = objMortgageTerm * 12;
	objFloatMonthlyPayment = objFloatBorrow * ( objFloatMonthlyInterest / (1 - Math.pow((1+objFloatMonthlyInterest),(objFloatMonths * -1)) ));
		
    objFloatMonthlyMortgage = document.getElementById("mortMonthlyMortgage");
    objFloatMonthlyMortgage.value = Math.round(objFloatMonthlyPayment*100)/100;
  	
  	//var P = objPrinciple - objDownPayment;
	//var J = objInterestRate / (12 * 100);
	//var N = objMortgageTerm * 12;
  	//objFloatMonthlyMortgage.value = P * ( J / (1 - Math.pow((1+J),(N * -1)) ));	
}


/* Health Calculations */
function calculateBMI() {
	objRadioImperial = document.getElementById("rdoMeasurementImperial");
	objRadioMetric = document.getElementById("rdoMeasurementMetric");
	
	if(objRadioImperial.checked == true) {
		objWeight = parseFloat(document.getElementById("bmiWeight").value);
		objHeight = parseFloat(document.getElementById("bmiHeight").value);
		objBMI = document.getElementById("bmiTotal");
		
		objFloatBMI = (objWeight * 703) / Math.pow(objHeight,2);
		
		objBMI.value = Math.round(objFloatBMI*100)/100;
	} else {
		objWeight = parseFloat(document.getElementById("bmiWeightMetric").value);
		objHeight = parseFloat(document.getElementById("bmiHeightMetric").value);
		objBMI = document.getElementById("bmiTotalMetric");
		
		objFloatBMI = (objWeight) / Math.pow(objHeight,2);
		
		objBMI.value = Math.round(objFloatBMI*100)/100;
	}
}


function calculateBodyFat() {
	objRadioMale = document.getElementById("rdoBodyfatMale");
	objRadioFemale = document.getElementById("rdoBodyfatFemale");
	
	if(objRadioMale.checked == true) {
		objWeight = parseFloat(document.getElementById("bodyfatWeight").value);
		objWaist = parseFloat(document.getElementById("bodyfatWaist").value);
		objBodyfat = document.getElementById("bodyfatPercentage");
		
		objFloatStep1 = (objWeight * 1.082) + 94.42;
		objFloatLeanBodyWeight = objFloatStep1 - (objWaist * 4.15);
		objFloatBodyfatPercent = ((objWeight - objFloatLeanBodyWeight) * 100)/objWeight;
		
		
		objBodyfat.value = Math.round(objFloatBodyfatPercent * 100)/100;
	} else {
		objWeight = parseFloat(document.getElementById("bodyfatWeightFemale").value);
		objWaist = parseFloat(document.getElementById("bodyfatWaistFemale").value);
		objWrist = parseFloat(document.getElementById("bodyfatWristFemale").value);
		objHip = parseFloat(document.getElementById("bodyfatHipFemale").value);
		objForearm = parseFloat(document.getElementById("bodyfatForearmFemale").value);
		objBodyfat = document.getElementById("bodyfatPercentageFemale");
		
		objFloatStep1 = objWeight * .732;
		objFloatStep2 = objFloatStep1 + 8.987;
		objFloatStep3 = objWrist / 3.14;
		objFloatStep4 = objWaist * .157;
		objFloatStep5 = objHip * .249;
		objFloatStep6 = objForearm * .434;
		objFloatStep7 = objFloatStep2 + objFloatStep3;
		objFloatStep8 = objFloatStep7 - objFloatStep4;
		objFloatStep9 = objFloatStep8 - objFloatStep5;
		objFloatStep10 = objFloatStep6 + objFloatStep9; //lean body mass
		objFloatStep11 = objFloatStep10 - objFloatStep1
		objFloatBodyfat = (objFloatStep11 * 100) / objWeight; 
		
		objBodyfat.value = Math.round(objFloatBodyfat * 100)/100;
	}
}


/* Automobile Calculations */	        
function calculateCarPayment()
{
	objPrinciple = parseFloat(document.getElementById("mortPrinciple").value);
	//objDownPayment = parseFloat(document.getElementById("mortDownPayment").value);
	objInterestRate = parseFloat(document.getElementById("mortInterestRate").value);
	objMortgageTerm = parseFloat(document.getElementById("mortTerm").value);
	
	objFloatBorrow = objPrinciple;
	objFloatMonthlyInterest = objInterestRate / (12 * 100);
	//objFloatMonths = objMortgageTerm * 12;
	objFloatMonths = objMortgageTerm;
	objFloatMonthlyPayment = objFloatBorrow * ( objFloatMonthlyInterest / (1 - Math.pow((1+objFloatMonthlyInterest),(objFloatMonths * -1)) ));
		
    objFloatMonthlyMortgage = document.getElementById("mortMonthlyMortgage");
    objFloatMonthlyMortgage.value = Math.round(objFloatMonthlyPayment*100)/100;
}


function calculateCarDepreciation()
{
	objMSRP = parseFloat(document.getElementById("carMSRP").value);
	objAge = parseFloat(document.getElementById("carAge").value);
	objFloatValueDepLow = document.getElementById("carDepreciatedValueLow");
	objFloatValueDepAvg = document.getElementById("carDepreciatedValueAvg");
	objFloatValueDepHigh = document.getElementById("carDepreciatedValueHigh");
	
	/*low depreciation, high value - best 27%*/
	floatValueHigh1 = objMSRP * .73;
	
	/*average depreciation, avg value - average 33%*/
	floatValueAvg1 = objMSRP * .67;
	
	/*high depreciation, lowest value - worst 39%*/
	floatValueLow1 = objMSRP * .61;
	
	
	if(objAge <= 1) {
		objFloatValueDepLow.value = Math.round(floatValueLow1*100)/100;
		objFloatValueDepAvg.value = Math.round(floatValueAvg1*100)/100;
		objFloatValueDepHigh.value = Math.round(floatValueHigh1*100)/100;
	} else { 
		floatValueLow = floatValueLow1;
		floatValueAvg = floatValueAvg1;
		floatValueHigh = floatValueHigh1;
		
		for(x=0; x < objAge; x++) {
			floatValueLow = floatValueLow * .80;
			floatValueAvg = floatValueAvg * .80;
			floatValueHigh = floatValueHigh * .80;
		}
		
		objFloatValueDepLow.value = Math.round(floatValueLow*100)/100;
		objFloatValueDepAvg.value = Math.round(floatValueAvg*100)/100;
		objFloatValueDepHigh.value = Math.round(floatValueHigh*100)/100;		
	}

}




/* General Functions */
function showAndHide(panel1, panel2) {
	objPanel1 = document.getElementById(panel1);
	objPanel1 = document.getElementById(panel1);
	
	objPanel2 = document.getElementById(panel2);
	objPanel2 = document.getElementById(panel2);
	
	if(objPanel2.style.visibility == "hidden" || objPanel2.style.display == "none") {
		objPanel1.style.visibility = "hidden";
		objPanel1.style.display = "none";
		objPanel2.style.visibility = "visible";
		objPanel2.style.display = "block";		
	} else {		
		objPanel2.style.visibility = "hidden";
		objPanel2.style.display = "none";
		objPanel1.style.visibility = "visible";
		objPanel1.style.display = "block";			
	}
	
	//document.getElementById("cc-qualifying-offer-index").style.visibility = "visible";
	//document.getElementById("cc-qualifying-offer-index").style.display = "block";
}


function displayErrorField(id) {
	document.getElementById(id).style.color = "#ffffff";
	document.getElementById(id).style.backgroundColor = "#aa0000";
}

function displayQualifyingOffer() {
	document.getElementById("cc-qualifying-offer-index").style.visibility = "visible";
	document.getElementById("cc-qualifying-offer-index").style.display = "block";
}
