var currentObject;

var Xurrency =
{
    initHomePage: function()
    {  
        $('#converter').submit(Xurrency_Controller_HomePage.convert);
    }
}

var Xurrency_Controller_HomePage =
{
    convert: function()
    {
        obj = this;

        amount = $('#amount').val();
        expreg = new RegExp(/^[0-9.]{1,12}$/);

        if (amount == '') {
            alert('Please insert a value');
            $('#amount').focus();
        } else if (amount <= 0) {
            alert('Sorry, the value needs to be greater than 0');
            $('#amount').focus();
        } else if (expreg.test(amount) == false) {
            alert('Sorry, the inserted value is not correct');
            $('#amount').focus();
        } else if ($('#base').val() == $('#target').val()) {
            alert('The base and target currencies are both the same');
        }
    
        $.getJSON('/api/'+$('#base').val()+'/'+$('#target').val()+'/'+amount, function(data) {
            if (data.status === 'ok') {
                $('#result').fadeOut('fast', function() {  $('#result').fadeIn(); $('#result').css('display', 'block'); });
                $('#result').html(amount+' '+data.result.base+' = '+data.result.value+' '+data.result.target);
            } else {
                alert(data.message);
            }
        });

        return false;
    }
}