﻿var nbCalcURL="./nbcalc/";
document.write('<table width="100%" border="0" cellspacing="0" cellpadding="0" id="calcTable">');
document.write('<form action="#" onSubmit="nbCalc();return false;">');
document.write('<tr>');
document.write('<th class="first2" colspan="2">Лизинговый калькулятор</th>');
document.write('</tr></tr>');
document.write('<th class="styled2">Вид клиента</th>');
document.write('<td class="styled2">');
document.write('<select id="nbCalcKlients" onChange="changeKlients(this.value);"><option value="j" SELECTED>предприятие</option><option value="f">частное лицо</option></select>');
document.write('</td></tr><tr>');

document.write('<th class="styled2">Цена автомашины</th>');
document.write('<td class="styled2">');
document.write('<input type="text" style="width: 50px;" id="nbCalcVal" value="0" onKeyPress="if(false && (event.keyCode<48||event.keyCode>=57))return false;" />');
document.write('&nbsp;<select id="nbCalcEx"><option value="LVL">LVL</option><option value="EUR">EUR</option></select>');
document.write('</td></tr><tr>');
document.write('<th class="styled2">Первый взнос</th>');
document.write('<td class="styled2">');
document.write('<select id="nbCalcFm"><option value="50">50%</option><option value="45">45%</option><option value="40">40%</option><option value="35">35%</option><option value="30">30%</option><option value="25">25%</option><option value="20"  selected="selected">20%</option><option value="10">10%</option><option value="5">5%</option></select>');
document.write('</td></tr><tr>');
document.write('<th class="styled2">Вид лизинга</th>');
document.write('<td class="styled2">');
document.write('<select id="nbCalcType" onChange="nbCalcChange();changeKlients(0);"><option value="f">финансовый</option><option value="o">оперативный</option></select>');
document.write('</td></tr><tr>');
document.write('<th class="styled2">Остаточная стоимость</th>');
document.write('<td class="styled2">');
document.write('<select id="nbCalcPm" disabled><option value="35">35%</option><option value="30">30%</option><option value="25">25%</option><option value="20">20%</option><option value="0" selected="selected">0%</option></select>');
document.write('</td></tr><tr>');
document.write('<th class="styled2">Процентная ставка</th>');
document.write('<td class="styled2">');
document.write('<select id="nbCalcPrc"><option value="8">8%</option><option value="9">9%</option><option value="10">10%</option><option value="11" selected="selected">11%</option><option value="12">12%</option><option value="13">13%</option><option value="14">14%</option><option value="15">15%</option><option value="16">16%</option><option value="17">17%</option><option value="18">18%</option><option value="19">19%</option><option value="20">20%</option></select>');
document.write('</td></tr><tr>');
document.write('<th class="styled2">Комиссионная плата</th>');
document.write('<td class="styled2">');
document.write('<select id="nbCalcComm"><option value="0">0%</option><option value="0.5">0.5%</option><option value="1">1%</option><option value="1.5" selected="selected">1.5%</option><option value="2">2%</option><option value="2.5">2.5%</option></select>');
document.write('</td></tr><tr>');
document.write('<th class="styled2">Срок (год)</th>');
document.write('<td class="styled2">');
document.write('<select id="nbCalcYear"><option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4" selected="selected">4</option><option value="5">5</option><option value="6">6</option><option value="7">7</option></select>');
document.write('</td></tr><tr>');
document.write('<th class="styled2" id="preTD1"><div id="nbCalcOutTitle" style="width:120px;">Месечная плата</div></th>');
document.write('<td class="styled2" id="preTD2">');
document.write('<b><div id="nbCalcOut">0.00</div></b>');
document.write('</td></tr><tr><td>&nbsp;</td>');
document.write('<td>');
document.write('<div style="float: left;"><br><input value="Раcсчитать" title="Рассчитать" type="image" src="/pic/calc/ru/calc.png" onClick="nbCalc();return false;"></div><div id="allpays" style="float:left; width:200px;"></div>');
document.write('</td></tr></table></div>');
document.write('</form>');

