
  function CalcLeasingMntPayment(newPrice, newInterest, newPeriod, newResidualvalue, newAdvanceProcent) {
		newAdvance = (newAdvanceProcent * newPrice / 100);
		if (newInterest==0)
			return (newPrice - newAdvance-(newPrice * newResidualvalue / 100)) / newPeriod;
		else
		return((newInterest / 100 / 12) * ((newPrice - newAdvance)-((newPrice * newResidualvalue / 100) / (Math.pow((newInterest / 100 / 12) + 1, newPeriod)))) / (1 - (1 / Math.pow((newInterest / 100 / 12) + 1, newPeriod))));
  }

  function makeNum(input) {
		str=input.value;
		if (str.indexOf(',')!=-1) {
			re= /,/gi;
			input.value=str.replace(re,'.');
		}
		str=input.value;
		if (str.indexOf(' ')!=-1) {
			re= / /gi;
			input.value=str.replace(re,'');
		}
		if (isNaN(input.value) || input.value=='') {
			input.focus();
			return false;
		} else {
			return true;
		}
  }

  function InitData() {
  	with (document.calc) {
		dataError = '';
		articlePrice = article_price.value;
			if (isNaN(articlePrice)) { 
				articlePrice = 118000;
				dataError = "Vara maksumus ei ole number, määran maksumuseks 118000<br>\n";
			}
			if (articlePrice > 999999999) {
				articlePrice = 999999999;
				dataError = "Vara maksimaalne maksumus on 999 999 999.00 krooni<br>\n";
			}
			
		articlePriceComKm = article_price_con_km.value;
			if (articlePriceComKm != 'no') articlePriceComKm = 'yes';
			
		leasingAdvance = leasing_advance.value;
			if (isNaN(leasingAdvance)) {
				leasingAdvance = 30;
				dataError = "Sissemakse ei ole number, määran sissemakseks 30%<br>\n";
			}
			if (leasingAdvance > 90) { 
				leasingAdvance = 90;
				dataError = "Maksimaalne omafinantseerimine on 90%<br>\n";
			}
		leasingInterest = 1 * leasing_interest.value;
			if (isNaN(leasingInterest)) {
				leasingInterest = 11;
				dataError = "Intress ei ole number, määran intressiks 30%<br>\n";
			}
			if (leasingInterest > 20) {
				dataError = "Maksimaalne intress on 50%<br>\n";
				leasingInterest = 20;
			}
		leasingPeriod = 1 * leasing_period.value;
			if (isNaN(leasingPeriod)) {
				leasingPeriod = 36;
				dataError = "Periood ei ole number, määran perioodiks 36 kuud<br>\n";
			}
			if (leasingPeriod > 600) {
				leasingPeriod = 600;
				dataError = "Periood liiga pikk<br>\n";
			}
		leasingResidualvalue = 0;
			if (isNaN(leasingResidualvalue)) leasingResidualvalue = 0;
			if (leasingResidualvalue > 95) leasingResidualvalue = 95;
			if ((eval(leasingResidualvalue) + eval(leasingAdvance)) > 100) leasingResidualvalue = 100 - eval(leasingAdvance);
	}		
  }

  function currency(anynum) {
		if (isNaN(anynum) || anynum == '') {
			if (anynum == 0) {
				return ("0.00");
			} else {
				return (anynum);
			}
		} else {   
			anynum=eval(anynum)
			workNum=Math.abs((Math.round(anynum*100)/100));workStr=""+workNum
			if (workStr.indexOf(".")==-1){workStr+=".00"}
			dStr=workStr.substr(0,workStr.indexOf("."));dNum=dStr-0
			pStr=workStr.substr(workStr.indexOf("."))
			while (pStr.length<3){pStr+="0"}
			if (dNum>=1000) {
      			dLen=dStr.length
      			dStr=parseInt(""+(dNum/1000))+"&nbsp;"+dStr.substring(dLen-3,dLen)
			}
			if (dNum>=1000000) {
				dLen=dStr.length
				dStr=parseInt(""+(dNum/1000000))+"&nbsp;"+dStr.substring(dLen-12,dLen)
			}
			if (dNum>=1000000000) {
				dLen=dStr.length
				dStr=parseInt(""+(dNum/1000000000))+"&nbsp;"+dStr.substring(dLen-21,dLen)
			}			
			retval = dStr + pStr 
			//-- Put numbers in parentheses if negative.
			if (anynum<0) {retval="-"+retval}
			return retval
		}
  }

  function get_selected(object){
		for (var i = 0; i < object.length; i++) {
			if (object[i].selected) return(object[i].value);
		}
  }

  function set_selected(object, t_value){
		for (var i = 0; i < object.length; i++) {
			if (object[i].value==t_value) object[i].selected="TRUE";
		}
  }

  function get_checked(object){
		for (var i = 0; i < object.length; i++) {
			if (object[i].checked) return(object[i].value);
		}
  }

  function set_checked(object, t_value){
		for (var i = 0; i < object.length; i++) {
			if (object[i].value==t_value) object[i].checked="TRUE";
		}
  }

  function changeAdvance() {
		document.calc.leasing_advance.value=document.calc.leasing_advance_sum.value/document.calc.article_price.value * 100;
		change();
  }

  function changeAdvanceSum() {
		document.calc.leasing_advance_sum.value=document.calc.article_price.value*document.calc.leasing_advance.value / 100;
		change();
  }
  
  function changeCasco() {
	with (document.calc) {
		if (casco[0].checked) {leasing_interest.value=6.9}
		if (casco[1].checked) {leasing_interest.value=12.9}
		change();
	}
  }
  
	
  function change() {
	with (document.calc) {
		
		ltype = 14;

		price=1 * article_price.value;
			if (ltype == 13) {price= price / 1.18;}
		
		advanceSum=price*leasing_advance.value / 100;
			leasing_advance_sum.value=(Math.round(advanceSum*100)/100);
			
		residual=0;
				
		theintr = 1 * leasing_interest.value;
		
		theperiod = 1 * leasing_period.value;
		
		if(theintr==0) {
			monthPayment= Math.round((price - advanceSum - (price * residual / 100)) / theperiod);
		} else {
			monthPayment= Math.round(((theintr / 100 / 12) * ((price - advanceSum)-((price * residual / 100) / (Math.pow((theintr / 100 / 12) + 1, theperiod)))) / (1 - (1 / Math.pow((theintr / 100 / 12) + 1, theperiod)))));
		}
		
		month_payment.value=monthPayment;

	}
  }
	
  function HtmlHeader() {
	graafik.document.write("<HTML>\n");
	graafik.document.write("<HEAD>\n");
	graafik.document.write("<TITLE>Hansa Liising Eesti - mootorrattaliisingu kalkulaator</TITLE>\n");
	graafik.document.write("</HEAD>\n");
	graafik.document.write("<BODY BGCOLOR=\"#FFFFFF\">\n");
	graafik.document.write("<style type=\"text/css\">\n");
	graafik.document.write("BODY, P, TD, SPAN, UL, OL { font-family : Verdana, Geneva, Arial, Helvetica, sans-serif; color: #003333;}\n");
	graafik.document.write(".textSmall { font-size : 10px; }\n");
	graafik.document.write(".textMedium { font-size : 11px; }\n");
	graafik.document.write(".textMediumDL { font-size : 11px; line-height : 16px; }\n");
	graafik.document.write(".tableHeaderCell { font-size: 10px; background-color:#F3BD87; }\n");
	graafik.document.write(".tableRegularCell1 { font-size : 9px; background-color:#FFFFFF; }\n");
	graafik.document.write(".tableRegularCell2 { font-size : 9px; background-color:#F8F5E6; }\n");
	graafik.document.write("</style>\n");
  }

  function ScheduleHeader(leftBoxText, bottomLineText) {
		graafik.document.write("<table border=\"0\" width=\"600\" cellspacing=\"0\" cellpadding=\"1\" align=\"center\">\n");
		graafik.document.write("<tr>\n");
		graafik.document.write("\t<td valign = \"top\" class=\"textMedium\">\n\t\t" + leftBoxText + "\n	</td>\n");
		graafik.document.write("\t<td align=\"right\" valign=\"top\">\n\t\t<img src=\"img/hl_logob.jpg\" border=\"0\">\n\t</td>\n");
		graafik.document.write("<tr>\n");
		graafik.document.write("\t<td colspan=\"2\" class=\"textSmall\">&nbsp;</td>\n");
		graafik.document.write("</tr>\n");
		graafik.document.write("</table>\n");
		graafik.document.write("<table border=\"0\" width=\"600\" cellspacing=\"0\" cellpadding=\"0\" align=\"center\" bgcolor=\"#F3BD87\">\n");		
		graafik.document.write("<tr>\n");		
		graafik.document.write("<td>\n");		
		graafik.document.write("<table border=\"0\" width=\"600\" cellspacing=\"1\" cellpadding=\"1\" align=\"center\">\n");
  }
	
  function ScheduleBottom() {
		graafik.document.write("\t</table>\n");
		graafik.document.write("</td>\n");
		graafik.document.write("</tr>\n");
		graafik.document.write("</table>\n");
		graafik.document.write("<table border=\"0\" width=\"600\" cellspacing=\"1\" cellpadding=\"2\" align=\"center\">\n");
		graafik.document.write("<tr>\n");
		graafik.document.write("\t<td class=\"tableRegularCell1\" align=\"center\">\n");
		graafik.document.write("\t\t<hr noshade size=\"1\">&copy; Hansa Liising Eesti, telefon: 613 1324, e-mail: liising@hansa.ee, Internet: www.hansa.ee\n");
		graafik.document.write("\t</td>\n");
		graafik.document.write("</tr>\n");
		graafik.document.write("</table>\n");
  }

  function HtmlBottom() {
		graafik.document.write("</BODY>\n");
		graafik.document.write("</HTML>\n");
  }

  function ScheduleHeaderItem(item1, item2, item3, item4, item5, item6, item7, item8, item9, item10) {
		graafik.document.write("<tr>\n");				
		if (item1) graafik.document.write("\t<td class=\"tableHeaderCell\" align=\"center\" height=\"30\">" + item1 + "</td>\n");
		if (item2) graafik.document.write("\t<td class=\"tableHeaderCell\" align=\"right\" height=\"30\">" + item2 + "</td>\n");
		if (item3) graafik.document.write("\t<td class=\"tableHeaderCell\" align=\"right\" height=\"30\">" + item3 + "</td>\n");
		if (item4) graafik.document.write("\t<td class=\"tableHeaderCell\" align=\"right\" height=\"30\">" + item4 + "</td>\n");
		if (item5) graafik.document.write("\t<td class=\"tableHeaderCell\" align=\"right\" height=\"30\">" + item5 + "</td>\n");
		if (item6) graafik.document.write("\t<td class=\"tableHeaderCell\" align=\"right\" height=\"30\">" + item6 + "</td>\n");
		if (item7) graafik.document.write("\t<td class=\"tableHeaderCell\" align=\"right\" height=\"30\">" + item7 + "</td>\n");
		if (item8) graafik.document.write("\t<td class=\"tableHeaderCell\" align=\"right\" height=\"30\">" + item8 + "</td>\n");
		if (item9) graafik.document.write("\t<td class=\"tableHeaderCell\" align=\"right\" height=\"30\">" + item9 + "</td>\n");
		if (item10) graafik.document.write("\t<td class=\"tableHeaderCell\" align=\"right\" height=\"30\">" + item10 + "</td>\n");
		graafik.document.write("</tr>\n");
  }

  function ScheduleItem(item1, item2, item3, item4, item5, item6, item7, item8, item9, item10) {
		if (addOrEven == 'add') {
			webClass = 'tableRegularCell1';
			addOrEven = 'even';
		} else {
			webClass = 'tableRegularCell2';
			addOrEven = 'add';
		}
		graafik.document.write("<tr>\n");
		if (item1 || item1 == 0) graafik.document.write("\t<td class=\"" + webClass + "\" align=\"center\">" + currency(item1) + "</td>\n");
		if (item2 || item2 == 0) graafik.document.write("\t<td class=\"" + webClass + "\" align=\"right\">" + currency(item2) + "</td>\n");
		if (item3 || item3 == 0) graafik.document.write("\t<td class=\"" + webClass + "\" align=\"right\">" + currency(item3) + "</td>\n");
		if (item4 || item4 == 0) graafik.document.write("\t<td class=\"" + webClass + "\" align=\"right\">" + currency(item4) + "</td>\n");
		if (item5 || item5 == 0) graafik.document.write("\t<td class=\"" + webClass + "\" align=\"right\">" + currency(item5) + "</td>\n");
		if (item6 || item6 == 0) graafik.document.write("\t<td class=\"" + webClass + "\" align=\"right\">" + currency(item6) + "</td>\n");
		if (item7 || item7 == 0) graafik.document.write("\t<td class=\"" + webClass + "\" align=\"right\">" + currency(item7) + "</td>\n");
		if (item8 || item8 == 0) graafik.document.write("\t<td class=\"" + webClass + "\" align=\"right\">" + currency(item8) + "</td>\n");
		if (item9 || item9 == 0) graafik.document.write("\t<td class=\"" + webClass + "\" align=\"right\">" + currency(item9) + "</td>\n");
		if (item10 || item10 == 0) graafik.document.write("\t<td class=\"" + webClass + "\" align=\"right\">" + currency(item10) + "</td>\n");
		graafik.document.write("</tr>\n");
  }

  function ScheduleBottomItem(item1, item2, item3, item4, item5, item6, item7, item8, item9, item10) {
		graafik.document.write("<tr>\n");
		if (item1 || item1 == 0) graafik.document.write("\t<td class=\"tableRegularCell2\" align=\"center\" height=\"20\">" + currency(item1) + "</td>\n");
		if (item2 || item2 == 0) graafik.document.write("\t<td class=\"tableRegularCell2\" align=\"right\" height=\"20\">" + currency(item2) + "</td>\n");
		if (item3 || item3 == 0) graafik.document.write("\t<td class=\"tableRegularCell2\" align=\"right\" height=\"20\">" + currency(item3) + "</td>\n");
		if (item4 || item4 == 0) graafik.document.write("\t<td class=\"tableRegularCell2\" align=\"right\" height=\"20\">" + currency(item4) + "</td>\n");
		if (item5 || item5 == 0) graafik.document.write("\t<td class=\"tableRegularCell2\" align=\"right\" height=\"20\">" + currency(item5) + "</td>\n");
		if (item6 || item6 == 0) graafik.document.write("\t<td class=\"tableRegularCell2\" align=\"right\" height=\"20\">" + currency(item6) + "</td>\n");
		if (item7 || item7 == 0) graafik.document.write("\t<td class=\"tableRegularCell2\" align=\"right\" height=\"20\">" + currency(item7) + "</td>\n");
		if (item8 || item8 == 0) graafik.document.write("\t<td class=\"tableRegularCell2\" align=\"right\" height=\"20\">" + currency(item8) + "</td>\n");
		if (item9 || item9 == 0) graafik.document.write("\t<td class=\"tableRegularCell2\" align=\"right\" height=\"20\">" + currency(item9) + "</td>\n");
		if (item10 || item10 == 0) graafik.document.write("\t<td class=\"tableRegularCell2\" align=\"right\" height=\"20\">" + currency(item10) + "</td>\n");
		graafik.document.write("</tr>\n");
  }
  
  function DrawScheduleLL4() {
		contractFee = 1;
		contractFeeMin = 2500;
		if (articlePriceComKm == 'no') articlePrice = articlePrice * 1.18;
		articlePriceMinusKm = articlePrice;
		
		leasingReminder = articlePriceMinusKm;
		
		ScheduleHeader("<table border=0 cellspacing=0 cellpadding=2 class=\"textMedium\">\n <tr><td align=right>mootorratta hind:</td><td><b>" + currency(articlePrice) + "</b></td></tr>\n  <tr><td align=right>esimene osamakse:</td><td><b>" + currency(leasingAdvance) + "</b>% \(<b>" + currency(leasingAdvance * articlePrice / 100) + "</b> krooni\)<br></td></tr>\n  <tr><td align=right>lepingu periood kuudes:</td><td><b>" + leasingPeriod + "</b></td></tr>\n  <!-- tr><td align=right>osamaksete arv:</td><td><b>" + (1*leasingPeriod+1) + "</b></td></tr -->\n  <tr><td align=right>intress:</td><td><b>" + currency(leasingInterest) + "</b>%</td></tr>\n  </table>\n", "");
		ScheduleHeaderItem("Kuupäev", "Tasumata<br>osamaksed", "Osamakse", "Intress", "Lepingutasu", "Käibemaks", "Kokku");

		contractFeePayment = articlePriceMinusKm * contractFee / 100;
		if (contractFeePayment < contractFeeMin) contractFeePayment = contractFeeMin;
		
		thisPayment = articlePriceMinusKm * leasingAdvance / 100;
		thisPaymentKm = 0;
		thisInterestPayment = 0;
		thisInterestPaymentKm = 0;
		thisContractFeePayment = contractFeePayment;
		thisContractFeePaymentKm = 0;
		thisTotalPayment = thisPayment + thisInterestPayment + thisContractFeePayment;
		thisTotalPaymentKm = thisPaymentKm + thisInterestPaymentKm + thisContractFeePaymentKm;
		thisPaymentSum = thisTotalPayment + thisTotalPaymentKm;
		
		ScheduleItem(PmtDay(0), leasingReminder, thisPayment, thisInterestPayment, thisContractFeePayment, thisTotalPaymentKm, thisPaymentSum);
		// Viimase rea summad;
			sumPayment = thisPayment;
			sumPaymentKm = thisPaymentKm;
			sumInterestPayment = thisInterestPayment;
			sumInterestPaymentKm = thisInterestPaymentKm;
			sumContractFeePayment = thisContractFeePayment;
			sumContractFeePaymentKm = thisContractFeePaymentKm;
			sumTotalPayment = thisTotalPayment;
			sumTotalPaymentKm = thisTotalPaymentKm;
			sumPaymentSum = thisPaymentSum;
			leasingReminder = leasingReminder - thisPayment;
		
		if (leasingResidualvalue>0) {
			leasingPeriod-=1;
		}
		
		monthPmt = CalcLeasingMntPayment(articlePriceMinusKm, leasingInterest, leasingPeriod, leasingResidualvalue, leasingAdvance);
		
		for (period = 1; period <= leasingPeriod; period++) {
			thisInterestPayment = leasingReminder * leasingInterest / 1200;
			thisInterestPaymentKm = 0;
			thisPayment = monthPmt - thisInterestPayment;
			thisPaymentKm = 0;
			thisContractFeePayment = 0;
			thisContractFeePaymentKm = 0;
			thisTotalPayment = thisPayment + thisInterestPayment + thisContractFeePayment;
			thisTotalPaymentKm = thisPaymentKm + thisInterestPaymentKm + thisContractFeePaymentKm;
			thisPaymentSum = thisTotalPayment + thisTotalPaymentKm;

			ScheduleItem(PmtDay(period), leasingReminder, thisPayment, thisInterestPayment, thisContractFeePayment, thisTotalPaymentKm, thisPaymentSum);
			// Viimase rea summad;
				sumPayment += thisPayment;
				sumPaymentKm += thisPaymentKm;
				sumInterestPayment += thisInterestPayment;
				sumInterestPaymentKm += thisInterestPaymentKm;
				sumContractFeePayment += thisContractFeePayment;
				sumContractFeePaymentKm += thisContractFeePaymentKm;
				sumTotalPayment += thisTotalPayment;
				sumTotalPaymentKm += thisTotalPaymentKm;
				sumPaymentSum += thisPaymentSum;
				leasingReminder = leasingReminder - thisPayment;
		}
		
		if (leasingResidualvalue>0) {
			ScheduleItem(PmtDay(period-1), leasingReminder, 0, 0, 0, 0, leasingReminder);
			sumTotalPayment += leasingReminder;
			sumPaymentSum += leasingReminder;
		}
		
		ScheduleBottomItem("Kokku", "---", sumPayment, sumInterestPayment, sumContractFeePayment, sumTotalPaymentKm, sumPaymentSum);
  }	

  function showGraph() {
	InitData();	
	addOrEven = 'add';

	graafik=window.open('graafik.html');
		HtmlHeader();
		DrawScheduleLL4();
		ScheduleBottom();
		HtmlBottom();
	graafik.document.close();
  }

  function PmtDay(addMonth) {
		tana = new Date();
		thisDay = tana.getDate();
		thisMonth = tana.getMonth();
		thisYear = tana.getYear();
		if (thisYear < 1000) thisYear += 1900;
		thisMonth += 1;
		
		if (addMonth > 0) {
			if (thisDay <= 15) thisDay=15; else thisDay=30;
			thisMonth += addMonth;
			while (thisMonth > 12) {
				thisYear += 1;
				thisMonth -= 12;
			}
			if ((thisMonth == 2) && (thisDay > 28)) thisDay = 28;
		}
		date = "";
		if ((""+thisDay).length == 1) date += "0";
		date += thisDay + ".";
		if ((""+thisMonth).length == 1) date += "0";
		date += thisMonth + "." + thisYear;
		
		return (date);
  }
    
