function calculateBorrowing(theForm) {
	if(theForm.incomeApp1.value == "" || isNaN(theForm.incomeApp1.value)){theForm.incomeApp1.value = 0;}
	if(theForm.incomeBonusComm1.value == "" || isNaN(theForm.incomeBonusComm1.value)){theForm.incomeBonusComm1.value = 0;}
	if(theForm.incomeApp2.value == "" || isNaN(theForm.incomeApp2.value)){theForm.incomeApp2.value = 0;}
	if(theForm.incomeBonusComm2.value == "" || isNaN(theForm.incomeBonusComm2.value)){theForm.incomeBonusComm2.value = 0;}

	var totalOfApplicant1 = parseInt(theForm.incomeApp1.value) + parseInt((theForm.incomeBonusComm1.value * 0.5));
	var totalOfApplicant2 = parseInt(theForm.incomeApp2.value) + parseInt((theForm.incomeBonusComm2.value * 0.5));
	var singleApplicantLimit = totalOfApplicant1 * 4;
	var jointApplicantLimit = (totalOfApplicant1 + totalOfApplicant2) * 3;

	if(isNaN(jointApplicantLimit)) {
		jointApplicantLimit = 0;
	}
	if(isNaN(singleApplicantLimit)) {
		singleApplicantLimit = 0;
	}
	if (singleApplicantLimit > jointApplicantLimit) {
		theForm.txtBorrowMaxAmount.value = currencyConvert(singleApplicantLimit);
	} else {
		theForm.txtBorrowMaxAmount.value = currencyConvert(jointApplicantLimit);
	}
}

function calculatePayment(theForm) {
	if(theForm.ddlRepaymentType.selectedIndex == 0) {
		var intRate = parseFloat(theForm.txtRateWholeNumber.value+"."+theForm.txtRateDecimalNumber.value);
		theForm.txtMonthlyPayment.value = currencyConvert((theForm.txtPrincipalAmount.value * (intRate / 100)) / 12);
	} else {
		var rate = (parseFloat(theForm.txtRateWholeNumber.value+"."+theForm.txtRateDecimalNumber.value) / 100) / 12;
		var calcVal = (parseFloat(theForm.txtPrincipalAmount.value * rate)) / (parseFloat(1 - Math.pow((1 + rate),-(theForm.ddlLoanDuration.options[theForm.ddlLoanDuration.selectedIndex].value*12))));
		
		theForm.txtMonthlyPayment.value = currencyConvert(calcVal);
	}
}

function calculateSaving(theForm) {
	var currMonthlyPayment2 = 0;
	var BBMonthlyPayment = 0;
	var monthlySaving = 0;
	if(theForm.ddlRepaymentType.selectedIndex == 0) {
		var intRate = parseFloat(theForm.txtCurrentRateWholeNumber.value+"."+theForm.txtCurrentRateDecimalNumber.value);
		if(isNaN(intRate) || intRate == 0) {
			currMonthlyPayment2 = parseFloat(theForm.txtCurrentMonthlyPayment1.value);
			theForm.txtCurrentMonthlyPayment2.value = currencyConvert(currMonthlyPayment2);
		} else {
			currMonthlyPayment2 = (parseFloat(theForm.txtPrincipalAmount.value) * (parseFloat(intRate) / 100)) / 12;
			theForm.txtCurrentMonthlyPayment2.value = currencyConvert(currMonthlyPayment2);
		}
	} else {
		var intRate = parseFloat(theForm.txtCurrentRateWholeNumber.value+"."+theForm.txtCurrentRateDecimalNumber.value);
		if(isNaN(intRate) || intRate == 0) {
			currMonthlyPayment2 = parseFloat(theForm.txtCurrentMonthlyPayment1.value);
			theForm.txtCurrentMonthlyPayment2.value = currencyConvert(currMonthlyPayment2);
		} else {
			var rate = (parseFloat(theForm.txtCurrentRateWholeNumber.value+"."+theForm.txtCurrentRateDecimalNumber.value) / 100) / 12;
			currMonthlyPayment2 = (parseFloat(theForm.txtPrincipalAmount.value * rate)) / (parseFloat(1 - Math.pow((1 + parseFloat(rate)),-(parseFloat(theForm.ddlLoanDuration.options[theForm.ddlLoanDuration.selectedIndex].value*12)))))
			theForm.txtCurrentMonthlyPayment2.value = currencyConvert(currMonthlyPayment2);
		}
	}

	var newIntRateRaw = parseFloat(theForm.txtBBRateWholeNumber.value+"."+theForm.txtBBRateDecimalNumber.value);
	var newIntRate = (parseFloat(newIntRateRaw) / 100) / 12;
	if(theForm.ddlRepaymentType.selectedIndex == 0) {
		BBMonthlyPayment = (theForm.txtPrincipalAmount.value * (newIntRateRaw / 100)) / 12;
		theForm.txtBBMonthlyPayment.value = currencyConvert(BBMonthlyPayment);
	} else {
		BBMonthlyPayment = (parseFloat(theForm.txtPrincipalAmount.value * newIntRate)) / (parseFloat(1 - Math.pow((1 + newIntRate),-(theForm.ddlLoanDuration.options[theForm.ddlLoanDuration.selectedIndex].value*12))));
		theForm.txtBBMonthlyPayment.value = currencyConvert(BBMonthlyPayment);
	}
	monthlySaving = parseFloat(currMonthlyPayment2) - parseFloat(BBMonthlyPayment);
	if(monthlySaving < 0) {
		theForm.txtMonthlySaving.value = "no saving";
		theForm.txtAnnualSaving.value = "no saving";
	} else {
		theForm.txtMonthlySaving.value = currencyConvert(monthlySaving);
		theForm.txtAnnualSaving.value = currencyConvert(monthlySaving * 12);
	}
}

