$(document).ready(function() {

	$("#confirm_prepaid_btn").click(function() {
		confirm_prepaid_payment();
	});

	if($("#show_prepaid_box").val()=="prepaid")
	{
		$("#pay_prepaid_btn").trigger('click');
	}
});

function checkout_creditcard(obj)
{
	if($("#pay_prepaid_box").length>0) $("#pay_prepaid_box").html('').hide();
	if($("#pay_ppcard_box").length>0) $("#pay_ppcard_box").html('').hide();
	if($("#topup_creditcard_box").length>0) $("#topup_creditcard_box").html('').hide();

	if($("#member_logged_in").val()=="yes")
	{
        	$.ajax({
                	type: "POST",
	                url: "/buy/checkout/",
        	        data: {
				cart_currency: $("#cart_currency").val(),
				cart_amount: $("#cart_amount").val(),
	                        request_type: "save_transaction"
        	        },
                	beforeSend: function(){
				$(obj).siblings(".payment_msg").html('<span class="loading"><img src="/images/common/loading.gif" /> processing payment...</span>');
	                },
        	        success: function(xml){
				$(obj).siblings(".payment_msg").html('');
				$("#pay_creditcard_box").html(xml).slideDown(function() {window.location = "#payment-method";} );
	                }
        	});
	} else
	{
		return false;
	}
}

function checkout_ppcard(obj, type)
{
        if($("#pay_creditcard_box").length>0) $("#pay_creditcard_box").html('').hide();
	if($("#pay_prepaid_box").length>0) $("#pay_prepaid_box").html('').hide();
	if($("#pay_ppcard_box").length>0) $("#pay_ppcard_box").html('').hide();
	if($("#topup_creditcard_box").length>0) $("#topup_creditcard_box").html('').hide();

        if($("#member_logged_in").val()=="yes")
        {
                $.ajax({
                        type: "POST",
                        url: "/buy/checkout/",
                        data: {
				payment_type: type,
                                request_type: "show_ppcard_box"
                        },
                        beforeSend: function(){
                                $(obj).siblings(".payment_msg").html('<span class="loading">'+ajax_loading+'</span>');
                        },
                        success: function(xml){
                                $(obj).siblings(".payment_msg").html('');
                                $("#pay_ppcard_box").html(xml).slideDown(function() {window.location = "#payment-method";} );
                        }
                });
        } else
        {
                return false;
        }
}

function cancel_checkout_ppcard()
{
	$("#pay_ppcard_box").html('').slideUp();
}

function confirm_checkout_ppcard()
{
        if($("#member_logged_in").val()=="yes")
        {
                $.ajax({
                        type: "POST",
                        url: "/buy/checkout/",
                        data: {
				payment_type: $("#payment_type").val(),
				ppcard_code: $("#ppcard_code").val(),
                                cart_currency: $("#cart_currency").val(),
                                cart_amount: $("#cart_amount").val(),
				captcha_code: $("#captcha_code").val(),
                                request_type: "checkout_ppcard"
                        },
                        beforeSend: function(){
                                $("#pay_ppcard_msg").html('<span class="loading">'+ajax_loading+'</span>');
                        },
                        success: function(xml){
                                $("#pay_ppcard_box").html(xml);
                        },
                        error: function() {
                                $("#pay_ppcard_msg").html('<span class="error_message">Sorry. We have some problem to process your request. Please try again.</a>');
                        }
                });
        } else
        {
                return false;
        }
}

function checkout_prepaid(obj)
{
	if($("#pay_creditcard_box").length>0) $("#pay_creditcard_box").html('').hide();
	if($("#pay_ppcard_box").length>0) $("#pay_ppcard_box").html('').hide();
	if($("#topup_creditcard_box").length>0) $("#topup_creditcard_box").html('').hide();

	if($("#member_logged_in").val()=="yes")
	{
                $.ajax({
                        type: "POST",
                        url: "/buy/checkout/",
                        data: {
                                cart_currency: $("#cart_currency").val(),
                                cart_amount: $("#cart_amount").val(),
                                request_type: "show_prepaid_credit_box"
                        },
                        beforeSend: function(){
                                $(obj).siblings(".payment_msg").html('<span class="loading">'+ajax_loading+'</span>');
                        },
                        success: function(xml){
				$(obj).siblings(".payment_msg").html('');
				$("#pay_prepaid_box").html(xml).slideDown(function() {window.location = "#payment-method";} );
                        }
                });
	} else
	{
		return false;
	}
}

function cancel_checkout_prepaid()
{
	$("#pay_prepaid_box").slideUp();
	return false;
}

