if(typeof(console)=="undefined") {console={}; console.log = function(){};}
function getMaxCreditSumm(period,borProfit,surProfit,provisionOfCredit,percent){ // period in months
	var sumProfit = surProfit+borProfit;
	var tax = sumProfit*0.13;
	var exspence = (sumProfit-tax)*0.3;
	var pureProfit = sumProfit - tax - exspence;
	return (period*pureProfit*12*30/365)/(1+(percent*period*30/365));
}
function writeMaxCreditSumm(period,borProfit,surProfit,provisionOfCredit,percent){
	$("#max_summ").text(getMaxCreditSumm(period,borProfit,surProfit,provisionOfCredit,percent).toFixed(2));
}
function calcShedule(creditSumm,period,percent,type){
	if(typeof(type) == "undefined")
		type = window.payType;
	if(!type){
		var shedule = [], today = new Date();
		shedule[0] = {
			'date' : today,
			'percentPayment':0,
			'debtPayment':0,
			'summPayment': -creditSumm,
			'rest':creditSumm
		};
		var days,currentDate, debtPayment, percentPayment, rest, currentMonth = today.getMonth(),currentYear = today.getFullYear(),beforeShedule;
		for(var i=1;i<=period+1;i++){
			beforeShedule = shedule[i-1];
			if(currentMonth > 11 ){
				currentYear++;
				currentMonth = 0;
			}
			if(i==1){
				days = meDate.getDays2MonthEnd(today)-1;
				currentDate = new Date (currentYear,currentMonth, meDate.getDaysInMonthDate(today));
			}
			else
			{
				currentDate = new Date(currentYear, currentMonth,1);
				currentDate = new Date(currentYear, currentMonth,meDate.getDaysInMonthDate(currentDate));
				days = meDate.getDaysInMonthDate(currentDate);
			}

			percentPayment = beforeShedule.rest*days*percent/((meDate.isLeap(today.getFullYear())?366:365));
			if(i != period+1)
				debtPayment = i==1 ? 0 : creditSumm/period;
			else
				debtPayment = shedule[i-1].rest;
			rest = shedule[i-1].rest - debtPayment;
			shedule[i] = {
				'days'           : days,
				'date'           : currentDate,
				'percentPayment' : percentPayment,
				'debtPayment'    : debtPayment,
				'summPayment'    : percentPayment+debtPayment,
				'rest'           : rest
			};
			currentMonth++;
		}
	}
	else{
		shedule = creditSumm*(percent/12)/(1-1/Math.pow(1+(percent/12),period)); 
	}
	return shedule;
}
function writeShedule(shedule,type){
	if(typeof(type) == "undefined")
		type = window.payType;
	var cur, all=0;
	if(!type){
		$('.shedule_table_row').remove();
		for(i in shedule){
			curr = shedule[i];
			$('#shedule_table').append('<tr class="shedule_table_row"><td>'+meDate.toString(curr['date'])+'</td><td>'+curr['summPayment'].toFixed(2)+
				'</td><td>'+curr['debtPayment'].toFixed(2)+'</td><td>'+curr['percentPayment'].toFixed(2)+'</td><td>'+curr['rest'].toFixed(2)+'</td></tr>');
			if (i>0) all+=curr['summPayment'];
		}	

		$('#all_summ').text(all.toFixed(2));
		$("#shedule_table_container").css({'display':'block'});
		$("#print_version").css({'display':'block'});
	}
	else{
		$("#mounth_pay").text(shedule.toFixed(2));
		$("#all_summ").text((shedule.toFixed(2)*window.period).toFixed(2));
	}
}
function printVersion(){
	if($.browser.name != 'msie'){
		document.getElementById('frameCheck').contentWindow.document.body.innerHTML = $("#shedule_table_container").html();
		document.getElementById('frameCheck').contentWindow.print();
	}
	else
	{
		var printWindow = window.open('/empty.html','kredit');
		printWindow.document.body.innerHTML = $("#shedule_table_container").html();
		printWindow.print();
	}
}
$(document).ready(function(){
	var today = new Date();
	window.payType = 1;
	var borProfit = 20000;
	window.period = 12;
	var surProfit = 0,provisionOfCredit=0,percent =0.19;
	var creditSumm = 100000;
	var annuitet = 0;
	$("#borrower_profit").change(function(){
		borProfit = $(this).val();
		$("#borrower_profit_slider").slider("value",1000*Math.log(borProfit));
		writeMaxCreditSumm(period,borProfit,surProfit,provisionOfCredit,percent);
		if(creditSumm)
			writeShedule(calcShedule(creditSumm,period,percent));
	});
	$("#surety_profit").change(function(){
		surProfit = $(this).val();
		writeMaxCreditSumm(period,borProfit,surProfit,provisionOfCredit,percent);
		if(creditSumm)
			writeShedule(calcShedule(creditSumm,period,percent));
	});
	$('#obespechenie').change(function(){
		if(parseInt($($(this).children(":selected")[0]).val())){
			percent = 0.16;
			$("#percent").val(16);
			$("#period").empty();
			for(i=0;i<3;i++){
				$("#period").append($("<option>"+(i+1)*12+" мес. </option>").val(i));
			}
			period = 12;
			writeMaxCreditSumm(period,borProfit,surProfit,provisionOfCredit,percent);
		}
		else{
			percent = 0.19;
			$("#percent").val(19);
			$("#period").empty().append($("<option value=0>12 мес.</option>"));
			period = 12;
			writeMaxCreditSumm(period,borProfit,surProfit,provisionOfCredit,percent);
		}
	});
	$("#period").change(function(){
		period = (parseInt($(this).children(":selected")[0].value)+1)*12;
		writeMaxCreditSumm(period,borProfit,surProfit,provisionOfCredit,percent);
		if(creditSumm)
			writeShedule(calcShedule(creditSumm,period,percent,payType),payType);
	});
	$("#pay-type").change(function(){
		payType = parseInt($($(this).children(":selected")[0]).val());
		if(creditSumm){
			console.log(payType);
			if(payType){
				annuitet = creditSumm*(percent/12)/(1-1/Math.pow(1+(percent/12),period));
				writeShedule(annuitet,payType);
				$("#shedule_table_container").css({'display':'none'});
				$("#print_version").css({'display':'none'});
				$("#mounth_pay_row").css({'display':'block'});
			}
			else{
				writeShedule(calcShedule(creditSumm,period,percent,payType),payType);
				$("#mounth_pay_row").css({'display':'none'});
				$("#shedule_table_container").css({'display':'block'});
				$("#print_version").css({'display':'block'});
			}
		}
	});
	$("#creditSumm").change(function(){

		creditSumm = parseInt($(this).val());
		if(parseInt($("#max_summ").text()) == 0){
			$(this).val(0);
			return;
		}
		if(creditSumm > parseInt($("#max_summ").text())){
			creditSumm = parseInt($("#max_summ").text());

		}

		if(creditSumm < 50000){
			creditSumm = 50000;
		}
		$(this).val(creditSumm);
		$("#credit_summ_slider").slider("value",1000*Math.log(creditSumm));
		writeShedule(calcShedule(creditSumm,period,percent));

		//console.log($("#borrower_profit_slider").slider("value"));
	});
	$("#borrower_profit_slider").slider({
		range: "min",
		value: 1000*Math.log(20000),
		min:   1000*Math.log(10000),
		max:   1000*Math.log(400000),
		step:  1,
		slide: function( event, ui ) {
			borProfit = 1000*Math.round(Math.pow(Math.E,parseInt(ui.value)/1000)/1000);
			$("#borrower_profit").val(borProfit);
			writeMaxCreditSumm(period,borProfit,surProfit,provisionOfCredit,percent);
			if(creditSumm)
				writeShedule(calcShedule(creditSumm,period,percent));
			return;
		}
	});
	$("#credit_summ_slider").slider({
		range: "min",
		value: 1000*Math.log(100000),
		min:   1000*Math.log(50000),
		max:   1000*Math.log(2400000),
		step:  1,
		slide: function( event, ui ) {
			creditSumm = 1000*Math.round(Math.pow(Math.E,parseInt(ui.value)/1000)/1000);
			
			if(creditSumm > parseInt($("#max_summ").text())){
				creditSumm = parseInt($("#max_summ").text());
				
			}


			$("#credit_summ_slider").slider("value",1000*Math.log(creditSumm));
			$("#creditSumm").val(creditSumm);
			writeShedule(calcShedule(creditSumm,period,percent));
			return;
		},
		stop: function( event, ui ) {
			creditSumm = 1000*Math.round(Math.pow(Math.E,parseInt(ui.value)/1000)/1000)
			if(creditSumm > parseInt($("#max_summ").text())){
				creditSumm = parseInt($("#max_summ").text());
			}
			$(this).slider('value', 1000*Math.log(creditSumm));
			$("#creditSumm").val(creditSumm);
			writeShedule(calcShedule(creditSumm,period,percent));
			return;
		}
	});
	writeMaxCreditSumm(period,borProfit,surProfit,provisionOfCredit,percent);
	writeShedule(calcShedule(creditSumm,period,percent));
});