function nbCalcPrepare(){

    var nbCalcOut=0;
	var nbCalcOutL=0;
	var nbCalcOOm=0;
	var nbCalcVal=document.getElementById("nbCalcVal").value;
	var nbCalcFm=document.getElementById("nbCalcFm").options[document.getElementById("nbCalcFm").selectedIndex].value;
	if (!isNaN(nbCalcVal)&&nbCalcVal){
	
		var nbCalcOm=nbCalcVal*(nbCalcFm / 100);
		var nbCalcTp=document.getElementById("nbCalcType").options[document.getElementById("nbCalcType").selectedIndex].value;
		var nbCalcComm=document.getElementById("nbCalcComm").options[document.getElementById("nbCalcComm").selectedIndex].value;
		var nbCalcCommSumm = nbCalcVal / 100 * nbCalcComm;
		if (nbCalcTp=="o"){
			var nbCalcPm=document.getElementById("nbCalcPm").options[document.getElementById("nbCalcPm").selectedIndex].value;
			var nbCalcOOm=nbCalcVal*(nbCalcPm / 100);
		} else var nbCalcOOm=0;
		var nbCalcPrc=document.getElementById("nbCalcPrc").options[document.getElementById("nbCalcPrc").selectedIndex].value;
		var nvCalcRate=nbCalcPrc/100/12;
		var nbCalcYear=document.getElementById("nbCalcYear").options[document.getElementById("nbCalcYear").selectedIndex].value;
		var nbCalcTerm=Math.ceil(nbCalcYear)*12;
		
		nbCalcOutL=((nvCalcRate*((nbCalcVal-nbCalcOm)-(nbCalcOOm/(Math.pow(nvCalcRate+1,nbCalcTerm))))/(1-(1/Math.pow(nvCalcRate+1,nbCalcTerm)))));
		nbCalOut=Math.round(nbCalcOutL*100)/100;
	}
	
	nbCalcOut = Math.round(nbCalOut*100)/100;
	nbKlients = document.getElementById("nbCalcKlients").value;
	if((nbKlients=='f') && (nbCalcTp!="o")){
		var tbl = document.getElementById('calcTable');
		var lastRow = tbl.rows.length;
		if(lastRow==10){
			addRowToTable();
		}
		document.getElementById("calcGPLfield").innerHTML = Math.round(getGPL(nbCalcVal-nbCalcOm-nbCalcCommSumm,nbCalcTerm,nbCalcOutL,nbCalcOOm)*100,2)/100 + '%';
	}
	
	return nbCalcOut;
}

function nbCalcChange(){
	var obj=document.getElementById("nbCalcPm");
	var nbCalcTp=document.getElementById("nbCalcType").options[document.getElementById("nbCalcType").selectedIndex].value;
	if (nbCalcTp=="o")obj.disabled=false;else obj.disabled=true;
}
function nbCalc(){
	var nbCalcOut = nbCalcPrepare();
	var nbCalcEx=document.getElementById("nbCalcEx").options[document.getElementById("nbCalcEx").selectedIndex].value;
	document.getElementById("nbCalcOut").innerHTML=nbCalcOut+"&nbsp;"+nbCalcEx;
	document.getElementById("nbCalcOutTitle").innerHTML='Месячный платеж';
}
function nbCalcAll(){
	var nbCalcYear=document.getElementById("nbCalcYear").options[document.getElementById("nbCalcYear").selectedIndex].value;
	var nbCalcTerm=Math.ceil(nbCalcYear)*12;
	var nbCalcOut = nbCalcPrepare();
	var nbCalcVal=document.getElementById("nbCalcVal").value;
	var nbCalcFm=document.getElementById("nbCalcFm").options[document.getElementById("nbCalcFm").selectedIndex].value;
	var nbCalcOm=nbCalcVal*(nbCalcFm / 100);
	
	var nbCalcComm=document.getElementById("nbCalcComm").options[document.getElementById("nbCalcComm").selectedIndex].value;
	var nbCalcCommSumm = (nbCalcVal) / 100 * nbCalcComm;
	
	var nbCalcEx=document.getElementById("nbCalcEx").options[document.getElementById("nbCalcEx").selectedIndex].value;
	document.getElementById("nbCalcOut").innerHTML=Math.round((nbCalcOut*nbCalcTerm + nbCalcCommSumm + nbCalcOm)*100)/100+"&nbsp;"+nbCalcEx;
	document.getElementById("nbCalcOutTitle").innerHTML='Общая сумма';
	
}

