// JavaScript Document
var firstLoadDone = 0;
var saveidTo = $('#saveidto').val();
function loadsaveTo()
{
    var saveidfrom = document.getElementById("saveidfrom").value;

    if (saveidfrom != "-1")
    {
        //get saveto Data
        $('#saveTo').load('transfermoneyAjaxController.php?do=loadsaveExpectId&saveidfrom=' + saveidfrom + '', function (response, status, xhr) {

            ////$('select').chosen();

            //$(".select2").attr('class',"select2 wdt100");

            $(".selectx1").select2();



            //get savefrom value
            $.ajax({
                url: 'transfermoneyAjaxController.php?do=getSaveValue&saveidfrom=' + saveidfrom,
                success: function (x) {
                    x = Number(x);
                    document.getElementById("savecurrentvalue").value = x;
                    document.getElementById("transfermoneyvalueCurr").value = x;
                    var cFactor = Number($('#cFactor').val());//Number($('#currencyId :selected').data('cfactor'));
                    xInMainCurr = Number((x / cFactor).toFixed(4));//convert to main curr
                    document.getElementById("transfermoneyvalue").value = xInMainCurr;
                    calcTransfermoneyvalueCurrSaveTo();


                    //@ edit & opening page first time make sure to put the value transfered not save value
                    if ($('#transfermoneyidold').length == 1 && firstLoadDone == 0) {
                        if ($('#currencyId').length > 0) {
                            //$('#currencyId').change();
                            $('#currencyText').text($('#currencyId').select2('data').text);


                            $('#saveidto').val(saveidTo).change();
                            //$('#saveid').change();//and this will trigger currency
                        }

                        $('#transfermoneyvalueCurr').val($('#transfermoneyvalueCurr_hidden').val()).keyup();
                    }
                    firstLoadDone = 1;

                }
            });
        });
        return false;
    }
}

function checksaveValue(TriggerInputName)
{
    if (TriggerInputName == 'transfermoneyvalueCurr') {
        //get transfermoneyvalue in Main currency
        var cFactor = Number($('#cFactor').val());//Number($('#currencyId :selected').data('cfactor'));
        var transfermoneyvalueCurr = Number($("#transfermoneyvalueCurr").val());
        transfermoneyvalue = Number((transfermoneyvalueCurr / cFactor).toFixed(4));//convert to main curr
        $("#transfermoneyvalue").val(transfermoneyvalue);
    } else if (TriggerInputName == 'transfermoneyvalue') {
        //transfermoneyvalue is in Main currency
        var transfermoneyvalueCurr = Number($("#transfermoneyvalueCurr").val());
        var transfermoneyvalue = Number($("#transfermoneyvalue").val());
//        if (transfermoneyvalueCurr == 0) {
//            var cFactor = Number($('#cFactor').val());
//            transfermoneyvalueCurr = Number((transfermoneyvalue * cFactor).toFixed(4));
//        }else
        cFactor = Number((transfermoneyvalueCurr / transfermoneyvalue).toFixed(7));
        $('#cFactor').val(cFactor);
        $('#otherCFactor').val(Number(1 / cFactor));
    }

    var savevalue = Number(document.getElementById("savecurrentvalue").value);
    //## check this payed money "transfermoneyvalueCurr" if it is in save from
    //1- get value
    var transfermoneyvalueCurr = Number($("#transfermoneyvalueCurr").val());//transfermoneyvalue
    //convert to main curr
    cFactor = Number($('#cFactor').val());
    transfermoneyvalueCurr = transfermoneyvalueCurr / cFactor;
    //then from main currency to save to currency
    var saveFromCfactor = Number($('#saveidfrom option:selected').data('cfactor'));
    transfermoneyvalueCurr = (transfermoneyvalueCurr * saveFromCfactor).toFixed(4);

    if (transfermoneyvalueCurr > savevalue)
    {
        //##allow up to max available money in save in terms of transfer currency
        //convert to main curr
        cFactor = Number($('#cFactor').val());
        savevalue = savevalue / saveFromCfactor;
        //then from main currency to transfer currency
        savevalue = Number((savevalue * cFactor).toFixed(4));



        //document.getElementById("transfermoneyvalue").value = savevalue;
        $("#transfermoneyvalueCurr").val(savevalue).keyup();
    }
    calcTransfermoneyvalueCurrSaveTo();
}

