// Legal number of days per year (SFS 1999:917)
days_in_year = 365.25;

// Legal number of days per month (SFS 1999:917)
days_in_month = 30.41666;

function dailyrate(rate){
    return Math.pow(((rate/10000) + 1), (1 / 365.25));
}

function periodicCost(sum, months, monthfee, startfee, rate){
    var daily_rate = dailyrate(rate);
    var monthpayment = calcMonthpayment(sum+startfee, daily_rate, months);
    return Math.round(monthpayment + monthfee);
}

function calcMonthpayment(sum, daily_rate, months){
    var dates = 0;
    var totdates = ((months -1) * 30);
    var denom = calcDenom(daily_rate, totdates);
    totdates = totdates + 60;
    return ((Math.pow(daily_rate, totdates) * sum) / denom);
}

function calcDenom(daily_rate, totdates){
    var sum = 1;
    var startdates = 0;
    while(totdates > startdates){
	startdates = startdates + 30;
	sum = (sum + Math.pow(daily_rate, startdates));
    }
    return sum;
}

function calculate_months(amount, monthcost, mul){
    month = 0;
    days = 2*days_in_month;
    prevamount = amount +1
    while(amount >= 0){
	newamount = amount*Math.pow(mul, days) - monthcost;
	if(newamount > prevamount){
	    return -1;
	}
	prevamount = amount;
	amount = newamount;
	days = days_in_month;
	month++;
    }
    return month;
}

function c(f){
    amount = myFloat(f.amount.value);
    months = parseInt(f.months.value);
    //rate = myFloat(f.rate.value);
    //startfee = myFloat(f.startfee.value);
    //monthfee = myFloat(f.monthfee.value);
    //monthcost = myFloat(f.monthcost.value);
    if(check(amount, months)){
        amountx = (amount * 30)/100;
        monthsx = months * 2210;
	periodic_cost = amountx + monthsx;
	f.pcost.value = Math.ceil(periodic_cost);
    }else{
	f.pcost.value = f.efrate.value = f.ccost.value = "";
    }
}

function get_start_fee_from_months(months){
    if(months > 6){
        return 295.0;
    }else if(months > 3){
        return 195.0;
    }else{
        return 95.0;
    }
}

function get_rate_from_months(months){
    if(months > 12){
        return 9.95;
    }else{
        return 0.0;
    }
}

function myFloat(str){
    v = parseFloat(str.replace(",","."));
    return v;
}

function check(amount, months){
    all_good = true;
    str = "";
    
    if(isNaN(amount)){
	all_good = false;
	str = str + err("Felaktigt belopp");
    }
    
    if(isNaN(months)){
	all_good = false;
	str = str + err("Felaktigt antal månader");
    }

    var a = document.getElementById("alert");
    if(all_good){
        a.innerHTML = "<br/>";
	return true;
    }else{
        a.innerHTML = str;
	return false;
    }
}

function err(str){
    return "<div class=\"error\">"+str+"</div>";
}

function apr(amount, rate, periods, startfee, monthfee, cost){
    rate = rate/100;
    daily_interest = Math.pow(1+rate, 1/days_in_year);
    month_interest = Math.pow(daily_interest, days_in_month);
    sum = 0;
    max_r = amount * 10;
    guess = 100*(startfee+periods*(monthfee+(month_interest-1)*amount))/amount;
    min_r = 1;//guess/100;
    return fix_p2(0, sum, amount, guess, max_r, min_r, periods, cost, monthfee +
		 startfee/periods, (month_interest-1));
}

function fix_p(count, sum, amount, guess, max_r, min_r, periods, cost,
	       monthfee, month_interest){
    if(count > 900){ // risk for to much recursion
	return -1;
    }
    if(sum + 0.001 > amount && sum < amount + 0.001){
	return (Math.pow(guess,12)-1)*100;
    }
    acc1 = amount;
    acc2 = 0;
    for(i=1; i<= periods; i++){
	if(i==1){
	    interest = 2*acc1*month_interest;
	}else{
	    interest = acc1*month_interest;
	}
	new_acc = acc1 + interest + monthfee;
	payment = min(cost, new_acc);
	g = payment/Math.pow(guess, i);
	acc1 = new_acc-payment;
	acc2 = acc2+g;
    }
    if(acc2 < amount){
	return fix_p(count+1, acc2, amount, (guess+min_r)/2, guess, min_r,
		     periods, cost, monthfee, month_interest);
    }else{
	return fix_p(count+1, acc2, amount, (guess+max_r)/2, max_r, guess,
		     periods, cost, monthfee, month_interest);
    }
}


function fix_p2(count, sum, amount, guess, max_r, min_r, periods, cost,
	       monthfee, month_interest){
    while(true){
	if(count > 25000){
	    return -1;
	}
	if(sum + 0.001 > amount && sum < amount + 0.001){
	    return (Math.pow(guess,12)-1)*100;
	}
	acc1 = amount;
	acc2 = 0;
	for(i=1; i<= periods; i++){
	    if(i==1){
		interest = 2*acc1*month_interest;
	    }else{
		interest = acc1*month_interest;
	    }
	    new_acc = acc1 + interest + monthfee;
	    payment = min(cost, new_acc);
	    g = payment/Math.pow(guess, i);
	    acc1 = new_acc-payment;
	    acc2 = acc2+g;
	}
	count = count+1;
	sum = acc2;
	if(acc2 < amount){
	    max_r = guess;
	    guess = (guess+min_r)/2;
	}else{
	    min_r = guess;
	    guess = (guess+max_r)/2;
	    
	}
    }
}

function min(x,y){
    if(x < y){
	return x;
    }else{
	return y;
    }
}


