$(function(){
    $("#add_address").click(function(){
        add_address();
    });
    $("#account_money").bind("blur",function(){
        $("#all_use_money").prop("checked","checked");
        count_order_total();
    });
    $("#all_use_money").click(function(){
        if($("#all_use_money").prop("checked")==false){
            $("#all_use_money").prop("checked",'')
            $("#account_money").val(0);
        }
        count_order_total();
    });
    $("#all_use_score").click(function(){
        count_order_total();
    });
    $("input[name='paytype']").bind("click",function(){
        count_order_total();
    });
    //切换支付方式
    $(".pay_styles dl dt ").each(function(k){
        $(this).bind('click',function(){
            if($(this).find("input[name='paytype']").is(':checked')){
                payment_id =$(this).find("input[name='paytype']").val();
            }else{
                payment_id=0;
            }
            count_order_total($(this));
        });
    });
    count_order_total();
});
//计算订单费用
function count_order_total(){
    var type=parseInt($("#is_addcart").val());
    var query=new Object();
    query.ids=order_id;
    if($("#all_use_money").is(':checked')){
        query.use_account=1;
    }
    query.type=type;
    query.account_money=$("input[name='account_money']").val();
    if(type==3){
        query.score=1;
    }else {
        if ($("#all_use_score").is(':checked')) {
            query.score = 1;
        }
    }
    query.payment_id=payment_id;
    //alert($("input[name='paytype']:checked").val());
    $.ajax({
        url:APP+'Mobile/Order/count_order_total',
        data:query,
        dataType:'json',
        success:function(obj){
            $("#count_total").html(obj.html);
            $("input[name='account_money']").val(obj.total_info.account_money);
            if(obj.total_info.payment_info){
                $("#pay_style").html(obj.total_info.payment_info.name);
            }else{
                $("#pay_style").html("请选择支付方式");
            }
            if(obj.total_info.pay_price==0){
                payment_id=0;
            }
        }
    });
}
//提交订单
function submit_continue_order(){
    var type=parseInt($("#is_addcart").val());
    var query=new Object();
    query.ids=order_id;
    if($("#all_use_money").is(':checked')){
        query.use_account=1;
    }
    query.type=type;
    query.account_money=$("input[name='account_money']").val();
    if(type==3){
        query.score=1;
    }else {
        if ($("#all_use_score").is(':checked')) {
            query.score = 1;
        }
    }
    query.payment_id=payment_id;
    //alert($("input[name='paytype']:checked").val());
    /*if (query.account_money || query.score) {
     $("#sub_order").next().next().fadeIn();
     *//*$("#areaMasks").fadeIn();*//*
     $("#sub_order").next().animate({"bottom": 0});
     var pay_pwd=$("#pay_pwd").val();
     if(!pay_pwd)return false;

     }*/
    query.pay_pwd=$("#pay_pwd").val();
    $.ajax({
        url: APP + "Mobile/Order/sub_continue_order",
        type: "post",
        data: query,
        dataType: "json",
        success:function(obj){
            if(obj.status==1){
                if(obj.pay){
                    window.location.href=APP+"Mobile/Order/order_success/id/"+obj.pay;
                }else{
                    window.location.href=APP+"Mobile/Order/order_success/order_ids/"+obj.data;
                }
            }else if(obj.status==3){
                if(obj.app_paydata=='app_notpay'){
                    $("#sub_order").next().next().fadeIn();
                    $("#sub_order").next().animate({"bottom": 0});
                    if(query.use_account==1 ||  query.score==1){
                        $('#order_pay').attr('style','display:block');
                        $('#paypassword').attr('style','display:block');
                        $('#payname').html('选择支付方式并输入本站支付密码');
                    }else{
                        $('#order_pay').attr('style','display:block');
                        $('#paypassword').attr('style','display:none');
                        $('#payname').html('请选择支付方式');
                    }
                }else if(obj.msg != '请输入支付密码'){
                    $.showErr(obj.msg,2,'hc');
                }else{
                    $("#sub_order").next().next().fadeIn();
                    $("#sub_order").next().animate({"bottom": 0});
                    $('#order_pay').attr('style','display:none');
                    $('#paypassword').attr('style','display:block;');
                    $('#payname').html('请输入支付密码');
                }
                /*$("#sub_order").next().next().fadeIn();
                 $("#areaMasks").fadeIn();*/
                /*$("#sub_order").next().animate({"bottom": 0});
                 var pay_pwd=$("#pay_pwd").val();
                 if(!pay_pwd)return false;
                 query.pay_pwd=$("#pay_pwd").val();
                 $.ajax({
                 url: APP + "Mobile/Order/sub_continue_order",
                 type: "post",
                 data: query,
                 dataType: "json",
                 success:function(obj){
                 if(obj.status==1){
                 if(obj.pay){
                 window.location.href=APP+"Mobile/Order/order_success/id/"+obj.pay;
                 }else{
                 window.location.href=APP+"Mobile/Order/order_success/order_ids/"+obj.data;
                 }
                 }else{
                 $.showErr(obj.msg,0,'hc');
                 }
                 }
                 });*/
            }else{
                if(obj.app_paydata=='app_notpay'){
                    $("#sub_order").next().next().fadeIn();
                    $("#sub_order").next().animate({"bottom": 0});
                    if(query.use_account==1 ||  query.score==1){
                        $('#order_pay').attr('style','display:block');
                        $('#paypassword').attr('style','display:block');
                        $('#payname').html('选择支付方式并输入本站支付密码');
                    }else{
                        $('#order_pay').attr('style','display:block');
                        $('#paypassword').attr('style','display:none');
                        $('#payname').html('请选择支付方式');
                    }
                }else{
                    showtoast(obj.msg);
                    var body = $('body').css("position");
                    if(body=='relative'){
                        $('body').css("position","");
                    }
                }
                // $.showErr(obj.msg,0,'hc');
            }
        }
    });
}
 