﻿//FUNCTION USED IN ALL CALCULATORS
//Allows only digits to be entered in amount inputs
function getkey(e) {
    if (window.event)
        return window.event.keyCode;
    else if (e)
        return e.which;
    else
        return null;
}
function goodchars(e, goods) {
    var key, keychar;
    key = getkey(e);
    if (key == null) return true;
    // get character
    keychar = String.fromCharCode(key);
    keychar = keychar.toLowerCase();
    goods = goods.toLowerCase();
    // check goodkeys
    if (goods.indexOf(keychar) != -1)
        return true;
    // control keys
    if (key == null || key == 0 || key == 8 || key == 9 || key == 13 || key == 27)
        return true;
    // else return false
    return false;
}






//Calcs currency
$(document).ready(function() {


    $('#calc_currency').click(function() {

        //get language
        var urlArr = window.location.href.split("/");
        var bg = false;
        for (var i = 0; i < urlArr.length; i++) {
            if (urlArr[i].toLowerCase() == 'bg-BG'.toLowerCase()) {
                bg = true;
                break;
            }
        }

        //localizing
        var msgCurrency;
        var msgSameAmount;
        if (bg) {
            msgCurrency = 'Избрана е една и съща валута.';
            msgSameAmount = 'Не е въведена сума.';
        }
        else {
            msgCurrency = 'Both currencies are the same.';
            msgSameAmount = 'Invalid amount.';
        }

        var rate;

        var amount = parseInt($('#currency_amount').attr('value'));

        var valFrom = $('#from_currency').val();
        var valTo = $('#to_currency').val();

        valFrom = valFrom.replace(",", ".");
        valTo = valTo.replace(",", ".");
        

        if (valFrom == valTo) {
            alert(msgCurrency);
            $('#result_currency').hide();
        }
        else if (amount == '0' || isNaN(amount)) {
            alert(msgSameAmount);
            $('#result_currency').hide();
        }

        else {

            $('#result_currency').show();


            // Convert from BGN
            if (valFrom == "BGN") {
                rate = 1 / valTo;
            }

            // Convert to BGN
            else if (valTo == "BGN") {
                rate = valFrom;
            }

            // Convert between currencies
            else {
                rate = valFrom / valTo;
            }

        }

        var price = amount * rate;

        //set it to span - result
        $('#result_value').text((parseInt(price * 100)) / 100);

        var selectedCurrName = $('#to_currency :selected').text();

        //set it to span - currency name
        $("#result_val_curr").text('  ' + selectedCurrName);

    });



    //clear btn - clear amount field and colapse
    $('#clear').click(function() {

        $('#result_currency').hide();

        $('#currency_amount').attr('value', '0')

    });

});

