$(document).ready(function(){
    //根据用户名跟订单编号查找商品
    $('#btnSearch').click(function(){
        if($('#txtMemberName').val()=='')
        {
            alert('用户名不能为空，请输入用户名');
            $('#txtMemberName').focus();
            return false;
        }
        if($('#txtOrderNo').val()=='')
        {
            alert('订单编号不能为空，请输入订单编号');
            $('#txtOrderNo').focus();
            return false;
        }
        //查找商品时把用户名传给隐藏域
        $('#membername').val($('#txtMemberName').val());
        $.ajax({
            url:'AppFormAdd.aspx?NocopyRight=1&type=ajax&Action=GetProducts&stype='+$('[@name=rblRepaireType][@checked]').val() +'&OrderNo=' + escape($.trim($('#txtOrderNo').val()))+'&membername=' + escape($.trim($('#membername').val())),
            async:false,
            cache:false,
            dataType:'json',
            success:function(msg){
                if(msg.OrderExist == 'true'){
                    var Products = msg.Products;
                    if(Products.length>0){
                        $('#tmp').nextAll().remove();
                        $.each(Products,function(i,n){
                            var row = $('#tmp').clone().show();
                            row.find('.orderpdt_pdtname').html(n.ProductName + (n.ProductSpecs != '' ? '<br>' + n.ProductSpecs : ''));
                            row.find('.orderpdt_pdtprice').html(n.SubPrice);
                            row.find('.orderpdt_pdtpmt').html(n.LessMoney);
                            row.find('.orderpdt_pdtbacknum').html("<img src='/" + shopFolder + "Template/" + templateFolder + "/images/cart_reduce.gif' alt='减少' title='减少数量' class=\"hand\" id=\"reduce_" + n.GoodsID + "\" /><input type=\"hidden\" name=\"num\" value=\""+n.Quantity+"\"/> <input type=\"text\" id=\"quantity_"+n.GoodsID+"\" name=\"quantity\" value=\""+n.Quantity+"\"style=\"width:30px\"/><img src='/" + shopFolder  + "Template/" + templateFolder + "/images/cart_add.gif' alt='增加' title='增加数量' class=\"hand\" id=\"add_" + n.GoodsID + "\" />");
                            row.find('.orderpdt_pdtbacknum').append('<input type="hidden" id="unitprice_'+n.GoodsID+'" name="unitprice" value="'+n.UnitPrice+'" /><input type="hidden" id="unitless_'+n.GoodsID+'" name="unitless" value="'+n.UnitLess+'" />');
                            var checkbox = $('<input type="CheckBox" id="gid_'+n.GoodsID+'"name="gid" value="' + n.GoodsID + '" />');
                            row.find('.orderpdt_check').append(checkbox);
                            $('#OrderProduct').append(row);                       
                            
                            if($('#txtReceiverAddress').val()=='')
                            $('#txtReceiverAddress').val(n.ReceiverAddress);
                            if($('#txtReceiverName').val()=='')
                            $('#txtReceiverName').val(n.ReceiverName);
                            if($('#txtMobile').val()=='')
                            $('#txtMobile').val(n.ReceiverMobile);
                            if($('#memberid').val()==''){
                            $('#memberid').val(n.MemberID);
                            }
                       });
                        $('#span_result').html('');
                        $('#OrderProduct').show();
                        $('#orderNo').val($('#txtOrderNo').val());
                    }
//                    else{
//                        $('#span_result').html('<font style="color:#ff0000;">此订单还不能退换货/返修</font>');
//                        $('#OrderProduct').hide();
//                        $("#OrderProduct  input[@name='gid']:checked").each(function(){
//                            $(this).attr("checked", false);
//                        });
//                    }
                }else{
                    $('#span_result').html(msg.ErrorMsg);
                    $('#OrderProduct').hide();
                    $("#OrderProduct  input[@name='gid']:checked").each(function(){
                        $(this).attr("checked", false);
                    });
                };
            },
            error:function(){alert('error');}
        });
    });
    
    $('input[@id^=gid_]').livequery("click",function(){
    var recID = $(this).attr('id').replace('gid_','');
    var obj=$('#quantity_'+recID);
    if($(this).attr('checked')){
    obj.attr("name","pnum");
    }else{
    obj.attr("name","quantity");
   }
   });
   
   $('[@name=rblRepaireType]').bind("click",function(){
       $('#btnSearch').click();
   })
    		        
    $('[@name=rblRepaireType][@value=1]').click(function(){
        $('.refundType').show();
        if($('[@name=rblRefundType][@value=2]').is(":checked")){
            $('.bankInfo').show();
        }
    })
    $('[@name=rblRepaireType][@value!=1]').click(function(){
        $('.refundType').hide();
        if($('[@name=rblRefundType]').is(":checked")){
           $('[@name=rblRefundType]').removeAttr("checked");
        }
        HideBankInfo();
    })
    $('[@name=rblRefundType][@value=2]').click(function(){
        $('.bankInfo').show();
    })
    $('[@name=rblRefundType][@value!=2]').click(function(){
        HideBankInfo();
    }) 
    
    $('input[@id^=quantity]').livequery('keydown',function(event){
    var curobj = $(this);
    curobj.attr('oldvalue',curobj.val());
    if((event.keyCode >= 48 && event.keyCode <= 57) || (event.keyCode >= 96 && event.keyCode <= 105) || event.keyCode == 37 || event.keyCode == 39 || event.keyCode == 46 || event.keyCode == 8){
    }else{return false;}
     }).livequery('keyup',function(event){
    var curobj = $(this);
    var curnum=Number(curobj.val());
    var maxNum=Number($(this).siblings('[@name=num]').val());
    if(curobj.attr('oldvalue') == curobj.val() || curobj.val() == ''){return ;};
    if(curnum<1){
        alert('退换货/返修的数量必须大于0');
        curobj.val(1);
    }
    else if(curnum>maxNum){
        alert('退换货/返修的数量不能大于购买数量');
        curobj.val(maxNum);
    }
    else{
       var curNum= $(this).val()
       unitprice= $(this).siblings('[@name=unitprice]').val();
       lessprice= $(this).siblings('[@name=unitless]').val();
       $(this).parent().parent().find('.orderpdt_pdtprice').empty().html((unitprice*curNum).toFixed(2));
       $(this).parent().parent().find('.orderpdt_pdtpmt').empty().html((lessprice*curNum).toFixed(2));                
    }
    });	     
    
    //减少数量
    $('[@id^=reduce_]').livequery('click',function(){
    var recID = $(this).attr('id').replace('reduce_','');
    var obj = $('#quantity_' + recID);
    var curNum= obj.val()-1;
    if(curNum>=1){
       obj.val(curNum);
       obj.keyup();
    }
    else{
        alert('退换货/返修的数量必须大于0');
    }
    });
    //增加数量
    $('[@id^=add_]').livequery('click',function(){
    var recID = $(this).attr('id').replace('add_','');
    var obj = $('#quantity_' + recID);
    var curNum=Number(obj.val())+1;
    var maxNum= $(this).siblings('[@name=num]').val();
    if(maxNum>=curNum){
      obj.val(curNum);
      obj.keyup();
    }
    else{
        alert('退换货/返修的数量不能大于购买数量');
    }
    });                          
    
    $('#btnSubmit').click(function(){
        if($('[@name=gid][@checked]').length == 0){
            alert('请选择要返修的商品');
            return false;
        };
    });
    HideBankInfo();

    $("#Form1").validate({
        event: "keyup",
        rules: {
            txtReason: {
                required: true,
                maxlength:200
            },
            txtReceiverAddress: {
                required:true,
                maxlength:255
            },
            
            txtReceiverName: {
                required:true,
                maxlength:50
            },
            txtMobile: {
                required:true,
                maxlength:20
            },
            txtBankName: {
                required:function(){
                    if($('#txtBankName').parent().parent().is(":hidden"))
                    return false
                    else
                    return true
                 },
                maxlength:50
            },
            txtBankAccount: {
                required:function(){
                    if($('#txtBankAccount').parent().parent().is(":hidden"))
                    return false
                    else
                    return true
                 },
                maxlength:50
             
            },
            txtBankAccount2: {
                required:function(){
                    if($('#txtBankAccount2').parent().parent().is(":hidden"))
                    return false
                    else
                    return true
                 },
                maxlength:50,
                equalTo:"#txtBankAccount"
            },
            txtAccountName: {
                required:function(){
                    if($('#txtAccountName').parent().parent().is(":hidden"))
                    return false
                    else
                    return true
                 },
                maxlength:50
            } 		            
        },		
        messages: {
            txtReason: {
                required: "退换货/返修理由必填",
                maxlength: "最大长度只能在(200个字符)以内"
            },
            txtReceiverAddress: {
                required: "收货地址必填",
                maxlength: "最大长度只能在(255个字符)以内"
            },
            txtReceiverName: {
                required:  "收货人必填",
                maxlength: "最大长度只能在(50个字符)以内"
            },
            txtMobile: {
                required:  "收货电话必填",
                maxlength: "最大长度只能在(20个字符)以内"
            },
            txtBankName: {
                required: "退款银行必填",
                maxlength: "最大长度只能在(50个字符)以内"
            },
            txtBankAccount: {
                required: "退款银行账户必填",
                maxlength: "最大长度只能在(50个字符)以内"
            },
            txtBankAccount2: {
                required: "再次输入银行账户",
                maxlength: "最大长度只能在(50个字符)以内",
                equalTo:"两次输入的账号名不同"
            },
            txtAccountName: {
                required: "退款银行账户名必填",
                maxlength: "最大长度只能在(50个字符)以内"
            }		            },
        debug:"false",
        submitHandler:function(form){
            form.submit();
        }
    });             
});
function HideBankInfo(){
    $('.bankInfo').hide();
    $('#txtBankName').val('');
    $('#txtBankAccount').val('');
    $('#txtAccountName').val('');
}