function calculateFlexible(theForm) {
	var principalAmount = parseFloat(theForm.txtPrincipalAmount.value);
	var monthlyOverPayment = "0";
	var interestRate = parseFloat(theForm.txtRateWholeNumber.value+"."+theForm.txtRateDecimalNumber.value);
	var rate = (interestRate / 100) / 12;
	var loanDuration1 = theForm.ddlLoanDuration.options[theForm.ddlLoanDuration.selectedIndex].value*12;
	var loanDuration2 = "0";
	var monthlyPayment1 = (parseFloat(principalAmount * rate)) / (parseFloat(1 - Math.pow((1 + rate),-(loanDuration1))));
	var totalCost1 = monthlyPayment1 * loanDuration1;
	var totalCost2 = "0";
	theForm.txtTotalCost1.value = currencyConvert(totalCost1);
	theForm.txtMonthlyPayment1.value = currencyConvert(monthlyPayment1);
	theForm.txtLoanDuration1.value = currencyConvert(loanDuration1/12, 1);

	if(!isNaN(theForm.txtOverPayment.value) && theForm.txtOverPayment.value != "") {
		monthlyOverPayment = theForm.txtOverPayment.value;
	}
	var monthlyPayments2 = parseFloat(monthlyPayment1) + parseFloat(monthlyOverPayment)
	theForm.txtMonthlyPayment2.value = currencyConvert(monthlyPayments2);
	
	if(monthlyOverPayment > 0) {
		var top = ((Math.log(((1200 * monthlyPayments2) / ((1200 * monthlyPayments2) - (principalAmount * interestRate)))))/ Math.log(10));
		var bottom = (12 * (Math.log(((1200 + interestRate) / 1200))) / Math.log(10));
		loanDuration2 = parseFloat(top) / parseFloat(bottom);
		theForm.txtLoanDuration2.value = currencyConvert(loanDuration2, 1);
		totalCost2 = parseFloat(monthlyPayments2) * parseFloat(loanDuration2) * 12;
		theForm.txtTotalCost2.value = currencyConvert(totalCost2);
	} else {
		totalCost2 = totalCost1;
		theForm.txtTotalCost2.value = currencyConvert(totalCost2);
		loanDuration2 = loanDuration1/12;
		theForm.txtLoanDuration2.value = currencyConvert(loanDuration2, 1);
	}
	
	var lumpSumAmount = theForm.txtLumpSum.value;
	var lumpSumYear = theForm.txtLumpSumYear.value;
	var remainingPrincipal = 0;
	loanDuration1 = parseFloat(loanDuration1) / 12;

	if ((lumpSumAmount != null && lumpSumAmount > 0) && (lumpSumYear != null && lumpSumYear > 0)) {
		totalCost2 = 0;
		if(parseFloat(lumpSumYear) > parseFloat(loanDuration2)){
			lumpSumYear = parseFloat(loanDuration2);
		} 
		var temp1 = (1 + (parseFloat(interestRate) / 1200));
		var temp2 = (-12 * (parseFloat(loanDuration2)-parseFloat(lumpSumYear)));
		temp1 = Math.pow(parseFloat(temp1), parseFloat(temp2));
		temp1 = (1 - parseFloat(temp1));
		temp1 = (parseFloat(temp1) * 1200 * parseFloat(monthlyPayments2));
		temp2 = (parseFloat(temp1) / parseFloat(interestRate));
		remainingPrincipal = parseFloat(temp2);

		if (parseFloat(lumpSumAmount) > parseFloat(remainingPrincipal)) {
			lumpSumAmount = parseFloat(remainingPrincipal);
		} 
		remainingPrincipal -= parseFloat(lumpSumAmount);
		
		var top = ((Math.log(((1200 * parseFloat(monthlyPayments2)) / ((1200 * parseFloat(monthlyPayments2)) - (parseFloat(remainingPrincipal) * parseFloat(interestRate))))))/ Math.log(10));
		var bottom = (12 * (Math.log(((1200 + parseFloat(interestRate)) / 1200))) / Math.log(10));
		loanDuration2 = (parseFloat(top) / parseFloat(bottom));
		loanDuration2 += parseFloat(lumpSumYear);
		theForm.txtLoanDuration2.value = currencyConvert(loanDuration2, 1);

		totalCost2 = (parseFloat(monthlyPayments2) * parseFloat(loanDuration2) * 12) + parseFloat(lumpSumAmount);
		theForm.txtTotalCost2.value = currencyConvert(parseFloat(totalCost2));
	} 
	theForm.txtLoanDurationSaved.value = currencyConvert((parseFloat(theForm.txtLoanDuration1.value)) - (parseFloat(theForm.txtLoanDuration2.value)),1);
	theForm.txtTotalCostSaved.value = currencyConvert(parseFloat(totalCost1) - parseFloat(totalCost2));
}