function confirm_prepaid_payment()
{
        if($("#member_logged_in").val()=="yes")
        {
                $.ajax({
                        type: "POST",
                        url: "/buy/payment/",
                        data: {
                                cart_currency: $("#cart_currency").val(),
                                cart_amount: $("#cart_amount").val(),
				checkout_password: $("#checkout_password").val(),
                                request_type: "checkout_prepaid"
                        },
                        beforeSend: function(){
                                $("#pay_prepaid_msg").html('<span class="loading"><img src="/images/common/loading.gif" /> processing payment...</span>');
                        },
                        success: function(xml){
				$("#checkout_box").html(xml);
				reload_hd_cart_count();
				window.location = "#page-top";
                        },
			error: function() {
				$("#pay_prepaid_msg").html('<span class="error_message">Please make sure you enter the correct password and try again.</span>');
			}
                });
        } else
        {
                return false;
        }
}

function topup_credit_card(obj)
{
        if($("#pay_creditcard_box").length>0) $("#pay_creditcard_box").html('').hide();
        if($("#pay_prepaid_box").length>0) $("#pay_prepaid_box").html('').hide();
        if($("#pay_ppcard_box").length>0) $("#pay_ppcard_box").html('').hide();

        if($("#member_logged_in").val()=="yes")
        {
                $.ajax({
                        type: "POST",
                        url: "/buy/checkout/",
                        data: {
                                cart_currency: $("#cart_currency").val(),
                                cart_amount: $("#cart_amount").val(),
                                request_type: "show_topup_creditcard_box"
                        },
                        beforeSend: function(){
                                $(obj).siblings(".payment_msg").html('<span class="loading">'+ajax_loading+'</span>');
                        },
                        success: function(xml){
                                $(obj).siblings(".payment_msg").html('');
                                $("#topup_creditcard_box").html(xml).slideDown();
                        }
                });
        } else
        {
                return false;
        }

}

function cancel_topup_credit_card()
{
        $("#topup_creditcard_box").slideUp();
        return false;
}

function confirm_topup_credit_card(obj)
{
	var amount = $("#topup_amount").val().replace(/^\s+|\s+$/g,"");
	if(isNaN(amount) || amount.length<1)
        {
		$(obj).siblings(".payment_msg").html('<span class="error_message">Invalid amount.</span>');
	} else
	{
                $.ajax({
                        type: "POST",
                        url: "/buy/checkout/",
                        data: {
                                cart_currency: $("#cart_currency").val(),
                                cart_amount: $("#topup_amount").val(),
				transaction_type: "prepaid_credit",
                                request_type: "save_transaction"
                        },
                        beforeSend: function(){
                                $(obj).siblings(".payment_msg").html('<span class="loading"><img src="/images/common/loading.gif" /> processing payment...</span>');
                        },
                        success: function(xml){
                                $(obj).siblings(".payment_msg").html('');
                                $("#pay_creditcard_box").html(xml).slideDown();
                        }
                });
        }
}

function redeem_ppcard()
{
	if($("#member_logged_in").val()=="yes")
	{
		$.ajax({
			type: "POST",
			url: "/buy/checkout/",
			data: {
				payment_type: $("#payment_type").val(),
				ppcard_code: $("#ppcard_code").val(),
				captcha_code: $("#captcha_code").val(),
				request_type: "redeem_ppcard"
			},
			beforeSend: function(){
				$("#pay_ppcard_msg").html('<span class="loading">'+ajax_loading+'</span>');
			},
			success: function(xml){
				$("#pay_ppcard_box").html(xml);
			},
			error: function() {
				$("#pay_ppcard_msg").html('<span class="error_message">Sorry. We have some problem to process your request. Please try again.</a>');
			}
		});
	} else
	{
		return false;
	}
}

function wrap_gift(id)
{
        $.ajax({
                type: "POST",
                url: "/common/dialog/add-gift-item/",
                data: {
                        cart_id: id,
                        request_type: "show_wrap_gift_dialog"
                },
                beforeSend: function(){
                        show_dialog_loading();
                },
                success: function(xml){
                        if($("#dim_overlay").length<1)
                        {
                                $(document.body).append("<div id='dim_overlay'></div>");
                                $("#dim_overlay").show();
                        }
                        if($("#dialog_box").length<1)
                        {
                                $(document.body).append("<div id='dialog_box' style='display:none;'></div>");
                        }
			$("#dialog_box").css("margin-left", "-200px");
                        $("#dialog_box").html(xml).show();
                }
        });
}
