﻿var EXList;
var frmCcxr;
var selectedFromCode;
var selectedToCode;
function LoadDataFromHD() {
    EXList = eval($("#hdCXR").val());
    var objs = EXList;
    var cbFrom = frmCcxr.cbCXDFrom;
    var cbTo = frmCcxr.cbCXDTo;
    
    var j = 0;   //add for selected
    
    $("#cbCXDTo,#cbCXDFrom").html('');
    for (var i = 0; i < objs.length; i++) {
        var newOption = document.createElement('option');
        newOption.value = objs[i].Code;

        newOption.innerHTML = oCCX.ShowCurrencyName ? objs[i].Name + " (" + objs[i].Code + ")" : objs[i].Code;
        
        //set selected
        if ((!$("#cbCXDFrom option:selected").length) && (newOption.value == "EUR")){
            j = 1;
            $(newOption).attr("selected", "selected");
        }

        cbFrom.appendChild(newOption);

        var newOption1 = document.createElement('option');
        newOption1.value = objs[i].Code;
        newOption1.innerHTML = oCCX.ShowCurrencyName ? objs[i].Name + " (" + objs[i].Code + ")" : objs[i].Code;
        cbTo.appendChild(newOption1);

    }
    //add for selected
    if( j == 1 )
        selectedFromCode = cbFrom.options[1].value;
        
    //selectedFromCode = cbFrom.options[0].value;
    selectedToCode = cbTo.options[0].value;
}
$(document).ready(function() {
    frmCcxr = $("#frmCcxr")[0];
    if (oCCX.EnableAjaxCurrencyList) {
        $.ajax({
            type: "GET",
            contentType: "text/json; charset=utf-8",
            url: "/Widgets/Travel/CurrencyConverter/CurrencyRate.ashx" + (oCCX.ShowCurrencyName == false ? "?Ignore=Currency_Name" : ""),
            data: [],
            dataType: "text",
            success: function(data) {
                $("#hdCXR").val(data);
                LoadDataFromHD();

            }
        });
    }
    else {
        LoadDataFromHD();
    }
});

function isNumberKey(evt) {
    var charCode = (evt.which) ? evt.which : event.keyCode
    if (charCode == 13) { $("#btnSubmit").trigger("click"); return false; }
    if (charCode > 31 && (charCode < 48 || charCode > 57) && charCode != 46)
        return false;

    return true;
}
function CalFromUSD(ToCode, val) {
    return val * MatchRate(ToCode);
}

function CalToUSD(FromCode, val) {
    return val * (1 / MatchRate(FromCode));
}

function MatchRate(codeName) {
    var objs = EXList;
    for (var i = 0; i < objs.length; i++) {
        if (objs[i].Code == codeName) {
            return objs[i].Rate;
        }
    }
    return -1;
}

function addCommas(nStr) {
    nStr += '';
    var c = 0;
    x = nStr.split('.');
    x1 = x[0];
    x2 = x.length > 1 ? '.' + x[1] : '';
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(x1)) {
        x1 = x1.replace(rgx, '$1' + ',' + '$2');
    }
    if (x2.length > 3) {
        while (x2.substring(x2.length - 1, x2.length) == '0' && c < 4) {
            x2 = x2.substring(0, x2.length - 1);
            c++;
        }
    }
    return x1 + x2;
}
function Calculate(amount, nFixed) {
    var usdAmount = CalFromUSD(selectedFromCode, amount);
    var result = CalToUSD(selectedToCode, usdAmount);
    var ans = addCommas(result.toFixed(nFixed));
    return ans;
}

$(document).ready(function() {
    $("#btnSubmit").click(function() {
        //Caculate
        //var cResult = $("#current").find("#result")[0];
        var cResult = $("#wCurrency0001").find("#result")[0];
        var ipStr = frmCcxr.currentAmount.value.replace(/,/g, '');
        var amount = new Number(ipStr);
        //var resultStr = "<p>" + addCommas(amount) + " " + selectedFromCode + " = " + Calculate(amount, 2) + " " + selectedToCode + "</p>1 " + selectedFromCode + " = " + Calculate(1, 6) + " " + selectedToCode;
        var resultStr = "<p1><br>Currency Converter Results:<br>" + addCommas(amount) + " " + selectedFromCode + " = " + Calculate(amount, 2) + " " + selectedToCode + "</p1>"; //1 " + selectedFromCode + " = " + Calculate(1, 6) + " " + selectedToCode;
        $(cResult).html(resultStr);
        $(cResult).show();
        return false;
    });
    $("#cbCXDFrom,#cbCXDTo").change(function() {
        var newCode = $("option:selected", this).attr("value"); //$(this).val();

        if ($(this).attr("id") == "cbCXDFrom")
            selectedFromCode = newCode;
        else
            selectedToCode = newCode;
    });
});