I am not quite familiar with js, so need a lil help please.
I have a calculator that works in firefox but does not work in ie, and works for about 3 sec in opera.
Also, since the results are not displayed on page load, I need to add a button that will initiate the calculation – i need the code for this button please.
Here is the code of my script. Please leave out the non-english part.
[code]
<html>
<head>
<style>
td{border: 1px solid #EEEEEE; padding: 4px;}
table{border-collapse: collapse;}
#results td{border: 1px solid #777777;}
#results .label{width: 200px;}
#results .value{width: 50px;}
</style>
<script language=”javascript”>
var ids = {};
function gebi(n)
{
return document.getElementById(n);
}
function indexId(n)
{
ids[n] = gebi(n);
}
function init()
{
indexId(‘gains_sign’);
indexId(‘credit_sign’);
indexId(‘out_1_rate’);
indexId(‘out_2_rate’);
indexId(‘out_1_sum’);
indexId(‘out_2_gains’);
indexId(‘out_1_payment’);
indexId(‘out_2_payment’);
indexId(‘tracer’);
//indexId(‘gains_sign’);
}
function setCurrencySign(val)
{
var sign = (val == ‘rur’) ? ‘руб.’ : ‘$’;
ids.gains_sign.innerHTML = sign;
ids.credit_sign.innerHTML = sign;
return true;
}
function getFormVal(frm,nam,floatval)
{
var val = frm.elements[nam].value;
if(val == ”)return 0;
if(!floatval)var numVal = parseInt(val);
else var numVal = parseFloat(val.replace(‘,’,’.’));
if(isNaN(numVal))alert(‘ведено не числовое значение (‘+frm.elements[nam].value+’)!’);
return numVal;
}
function recalc(elm)
{
var f = elm.form;
var kurs = getFormVal(f,’kurs’);
var d = getFormVal(f,’gains’);
var s = getFormVal(f,’credit’);
var m = getFormVal(f,’period’)*12;
var p = getFormVal(f,’rate’,true);
var currency = f.elements[‘currency’].value;
var currSign = currency==”usd” ? ‘$’ : ‘руб.’;
ids.tracer.value = d+’; ‘+s+’; ‘+m+’; ‘+p+’; ‘;
var koef1 = [0.4,0.475,0.55];
var koef2 = [0.7,0.75,0.8];
var res = [];
var sConv = (currency == ‘usd’) ? s : s/kurs;
var montPaym2 = sConv*(p/1200)/(1-(Math.pow((p/1200+1),-m)));
for(var i = 0; i < 3; i++)
{
var v1 = montPaym2/koef1[i];
var v2 = ((sConv*1.1*1.014/100/12)+334.04+montPaym2)/koef2[i];
res[i] = (v1 > v2) ? v1 : v2;
}
var res2 = [res[0],res[1],res[2]];
var out = -1;
if(res[0] <= 1000)out = res[0];
else if(res[1] > 1000 && res[1] <= 2500)out = res[1];
else if(res[2] > 2500)out = res[2];
else
{
out = Number.POSITIVE_INFINITY;
for(i = 0; i < 3; i++)
{
if(res[i] < out)out = res[i];
}
}
if(currency == ‘rur’)
{
out *= kurs;
montPaym2 *= kurs;
}
ids.out_2_payment.innerHTML = Math.ceil(montPaym2)+’ ‘+currSign;
ids.out_2_gains.innerHTML = Math.ceil(out)+’ ‘+currSign;
var dConv = (currency == ‘usd’) ? d : d/kurs;
var dd = 0.55;
var v1 = (dConv*dd*(1-Math.pow((1+p/1200),-(m-2))))/(p/1200);
var v2 = v1/(((1-Math.pow((1+p/1200),-(m-2))))/(p/1200));
var montPaym1 = v2;
var maxSum1 = montPaym1*(1-Math.pow((1+p/1200),-m))/(p/1200);
if(currency == ‘rur’)
{
maxSum1 *= kurs;
montPaym1 *= kurs;
}
ids.out_1_payment.innerHTML = Math.ceil(montPaym1)+’ ‘+currSign;
ids.out_1_sum.innerHTML = Math.ceil(maxSum1)+’ ‘+currSign;
ids.out_1_rate.innerHTML = ids.out_2_rate.innerHTML = p;
}
</script>
</head>
<body OnLoad=”init();”>
<form name=”main”>
<table width=”100%” border=”0″ cellpadding=”0″ cellspacing=”0″>
<tr>
<td>Курс доллара:</td>
<td><input type=”text” name=”kurs” value=”25″/> руб.</td>
</tr>
<tr>
<td>Доходы заемщиков (на руки):</td>
<td><input type=”text” name=”gains” onKeyUp=”recalc(this);” value=”10000″/> руб.</td>
</tr>
<tr>
<td>Размер желаемого кредита:</td>
<td><input type=”text” name=”credit” onKeyUp=”recalc(this);” value=”200000″/> руб.</td>
</tr>
<tr>
<td>Валюта кредита:</td>
<td>
<select name=”currency” onChange=”setCurrencySign(this.value); recalc(this);”>
<option value=”rur”>российские рубли</option>
<option value=”usd”>доллар США</option>
</select> </td>
<td><span id=”credit_sign”></td>
</tr>
<tr>
<td>Срок кредита (лет):</td>
<td><input type=”text” name=”period” onKeyUp=”recalc(this);” value=”3″/> лет</td>
</tr>
<tr>
<td>Процентная ставка:</td>
<td><input type=”text” name=”rate” onKeyUp=”recalc(this);” value=”13.9″/> %</td>
</tr>
<tr>
<td colspan=”2″><div align=”center”>
<INPUT TYPE=”button” NAME=”myButton” VALUE=”Рассчитать” onKeyUp=”recalc(this)”>
</div></td>
</table>
</form>
<input type=”hidden” id=”tracer”>
<br>
<br>
<table width=”100%” border=”0″ cellpadding=”0″ cellspacing=”0″ id=”results”>
<tr>
<td colspan=”2″><b>Расчет по указанному доходу<b></td>
<td colspan=”2″><b>Расчет по желаемому кредиту<b></td>
</tr>
<tr>
<td>Процентная ставка</td>
<td width=”70″ id=”out_1_rate”> %</td>
<td>Процентная ставка</td>
<td width=”70″ id=”out_2_rate”> %</td>
</tr>
<tr>
<td>Максимальная сумма кредита</td>
<td id=”out_1_sum”></td>
<td>Минимальный доход</td>
<td id=”out_2_gains”></td>
</tr>
<tr>
<td>Ежемесячный платеж</td>
<td id=”out_1_payment”></td>
<td>Ежемесячный платеж</td>
<td id=”out_2_payment”></td>
</tr>
</table>
</body>
</html>