<%@ page language="C#" masterpagefile="~/Member/MyShopMasterPage.master" autoeventwireup="true" inherits="Member_JXC_OrderSale, WebApp_deploy" title="销售订单" enableeventvalidation="false" %>

<asp:Content ID="cphContent" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <script src="../../Js/My97DatePicker/config.js" type="text/javascript"></script>
    <script src="../../Js/My97DatePicker/WdatePicker.js" type="text/javascript"></script>
    <script src="../Js/jquery-ui-1.8.2.custom.min.js" type="text/javascript"></script>
    <link href="../Js/jquery-ui-1.8.2.custom.css" rel="stylesheet" type="text/css" />
    <div class="pageTitle" style="line-height: 30px; vertical-align: middle;">
        &nbsp;&nbsp;&nbsp;&nbsp;销售订单</div>
    <div id="TabText">
        <asp:UpdatePanel ID="up1" runat="server">
            <ContentTemplate>
                <table class="ShopOrderStyle">
                    <caption>
                        订单状态信息</caption>
                    <tr>
                        <td class="OrderTitle">
                            订单状态：
                        </td>
                        <td style="width: 300px">
                            <asp:DropDownList ID="DroOrderStatic" runat="server">
                                <%--<asp:ListItem Value="0">等待确认</asp:ListItem>--%>
                                <asp:ListItem Value="1">已经确认</asp:ListItem>
                                <%--<asp:ListItem Value="2">交易完成</asp:ListItem>
                                <asp:ListItem Value="4">交易取消</asp:ListItem>--%>
                            </asp:DropDownList>
                        </td>
                        <td class="OrderTitle">
                            物流状态：
                        </td>
                        <td>
                            <asp:DropDownList ID="DropDeliverStatus" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDeliverStatus_SelectedIndexChanged">
                                <%--<asp:ListItem Value="0">等待发货</asp:ListItem>--%>
                                <asp:ListItem Value="1">已发货</asp:ListItem>
                                <%--<asp:ListItem Value="2">已签收</asp:ListItem>
                                <asp:ListItem Value="4">交易取消</asp:ListItem>--%>
                            </asp:DropDownList>
                        </td>
                    </tr>
                    <tr>
                        <td class="OrderTitle">
                            付款状态：
                        </td>
                        <td>
                            <asp:DropDownList ID="DroPayStatic" runat="server">
                                <%--<asp:ListItem Value="0">等待付款</asp:ListItem>--%>
                                <asp:ListItem Value="1">付款确认</asp:ListItem>
                                <%--<asp:ListItem Value="2">已经付清</asp:ListItem>--%>
                            </asp:DropDownList>
                        </td>
                        <td class="OrderTitle">
                            客户名称：
                        </td>
                        <td>
                            <asp:TextBox ID="txtBuyer" runat="server" onblur="return khNameChange()"></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <%--<td class="OrderTitle">
                            已付金额：
                        </td>
                        <td>
                            <asp:Label ID="lblPayMoney" runat="server"></asp:Label>
                        </td>--%>
                        <td class="OrderTitle">
                            性别：
                        </td>
                        <td>
                            <asp:RadioButtonList ID="rblSex" runat="server" RepeatDirection="Horizontal">
                            </asp:RadioButtonList>
                        </td>
                    </tr>
                </table>
            </ContentTemplate>
            <Triggers>
                <asp:AsyncPostBackTrigger ControlID="DropDeliverStatus" EventName="SelectedIndexChanged" />
            </Triggers>
        </asp:UpdatePanel>
        <table class="ShopOrderStyle">
            <caption>
                收货地址信息
            </caption>
            <tr>
                <td class="OrderTitle">
                    姓名：
                </td>
                <td style="width: 300px">
                    <asp:TextBox ID="Contacter_Name" runat="server" />
                </td>
                <td class="OrderTitle">
                    邮政编码：
                </td>
                <td>
                    <asp:TextBox ID="Contacter_ZipCode" runat="server" />
                </td>
            </tr>
            <tr>
                <td class="OrderTitle">
                    所在地：
                </td>
                <td>
                    <hl:HoWaveProvinceCity ID="HoWaveProvinceCity1" runat="server" EnableViewState="true" />
                </td>
                <td class="OrderTitle">
                    详细地址：
                </td>
                <td>
                    <asp:TextBox ID="Contacter_Address" runat="server" />
                </td>
            </tr>
            <tr>
                <td class="OrderTitle">
                    邮箱：
                </td>
                <td>
                    <asp:TextBox ID="Contacter_Email" runat="server" />
                </td>
                <td class="OrderTitle">
                    联系电话：
                </td>
                <td>
                    <asp:TextBox ID="Contacter_Phone" runat="server" />(电话)<br />
                    <asp:TextBox ID="Contacter_Mobile" runat="server" />(手机)
                </td>
            </tr>
        </table>
        <table class="ShopOrderStyle">
            <tr>
                <td colspan="4">
                    <asp:HiddenField ID="hfProductID" runat="server" />
                    <asp:HiddenField ID="hfProductStr" runat="server" />
                    <%--hfProductStr 商品（ID,数量,价格,单位）--%>
                    <asp:HiddenField ID="hfNeedInvioce" runat="server" />
                    <asp:HiddenField ID="hfDeliverCharge" runat="server" />
                    <div id="divProduct" runat="server">
                    </div>
                </td>
            </tr>
            <tr>
                <td class="OrderTitle">
                    商品总额：
                </td>
                <td style="width: 300px">
                    <asp:Label ID="lblGoodsTotal" runat="server" Text="0"></asp:Label>
                </td>
                <td class="OrderTitle">
                    发票总额：
                </td>
                <td>
                    <asp:Label ID="lblInvoiceTotal" runat="server" Text="0"></asp:Label>
                </td>
            </tr>
            <tr>
                <td class="OrderTitle">
                    运费总额：
                </td>
                <td>
                    <asp:Label ID="lblDeliverTotal" runat="server" Text="0"></asp:Label>
                </td>
                <td class="OrderTitle">
                    订单总额：
                </td>
                <td>
                    <asp:Label ID="lblOrderTotal" runat="server" Text="0"></asp:Label>
                </td>
            </tr>
            <tr>
                <td colspan="4">
                    <span style="margin-left: 160px">&nbsp;</span>
                    <input type="button" id="btnSelectProduct" onclick="SelectProduct()" value="选择商品" />&nbsp;&nbsp;
                    <asp:Button ID="btnSave" runat="server" OnClientClick="return checkPass()" Text="保存"
                        OnClick="btnSave_Click" />
                </td>
            </tr>
        </table>
        <br />
    </div>
    <script type="text/javascript">
        //基本信息
        function checkBuyer() {
            var txtBuyer = $("#<%=txtBuyer.ClientID %>"); //客户名称
            if (txtBuyer.val().length == 0) {
                alert("客户名称不能为空!");
                txtBuyer.focus();
                return false;
            }
            return true;
        }
        //姓名
        function checkContacter_Name() {
            var Contacter_Name = $("#<%=Contacter_Name.ClientID %>");
            if (Contacter_Name.val().length == 0) {
                alert("收货人姓名不能为空!");
                Contacter_Name.focus();
                return false;
            }
            return true;
        }
        //邮政编码
        function checkContacter_ZipCode() {
            var Contacter_ZipCode = $("#<%=Contacter_ZipCode.ClientID %>");
            if (Contacter_ZipCode.val().length != 0) {
                var word1 = /\d{6}/;
                if ((word1.test(Contacter_ZipCode.val())) == false) {
                    alert('邮政编码为数字或格式不对！');
                    Contacter_ZipCode.focus();
                    return false;
                }
            }
            return true;
        }

        function checkProvince() {
            //获取用户控件中的dropdownlist的选择值 
            var hpc_Province = document.getElementById('<%=HoWaveProvinceCity1.ClientID %>' + "_Province");
            var bojvalueProvince = "";
            for (i = 0; i < hpc_Province.options.length; i++) {
                if (hpc_Province.options[i].selected)
                    bojvalueProvince = hpc_Province.options[i].value;
            }
            if (bojvalueProvince.length == 0) {
                alert("请选择省份！");
                hpc_Province.focus();
                return false;
            }


            //获取用户控件中的dropdownlist的选择值
            var hpc_City = document.getElementById('<%=HoWaveProvinceCity1.ClientID %>' + "_City");
            if (hpc_City.options.length > 0) {
                var bojvalueCity = "";
                for (i = 0; i < hpc_City.options.length; i++) {
                    if (hpc_City.options[i].selected)
                        bojvalueCity = hpc_City.options[i].value;
                }
                if (bojvalueCity.length == 0) {
                    alert("请选择城市！");
                    hpc_City.focus();
                    return false;
                }

                //获取用户控件中的dropdownlist的选择值
                var hpc_ddlArea = document.getElementById('<%=HoWaveProvinceCity1.ClientID %>' + "_ddlArea");
                if (hpc_ddlArea.options.length > 0) {
                    var bojvalueArea = "";
                    if (hpc_ddlArea.options.length >= 2) {
                        if (hpc_ddlArea.options[1].value != "") {
                            for (i = 0; i < hpc_ddlArea.options.length; i++) {
                                if (hpc_ddlArea.options[i].selected) {
                                    bojvalueArea = hpc_ddlArea.options[i].value;
                                }
                            }
                            if (bojvalueArea.length == 0) {
                                alert("请选择县/区！");
                                hpc_ddlArea.focus();
                                return false;
                            }
                        }
                    }
                }
            }
            return true;
        }
        //详细地址
        function checkContacter_Address() {
            var Contacter_Address = $("#<%=Contacter_Address.ClientID %>");
            if (Contacter_Address.val().length == 0) {
                alert("详细地址不能为空!");
                Contacter_Address.focus();
                return false;
            }
            return true;
        }
        //Email地址
        function checkContacter_Email() {
            var Contacter_Email = $("#<%=Contacter_Email.ClientID %>");
            if (Contacter_Email.val().length == 0) {
                alert("Email不能为空!");
                Contacter_Email.focus();
                return false;
            }
            var word = /\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/; //Email格式
            if ((word.test(Contacter_Email.val())) == false) {
                alert('Email格式不对！');
                Contacter_Email.focus();
                return false;
            }
            return true;
        }
        //联系电话
        function checkContacter_Phone() {
            var homePhone = $("#<%=Contacter_Phone.ClientID %>"); //电话
            var mobile = $("#<%=Contacter_Mobile.ClientID %>"); //手机

            var word = /^(0[0-9]{2,3}\-)?([1-9][0-9]{6,7})+(\-[0-9]{1,4})?$/; //电话号码格式
            var word1 = /\d{11}/; //手机号码格式/^1\d{10}$/;  

            if (mobile.val().length == 0 && homePhone.val().length == 0) {
                alert("电话号码和手机号码至少填写一个!");
                homePhone.focus();
                return false;
            } else {
                if (mobile.val().length != 0) {
                    if ((word1.test(mobile.val())) == false) {
                        alert('手机号码为数字或格式不对！');
                        mobile.focus();
                        return false;
                    }
                }
                if (homePhone.val().length != 0) {
                    if ((word.test(homePhone.val())) == false) {
                        alert('电话号码为数字或格式不对！');
                        homePhone.focus();
                        return false;
                    }
                }
            }
            return true;
        }
        function checkPass() {
            if (checkBuyer() && checkContacter_Name() && checkContacter_ZipCode() && checkProvince() && checkContacter_Address() && checkContacter_Email() && checkContacter_Phone() && GetAddProducts()) {
            //if (GetAddProducts()) {
                return true;
            } else {
                return false;
            }
        }
        //商品
        function GetAddProducts() {
            var productstr = $("#<%=hfProductStr.ClientID %>"); //商品（ID,数量,价格,单位）
            var productlist = $("#tb_rptProduct tr td table"); //商品信息
            var hfDeliverCharge = $("#<%=hfDeliverCharge.ClientID %>");//运费
            if (productlist.length < 1) {
                alert("没有商品数据！");
                return false;
            }
            var hfNeedInvioce = $("#<%=hfNeedInvioce.ClientID %>"); //是否需要发票(商品ID，是否，内容)
            var invoicestr = "";
            var newvoicestr = "";

            var str = "";
            var newstr = ""; //商品（ID,数量,价格,单位）
            var deliverchargestr = "";
            if (productlist.length > 0) {
                for (var i = 0; i < productlist.length; i++) {
                    var vproductid = $("#productID" + i); //商品ID
                    var vprocount = $("#procount" + i); //数量
                    var vprice = $("#price" + i); //价格
                    var vunit = $("#unit" + i); //单位
                    if (parseInt(vprocount.val()) < 1) {
                        alert('商品数量不能小于0！');
                        vprocount.focus();
                        vprocount.val("1");
                        return;
                    }

                    newstr = vproductid.val() + "," + vprocount.val() + "," + vprice.text() + "," + vunit.val() + "|";
                    str += newstr;

                    var invoicehas = $("#invoicehas" + i); //是否有发票
                    var cbkInvoice = $("#needinvoice" + i); //是否需要发票
                    var InvoiceHead = $("#InvoiceHead" + i); //发票内容
                    if (invoicehas.val() == "true" || invoicehas.val()=="1") {
                        if (cbkInvoice.attr("checked") == "checked") {
                            newvoicestr = vproductid.val() + ",1," + InvoiceHead.val() + "|";
                        } else {
                            newvoicestr = vproductid.val() + ",0," + InvoiceHead.val() + "|";
                        }
                        invoicestr += newvoicestr;
                    }

                    //运费承担
                    var hidDeliverPayType = $("#hidDeliverPayType" + i);
                    if (hidDeliverPayType.val() != "0") {
                        var select_charge = document.getElementById("selDeliverCharge" + i);
                        var bojvalue = "";
                        for (indexed = 0; indexed < select_charge.options.length; indexed++) {
                            if (select_charge.options[indexed].selected)
                                bojvalue = select_charge.options[indexed].value;
                        }
                        if (select_charge.length == 0) {
                            alert("请选择运费！");
                            select_charge.focus();
                        }
                        deliverchargestr = bojvalue;
                    }
                }
                var position = str.lastIndexOf('|');
                if (position > 0) {
                    str = str.substring(0, str.length - 1);
                }
                productstr.val(str); 
                //发票
                var position1 = invoicestr.lastIndexOf('|');
                if (position1 > 0) {
                    invoicestr = invoicestr.substring(0, invoicestr.length - 1);
                }
                hfNeedInvioce.val(invoicestr); 
                hfDeliverCharge.val(deliverchargestr);
            }
            return true;
        }
    </script>
    <script type="text/javascript">
        $(function () {
            //添加：addinfo、修改：editinfo
            var actionInfo = "<%=Action %>";
            var Ownerpower = "<%=Ownerpower %>";

            if (actionInfo == "addinfo") {
                document.getElementById("<%=btnSave.ClientID %>").disabled = true; //保存按钮不可用

            } else if (actionInfo == "editinfo") {
                document.getElementById("btnSelectProduct").disabled = true; //选择商品按钮不可用
            }
        });
    </script>
    <%--商品 begin--%>
    <script type="text/javascript">

        //选择商品
        function SelectProduct() {
            var ctlName = document.getElementById("<%=hfProductID.ClientID %>");
            var arr = showModalDialog('/Member/ProductManage.aspx?ProductType=0&SelectOne=1&DefaultValue=' + ctlName.value, '', 'dialogWidth:800px; dialogHeight:650px; help: no; scroll: yes; status: no');
            if (arr != null) {
                ctlName.value = arr; //商品ID
                document.getElementById("<%=btnSave.ClientID %>").disabled = false; //保存按钮可用
                GetProductByProductids(arr);
                //window.location.href = "../test2.aspx?ProductIDS=" + arr;
            }
        }     
    </script>
    <script type="text/javascript">
        //商品信息
        function GetProductByProductids(productids) {
            var prodcutStr = "";
            $("#<%=divProduct.ClientID %>").html("");

            prodcutStr += "<table class='' width='100%' id='tb_rptProduct'>";
            $.post("../../WebService/ShopService.ashx", { 'Action': 'GetProductByProductIDsInAddOrder', 'ProductIDs': productids }, function (data, state) {
                if (state == "success") {
                    if (data.toString() != "") {
                        var d = eval(data);
                        $.each(d.table1, function (i) {
                            var productid = d.table1[i].ProductID;
                            var productimg = "../../Images/show_nopic.gif";
                            if (d.table1[i].ProductSmallImageUrl != "") {
                                productimg = "../../" + d.table1[i].ProductSmallImageUrl;
                            }
                            var productname = d.table1[i].ProductName;
                            var pt = d.table1[i].ProductType;
                            var producttype = "普通商品";
                            //商品类型（一口价：1、拍卖：2、团购：3、促销(秒杀)：4、积分商品：5）
                            if (pt == "4") {
                                producttype = "促销(秒杀)";
                            } else if (pt == "5") {
                                producttype = "积分商品";
                            }
                            var productstocks = d.table1[i].Stocks;
                            var productprice = d.table1[i].StandPrice;
                            var productunit = d.table1[i].PUnit;
                            if (productname.length > 20) {
                                // productname = productname.substring(0, 18) + "...";<input type='button' name='del1' onclick='del(" + productid + ")' value='删除' />
                            }

                            var m_deliverPayType = d.table1[i].DeliverPayType; //运费负担
                            var deliverPayType = "";
                            if (m_deliverPayType == "0") {
                                //卖家承担运费
                                deliverPayType = "卖家承担运费";
                            }
                            else if (m_deliverPayType == "1") {
                                //买家承担运费                                    
                                deliverPayType = "买家承担运费";
                            }
                            else if (m_deliverPayType == "2") {
                                //货到买家承担
                                deliverPayType = "货到买家承担";
                            }
                            var deliverType = d.table1[i].DeliverType; //运费类型
                            var needInvoice = d.table1[i].NeedInvoice; //是否有发票
                            var invoiceRate = d.table1[i].TaxRate; //税率
                            var procount = 0; //数量
                            var goodsMoney = 0; //金额
                            var invoiceTotal = 0; //发票金额
                            var deliverTotal = 0; //总运费
                            var orderTotal = 0;

                            var dadefault = 0;
                            var dadefIncrement = 0;
                            var deliver
                            var deliverStr = "";

                            if (m_deliverPayType != "0") {
                                var deliverTypename = d.table1[i].TypeName; //名称
                                var delivercharge = d.table1[i].deliverCharge;

                                if (delivercharge.length > 0) {
                                    var obj = "<select name='sel' id='selDeliverCharge" + i + "' onchange='SelCharge(" + i + ")'>";
                                    var de_position = delivercharge.indexOf('|');
                                    //判断是否存在"|"符号
                                    if (de_position > 0) {
                                        var temparray = delivercharge.split('|');
                                        for (var de_index1 = 0; de_index1 < temparray.length; de_index1++) {
                                            var optionValue = temparray[de_index1]; //Value值
                                            var optionText = ""; //Text值
                                            var deliverName = "";
                                            var arriveArea = "";
                                            var defaultPrice = 0;
                                            var deincrementPrice = 0;

                                            var tempArrayOption = optionValue.split('$');
                                            for (var de_index2 = 0; de_index2 < tempArrayOption.length; de_index2++) {
                                                var tempArrayValue = tempArrayOption[de_index2];
                                                var optionText = "";
                                                var de_position2 = tempArrayValue.indexOf('#');
                                                var de_lenght = tempArrayValue.length;
                                                if (de_index2 > 0) {
                                                    var firstText = tempArrayValue.substring(0, de_position2);
                                                    var begin = parseInt(de_position2) + 1;
                                                    var end = parseInt(de_lenght);
                                                    var secondText = tempArrayValue.substring(begin, end);
                                                    // alert("firstText:" + firstText); alert("secondText:" + secondText);
                                                    if (firstText == "deliverName") {
                                                        deliverName = secondText;
                                                    }
                                                    if (firstText == "arriveArea") {
                                                        arriveArea = secondText;
                                                    }
                                                    if (firstText == "defaultPrice") {
                                                        defaultPrice = secondText;
                                                    }
                                                    if (firstText == "deincrementPrice") {
                                                        deincrementPrice = secondText;
                                                    }
                                                }
                                            }
                                            var isfirst = "";
                                            if (de_index1 == 0) {
                                                dadefault = defaultPrice;
                                                dadefIncrement = deincrementPrice;
                                                isfirst = "selected='true'";
                                            }
                                            optionText = deliverName + " " + defaultPrice + " 元每加一件加" + deincrementPrice + "元 至" + arriveArea;
                                            obj += "<option value='" + optionValue + "'" + isfirst + ">" + optionText + "</option>";
                                        }
                                    } else {
                                        var optionValue = delivercharge; //Value值
                                        var optionText = ""; //Text值
                                        var deliverName = "";
                                        var arriveArea = "";
                                        var defaultPrice = 0;
                                        var deincrementPrice = 0;

                                        var tempArrayOption = optionValue.split('$');
                                        for (var de_index2 = 0; de_index2 < tempArrayOption.length; de_index2++) {
                                            var tempArrayValue = tempArrayOption[de_index2];
                                            var optionText = "";
                                            var de_position2 = tempArrayValue.indexOf('#');
                                            var de_lenght = tempArrayValue.length;
                                            if (de_index2 > 0) {
                                                var firstText = tempArrayValue.substring(0, de_position2);
                                                var begin = parseInt(de_position2) + 1;
                                                var end = parseInt(de_lenght);
                                                var secondText = tempArrayValue.substring(begin, end);
                                                // alert("firstText:" + firstText); alert("secondText:" + secondText);
                                                if (firstText == "deliverName") {
                                                    deliverName = secondText;
                                                }
                                                if (firstText == "arriveArea") {
                                                    arriveArea = secondText;
                                                }
                                                if (firstText == "defaultPrice") {
                                                    defaultPrice = secondText;
                                                }
                                                if (firstText == "deincrementPrice") {
                                                    deincrementPrice = secondText;
                                                }
                                            }
                                        }

                                        var isfirst = "selected='true'";
                                        dadefault = defaultPrice;
                                        dadefIncrement = deincrementPrice;
                                        optionText = deliverName + " " + defaultPrice + " 元每加一件加" + deincrementPrice + "元 至" + arriveArea;
                                        obj += "<option value='" + optionValue + "'" + isfirst + ">" + optionText + "</option>";
                                    }
                                    obj += "</select>";
                                    deliverStr = obj;
                                }
                            }
                            prodcutStr += "<tr><td>" +
                                        "<table class='ShopOrderStyle'><caption><input type='hidden' id='invoicehas" + i + "' value='" + needInvoice.toString().toLocaleLowerCase() + "' /><input type='hidden' id='unit" + i + "' value='" + productunit + "' /><input type='hidden' id='productID" + i + "' value='" + productid + "' />" + productname + "</caption><tr>" +
                                       "<tr><td class='OrderTitle'>数量：</td><td style='width:220px;'><input type='text' style='width:80px' id='procount" + i + "' onblur='SumKuCun(this," + i + ")' value='" + procount + "'/>" +
                                       "（库存:<span style='color:black' id='stock_before" + i + "'>" + productstocks + "</span>）</td>" +
                                        "<td class='OrderTitle'>当前单价：</td><td style='width:220px;><span style='color:black' id='price" + i + "'>" + productprice + "</span></td></tr>";
                            prodcutStr += "<tr><td class='OrderTitle'>运费负担：</td><td><input type='hidden' id='hidDeliverPayType" + i + "' value='" + m_deliverPayType + "' /><span style='color:black' id='DeliverPayType" + i + "'>" + deliverPayType + "</span></td>" +
                                        "<td class='OrderTitle'>商品运费：</td><td><span style='color:Green' id='deliverTotal" + i + "'>" + deliverTotal + "</span>" +
                                        "<input type='hidden' id='dadefault" + i + "' value='" + dadefault + "' />" +
                                        "<input type='hidden' id='dadefIncrement" + i + "' value='" + dadefIncrement + "' /></td>" +
                                        "</tr>";
                            if (m_deliverPayType != "0") {
                                prodcutStr += "<tr><td class='OrderTitle'>配送方式：</td><td colspan='3'>" + deliverStr + "</td></tr>";
                            }

                            //发票信息
                            if (needInvoice.toString().toLocaleLowerCase() == "true" || needInvoice.toString() == "1") {
                                prodcutStr += "<tr><td class='OrderTitle'>是否开发票：<td><input type='checkbox' id='needinvoice" + i + "' onclick='CheckInvoiceNeed(this," + i + ")' /></td>" +
                                        "<td class='OrderTitle'>发票抬头：</td><td><input type='text'  id='InvoiceHead" + i + "' /></td></tr>" +
                                        "<tr><td class='OrderTitle'>发票税率：</td><td><span style='color:black' id='invoiceRate" + i + "'>" + invoiceRate + "</span></td>" +
                                        "<td class='OrderTitle'>发票金额：</td><td><span style='color:Green' id='invoiceTotal" + i + "'>" + invoiceTotal + "</span></td>";
                            }
                            prodcutStr += "<tr><td class='OrderTitle'>商品金额：</td><td><span style='color:Green' id='goodsMoney" + i + "'>" + goodsMoney + "</span></td>" +
                                        "<td class='OrderTitle'>订单金额：</td><td><span style='color:Green' id='orderTotal" + i + "'>" + orderTotal + "</span></td>" +
                                        "</tr></table>" +
                                        "</td></tr>";

                        });
                    }
                    prodcutStr += "</table>";
                    $("#<%=divProduct.ClientID %>").append(prodcutStr);
                }
            });
        }  
    </script>
    <script type="text/javascript">
        //入库后的库存量(库存失去焦点时)
        function SumKuCun(sen, i) {
            var goodsPrice = $("#price" + i); //单价
            var goodsMoney = $("#goodsMoney" + i); //金额
            var beforestocks = $("#stock_before" + i); //库存
            var proCount = $(sen); //数量
            var sumMoney = 0;
            //输入的数量是数字且不能小于0
            if (proCount.val() == null || proCount.val() == "") {
                alert("请输入商品的入/出库数量！");
                proCount.focus();
                return;
            }
            if ((isNaN(proCount.val()) == true)) {//判断输入的是否为数字
                alert("请您输入0~9之间的数字！");
                proCount.focus();
                return;
            }
            if (parseInt(proCount.val()) < 1) {
                alert('您输入数量不能小于0！');
                proCount.focus();
                proCount.val("1");
                return;
            }

            if (parseInt(proCount.val()) > parseInt(beforestocks.html())) {
                alert("输入的数量不能大于当前的商品库存数！当前商品库存数量为" + beforestocks.html() + "！");
                proCount.focus();
                return;
            }

            if (goodsPrice.html() != null) {
                sumMoney = parseFloat(goodsPrice.html()) * parseInt(proCount.val());
            }
            goodsMoney.html(sumMoney); //商品金额
            checkDeliverTotal(proCount.val(), i); //运费
            CheckInvoiceNeed("#needinvoice" + i, i);
            TotalMoneyFum(i); //订单金额
        }
        //选择运费项
        function SelCharge(i) {
            //获取下拉列表的选择值
            var select_charge = document.getElementById("selDeliverCharge" + i);
            var bojvalue = "";
            for (indexed = 0; indexed < select_charge.options.length; indexed++) {
                if (select_charge.options[indexed].selected)
                    bojvalue = select_charge.options[indexed].value;
            }
            if (select_charge.length == 0) {
                alert("请选择运费！");
                select_charge.focus();
            }

            var sen_dadefault = "#dadefault" + i;
            // alert("sen_dadefault：" + sen_dadefault);
            var da_default = $(sen_dadefault); //基本价
            var da_defIncrement = $("#dadefIncrement" + i); //多加价
            if (bojvalue.length > 0) {
                var optionValue = bojvalue; //Value值
                var defaultPrice = 0;
                var deincrementPrice = 0;
                var tempArrayOption = optionValue.split('$');
                for (var de_index2 = 0; de_index2 < tempArrayOption.length; de_index2++) {
                    var tempArrayValue = tempArrayOption[de_index2];
                    var optionText = "";
                    var de_position2 = tempArrayValue.indexOf('#');
                    var de_lenght = tempArrayValue.length;
                    if (de_index2 > 0) {
                        var firstText = tempArrayValue.substring(0, de_position2);
                        var begin = parseInt(de_position2) + 1;
                        var end = parseInt(de_lenght);
                        var secondText = tempArrayValue.substring(begin, end);
                        // alert("firstText:" + firstText); alert("secondText:" + secondText);

                        if (firstText == "defaultPrice") {
                            defaultPrice = secondText;
                        }
                        if (firstText == "deincrementPrice") {
                            deincrementPrice = secondText;
                        }
                    }
                }
                da_default.attr("value", defaultPrice);
                da_defIncrement.attr("value", deincrementPrice);
            }

            var goodsPrice = $("#price" + i); //单价
            var goodsMoney = $("#goodsMoney" + i); //金额
            var coun = "#procount" + i;
            var procount = $(coun); //数量
            var sumMoney = 0;
            if (goodsPrice.html() != null) {
                sumMoney = parseFloat(goodsPrice.html()) * parseInt(procount.val());
            }

            goodsMoney.html(sumMoney); //商品金额
            checkDeliverTotal(procount.val(), i); //运费
            CheckInvoiceNeed("#needinvoice" + i, i); //发票
            TotalMoneyFum(i); //订单金额
        }

        //运费(数量)
        function checkDeliverTotal(procountValue, i) {
            var dadefault = $("#dadefault" + i); //基本价
            var dadefIncrement = $("#dadefIncrement" + i); //多加价
            var deliverTotal = $("#deliverTotal" + i); //运费

            var datotal = 0;
            if (dadefault.val() != "") {
                datotal = parseFloat(dadefault.val());
                if (dadefIncrement.val() != "") {
                    datotal += parseFloat(dadefIncrement.val()) * (parseInt(procountValue) - 1);
                }
            }
            deliverTotal.html(datotal);
        }
        //发票金额
        function CheckInvoiceNeed(sen, i) {
            var coun = "#procount" + i;
            var procount = $(coun); //数量
            if (parseInt(procount.val()) <= 0) {
                SumKuCun(coun, i); //商品的数量
            }
            var goodsPrice = $("#price" + i); //单价
            var goodsMoney = $("#goodsMoney" + i); //金额

            var cbkInvoice = $(sen); //发票
            var invoiceRate = $("#invoiceRate" + i); //税率
            var invoiceTotal = $("#invoiceTotal" + i); //发票金额
            var sumInvoiceTotal = 0;
            var sumMoney = 0; //商品金额

            sumMoney = parseFloat(goodsPrice.html()) * parseInt(procount.val()); //商品金额
            if (cbkInvoice.attr("checked") == "checked") {
                if (invoiceRate.html() != null) {
                    if (parseFloat(invoiceRate.html()) > 0) {
                        sumInvoiceTotal = (parseFloat(sumMoney) * parseFloat(invoiceRate.html())) / 100;
                    }
                }
            }
            invoiceTotal.html(sumInvoiceTotal); //发票金额
            goodsMoney.html(sumMoney); //商品金额
            checkDeliverTotal(procount.val(), i); //运费
            TotalMoneyFum(i); //订单金额
        }
        //订单金额
        function TotalMoneyFum(i) {
            var goodsMoney = $("#goodsMoney" + i); //商品金额
            var invoiceTotal = $("#invoiceTotal" + i); //发票金额
            var deliverTotal = $("#deliverTotal" + i); //运费
            var orderTotal = $("#orderTotal" + i); //订单金额
            var moneygoods = 0;
            if (goodsMoney.html() != null) {
                moneygoods = parseFloat(goodsMoney.html());
            }
            var moneyincoice = 0;
            if (invoiceTotal.html() != null) {
                moneyincoice = parseFloat(invoiceTotal.html());
            }
            var moneydeliver = 0;
            if (deliverTotal.html() != null) {
                moneydeliver = parseFloat(deliverTotal.html());
            }
            var sumtotalmoney = 0;
            sumtotalmoney = parseFloat(moneygoods) + parseFloat(moneyincoice) + parseFloat(moneydeliver);
            orderTotal.html(sumtotalmoney);
            TotalFun();
        }
        function TotalFun() {
            var lblGoodsTotal = $("#<%=lblGoodsTotal.ClientID %>"); //商品总额
            var lblInvoiceTotal = $("#<%=lblInvoiceTotal.ClientID %>"); //发票总额
            var lblDeliverTotal = $("#<%=lblDeliverTotal.ClientID %>"); //运费总额
            var lblOrderTotal = $("#<%=lblOrderTotal.ClientID %>"); //订单总额
            var productlist = $("#tb_rptProduct tr td table"); //商品信息
            var moneygoods = 0;
            var moneyincoice = 0;
            var moneydeliver = 0;
            var sumtotalmoney = 0;
            for (var i = 0; i < productlist.length; i++) {
                var goodsMoney = $("#goodsMoney" + i); //商品金额
                var invoiceTotal = $("#invoiceTotal" + i); //发票金额
                var deliverTotal = $("#deliverTotal" + i); //运费
                var orderTotal = $("#orderTotal" + i); //订单金额

                if (goodsMoney.html() != null) {
                    moneygoods += parseFloat(goodsMoney.html());
                }
                if (invoiceTotal.html() != null) {
                    moneyincoice += parseFloat(invoiceTotal.html());
                }
                if (deliverTotal.html() != null) {
                    moneydeliver += parseFloat(deliverTotal.html());
                }
            }
            sumtotalmoney = parseFloat(moneygoods) + parseFloat(moneyincoice) + parseFloat(moneydeliver);
            lblGoodsTotal.text(moneygoods);
            lblInvoiceTotal.text(moneyincoice);
            lblDeliverTotal.text(moneydeliver);
            lblOrderTotal.text(sumtotalmoney);
        }

        //删除
        function del(num) {
            var productstr = $("#<%=hfProductStr.ClientID %>"); //商品（ID,库存,价格,单位,入库后）
            var productlist = $("#tb_rptProduct tr"); //商品信息
            var producttable = document.getElementById("tb_rptProduct"); //商品信息
            var str = "";
            if (productlist.length - 1 > 0) {
                for (var i = 0; i < productlist.length - 1; i++) {
                    var vproductid = $("#productID" + i); //商品ID
                    if (vproductid.val() == num) {
                        var row = parseInt(i + 1);
                        producttable.deleteRow(row);
                    } else {
                        str += vproductid.val() + ",";
                    }
                }
            }
            var position = str.lastIndexOf(',');
            if (position > 0) {
                str = str.substring(0, str.length - 1);
            }
            GetProductByProductids(str); //商品信息
            return true;
        } 
    </script>
    <%--商品 end--%>
    <script type="text/javascript">
        
        $(function () {
            var ct = "gr";//个人       
            //用户名
            $("#<%=txtBuyer.ClientID %>").autocomplete({
                source: '/WebService/ClientService.ashx?Action=GetClientInfoName&CT=' + ct + '&currentUser='+<%= user.UserID %> +'&d1=' + new Date().getMilliseconds()
            });
        });

        //下拉列表
        function ChangeDDL(ddl, objvalue) {
            for (i = 0; i < ddl.length; i++) {
                if (objvalue == ddl[i].text) {
                    ddl[i].selected = true;
                    break;
                }
            }
        }
        //城市
        function getCity(ddlCity, pro, citys) {
            if (pro != "") {
                ddlCity.options.length = 0; //清空下拉列表值
                $.post("../../WebService/ClientService.ashx", { 'Action': 'GetCityByProvince', 'Province': pro }, function (data, state) {
                    if (state == "success") {
                        if (data.toString() != "") {
                            var da = eval(data);
                            $.each(da.table1, function (p) {
                                var strName = da.table1[p].City; //文本值
                                var objOption = new Option(strName, strName); // 建立Option对象
                                ddlCity.options.add(objOption);
                                if (citys == strName) {
                                    //   ddlCity.selected =  'selected';
                                    ChangeDDL(ddlCity, citys);
                                }
                            });
                        }
                    }
                });
            }
        }
        //县/区
        function getArea(ddlArea, citys, areas) {
            if (citys != "") {
                ddlArea.options.length = 0; //清空下拉列表值
                $.post("../../WebService/ClientService.ashx", { 'Action': 'GetAllAreaByCity', 'City': citys }, function (data, state) {
                    if (state == "success") {
                        if (data.toString() != "") {
                            var dat = eval(data);
                            $.each(dat.table1, function (c) {
                                var strName = dat.table1[c].Area; //文本值
                                var objOption = new Option(strName, strName); // 建立Option对象
                                ddlArea.options.add(objOption);
                                if (areas == strName) {
                                    //ddlArea.selected = 'selected';
                                    ChangeDDL(ddlArea, areas);
                                }
                            });
                        }
                    }
                });
            }
        }
        
        function khNameChange() {
            var clientName = $("#<%=txtBuyer.ClientID %>"); //客户名称
            var txtShortedForm = $("#<%=Contacter_Name.ClientID %>"); //简称
            //联络信息
            var province = document.getElementById('<%=HoWaveProvinceCity1.ClientID %>' + "_Province"); //省
            var city = document.getElementById('<%=HoWaveProvinceCity1.ClientID %>' + "_City"); //市
            var cmArea = document.getElementById('<%=HoWaveProvinceCity1.ClientID %>' + "_ddlArea"); //县/区
            var address = $("#<%=Contacter_Address.ClientID %>");  //详细地址
            var zipCode = $("#<%=Contacter_ZipCode.ClientID %>"); //邮政编码
            var pPhone = $("#<%=Contacter_Phone.ClientID %>"); //固定电话
          
            //个人信息
           // var rblSex = document.getElementsByName("<%=rblSex.ClientID %>"); //性别——0：男、1：女、2、保密               
            var txtMobile = $("#<%=Contacter_Mobile.ClientID %>"); //移动电话
            var txtEmail = $("#<%=Contacter_Email.ClientID %>"); //Email地址

            $.post("../../WebService/ClientService.ashx", { 'Action': 'GetClientInfoByClientName', 'ClientName': clientName.val() }, function (data, state) {
                if (state == "success") {
                    if (data.toString() != "") {
                        var d = eval(data);
                        $.each(d.table1, function (i) {
                            txtShortedForm.attr("value", d.table1[i].TrueName);
                                var pro = d.table1[i].Province;
                                var c = d.table1[i].City;
                                var a = d.table1[i].Districts;
                                ChangeDDL(province, pro); //省
                                getCity(city, pro, c); //市
                                getArea(cmArea, c, a); //县/区
                                address.attr("value", d.table1[i].Address);
                                if (d.table1[i].ZipCode.toString() != "0") {
                                    zipCode.attr("value", d.table1[i].ZipCode);
                                }
                                pPhone.attr("value", d.table1[i].HomePhone);

                               //性别
                               $('[name="ctl00$ContentPlaceHolder1$rblSex"]:radio').each(function() {
                                    if (this.value == d.table1[i].Sex) { 
                                        this.checked = true;
                                    }
                                });
                                txtMobile.attr("value", d.table1[i].Mobile);
                                txtEmail.attr("value", d.table1[i].Email);                            
                        });
                    }
                }
            });           
        }
      
    </script>
</asp:Content>
