﻿/**
 * Functions to handle data validation and calculations.
 */

/**
 * Hash structures to hold different interest percents
 * NOTE: They are *year* percent
 */
var standart_interest = {
    'BGN': {
        '1' : 2.25,
        '3' : 3.25,
        '6' : 4.25,
        '12': 5.5,
        '24': 6,
        '36': 6.25
    },
    'EUR': {
        '1' : 1.1,
        '3' : 1.5,
        '6' : 2.75,	// Changed from 3.50% on 2011-02-01
        '12': 4.5,	// Changed from 3.50% on 2011-03-01
        '24': 4.7,
        '36': 5
    },
    'USD': {
        '1' : 0.7,	// Changed from 2.50% on 2010-07-15
        '3' : 0.95,	// Changed from 3.00% on 2010-07-15
        '6' : 1.2,	// Changed from 3.25% on 2010-07-15
        '12': 1.45,	// Changed from 3.50% on 2010-07-15
        '24': 1.7,	// Changed from 4.00% on 2010-07-15
        '36': 1.95	// Changed from 4.50% on 2010-07-15
    },
    'GBP': {
        '1' : 2.25,
        '2' : 2.30,
        '3' : 2.35,
        '6' : 2.60,
        '9' : 2.80,
        '12': 3,
        '24': 3.4,
        '36': 3.6
    }
};
var flexible_interest = {
    'BGN': {
        '1' : 2.00,
        '3' : 3.00,
        '6' : 4.00,	// Changed from 4.50% on 2011-02-01
        '12': 5.25,	// Changed from 5.25% on 2011-03-01
        '18': 6.25,
        '24': 5.50,
        '36': 6.00
    },
    'EUR': {
        '1' : 0.85,
        '3' : 1.25,
        '6' : 1.75,	// Changed from 3.00% on 2011-02-01
        '12': 4,	// Changed from 3.25% on 2011-03-01
        '18': 4.30,
        '24': 4.25,
        '36': 4.5
    },
    'USD': {
        '1' : 2.00,
        '3' : 2.50,
        '9' : 2.80,
        '12': 3.00,
        '18': 3.2,
        '24': 3.5,
        '36': 4.0
    },
    'GBP': {
        '1' : 1.75,
        '2' : 1.80,
        '3' : 1.85,
        '6' : 2.10,
        '9' : 2.30,
        '12': 2.50,
        '18': 2.70,
        '24': 2.90,
        '36': 3.10
    }

};

var bank_deposit_interest = {
    'BGN': 2.75,	// From 2011-06-01 there is no differentiation based on the sum  
    'EUR': 1.50		// From 2011-06-01 there is no differentiation based on the sum
};

/*
var bank_deposit_interest = {
    'BGN': {
        '1' : 2.75,
        '2' : 2.00,	// Changed from 3.25% on 2010-07-15
        '3' : 2.00,	// Changed from 3.50% on 2010-07-15
        '4' : 2.25	// Changed from 4.00% on 2010-07-15
    },
    'EUR': {
        '1' : 1.50,
        '2' : 0.85,	// Changed from 2.25% on 2010-07-15
        '3' : 0.85,	// Changed from 2.50% on 2010-07-15
        '4' : 1.10	// Changed from 3.00% on 2010-07-15
    }
};
*/

/**
 * Returns interest value based on form parameters
 */
function get_interest(form) {
    var currency = form.currency_select.value;

    if (form.id == "Savings") {
        
	/*	This has changed from 2011-06-21 and the interest is NOT calculated based on the sum range
		// Because the interest is calculated based on sum range
        // we find the current idx
        // var sum = parseInt(form.sum.value);
        
		var idx = 0;
        if (sum > 5000) { // Changed from 3000 on 2010-07-15
            idx = 4;
        }
        else if (sum > 2000) {
            idx = 3;
        }
        else if (sum > 1000) {
            idx = 2;
        }
        else {
            idx = 1;
        }
	*/
        return bank_deposit_interest[currency];
    }
    else if (form.id == "Standart") {
        var month = form.period.options[form.period.selectedIndex].value;

        return standart_interest[currency][month];
    }
    else {
        var month = form.period.options[form.period.selectedIndex].value;

        return flexible_interest[currency][month];
    }
}

/**
 * Performs calculation based on form_name values
 * and sets appropriate result fields.
 * Expects all required fields to be filled with correct values.
 */
