

$(document).ready(function(){
    //loading
    $('#loadingContainer').ajaxStart(function(){$(this).show();}).ajaxStop(function(){$(this).hide();});	
    //收款单开始
    $('#btnShowReceivePay').click(function(){
        $('#ShowReceivePay').popup({width:'60%',height:'80%',popupHead:'ReceivePayHead',btnClose:'ReceivePayClose'});
        BindPayment($('#ddlPayment'),$('input[@name=txtPaymentName]'));     //支付方式
        BindBank($('#ddlBrankInfo'),$('input[@name=txtReceiveBank]'),$('input[@name=txtReceiveAccount]'));      //银行帐号
        $.ajax({
            url:'Receipt.aspx?NoCopyRight=1&Action=GetLeftReceivTotal&OrderNo='+escape($('#PayOrderNo').val()),
            async:false,
            cache:false,
            success:function(data){if(!checkLoginStatus(data)){return false;};$('#txtReceivePrice').val(data);$('#span_leftreceive').html(data);},
            error:function(){alert('error');}
        });
        $('#txtReceivePayRemark').unbind().keydown(function(){showLen($(this),$('#showLen'),250);}).keyup(function(){showLen($(this),$('#showLen'),250);});
        $('input[@name=membername]').val($('#txtMemberName').val());
        $('#frmAddReceivePay').unbind().submit(function(){
            if($('[@name=paytmethod][@checked]',this).length==0){alert('请选择支付类型');return false;};
            if(!showLen($('#txtReceivePayRemark'),$('#showLen'),250)){alert('备注字数太长');return false;};
            $.ajax({
                url:'Receipt.aspx?NoCopyRight=1&Action=AddReceivePay',
                type:'post',
                data:$('#frmAddReceivePay').serialize(),
                cache:false,
                success:function(data){
                    if(!checkLoginStatus(data)){return false;};
                    if(data>0){
                        alert('操作成功');
                    }else if(data == 'LEFTLESS'){
                        alert('操作失败!输入的收款金额大于未收款总额');
                    }else{
						alert('操作失败!');
                    };
                },
                error:function(){alert('error');}
            });
            return false;
        });
        
    });//end click
    //收款单结束
    
    //退款单开始
    $('#btnShowRefund').click(function(){
        $('#ShowRefund').popup({width:'60%',height:'80%',popupHead:'RefundHead',btnClose:'RefundClose'});
        BindPayment($('#ddlRefundPayment'),$('input[@name=txtRefundPaymentName]'));     //支付方式
        BindBank($('#ddlRefundBank'),$('input[@name=txtRefundBank]'),$('input[@name=txtRefundAccount]'));      //银行帐号
        $.ajax({
            url:'Receipt.aspx?NoCopyRight=1&Action=GetReceivedTotal&OrderNo='+escape($('#RefundOrderNo').val()),
            async:false,
            cache:false,
            success:function(data){if(!checkLoginStatus(data)){return false;};$('#txtRefundPrice').val(data);$('#span_leftrefund').html(data);},
            error:function(){alert('error');}
        });
        $('#txtRefunRemark').unbind().keydown(function(){showLen($(this),$('#refund_showLen'),250);}).keyup(function(){showLen($(this),$('#refund_showLen'),250);});
        $('input[@name=membername]').val($('#txtMemberName').val());
        $('#frmRefund').unbind().submit(function(){
            if($('[@name=paytmethod][@checked]',this).length==0){alert('请选择退款类型');return false;};
            if(!showLen($('#txtRefunRemark'),$('#refund_showLen'),250)){alert('备注字数太长');return false;};
            $.ajax({
                url:'Receipt.aspx?NoCopyRight=1&Action=AddRefund',
                type:'post',
                data:$('#frmRefund').serialize(),
                cache:false,
                success:function(data){
                    if(!checkLoginStatus(data)){return false;};
                    if(data>0){
                        alert('操作成功');
                    }else if(data == 'LEFTLESS'){
                        alert('操作失败!输入的退款金额大于已收款总额');
                    };
                },
                error:function(){alert('error');}
            });
            return false;
        });
    });
    //退款单结束
    
    //发货
    $('#btnShowShippingGoods').click(function(){
        $('#IsReturn').val('0');
        $('#span_shiptitle').html('发货单');
        $('#span_name').html('收货人');
        $('#span_remark').html('发货备注');
        $('#btnAddShipping').val('发货');
        AddShippingInvoice();
    });
    //退货
    $('#btnShowReturnGoods').click(function(){
        $('#IsReturn').val('1');
        $('#span_shiptitle').html('退货单');
        $('#span_name').html('退货人');
        $('#span_remark').html('退货备注');
        $('#btnAddShipping').val('退货');
        AddShippingInvoice();
    });
       

});//end ready