$('#saveTrnMnyBtn').live('click', function (e) {
    var savefrom = $('#saveidfrom').val();
    var saveto = $('#saveidto').val();

    var value = $('#transfermoneyvalue').val();
    value = value.trim();
    if (savefrom == '-1') {
        $('#fromError').show();
        $('#fromError').html("<p style='color: #CC3333;border: 1px solid #CC3333;width: 69px;'>*اختر خزنة</p>");
    } else if (saveto == '-1') {
        $('#fromError').hide();
        $('#toError').show();
        $('#toError').html("<p style='color: #CC3333;border: 1px solid #CC3333;width: 69px;'>*اختر خزنة</p>");
    } else if (value == '') {
        $('#fromError').hide();
        $('#toError').hide();
        $('#valueError').show();
        $('#valueError').html("<p style='color: #CC3333;border: 1px solid #CC3333;width: 120px;'>*ادخل القيمة المحولة</p>");
    } else if (value.length > 0) {
        $('#fromError').hide();
        $('#toError').hide();
        $('#valueError').hide();
        $('#myTranMnyform').submit();
        $('#saveTrnMnyBtn').attr("disabled", true);
    }
});

$('#currencyId').live('change', function (e) {
    $('#currencyText').text($('#currencyId').select2('data').text);
    var cfactor = $('#currencyId').select2("data").element[0].attributes['data-cfactor'].value
    $('#cFactor').val(cfactor);
    $('#otherCFactor').val(Number(1 / cfactor));
    //checksaveValue();
    $('#transfermoneyvalueCurr').val(0);
    $('#transfermoneyvalueCurrSaveTo').val(0);
    $('#transfermoneyvalue').val(0);
});
$('.calcOther').live('keyup change', function () {
    var val = Number($(this).val());
    if (val == 0) {
        var otherVal = 0;
    } else {
        var otherVal = Number((1 / val).toFixed(6));
    }
    if ($(this).attr('id') == 'cFactor') {
        $('#otherCFactor').val(otherVal);
    } else {
        $('#cFactor').val(otherVal);
    }

});
$('#cFactor').live('blur', function (e) {
    checksaveValue('transfermoneyvalueCurr');
});
$('#saveidfrom').live('change', function (e) {
    var curr = Number($('#saveidfrom option:selected').data('curr'));
    $('.currencyTextFrom').text($('#saveidfrom option:selected').data('curr-name'));
    $('#currencyId').val(curr).change();
});
$('#saveidto').live('change', function (e) {
    $('.currencyTextTo').text($('#saveidto option:selected').data('curr-name'));
    calcTransfermoneyvalueCurrSaveTo();
});
function calcTransfermoneyvalueCurrSaveTo() {
    var saveToCfactor = Number($('#saveidto option:selected').data('cfactor'));
    if (saveToCfactor) {
        var cFactor = $('#cFactor').val();
        var transfermoneyvalueCurr = Number($("#transfermoneyvalueCurr").val());
        //convert to main curr
        var transfermoneyvalueCurrSaveTo = transfermoneyvalueCurr / cFactor;
        //then from main currency to save to currency
        transfermoneyvalueCurrSaveTo = transfermoneyvalueCurrSaveTo * saveToCfactor;
        transfermoneyvalueCurrSaveTo = Number(transfermoneyvalueCurrSaveTo.toFixed(4));//convert to main curr
        $('#transfermoneyvalueCurrSaveTo').val(transfermoneyvalueCurrSaveTo);
    } else {
        $('#transfermoneyvalueCurrSaveTo').val(0);
    }
}
function SaveToMoneyChangeReCalcCfactor() {
    var transfermoneyvalueCurr = Number($("#transfermoneyvalueCurr").val());
    var transfermoneyvalueCurrSaveTo = Number($("#transfermoneyvalueCurrSaveTo").val());
    var saveToCfactor = Number($('#saveidto option:selected').data('cfactor'));
    if (saveToCfactor && transfermoneyvalueCurrSaveTo != 0 && transfermoneyvalueCurr != 0) {
        cFactor = transfermoneyvalueCurr / (transfermoneyvalueCurrSaveTo / saveToCfactor);
        cFactor = Number(cFactor.toFixed(7)); //fraction is important
        $('#cFactor').val(cFactor);
        $('#otherCFactor').val(Number(1 / cFactor));
        $('#saveidfrom option:selected').data('cfactor', cFactor);
        checksaveValue('transfermoneyvalueCurr');
    }
}
$(function () {
    if ($('#currencyId').length > 0) {
        //$('#currencyId').change();
        $('#currencyText').text($('#currencyId').select2('data').text);


        $('#saveidto').change();
        //$('#saveid').change();//and this will trigger currency
    }

    if ($('#saveidfrom').val() > 0) {
        $('#saveidfrom').change();
    }
});