function calculate(form_name) {
    var sum = parseInt(form_name.sum.value);
    var percent = get_interest(form_name);

    if (form_name.id == "Savings") {
        var period = 1;
    }
    else {
        var period = form_name.period.options[form_name.period.selectedIndex].value;
    }
    
	var today = new Date();
	var currentMonth = today.getMonth();
	var numberOfDays = 0;
	
	var months = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
	
	while(period > 0) {
		numberOfDays += months[currentMonth];
		
		currentMonth++;
		if(currentMonth > 11) {
			currentMonth = 0;
		}
		
		period--;
	}
	var interest = (sum * (percent / 100) * numberOfDays / 360).toFixed(2);
	
	// Basic formula:
    //      Interest is %, so we divide with 100
    //      Because it's year interest we cast it to one month
    // var interest = r(sum * (percent / 100) * (period / 12), 2);
	

    switch(form_name.id) {
        case "Standart":
        case "ProVlog" :
        case "ProAvans":
        case "Savings" :
            form_name.interest.value = interest;
            break;
        case "ProRent":
            var period = parseInt(form_name.interest_period.value);
            form_name.interest.value = r(interest * period / period, 2);
            break;
        case "ProTen":
            form_name.free_sum.value = r(sum * 0.1, 2);
            form_name.interest.value = interest;
            break;
        case "ProInvest":
            form_name.free_sum.value = r(sum / period, 2);
            form_name.interest.value = interest;
            break;
    }
}
/**
 * Validate if the all required input fields are filled
 * and their value is correct.
 */
function validate_fields(form_name) {
    // Because every form has a sum field we validate that first
    if (form_name.sum.value == "") {
        form_name.interest.value = "";
        alert("Въведете сума");
        form_name.sum.focus();
        return false;
    }
    else if (isNaN(form_name.sum.value) || parseInt(form_name.sum.value) < 0) {
        form_name.interest.value = "";
        alert("Въведете правилна сума");
        form_name.sum.focus();
        return false;
    }
    else if (form_name.id != "Savings" && parseInt(form_name.sum.value) < 100) {
        form_name.interest.value = "";
        alert("Въведете сума по-голяма или равна на 100");
        form_name.sum.focus();
        return false;
    }
    if(form_name.id == "ProRent" && (parseInt(form_name.interest_period.value) > parseInt(form_name.period.value))){
        form_name.interest.value = "";
        alert("Периодът за изплащане не може да е по-голям от периода на депозита");
        form_name.interest_period.focus();
        return false;
    }

    return true;
}

/**
 * This function is called on onchange event of the required
 * fields.
 */
function change_forms(form_name) {
    if (validate_fields(form_name)) {
        calculate(form_name);
    }
}
/**
 * Handles specific cases and show/hide options in period <select>
 *   - if period is 9, removes 6 from interest_period
 *   - if period is 18, removes 12 from interest_period
 */
function change_period(form) {

    if (!this.init) {
        Periods.init("interest_period");
        this.init = 1;
    }
    if (this.init) {
        Periods.restore(form.interest_period);
        var month = parseInt(form.period.options[form.period.selectedIndex].value);

        Periods.removeGreater(form.interest_period, month);
        switch(month) {
            case 9:
                Periods.removeNode(form.interest_period, 6);
                break;
            case 18:
                Periods.removeNode(form.interest_period, 12);
                break;
        }

    }

    change_forms(form);
}

var Periods = {
    periods: new Array(),

    init: function(select) {
        period = document.getElementsByName(select)[0];
        options = period.getElementsByTagName("option");
        for (var i = 0; i < options.length; i++) {
            Periods.periods[i] = options[i];
        }
    },

    restore: function(src) {
        for (var i = 0; i < this.periods.length; i++) {
            src.appendChild(this.periods[i]);
        }
    },

    removeNode: function(src, node) {
        for (var i = 0; i < this.periods.length; i++) {
            if (this.periods[i].value == node) {
                src.removeChild(this.periods[i]);
            }
        }
    },

    removeFrom: function(src, targets) {
        for (var i = 0; i < targets.length; i++) {
            src.removeChild(targets[i]);
        }
    },

    appendTo: function(src, targets) {
        for (var i = 0; i < targets.length; i++) {
            scr.appendChild(targets[i]);
        }
    },

    getGreaters: function(node) {
        var greaters = new Array();
        var c = 0;

        for (var i = 0; i < Periods.periods.length; i++) {
            if (Periods.periods[i].value > node) {
                greaters[c++] = Periods.periods[i];
            }
        }
        return greaters;
    },

    removeGreater: function(src, node) {
        var targets = this.getGreaters(node);
        this.removeFrom(src, targets);
    },

    appendGreater: function(src, node) {
        var targets = this.getGreaters(node);
        this.appendTo(src, targets);
    }
}

/**
 * Because js sucks we implement
 * rounding function.
 */
function r(num, dec) {
    return Math.round(Math.pow(10, dec) * num)/Math.pow(10, dec);
}

// vim: sw=4 ts=4 sts=4