function changeKlients(t){
	if(t == 0){
		var nbCalcKl=document.getElementById("nbCalcKlients").options[document.getElementById("nbCalcKlients").selectedIndex].value;
		t = nbCalcKl;
	}
	var nbCalcTp=document.getElementById("nbCalcType").options[document.getElementById("nbCalcType").selectedIndex].value;
	if((t == 'j') || (nbCalcTp=="o")) {removeRowFromTable();}
	else{ addRowToTable();}
}
function addRowToTable()
{

  var tbl = document.getElementById('calcTable');
  var lastRow = tbl.rows.length;
  if(lastRow==11){
	  var iteration = lastRow;
	  var row = tbl.insertRow(lastRow-1);
	  
	  document.getElementById('preTD1').style.borderBottom = 'solid 1px #d9e2ea';
	  document.getElementById('preTD2').style.borderBottom = 'solid 1px #d9e2ea';
	  
	  // left cell
	  var cellLeft = row.insertCell(0);
	  var textNode = document.createTextNode('GPL');
	  cellLeft.appendChild(textNode);
	  
	  // right cell
	  var cellRight = row.insertCell(1);
	  var textNode = document.createTextNode('0.00%');
	  cellRight.appendChild(textNode);
	  cellRight.id = 'calcGPLfield';
	  
	  // Add style
	  cellRight.style.backgroundColor="#F1F1F1";
	  cellLeft.style.backgroundColor="#F1F1F1";
	  cellRight.style.borderBottom = 'solid 1px #5bb2e5';
	  cellLeft.style.borderBottom = 'solid 1px #5bb2e5';
	  cellRight.style.font = 'bold 12px arial';
	  cellLeft.style.font = 'bold 12px arial';
	  cellRight.style.padding = '4px 4px 4px 5px';
	  cellLeft.style.padding = '4px 4px 0px 2px';
	  document.getElementById('allpays').innerHTML = '<BR>&nbsp;<input value="Общая сумма" title="Общая сумма" type="image" src="/pic/calc/ru/kopeja_summa.gif" onClick="nbCalcAll();return false;">';
  }
}
function removeRowFromTable()
{
	var tbl = document.getElementById('calcTable');
	var lastRow = tbl.rows.length;
	if(lastRow==12){
		tbl.deleteRow(10);
		document.getElementById('preTD1').style.borderBottom = 'solid 2px #d9e2ea';
		document.getElementById('preTD2').style.borderBottom = 'solid 2px #d9e2ea';
		document.getElementById('allpays').innerHTML = '';
	}
}

function checkIDe(n){
	if(document.getElementById(n)){ return true;}
	else{ return false; }
}


function powX(zx, zc){
	if (zx == 0) {
		if (zc == 0) return false;
		else return 0;
	}
	if (zc == 0) return 1;
	if (Math.round(zc) == zc) {
		rx = 1;
		for (i = 0; i < zc; i++) rx = rx*zx;
		for (i = 0; i < -zc; i++) rx = rx/zx;
	}
	else if (zx < 0) return false;
	else {
		if (Math.abs(zc) > .5) return powX(zx, Math.round(zc)) * powX(zx, zc - Math.round(zc));
		rx = 1;px = 1; mx = Math.log(zx) * zc;
		for (i = 1; i < 100; i++) {
			px = px*mx/i; rx = rx+px;
			if (i > 100 || (Math.log((Math.abs(px)))/Math.log(10)) < -10) break;
		}
	}
	return rx;
}
function getGPL(summ, termm, payment, pluss){

	i = 1; s = 0; payments=[];
	for(trm = 1; trm <= termm; trm++) {	s = s+payment;payments[trm] = payment;}
	s = s+pluss; payments[termm] = payments[termm] + pluss;

	if (s > summ) {
		x_res = x_res1 = x_res2 = x_ressum = x_ressum1 = x_ressum2 = 0; x_resnew = 1; x_diff = .1; xi = 0;
		while (true) {
			x_ressum = 0;
			for (trm = 1; trm <= termm; trm++) {
				x_ressum += payments[trm] / powX(1 + x_res, trm/12);
			}
			if(xi == 0){ x_ressum1 = x_ressum; x_res1 = x_res; x_res = x_res+x_diff;}
			else{
				if(xi == 1){ x_ressum2 = x_ressum; x_res2 = x_res; x_res = x_res1 + x_diff * (summ - x_ressum1) / (x_ressum2 - x_ressum1); }
				else{ x_resnew = x_res2 + (summ - x_ressum2) / ((x_ressum - x_ressum1) / (x_res - x_res1)); x_res1 = x_res2; x_ressum1 = x_ressum2; x_res2 = x_res; x_ressum2 = x_ressum; x_res = x_resnew;}
			}
			if (Math.round(x_resnew*1000000)/1000000 == Math.round(x_res2*1000000)/1000000 || xi > 10) { x_res = x_resnew; break;}
			xi = xi + 1;
		}
		result  = x_res * 100;
	}
	else result = 0;
	return result;
}