/*global window, setTimeout, Ajax, $, $H, document, dollarName */
var qc_inOperation = 1;

var input_amount = 0;
var first_currency = '-';
var second_currency = '-';

function qc_showResponse(transport, url) {

    if ((transport.responseText).isJSON()) {
        var qc_result = (transport.responseText).evalJSON();

        var dollarNameHash = $H(dollarName);
        var dNameA = dollarNameHash.get(first_currency);
        var dNameB = dollarNameHash.get(second_currency);

        var output_curAB = input_amount + " " + dNameA + " = " + qc_result.ask_amount + " " + dNameB;
        var output_curBA = input_amount + " " + dNameB + " (" + second_currency + ") = " + qc_result.bid_amount + " " + dNameA + " (" + first_currency + ")";
        var output_median = qc_result.bid + " / " + qc_result.ask;
        var output_min = qc_result.min_bid + " / " + qc_result.min_ask;
        var output_max = qc_result.max_bid + " / " + qc_result.max_ask;

        $('quickconverter_date').update(qc_result.date);
        $('quickconverter_ab').update(output_curAB);
        $('quickconverter_ba').update(output_curBA);
        $('quickconverter_median').update(output_median);
        $('quickconverter_showInfo').style.display = 'block';

        if (qc_result.min_bid != "na" && qc_result.min_ask != "na" && qc_result.max_bid != "na" && qc_result.max_ask != "na") {
            $('quickconverter_min').update(output_min);
            $('quickconverter_max').update(output_max);
            $('quickconverter_show_minmax').style.display = 'block';
        }
        else {
            $('quickconverter_show_minmax').style.display = 'none';
        }

        //$('convert_').value = "New Conversion";
    }
    qc_inOperation = 1;
}
function qc_requestInfo(thisForm, url) {
    var myAjax = new Ajax.Updater('shownull', url, {
        method: 'post',
        parameters: $(thisForm).serialize(),
        onComplete: function(transport) {
            qc_showResponse(transport, url);
        }
    });
}
function qc_ajaxRequest(thisForm, url) {
    if (qc_inOperation == 1) {
        qc_inOperation = 0;
        setTimeout(
        function() {
            qc_requestInfo(thisForm, url);
        },
        500);
    }

}
function qc_validate_form(f) {

    $('quickconverter_showError').style.display = "none";
    $('timezone_offset').value = (new Date()).getTimezoneOffset() / 60;

    input_amount = (($('inputcurrency_').value).strip()).replace(/^0+/g, '');
    input_amount = (input_amount === '') ? '1' : input_amount;
    $('inputcurrency_').value = input_amount;

    first_currency = $('firstCurr').value;
    second_currency = $('secondCurr').value;

    if (first_currency == "-" || second_currency == "-" || first_currency == "empty" || second_currency == "empty") {
        $('quickconverter_showInfo').style.display = "none";
        $('quickconverter_show_minmax').style.display = 'none';
        return false;
    }

    if (input_amount === "" || input_amount.search(/[^0-9\.]/) != -1 || input_amount.search(/(\d*\.\d*){2,}/) != -1) {
        $('quickconverter_showInfo').style.display = "none";
        $('quickconverter_show_minmax').style.display = 'none';
        $('quickconverter_showError').style.display = "block";
        return false;
    }

    return true;
}
function qc_buttonSubmit(thisForm, url) {
    if (qc_validate_form(thisForm)) {
        qc_ajaxRequest(thisForm, url);
    }
    return false;
}
function qc_keyboardSubmit(thisForm, url, e) {
    var kc = 0;
    if (window.event) {
        kc = window.event.keyCode;
    } else if (e) {
        kc = e.which;
    }

    if (kc == 13) {
        if (qc_validate_form(thisForm)) {
            qc_ajaxRequest(thisForm, url);
        }
        return false;
    }
    return true;
}
function get_currencylist(id, selectlist_prefix) {

    var h = $H(dollarName);

    var current_cap = '';
    var selectlist = '';

    selectlist_prefix.each(function(c) {
        if (selectlist === '') {
            selectlist += '<option value="' + c + '" selected="selected">' + h.get(c) + ' | ' + c + '</option>';
        } else {
            selectlist += '<option value="' + c + '">' + h.get(c) + ' | ' + c + '</option>';
        }
    });

    h.each(function(pair) {
        var cap = (pair.value).substring(0, 1);

        if (current_cap != cap) {
            current_cap = cap;
            selectlist += '<option value="empty"></option><option value="-">- ' + cap + ' -</option>';
        }

        selectlist += '<option value="' + pair.key + '">' + pair.value + ' | ' + pair.key + '</option>';
    });

    $(id).insert(selectlist);
}
