var PVN = 1.21;

fmtMoney = function(n, c, d, t){ 
	var m = ( c = Math.abs(c) + 1 ? c : 2, d = d || ",", t = t || ".", /(\d+)(?:(\.\d+)|)/.exec(n + "")), x = m[1].length > 3 ? m[1].length % 3 : 0;
	return ( x ? m[1].substr( 0, x ) + t : "") + m[1].substr( x ).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + (+m[2] || 0).toFixed(c).substr(2) : "");
};

calcLeasing_by_type_proc = function(price){
//	var price = parseInt(price);
    var price = parseInt($('article_price').value);
	var first_proc_custom = $('leasing_first_proc_custom');
	var first_sum = $('leasing_first_sum').value;
	//first_sum = Math.round(first_sum);
	//$('leasing_first_sum').value = first_sum;
	var proc = first_sum/price*100;
	proc = proc.toFixed(5);
	first_proc_custom.value = proc;
	//calcLeasing(price)
}

// Function needed for nelss leasing
function GetPPMT(intr, Per, NPer, PV, FV, Due){
	float1 = Per - (Due?2:1);
	float2 = - FV*intr/((Math.pow(1+ intr,NPer)- 1)*(Due?1+ intr:1))+ - PV/((Due?1:0)+ 1/intr*(1- 1/Math.pow(1+ intr,NPer- (Due?1:0))));
	return ((- FV*intr/((Math.pow(1+ intr,NPer)- 1)*(Due?1+ intr:1))+ - PV/((Due?1:0)+ 1/intr*(1- 1/Math.pow(1+ intr,NPer- (Due?1:0))))- - (PV*Math.pow(1+ intr,float1)- (- 0*Math.pow(1+ intr,float1)+ - (1/intr)*float2*(Math.pow(1+ intr,float1)- 1)*(Due?1+ intr:1)- (Due?float2:0)))*intr)*-1);
}

//PPmt = Pmt - IPmt
function GetIPMT(intr, Per, NPer, PV, FV, Due){
	float1 = Per - (Due?2:1);
	float2 = - FV*intr/((Math.pow(1+ intr,NPer)- 1)*(Due?1+ intr:1))+ - PV/((Due?1:0)+ 1/intr*(1- 1/Math.pow(1+ intr,NPer- (Due?1:0))));
	return -(((PV*Math.pow(1+ intr,float1)- (- 0*Math.pow(1+ intr,float1)+ - (1/intr)*float2*(Math.pow(1+ intr,float1)- 1)*(Due?1+ intr:1)- (Due?float2:0)))*intr)*-1);	
}
function roundNumber(rnum, rlength) { // Arguments: number to round, number of decimal places
  var newnumber = Math.round(rnum*Math.pow(10,rlength))/Math.pow(10,rlength);
  return newnumber;
}