function AddShippingInvoice(){
    $('#ShowShipping').popup({width:'60%',height:'80%',popupHead:'ShippingHead',btnClose:'ShippingClose'});
    $('input[@name=membername]').val($('#txtMemberName').val());
    var objShipping = $('#ddlShippingName');
    objShipping.append($("#ddlShipping").html());
//    if($('option',objShipping).length <= 1){
//        $.ajax({
//            url:'Receipt.aspx?NoCopyRight=1&Action=GetShippings',
//            cache:false,
//            success:function(data){if(!checkLoginStatus(data)){return false;};objShipping.append(data);},
//            error:function(){alert('error');}
//        });
//    };
//    objShipping.unbind().change(function(){$('#txtShippingName').val($(this).val());});
    var objShipCompany = $('#ddlShippingCompany');
    if($('option',objShipCompany).length <= 1){
        $.ajax({
            url:'Receipt.aspx?NoCopyRight=1&Action=GetShipCompany',
            cache:false,
            success:function(data){
                if(!checkLoginStatus(data)){return false;};
                objShipCompany.append(data);
            },
            error:function(){alert('error');}
        });
    };
    objShipCompany.unbind().change(function(){$('#txtCompanyName').val($(this).val());});
    $('#txtShippingRemark').unbind().keydown(function(){showLen($(this),$('#shipping_showLen'),250);}).keyup(function(){showLen($(this),$('#shipping_showLen'),250);});
    
    $('#AddShipping').unbind().submit(function(){
        if($("#txtShippingNo").val()==""){alert("物流单号必须填写");return false;};
        if($("#txtCompanyName").val()==""){alert("物流公司必须填写");return false;};
        if(!showLen($('#txtShippingRemark'),$('#shipping_showLen'),250)){alert('备注字数太长');return false;};
        $.ajax({
            url:'Receipt.aspx?NoCopyRight=1&Action=AddShippingInvoice',
            type:'post',
            data:$('#AddShipping').serialize(),
            cache:false,
            success:function(data){
                if(!checkLoginStatus(data)){return false;};
                if(data=='SHIPPED'){alert('单据已经填写过');}else if(data > 0){alert('操作成功');$("#ShippingClose").click();};
            },
            error:function(){alert('error');}
        });
        return false;
    });
};

function BindPayment(objPayment,objTxt){
    if($('option',objPayment).length <= 1){
        $.ajax({
            url:'Receipt.aspx?NoCopyRight=1&Action=GetPayment',
            cache:false,
            success:function(data){if(!checkLoginStatus(data)){return false;};objPayment.append(data);},
            error:function(){alert('error');}
        });
    };
    objPayment.unbind().change(function(){objTxt.val(objPayment.val());});
};

function BindBank(objBankAccount,objTxtBank,objTxtAccount){
    if($('option',objBankAccount).length <= 1){
        $.ajax({
            url:'Receipt.aspx?NoCopyRight=1&Action=GetExistedBank',
            cache:false,
            success:function(data){if(!checkLoginStatus(data)){return false;};objBankAccount.append(data);},
            error:function(){alert('error');}
        });
    };
    objBankAccount.unbind().change(function(){
        objTxtBank.val(objBankAccount.val().split(']-[')[0]);
        objTxtAccount.val(objBankAccount.val().split(']-[')[1]);
    });
};

function showLen(objtxt,objshow,maxLen){
    objshow.html(objtxt.val().length);
    if(objtxt.val().length>=maxLen){
        return false;
    };
    return true;
};

function checkLoginStatus(data){
    var result = true;
    if(data.indexOf('<!DOCTYPE') != -1){
        result = false;
        alert('身份已过期,请重新登陆!');
    };
    return result;
};