function calculateWhat(theForm) {
	var i, x;
	var j = 17;

	arrData = new Array(j);
	for (i = 0; i < arrData.length; ++ i) {
		arrData [i] = new Array(0.0,0.0,0.0,0.0,0.0,0.0);
	}
	var count = 0;
	var allSelectTags = document.getElementsByTagName("select");
	for(var i = 1; i < allSelectTags.length; i++) {
		if(allSelectTags[i].name.substring(0,4) == "ddlQ") {
			count++;
			x = 2 * count;
			arrData[x][0] = eval("theForm.ddlQ"+i+".options[theForm.ddlQ"+i+".selectedIndex].value");
		}
	}

	arrData[2][1] = arrData[2][0];
	if (parseFloat(arrData[2][0]) > 4) {
		arrData[4][1] = arrData[4][0];  
	} else if (parseFloat(arrData[2][0]) > 3) { 
		arrData[4][1] = arrData[4][0] / 2; 
	} else {
		arrData[4][1] = 0;
	}
		
	arrData[4][0] = 2;
	if (arrData[4][0] == 1) 
		arrData[4][5] = (6 - arrData[4][0]) * (6 - arrData[6][0]) / 5; 
	else if (arrData[4][0]==2) 
		arrData[4][5] = (6 - arrData[4][0]) * (6 - arrData[6][0]) / 5 / 1.5; 
	else 
		arrData[4][5] = 0;
		
	arrData[5][0] = 6 - arrData[4][0];	
	
	if (arrData[5][0] == 1) 
		arrData[5][1] = arrData[6][0];  
	else if (arrData[5][0] == 2) 
		arrData[5][1] = arrData[6][0]/1.5; 
	else 			
		arrData[5][1] = 0; 
	
	arrData[5][1] = arrData[5][1] * 1.1;
	
	if (arrData[2][0] > 4) 
		arrData[6][1] = arrData[6][0];  
	else if (arrData[2][0] > 3) 
		arrData[6][1] = arrData[6][0]/2;  
	else 
		arrData[6][1] = 0;
	
	arrData[8][3] = arrData[8][0];

	arrData[10][5] = arrData[10][0];

	arrData[12][1] = arrData[12][0];

	arrData[14][4] = arrData[14][0] * (1 - (arrData[4][1] + arrData[6][1] * 0) / 20);

	arrData[16][1] = arrData[16][0];

	
	for (x=1; x<6; x++) 
	{
		for (i=0; i<j; i++) 
		{
			arrData[0][x] += parseFloat(arrData[i][x]);
		}
	}
	
	arrData[0][1] += -2; 
			
	var fixedRate = 0;
	if (parseFloat(arrData[0][1]) >= 0 && parseFloat(arrData[0][1]) < 6) 
		fixedRate = 1; 
	else if (parseFloat(arrData[0][1]) >= 6 && parseFloat(arrData[0][1]) < 8) 
		fixedRate = 2; 				
	else if (parseFloat(arrData[0][1]) >= 8 && parseFloat(arrData[0][1]) < 12) 
		fixedRate = 3; 
	else if (parseFloat(arrData[0][1]) >= 12 && parseFloat(arrData[0][1]) < 20) 
		fixedRate = 4;
	else if (parseFloat(arrData[0][1]) >= 20) 
		fixedRate = 5; 	


	var discount = 0;
	if (parseFloat(arrData[0][1]) >= 0 && parseFloat(arrData[0][1]) < 6) 
		discount = 5; 
	else if (parseFloat(arrData[0][1]) >= 6 && parseFloat(arrData[0][1]) < 8) 
		discount = 4; 
	else if (parseFloat(arrData[0][1]) >= 8 && parseFloat(arrData[0][1]) < 12) 
		discount = 3; 
	else if (parseFloat(arrData[0][1]) >= 12 && parseFloat(arrData[0][1]) < 20) 
		discount = 2;
	else if (parseFloat(arrData[0][1]) >= 20) 
		discount = 1; 	


	arrData[0][5]++;
	var flexible = 0;
	if (parseFloat(arrData[0][5]) > 0 && parseFloat(arrData[0][5]) <= 2) 
		flexible = 1; 
	else if (parseFloat(arrData[0][5]) > 2 && parseFloat(arrData[0][5]) < 4) 
		flexible = 2; 
	else if (parseFloat(arrData[0][5]) >= 4 && parseFloat(arrData[0][5]) < 6) 
		flexible = 3; 
	else if	(parseFloat(arrData[0][5]) >= 6 && parseFloat(arrData[0][5]) < 8) 
		flexible = 4;
	else if (parseFloat(arrData[0][5]) >= 8) 
		flexible = 5; 	


	var redemption =  6 - parseFloat(arrData[0][3]);   


	var cashback = 0;
	if (parseFloat(arrData[0][4]) > 0 && parseFloat(arrData[0][4]) <= 1) 
		cashback = 1; 
	else if (parseFloat(arrData[0][4]) > 1 && parseFloat(arrData[0][4]) <= 2) 
		cashback = 2; 
	else if (parseFloat(arrData[0][4]) > 2 && parseFloat(arrData[0][4]) <= 3) 
		cashback = 3; 
	else if	(parseFloat(arrData[0][4]) > 3 && parseFloat(arrData[0][4]) <= 4) 
		cashback = 4;
	else if (parseFloat(arrData[0][4]) > 4) 
		cashback = 5;

	document.getElementById('resultsTable').style.display = "block";
	document.getElementById('slider1').src = "http://www.iii.co.uk/icons/unysenimages/userimages/sliders" + fixedRate + ".gif";
	document.getElementById('slider2').src = "http://www.iii.co.uk/icons/unysenimages/userimages/sliders" + discount + ".gif";
	document.getElementById('slider3').src = "http://www.iii.co.uk/icons/unysenimages/userimages/sliders" + flexible + ".gif";
	document.getElementById('slider4').src = "http://www.iii.co.uk/icons/unysenimages/userimages/sliders" + redemption + ".gif";
	document.getElementById('slider5').src = "http://www.iii.co.uk/icons/unysenimages/userimages/sliders" + cashback + ".gif";
}

function currencyConvert(strConvert, decimalPlaces) {
	var dec = 3;
	var strDecimal = ".00";
	if(!isNaN(decimalPlaces) && decimalPlaces != null) {
		dec = parseInt(decimalPlaces) + 1;
		if(dec == 2){strDecimal = ".0";}else if(dec == 4){strDecimal = ".000";}
	}
	strConvert += "";
	if(strConvert.indexOf(".") != -1) {
		strDecimal = strConvert.substring(strConvert.indexOf("."), strConvert.indexOf(".")+dec);
		strConvert = strConvert.substring(0, strConvert.indexOf("."));
	}
	if(strConvert.length > 3 && strConvert.length < 7) {
		var thousandSep = strConvert.length - 3;
		strConvert = strConvert.substring(0, thousandSep) + "," + strConvert.substring(thousandSep, strConvert.length);
	} else if(strConvert.length > 6 && strConvert.length < 10) {
		var thousandSep = strConvert.length - 6;
		var thousandSep2 = strConvert.length - 3;
		strConvert = strConvert.substring(0, thousandSep) + "," + strConvert.substring(thousandSep, thousandSep2) + "," + strConvert.substring(thousandSep2, strConvert.length);
	}
	return strConvert+strDecimal;
}