﻿$('input[type="text"]', $('#basket')).each(function() {
    $(this).attr("disabled", "disabled");
});

$('.price', $('#basket')).each(function() {
    $(this).data('price', parseFloat($(this).text()) / parseInt($('input[type="text"]', $(this).parent()).val()));
});

change_currency();

$('input[type="button"]', $('#basket')).each(function() {
    $(this).removeAttr("disabled");
});

function up(b, p) {
    var txt = $('input[type="text"]', $(b).parent().parent());
    if (parseInt(txt.val()) >= 10) {
        return;
    }

    txt.val(parseInt(txt.val()) + 1);
    var td_price = $('.price', txt.parent().parent().parent());
    td_price.text(get_currency_char() + Math.round(td_price.data('price') * parseInt(txt.val()) * $('#d_currency').data('rate') * 100) / 100);

    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "/services/checkout.asmx/up",
        data: "{p:" + p + "}",
        dataType: "json",
        success: function(result) {
            if (result.d.error) {
                show_error(result.d.error);
            }
        },
        error: function(b, a, c) { show_error("Error Occured!") }
    });
}

function get_currency_char() {
    switch ($('#d_currency').val()) {
        case "USD":
            {
                return "$";
                break;
            }
        case "EUR":
            {
                return "€";
                break;
            }
        case "GBP":
            {
                return "£";
                break;
            }
    }
}

function change_currency() {
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "/services/quickshoppingservice.asmx/getrate",
        data: "{c:'" + $('#d_currency').val() + "'}",
        dataType: "json",
        success: function(result) {
            if (result.d.error && result.d.error != '') {
                show_error(result.d.error);
                return;
            }

            $('#d_currency').data('rate', result.d);

            $('.price', $('#basket')).each(function() {
                $(this).text(get_currency_char() + Math.round($(this).data('price') * parseInt($('input[type="text"]', $(this).parent()).val()) * result.d * 100) / 100);
            });
        },
        error: function(b, a, c) { show_error("Error Occured!") }
    });
}

$('#d_currency').bind("change", change_currency);

function down(b, p) {
    var txt = $('input[type="text"]', $(b).parent().parent());
    txt.val(parseInt(txt.val()) - 1);
    var td_price = $('.price', txt.parent().parent().parent());
    td_price.text(get_currency_char() + Math.round(td_price.data('price') * parseInt(txt.val()) * $('#d_currency').data('rate') * 100) / 100);

    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "/services/checkout.asmx/down",
        data: "{p:" + p + "}",
        dataType: "json",
        success: function(result) {
            if (result.d.error) {
                show_error(result.d.error);
            }

            if (parseInt(txt.val()) <= 0) {
                $('tr[id=' + p + ']', '#basket').remove();
            }

            if ($('tr', '#basket').length <= 1) {
                window.location = "/no-item-in-cart.htm";
            }
        },
        error: function(b, a, c) { show_error("Error Occured!") }
    });
}

function validate() {
    if (/^\s*$/.test($('#t_character_name').val()) == true) {
        show_error("Please input character name.");
        return false;
    }

    if (/^\s*$/.test($('#t_fullname').val()) == true) {
        show_error("Please input fullname.");
        return false;
    }

    if (/^\s*$/.test($('#t_email').val()) == true) {
        show_error("Please input email.");
        return false;
    }

    if (/^\s*([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+\s*$/.test($('#t_email').val()) == false) {
        show_error("Please input correct email.");
        return false;
    }

    var pm = '';
    $('input[name=payment]').each(
        function() {
            if (this.checked == true) {
                pm = this.value
            }
        }
    );

    if (!pm) {
        show_error("Please select a payment."); return;
    }
}

function show_error(msg) {
    $("#q_error").html(msg);
    $("#q_container").hide();
    $("#q_container").fadeIn("slow")
}