calcLeasing = function(){
	// for showing the days
	/*
	if($('p-leasing_day')){
	 if($('leasing_hansa').checked==true || $('leasing_ge_money').checked==true){
    $('p-leasing_day').show();
    if($('leasing_hansa').checked==true){
     $('leasing_day_hansa').show();
     $('leasing_day_ge_money').hide();
	  }else if($('leasing_ge_money').checked==true){
	 	$('leasing_day_hansa').hide();
     $('leasing_day_ge_money').show();
	  }
	 }else{
	  $('p-leasing_day').hide();
	 }
	}*/
	
	var atOrder = arguments[1];
	var docl = document.location+'';
	if(docl.indexOf('/admin/')!=-1) {
		CategoryID = arguments[2];
	}
	var prefix = '';
	if(atOrder)prefix='leasing_';
//	var price = parseInt(price);
    var price = parseInt($('article_price').value);
	var first_proc = $(prefix+'first_proc');
	var first_proc_custom = $(prefix+'first_proc_custom');
	var period = $(prefix+'period');
	var period_custom = $(prefix+'period_custom');
	var first_sum = $(prefix+'first_sum');
	var monthly_sum = $(prefix+'monthly_sum');
	// first proc val
	var first_proc_val = parseInt(first_proc.options[first_proc.selectedIndex].value);
	
	if(parseInt(first_proc_custom.value)>=0){
		if (docl.indexOf('/admin/')==-1) {
			if(parseInt(first_proc_custom.value)>50) first_proc_custom.value = 50;
		}
		
		first_proc.disabled=true;
		
		if(docl.indexOf('/admin/')==-1){
			var first_proc_val = parseInt(first_proc_custom.value);
		} else {
			var first_proc_val = parseFloat(first_proc_custom.value);
		}
	} else {
		first_proc.disabled=false;
		first_proc_custom.value = '';
	}
	
	// period val
	var max_period = 36;
//	if(CategoryID == 102)max_period = 24;
	var period_val = period.options[period.selectedIndex].value;
	
	if (parseInt(period_custom.value) > 2) {
		if(parseInt(period_custom.value)>max_period)period_custom.value = max_period;
		period.disabled=true;
		var period_val = parseInt(period_custom.value);
	} else {
		period.disabled=false;
		if(!parseInt(period_custom.value))period_custom.value = '';
	}
 
	// start calculating

	 /*else if($('leasing_parex').checked==true) {
		var first_sum_val_exact = price*first_proc_val/100;
	}else if($('leasing_julianus').checked==true){
		var first_sum_val_exact = price*first_proc_val/100;
	}
	else if($('leasing_ge_money').checked==true){
		var first_sum_val_exact = price*first_proc_val/100;
	}else if($('leasing_ge_money').checked==true){
//  =CEILING(A4*(D4+L4);0,01)
		var comission = 3; // percents
		var first_sum_val_exact = price*(parseInt(first_proc_val)+comission)/100;
	}*/
	

	var first_sum_val_exact = price*first_proc_val/100;
	
	/*if ($('leasing_julianus')) {
		if ($('leasing_julianus').checked==true) {
			var first_sum_val_exact = price*first_proc_val/100;
		}
	}*/
	
	first_sum_val = first_sum_val_exact.toFixed(2);
	first_sum_val = fmtMoney(first_sum_val,2,',',' ');
	first_sum_val = first_sum_val.replace(' ,',',');
	first_sum.innerHTML = first_sum_val;
 
// var monthly_sum_val = (price-first_sum_val_exact)/period_val;
//Math.round(((Math.round(frm.total.value*(1-(frm.first.value/100))/1.18*100)/100)/((1-(1/Math.pow(1+frm.proc.value/100/12,frm.time.value)))/(frm.proc.value/100/12)))*1.18*100)/100;
//Math.round(((Math.round(price*(1-(1-first_sum_val_exact/100))/1.18*100)/100)/((1-(1/Math.pow(1+PROC/100/12,period_val)))/(PROC/100/12)))*1.18*100)/100;

	if ($('p-hansa-reward')!=null) $('p-hansa-reward').hide();
	
	if ($('leasing_hansa') && $('leasing_hansa').checked==true){

		var PROC = 0;
		/*
		if(CategoryID==102){
					PROC = 30;
				}else{
					PROC = 29;
				}*/
		
		/*else if(price>=45 && price<=500){
			PROC = 25;
		}else if(price>500 && price<=1000){
			PROC = 23;
		}else if(price>1000 && price<=4000){
			PROC = 22;
		}else if(price>4000 && price<=7000){
			PROC = 20;
		}*/
		var str='Math.round(((Math.round('+price+'*(1-('+first_proc_val+'/100))/'+PVN+'*100)/100)/((1-(1/Math.pow(1+'+PROC+'/100/12,'+period_val+')))/('+PROC+'/100/12)))*'+PVN+'*100)/100';
		var monthly_sum_val = eval(str);

  		var reward = 0;
  		var reward_min = 3;
  		
  		if ($("usertype") && $("usertype").value == 1) {
  			reward_min = 10;
			}
  		
  		/*if (CategoryID == 102) {
  			reward = (price * 0.025) > reward_min ? fmtMoney((price * 0.025 * PVN), 2, ',', ' ') : reward_min;
			} else {
				reward = (price * 0.015) > reward_min ? fmtMoney((price * 0.015 * PVN), 2, ',', ' ') : reward_min;
			}*/
			reward = (price * 0.03) > reward_min ? fmtMoney((price * 0.03 * PVN), 2, ',', ' ') : reward_min;
  		
  		$('p-hansa-reward').show();
		$('leasing_reward_sum').value = reward;

	
	} /*else if($('leasing_parex').checked==true){
 
		var PROC = 24;
		var giving_sum = price-first_sum_val_exact;
		var proc2 = PROC/12/100;
		//Aizd_summa * proc2 (1-1/(1-pow(1+proc2,men)))
//  =ROUND(B6*B7*(1-1/(1-POWER(1+B7;B3)));2)
		var monthly_sum_val = giving_sum * proc2 * (1-1/(1-Math.pow((1+proc2),period_val)));

	}else if($('leasing_julianus').checked==true){

		PROC = 28;	
		var giving_sum = price-first_sum_val_exact;
		var proc2 = PROC/12/100;
		var monthly_sum_val = giving_sum * proc2 * (1-1/(1-Math.pow((1+proc2),period_val)));
	
	}	else if($('leasing_ge_money').checked==true){
		
		var PROC = 0;
		if(price>=49 && price<=499){
			PROC = 29.9;
		}else if(price>500 && price<=2500){
			PROC = 27.9;
		}else if(price>=2501 && price<=10000){
			PROC = 24.9;
		}

		var giving_sum = price-first_sum_val_exact;
		var proc2 = PROC/12/100;
		var monthly_sum_val = giving_sum * proc2 * (1-1/(1-Math.pow((1+proc2),period_val)));
		
	
	}
	
	if($('leasing_julianus')){
		if($('leasing_julianus').checked==true){

			PROC = 28;	
			var giving_sum = price-first_sum_val_exact;
			var proc2 = PROC/12/100;
			var monthly_sum_val = giving_sum * proc2 * (1-1/(1-Math.pow((1+proc2),period_val)));
		}	
	}*/

		
		var PROC = 0;
		var PROC_arr_keys = { 3:0, 6:1, 12:2, 18:3, 24:4, 36:5 };
		var PROC2 = [8, 8, 9, 10, 10, 10];
		if(price>=40 && price<=500){
			//  Period - 3, 6, 12, 18, 24, 36
			PROC = [0.9, 1.5, 1.7, 1.7, 1.8, 1.8];
		}else if(price>500){
			PROC = [0.9, 1.4, 1.6, 1.6, 1.7, 1.8];
		}
		
		PROC = PROC[PROC_arr_keys[period_val]]/100;
		PROC2 = PROC2[PROC_arr_keys[period_val]]/100;
		
		//function GetPPMT(intr, Per, NPer, PV, FV, Due)
		var nelss_val = roundNumber(GetPPMT(PROC,1,period_val,price+price*PROC2,0,0),2);
		var nelss_val2 = roundNumber(GetIPMT(PROC,1,period_val,price+price*PROC2,0,0),2);
		var nelss_val3 = 1;
		
		//console.info(GetPPMT(PROC,1,period_val,price+price*PROC2,0,0));
		//console.info(GetIPMT(PROC,1,period_val,price+price*PROC2,0,0));
		//console.info(PROC+','+1+','+period_val+','+(price+price*PROC2));
		
		monthly_sum_val = nelss_val + nelss_val2 + nelss_val3;
		
	
 if(PROC>0){
  monthly_sum_val = fmtMoney(monthly_sum_val,2,',',' ');
  monthly_sum_val = monthly_sum_val.replace(' ,',',');
  monthly_sum_val = monthly_sum_val.replace('N','00');
 }

 monthly_sum.innerHTML = monthly_sum_val;
 
}
