<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AddNew.aspx.cs" Inherits="LMN.Web.Admin.Shop.AddNew" ValidateRequest="false" %>

<%@ Import Namespace="LMN.Common" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<link href="../css/admin.css" rel="stylesheet" type="text/css" />
<link href="../Scripts/wbox/wbox.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="../Scripts/jquery-1.4.1.min.js"></script>
<script src="../Scripts/wbox.js" type="text/javascript"></script>
<script src="../Scripts/swfupload.js" type="text/javascript"></script>
<script src="../Scripts/handlers.js" type="text/javascript"></script>
<script src="../../ckeditor/fckeditor.js" type="text/javascript"></script>
<script src="../Scripts/common.js" type="text/javascript"></script>
</head>
<body>
<form id="form1" runat="server" target="iframe" onsubmit="return chkForm()">
    <div style="padding: 20px;">
        <h4 class="title">基本信息</h4>
        <div class="division">
            <table>
                <tr>
                    <th class="w120">商品编号：</th>
                    <td><input runat="server" type="text" name="gno" readonly="readonly" id="gno" /> </td>
                    <td rowspan="11" style="width: 280px; vertical-align: top;">
                        <div class="division">
                            <div style="height: 22px;">
                                <%--<div><a id="filemanager" href="javascript:;">选择图片</a>
            </div>--%><span id="uploadinfo" style="float: right;"></span><span id="uploading"></span></div>
                            <div id="imageview">
                                <p class="center">
                                    <br />
                                    <br />
                                    此处显示商品页默认图片<br />
                                    [您还未上传商品图片！]</p>
                            </div>
                            <div id="imagelist">
                            </div>
                            <div class="clear">
                            </div>
                            <input type="hidden" runat="server" id="images" name="images" /> <input type="hidden" runat="server" id="cover" name="cover" />
                        </div>
                    </td>
                </tr>
                <tr>
                    <th>选择分类：</th>
                    <td><select id="category" name="category" size="1" style="float: left; margin-right: 10px;">
                        <option value="0">请选择</option>
                        <%=CategorySelectList%></select><asp:DropDownList ID="model" name="model" size="1" runat="server" Style="float: left; margin-right: 10px;">
                        </asp:DropDownList>
                        <asp:DropDownList ID="brand" name="brand" size="1" runat="server" Style="float: left; margin-right: 10px;">
                        </asp:DropDownList>
                        <input type="checkbox" value="1" id="islince" name="islince" runat="server" /><label for="islince">正品</label></td>
                </tr>
                <tr>
                    <th>商品名称：</th>
                    <td><input runat="server" type="text" id="name" name="name" size="40" /> </td>
                </tr>
                <tr>
                    <th>商品简称：</th>
                    <td><input runat="server" type="text" id="alias" name="alias" size="30" maxlength="40" /> </td>
                </tr>
                <tr>
                    <th>商品标签：</th>
                    <td><input runat="server" type="text" id="keys" name="keys" size="50" /> </td>
                </tr>
                <tr>
                    <th>商品简介：</th>
                    <td><input runat="server" type="text" id="intro" name="intro" size="50" maxlength="255" style="width: 90%;" /></td>
                </tr>
                <tr>
                    <th>用户简评：</th>
                    <td><input type="text" id="comment" name="comment" size="50" runat="server" style="width: 80%;" /> <i style="color: red;" title="格式：昵称:内容(昵称+内容在13个汉字以内)，填写两条记录“|”分隔。例：魔幻天空:超大屏幕,喜欢|小强:功能超强,三防技术">?</i></td>
                </tr>
                <tr>
                    <th>计量单位：</th>
                    <td>
                        <asp:DropDownList ID="unit" name="unit" size="1" runat="server">
                        </asp:DropDownList>
                        <%--<select size="1" name="unit" id="unit">
                        <option value="">请选择</option>
                        <%=UnitSelectList %>
                    </select> --%></td>
                </tr>
                <tr>
                    <th>推荐设置：</th>
                    <td><input runat="server" type="checkbox" name="issale" id="issale" checked="true" value="1" /><label for="issale">上架</label> <input runat="server" type="checkbox" id="isrecom" name="isrecom" value="1" /><label for="isrecom">推荐</label> <input runat="server" type="checkbox" id="isnew" name="isnew" value="1" /><label for="isnew">新品</label> <input runat="server" type="checkbox" id="ishot" name="ishot" value="1" /><label for="ishot">热门</label> <input runat="server" type="checkbox" id="isprice" name="isprice" value="1" /><label for="isprice">特价</label> <input runat="server" type="checkbox" id="isfree" name="isfree" value="1" /><label for="isfree" class="red">免邮</label> <input runat="server" type="text" id="hits" name="hits" size="1" value="1" style="width: 10px;" /><label for="hits">点击</label> <input runat="server" type="text" id="sort" name="sort" size="1" value="999" style="width: 30px;" /><label for="sort">排序</label></td>
                </tr>
            </table>
        </div>
        <h4 class="title">商品价格</h4>
        <div class="division">
            <table id="nospec" width="100%">
                <tr>
                    <th class="w120">货号：</th>
                    <td><input runat="server" readonly="readonly" type="text" name="no" maxlength="25" id="no" /> </td>
                </tr>
                <tr>
                    <th>库存：</th>
                    <td><input runat="server" type="text" id="store" name="store" maxlength="25" value="100" /> </td>
                </tr>
                <tr>
                    <th>价格：</th>
                    <td>市场价: <input runat="server" type="text" id="market" name="market" size="10" value="0.00" /> 销售价: <input runat="server" type="text" name="price" id="price" size="10" value="0.00" /> 成本价: <input runat="server" type="text" name="cost" id="cost" title="前台不会显示，仅供后台使用。" size="10" value="0.00" /> </td>
                </tr>
                <tr>
                    <th>重量：</th>
                    <td><input runat="server" type="text" name="weight" id="weight" maxlength="25" value="0" /> 克(g) </td>
                </tr>
            </table>
            <table>
                <tbody id="specBox">
                </tbody>
            </table>
            <table>
                <tr>
                    <th class="w120">规格：</th>
                    <td><span class="sysiconBtn" id="specCtrl" title="开启绑定规格">开启规格</span>&nbsp; <span class="sysiconBtn" id="specCust" title="重新自定义规格">自定规格</span></td>
                </tr>
            </table>
            <table>
                <tbody id="itemBox">
                </tbody>
            </table>
        </div>
        <h4 class="title">商品属性</h4>
        <div class="division">
            <table>
                <tbody id="attrBox">
                </tbody>
            </table>
        </div>
        <h4 class="title">扩展属性</h4>
        <div class="division">
            <table>
                <tr>
                    <th class="w120">添加扩展 </th>
                    <td><select size="1" id="extra_type" name="extra_type">
                        <option value="1">文字描述</option>
                        <option value="2">图文描述</option>
                        <option value="3">扩展图组</option>
                        <option value="9">搜索优化</option>
                    </select> <input type="text" size="10" id="extra_title" name="extra_title" value="商品描述" /> <input type="checkbox" id="extra_iswap" name="extra_iswap" value="1" checked="checked" /><label for="extra_iswap">Wap</label><%-- <input type="checkbox" id="extra_isdisp" name="extra_isdisp" value="1" /><label for="extra_isdisp" title="是否显示在产品详情首页">显示</label>--%>
                        <span class="sysiconBtn" id="extraAdd">添加</span></td>
                </tr>
                <tbody id="extraBox">
                </tbody>
            </table>
        </div>
        <%--<h4 class="title">参加活动 <a href="javascript:;" id="eventAdd">+<span>添加</span></a></h4>
        <div class="division">
            <table>
                <thead>
                    <tr>
                        <th class="w20">ID</th>
                        <th>标题</th>
                        <th class="w20">移除</th>
                    </tr>
                </thead>
                <tbody id="eventBox">
                </tbody>
            </table>
        </div>--%>
        <h4 class="title">赠送礼品 <a href="javascript:;" id="giftAdd">+<span>添加</span></a></h4>
        <div class="division">
            <table>
                <thead>
                    <tr>
                        <th class="w20">ID</th>
                        <th>标题</th>
                        <th class="w20">移除</th>
                    </tr>
                </thead>
                <tbody id="giftBox">
                </tbody>
            </table>
        </div>
        <h4 class="title">相关专题 <a href="javascript:;" id="topicAdd">+<span>添加</span></a></h4>
        <div class="division">
            <table>
                <thead>
                    <tr class="left">
                        <th class="w160">专题名称</th>
                        <th class="w200">商品名称</th>
                        <th class="w240">商品图片</th>
                        <th>商品点评</th>
                        <th class="w40">排序</th>
                        <th class="w20" onclick="//alert(topic())">删除</th>
                    </tr>
                </thead>
                <tbody id="topicBox">
                </tbody>
                <tbody id="topicAddBox">
                </tbody>
            </table>
        </div>
        <h4 class="title">相关商品 <a href="javascript:;" id="prodAdd">+<span>添加</span></a></h4>
        <div class="division">
            <table>
                <thead>
                    <tr class="left">
                        <th class="w20">ID</th>
                        <th>商品名称</th>
                        <th class="w20">移除</th>
                    </tr>
                </thead>
                <tbody id="prodBox">
                </tbody>
            </table>
        </div>
        <h4 class="title">相关文章 <a href="javascript:;" id="artiAdd">+<span>添加</span></a></h4>
        <div class="division">
            <table>
                <thead>
                    <tr>
                        <th class="w20">ID</th>
                        <th>标题</th>
                        <th class="w20">移除</th>
                    </tr>
                </thead>
                <tbody id="artiBox">
                </tbody>
            </table>
        </div>
        <h4 class="title">保存</h4>
        <div class="division">
            <table>
                <tr>
                    <th class="w120"></th>
                    <td><b class="submitBtn"><input type="submit" value="保存" /></b> </td>
                </tr>
            </table>
        </div>
    </div>
</form>
<iframe id="iframe" name="iframe" src="about:blank"></iframe>
<script type="text/javascript">
    // 定义变量
    var wBox;
    var GoodsId = <%=GoodsId %>;
    var postParams = {
        "ASPSESSID": "<%=Session.SessionID %>",
        "GoodsId": GoodsId,
        "LoginInfo": "<%=LoginInfo %>"
    };
    
    //#region 加载全部专题(Json)
    $.get("Action.aspx?action=gettopic&" + new Date(), function(data) {
        if (data != "null") {
            jsonTopic = eval(data);
        }
    });
    //#endregion
</script>
<script type="text/javascript" src="AddNew.js"></script>
<script type="text/javascript">
    //#region SWFUpload
    var swfu = new SWFUpload({
        // Backend Settings
        upload_url: "../FileManager/SwfImgUpload.aspx",
        post_params: postParams,

        // File Upload Settings
        file_size_limit: "2 MB",
        file_types: "*.jpg;*.gif;*.bmp;*.png;*.jpeg",
        file_types_description: "All Images...",
        file_upload_limit: "20",    // Zero means unlimited

        file_queue_error_handler: fileQueueError,
        file_dialog_complete_handler: fileDialogComplete,
        upload_progress_handler: uploadProgress,
        upload_error_handler: uploadError,
        upload_success_handler: uploadSuccess,
        upload_complete_handler: function() {
            try {
                /*  I want the next upload to continue automatically so I'll call startUpload here */
                if (this.getStats().files_queued > 0) {
                    this.startUpload();
                } else {
                    getSWFUpload();
                    $("#uploadinfo").html('全部完成！<a href="javascript:;" onclick="GetSWFUpload()">刷新</a>(<a href="javascript:;" onclick="clearSWFUpload()">清空</a>)');
                }
            } catch (ex) {
                this.debug(ex);
            }
        },
        // Button settings
        button_image_url: "../Scripts/swfup/upload.gif",
        button_placeholder_id: "uploading",
        button_width: 74,
        button_height: 22,
        // Flash Settings
        flash_url: "../Scripts/swfupload.swf", // Relative to this file
        custom_settings: {
            upload_target: "uploadinfo"
        },
        // Debug Settings
        debug: false
    });
    //#endregion

    //#region 图片
    // 绑定上传的图片
    function getSWFUpload() {
        $.get("Action.aspx?action=loadswfupload&" + new Date(), function(data) {
            if (data != "null") {
                $("#images").val(data);
                var json = eval(data);
                var str = '';
                for (var j = 0; j < json.length; j++) {
                    str += '<img alt="view[' + j + ']" onclick="imageSetDefault(this)" src="' + json[j].path + '50/' + json[j].filename + json[j].fileext + '" />';
                    if (j == 0) {
                        $("#cover").val(json[j].id);
                        $("#imageview").html('<img alt="view" src="' + json[j].path + '200/' + json[j].filename + json[j].fileext + '" />');
                    }
                }
                $("#imagelist").html(str);
                $("#uploadinfo").html('<a href="javascript:;" onclick="clearSWFUpload()">清空</a>');
            } else {
                $("#imageview").html('<p class="center"><br /><br />此处显示商品页默认图片<br />[您还未上传商品图片！]</p>');
                $("#imagelist").html('');
                $("#uploadinfo").html('');
            }
        });
    }
    // 清除上传图片
    function clearSWFUpload() {
        if (!confirm("确认操作？"))
            return false;

        $.get("Action.aspx?action=clearswfupload&" + new Date(), function(data) {
            if (data != "1") {
                alert("清除失败，请重试！");
                return false;
            }
            getSWFUpload();
            return true;
        });
    }
    // 设置默认图片
    function imageSetDefault(o) {
        if ($("#images").val() == "") {
            return false;
        }
        var src = $(o).attr("src");
        var json = eval($("#images").val()); // $.cookie("SwfImageUpload")
        var filename = getFileName(src).replace("/50", "/");
        for (var j = 0; j < json.length; j++) {
            if (json[j].filename == filename) {
                $("#cover").val(json[j].id);
            }
        }
        $("#imageview").children("img").attr("src", src.replace("/50/", "/200/"));
        return true;
    }
    //#endregion

    //#region 分类选择事件
    function resetSpec() {
        $("#model").val(0);
        $("#nospec").css("display", "");
        $("#specBox").html("");
        $("#itemBox").html("");
        $("#attrBox").html("");
    }
    $("#category").change(function() {
        var cateid = $(this).val(); // children("option:selected").
        resetSpec();
        if (cateid == "0") {
            // alert("请选择分类！");
            return;
        }
        // 触发绑定模型品牌事件
        bindModel(cateid);
        bindBrands(cateid);
        bindAttr(cateid);
        return;
    });
    // 根据分类绑定模型
    function bindModel(cateid) {
        $.get("Action.aspx?action=getmodelidbycategoryid&id=" + cateid + "&" + new Date(), function(data) {
            $("#model").val(data);
        });
    }
    // 根据分类绑定绑定品牌
    function bindBrands(cateid) {
        $.get("Action.aspx?action=getbrandsbycategoryid&id=" + cateid + "&" + new Date(), function(data) {
            if (data == "null") {
                $("#brand").html('<option value="0">----</option>');
            } else {
                var list = eval(data);
                var str = '<option value="0" selected="selected">请选择</option>';
                for (var j = 0; j < list.length; j++) {
                    str += '<option value="' + list[j].id + '">' + list[j].name + '</option>';
                }
                $("#brand").html(str);
            }
        });
    }
    // 打开Model绑定的规格，否则打开规则选择窗口
    function bindSpecs(categoryId) {
        $.get("Action.aspx?action=getspecsbycategoryid&id=" + categoryId + "&" + new Date(), function(data) {
            if (data != "null") {
                var jsonData = eval(data);
                var str = "";
                for (var j = 0; j < jsonData.length; j++) {
                    str += specRow(jsonData[j]);
                }
                $("#specBox").html(str);
                $("#specCtrl").text("取消规格");
                $("#nospec").css("display", "none");
                return;
            }
            wBox = $("#hoho").wBox({
                show: true,
                title: "启用规格",
                requestType: "ajax",
                target: "Action.aspx?action=loadspec&" + new Date()
            });
            return;
        });        // 绑定规格结束
    }
    //#endregion

    //#region 规格
    // Spec Row //规则行
    function specRow(obj) {
        var str = '<tr specid="' + obj.id + '"><th class="w120">' + obj.name + '</th><td>';
        if (obj.values != null && obj.values != "" && obj.values != "null") {
            var valueJson = obj.values;
            for (var k = 0; k < valueJson.length; k++) {
                str += '<input type="checkbox" id="value_' + valueJson[k].id + '" name="valueId" value="' + valueJson[k].id + '" text="' + valueJson[k].text + '" spec="' + obj.name + '" spec="' + obj.id + '" color="' + obj.color + '" onclick="selectSpecValue(this)" /><label for="value_' + valueJson[k].id + '">';
                if (obj.type == 1) {
                    str += '<img src="' + valueJson[k].icon + '" alt="' + valueJson[k].text + '" />';
                } else {
                    str += valueJson[k].text;
                }
                str += '</label>';
            }
        }
        str += '<div id="bindimg_' + obj.id + '"></div></td></tr>'; // (obj.color == 1 ? '' : '') + 
        return str;
    }
    // 重新定义规格
    $("#specCust").click(function() {
        var categoryId = $("#category").val();
        if (categoryId == 0) {
            showTips("请选择分类! ", 3);
            return;
        }
        wBox = $("#hoho").wBox({
            show: true,
            title: "启用规格",
            requestType: "ajax",
            target: "Action.aspx?action=loadspec&" + new Date()
        });
        return;
    });
    // 打开规格
    $("#specCtrl").click(function() {
        if ($("#specCtrl").text() == "取消规格") {
            $("#specBox").html("");
            $("#specCtrl").text("开启规格");
            $("#nospec").css("display", "");
            $("#itemBox").html("");
            return;
        }
        var categoryId = $("#category").val();
        if (categoryId == 0) {
            showTips("请选择分类! ", 3);
            return;
        }
        bindSpecs(categoryId);
    });
    // 选择规则
    function addSpec() {
        var obj = $(".valueList input[name=color]:checked");
        if (obj.length != 1 || isNaN(obj.val())) {
            alert("请选择颜色类规格且最多只能选择一项！");
            return false;
        }
        var val = obj.val();
        obj = $(".valueList input[name=size]:checked");
        if (obj.length > 0 && obj.length != 1) {
            alert("尺码类规格最多只能选择一项！");
            return false;
        }
        if (!isNaN(obj.val())) {
            val += "," + obj.val();
        }

        $.get("Action.aspx?action=getspec&id=" + val + "&" + new Date(), function(data) {
            if (data != "null") {
                var spec = eval(data);
                var str = "";
                for (var j = 0; j < spec.length; j++) {
                    str += specRow(spec[j]);
                }
                // alert(str);
                $("#nospec").css("display", "none");
                $("#specBox").css("display", "").html(str);
                $("#specCtrl").text("取消规格");
                wBox.close();
                return true;
            } else {
                alert("请选择要添加的规则！");
                return false;
            }
        });
        return true;
    }
    // 绑定规格值
    function selectSpecValue(o) {
        var valueId = $(o).attr("value");
        var text = $(o).attr("text");
        var color = $(o).attr("color");
        if (color == 1) {
            if ($(o).attr("checked")) {
                // 添加绑定图片控件
                var s = $(o).parent().children("div").html();
                s += '<div id="valueimg_' + valueId + '" class="valueimg"><span class="imgs"></span> ' + text + ' ';
                s += '<a href="javascript:;" onclick="openBind(' + valueId + ');">绑定图片</a>';
                s += '<input type="hidden" id="valueImage_' + valueId + '" name="valueImage" value="" />';
                s += '</div>';
                $(o).parent().children("div").html(s);
            } else {
                // 移除绑定图片控件
                $("#valueimg_" + valueId).remove();
            }
        }

        var str = "";
        $("#specBox").children("tr").each(function() {
            //var specId = $(this).attr("specid");
            var str2 = "";
            $(this).find("td input[name=valueId]:checked").each(function() {
                str2 += (str2 == "" ? "" : ",") + $(this).attr("value");
            });

            if (str == "") {
                str += str2;
            } else {
                if (str2 != "") {
                    str += "|" + str2;
                }
            }
        });

        if (str == "") {
            return;
        }

        // 组合结果
        var arr = str.split("|");
        if (arr.length == 1) {
            createItem(str);
        } else {
            var t = "";
            var t1 = arr[0].split(",");
            var t2 = arr[1].split(",");
            for (var j = 0; j < t1.length; j++) {
                for (var k = 0; k < t2.length; k++) {
                    t += (t == "" ? "" : ",") + t1[j] + "|" + t2[k];
                }
            }
            createItem(t);
        }
        return;
    }
    // 生成产品
    function createItem(value) {
        // alert("Group: " + value);
        if (value == "")
            return false;

        var itemList = "";
        var arr = value.split(",");
        for (var j = 0; j < arr.length; j++) {
            var arr2 = arr[j].split("|");
            var k;
            // 第一次循环输出表头
            if (j == 0) {
                itemList += '<thead>';
                itemList += '<tr>';
                for (k = 0; k < arr2.length; k++) {
                    var name = $("#value_" + arr2[k]).parent().parent().find("th").text();
                    itemList += '<th style="width: auto; text-align: center; ">' + name + '</th>';
                }
                itemList += '<th style="width: auto; text-align: left; ">货号</th>';
                itemList += '<th style="width: auto; text-align: left; ">库存/<a href="javascript:;" onclick="fillItem()">*</a></th>';
                itemList += '<th style="width: auto; text-align: left; ">市场价/<a href="javascript:;" onclick="fillItem()">*</a></th>';
                itemList += '<th style="width: auto; text-align: left; ">销售价/<a href="javascript:;" onclick="fillItem()">*</a></th>';
                itemList += '<th style="width: auto; text-align: left; ">成本价/<a href="javascript:;" onclick="fillItem()">*</a></th>';
                itemList += '<th style="width: auto; text-align: left; ">重量/<a href="javascript:;" onclick="fillItem()">*</a></th>';
                itemList += '<th style="width: auto; text-align: left; ">移除</th>';
                itemList += '</tr>';
                itemList += '</thead>';
                itemList += '<tbody>';
            }
            // 循环输出记录
            itemList += "<tr>";
            //var specs = "";
            var colorId = 0, sizeId = 0;
            var colorName = "", sizeName = "";
            for (k = 0; k < arr2.length; k++) {
                var text = $("#value_" + arr2[k]).attr("text");
                var color = $("#value_" + arr2[k]).attr("color");
                itemList += '<td class="center">' + text + '</td>';
                if (color == "1") {
                    colorId = arr2[k];
                    colorName = text;
                }
                else {
                    sizeId = arr2[k];
                    sizeName = text;
                }
            }

            var strNo = $("#gno").val() + '-' + (j + 1);
            itemList += '<td><input name="itemNo" readonly="readonly" type="text" size="12" id="itemNo_' + j + '" value="' + strNo + '" /></td>';
            itemList += '<td><input name="itemStore" type="text" size="5" id="itemStore_' + j + '" value="0" /></td>';
            itemList += '<td><input name="itemMarket" type="text" size="5" id="itemMarket_' + j + '" value="0.00" /></td>';
            itemList += '<td><input name="itemPrice" type="text" size="5" id="itemPrice_' + j + '" value="0.00" /></td>';
            itemList += '<td><input name="itemCost" type="text" size="5" id="itemCost_' + j + '" value="0.00" /></td>';
            itemList += '<td><input name="itemWeight" type="text" size="5" id="itemWeight_' + j + '" value="0" />(g)</td>';
            itemList += '<td><input type="hidden" id="itemId_' + j + '" name="itemId" value="0" />';
            itemList += '<input type="hidden" value="' + colorId + '" id="itemColor_' + j + '" name="itemColor" />';
            itemList += '<input type="hidden" value="' + colorName + '" id="itemColorName_' + j + '" name="itemColorName" />';
            itemList += '<input type="hidden" value="' + sizeId + '" id="itemSize_' + j + '" name="itemSize" />';
            itemList += '<input type="hidden" value="' + sizeName + '" id="itemSizeName_' + j + '" name="itemSizeName" />';
            itemList += '<img src="../images/delete.gif" onclick="deleteItem(this)"></td>';
            itemList += "</tr>";
        }
        $("#itemBox").html(itemList + '</tbody>');
        return true;
    }
    function deleteItem(o) {
        if (confirm("确认删除？")) {
            $(o).parent().parent().remove();
        }
    }
    // 绑定图片
    function openBind(id) {
        var str = $("#images").val();
        if (str != "") {
            var json = eval(str);

            str = '';
            str += '<div class="bind-img">';
            str += '<ul>';
            for (var j = 0; j < json.length; j++) {
                str += '<li><img src="' + json[j].path + '50/' + json[j].filename + json[j].fileext + '" alt="" /><input type="checkbox" value="' + json[j].id + '" id="bind-img-' + json[j].id + '" text="' + json[j].path + '50/' + json[j].filename + json[j].fileext + '" name="bind-img" /></li>';
            }
            str += '</ul>';
            str += '<div style="clear: both; text-align: center; "><input type="button" value="确认" onclick="bindImg(' + id + ')" /></div>';
            str += '</div>';

            wBox = $("#wbox1").wBox({
                title: "请选择图片",
                html: str,
                show: true
            });
        } else {
            alert("没有上传任何图片! ");
        }
        return false;
    }
    function bindImg(id) {
        var ids = ""; //$("#valueimg_" + id).children("input[name=valueImage]").val();
        var imgs = ""; //$("#valueimg_" + id).children("span").html();
        // alert($("input[name=bind-img]:checked").length);
        var iswap = 0;
        $("input[name=bind-img]:checked").each(function() {
            var s = $(this).val();
            var w = $(this).attr("text");
            // alert(ids + "," + s);
            // alert(s + "|" + w);
            if (ids.indexOf(s) == -1) {
                // ids += (ids == '' ? '' : ',') + s;
                if (ids != "")
                    ids += "|";

                ids += s;

                if (imgs != "")
                    imgs += " ";

                if (iswap == 0) {
                    imgs += '<img src="' + w + '" alt="WAP" imgid="' + s + '" class="wap" onclick="selwap(this)" />';
                    iswap = s;
                } else {
                    imgs += '<img src="' + w + '" alt="WAP" imgid="' + s + '" onclick="selwap(this)" />';
                }
            }
        });
        $("#valueimg_" + id).children("input[name=valueImage]").val(id + "#" + iswap + "#" + ids);
        $("#valueimg_" + id).children("span").html(imgs);
        wBox.close();
        // alert($("#valueimg_" + id).children("input").val() + "|" + $("#valueimg_" + id).children("span").html());
    }
    // 按第一行填充所有规格数据
    function fillItem() {
        var store = $("input[name=itemStore]").first().val();
        $("input[name=itemStore]").each(function() {
            $(this).val(store);
        });
        var market = $("input[name=itemMarket]").first().val();
        $("input[name=itemMarket]").each(function() {
            $(this).val(market);
        });
        var price = $("input[name=itemPrice]").first().val();
        $("input[name=itemPrice]").each(function() {
            $(this).val(price);
        });
        var cost = $("input[name=itemCost]").first().val();
        $("input[name=itemCost]").each(function() {
            $(this).val(cost);
        });
        var weight = $("input[name=itemWeight]").first().val();
        $("input[name=itemWeight]").each(function() {
            $(this).val(weight);
        });
    }
    //#endregion 规格

    //#region 扩展
    $("#extra_type").change(function() {
        var val = $(this).val();
        if (val == 1) {
            $("#extra_title").val("商品描述");
        }
        if (val == 2) {
            $("#extra_title").val("详细介绍");
        }
        if (val == 3) {
            $("#extra_title").val("正面图");
        }
        if (val == 9) {
            $("#extra_title").val("搜索优化");
        }
    });
    // 添加扩展事件
    $("#extraAdd").click(function() {
        if (!chkField("extra_type") || !chkField("extra_title")) {
            alert("请选择扩展类型并填写扩展名称！");
            return false;
        }
        var extType = $("#extra_type").val();
        var extTitle = $("#extra_title").val();
        var isWap = $("#extra_iswap").attr("checked") ? 1 : 0;

        if (extType == 1) {
            extraAdd(extType, { id: 0, title: extTitle, iswap: isWap, value: "" });
        }
        if (extType == 2 || extType == 3) {
            extraAdd(extType, { id: 0, title: extTitle, iswap: isWap, value: "[{ title:'', image:'', intro:'' }]" });
        }
        if (extType == 9) {
            extraAdd(extType, { id: 0, title: extTitle, iswap: isWap, value: "{ title:'', keyword:'', desc:'' }" });
        }
        return true;
    });
    // 删除扩展
    function extraDele(o) {
        if (!confirm("确认删除？"))
            return false;
        $(o).parent().parent().remove();
        return true;
    }
    // 选择图片
    function extraImageSelect(o) {
        var id = $(o).parent().children("input[name=extra-image-image]").attr("id");
        // 选择图片
        wBox = $("#hoho").wBox({
            show: true,
            requestType: "iframe",
            iframeWH: { width: 800, height: 600, resize: false },
            target: "../FileManager/Default.aspx?target=" + id
        });
        return true;
    }
    // 删除图片
    function extraImageDele(o) {
        if (!confirm("确认删除？"))
            return false;
        $(o).parent().remove();
        return true;
    }
    // 添加图片
    function extraImageAdd(o) {
        var extra = $(o).parent().parent().parent().attr("index");
        var index = parseInt($(o).parent().last().attr("index")) + 1;

        var str = '<div class="extra-list-item" index="' + index + '">标题 <input type="text" id="extra-image-title-' + index + '" name="extra-image-title" size="20" /> 图片 <input type="text" id="extra-image-' + extra + '-' + index + '" name="extra-image-image" size="20" />[<a href="javascript:;" onclick="extraImageSelect(this)">选择</a>]<br /><textarea id="extra-image-intro-' + index + '" name="extra-image-intro" rows="2"></textarea> <a href="javascript:;" onclick="extraImageDele(this)">删除</a> <a href="javascript:;" onclick="extraImageAdd(this)">添加</a></div>';
        $(o).parent().parent().append(str);
        return true;
    }
    // 添加扩展
    function extraAdd(type, objValue) {
        // 判断SEO、标题
        var haveSeo = false;
        var haveTitle = false;
        if ($("#extraBox").children("tr").length != 0) {
            $("#extraBox").children("tr").each(function() {
                var val = $(this).attr("type");
                if (type == 9 && val == 9) {
                    haveSeo = true;
                }
                var tit = $(this).children("th").children("div").text();
                if (tit == objValue.title) {
                    haveTitle = true;
                }
            });
            if (haveSeo) {
                alert("搜索优化信息只能添加一组");
                return false;
            }
            if (haveTitle) {
                alert(objValue.title + "已经存在！");
                return false;
            }
        }
        // 计算索引
        var index = 0;
        if ($("#extraBox").children("tr").length != 0) {
            index = parseInt($("#extraBox tr").last().attr("index")) + 1;
        }

        if (objValue.iswap == "true" || objValue.iswap == true)
            objValue.iswap = 1;
        else
            objValue.iswap = 0;

        var str = '<tr type="' + type + '" index="' + index + '"><th class="w120">';
        str += '<div>' + objValue.title + '</div>';
        str += 'Wap:' + (objValue.iswap == 1 ? 'Yes' : 'No') + ' ';
        str += '<a href="javascript:;" onclick="extraDele(this)">删除</a></th><td>';
        str += '<input type="hidden" name="extraId" id="extraId-' + index + '" value="' + objValue.id + '" />';
        str += '<input type="hidden" name="extraType" id="extraType-' + index + '" value="' + type + '" />';
        str += '<input type="hidden" name="extraIswap" id="extraIswap-' + index + '" value="' + objValue.iswap + '" />';
        str += '<input type="hidden" name="extraTitle" id="extraTitle_' + index + '" value="' + objValue.title + '" />';
        if (type == 1) {
            str += '<textarea name="extra-editor" id="extra-editor-' + index + '">' + objValue.value + '</textarea><textarea name="extraContent" id="extraContent_' + index + '" style="display: none; "></textarea></td></tr>'; // ' + objValue.value + '
            $("#extraBox").append(str);

            // ckeditor
            var oFcKeditor = new FCKeditor('extra-editor-' + index);
            oFcKeditor.Config['ToolbarStartExpanded'] = false;
            oFcKeditor.BasePath = "<%=Config.SitePath %>ckeditor/";
            oFcKeditor.Height = 80;
            oFcKeditor.ToolbarSet = 'Desc';
            oFcKeditor.ReplaceTextarea();
        } else if (type == 2 || type == 3) {
            str += '<textarea style="display: none;" name="extraContent" id="extraContent-' + index + '">' + objValue.value + '</textarea>';
            var list = eval(objValue.value);
            for (var j = 0; j < list.length; j++) {
                str += '<div class="extra-list-item" index="' + j + '">标题 <input type="text" id="extra-image-title-' + j + '" name="extra-image-title" size="30" value="' + list[j].title + '" /> 图片 <input type="text" id="extra-image-' + index + '-' + j + '" name="extra-image-image" size="40" value="' + list[j].image + '" />[<a href="javascript:;" onclick="extraImageSelect(this)">选择</a>]<br /><textarea id="extra-image-intro-' + j + '" name="extra-image-intro" rows="2">' + list[j].intro + '</textarea> <a href="javascript:;" onclick="extraImageDele(this)">删除</a> <a href="javascript:;" onclick="extraImageAdd(this)">添加</a></div>';
            }
            str += '</td></tr>';
            $("#extraBox").append(str);
        } else {
            str += '<textarea style="display: none;" name="extraContent" id="extraContent-' + index + '">' + objValue.value + '</textarea>';
            var oSeo = eval("(" + objValue.value + ")");
            str += '<div class="extra-list-item">标题 <input type="text" id="extra-seo-title" name="extra-seo-title" size="20" value="' + oSeo.title + '" /> 关键字 <input type="text" id="extra-seo-keyword" name="extra-seo-keyword" size="20" value="' + oSeo.keyword + '" /><textarea id="extra-seo-desc" name="extra-seo-desc" rows="2">' + oSeo.desc + '</textarea></div></td></tr>';
            $("#extraBox").append(str);
        }
        return true;
    }
    // 检测扩展
    function extra() {
        if ($("#extraBox").children("tr").length == 0)
            return true;

        var b = true;
        $("#extraBox").children("tr").each(function() {
            var type = $(this).attr("type");
            var value = "";
            // 整理文本
            if (type == 1) {
                if ($(this).find("textarea[name=extra-editor]").val() == "") {
                    alert("请填写“" + $(this).find("th div").html() + "”的内容！");
                    b = false;
                    return false;
                }
                //$(this).find("textarea[name=extraContent]").val($(this).find("textarea[name=extra-editor]").val());
                value = $(this).find("textarea[name=extra-editor]").val();
            }
            if (type == 2 || type == 3) {
                var tits = "";
                var imgs = "";
                var ints = "";
                $(this).find("input[name=extra-image-title]").each(function() {
                    tits += (tits == "" ? "" : ",") + $(this).val();
                });
                $(this).find("input[name=extra-image-image]").each(function() {
                    imgs += (imgs == "" ? "" : ",") + $(this).val();
                });
                $(this).find("textarea[name=extra-image-intro]").each(function() {
                    ints += (ints == "" ? "" : ",") + $(this).val();
                });
                var arr1 = tits.split(","); var arr2 = imgs.split(","); var arr3 = ints.split(",");
                var content = "";
                for (var j = 0; j < arr1.length; j++) {
                    if (arr1[j] == "" || arr2[j] == "") {
                        alert("请将“" + $(this).find("th div").html() + "”的内容填写完整！");
                        b = false;
                        break;
                    }
                    content += (content == "" ? "" : ",") + "{title:'" + arr1[j] + "',image:'" + arr2[j] + "',intro:'" + (typeof (arr3[j]) == "undefined" ? "" : arr3[j]) + "'}";
                }
                value = "[" + content + "]";
            }
            if (type == 9) {
                var tt = $(this).find("input[name=extra-seo-title]").val();
                var kw = $(this).find("input[name=extra-seo-keyword]").val();
                var des = $(this).find("textarea[name=extra-seo-desc]").val();
                if (tt == "" || kw == "" || des == "") {
                    alert("请完整填写搜索优化信息");
                    b = false;
                    return false;
                }
                // $(this).find("textarea[name=extra-content]").val("{title:'" + tt + "',keyword:'" + kw + "',description:'" + des + "'}");
                value = "{title:'" + tt + "',keyword:'" + kw + "',description:'" + des + "'}";
            }
            value = value.replace(/,/g, "|");
            $(this).find("textarea[name=extraContent]").val(value);
            //alert($(this).find("textarea[name=extra-content]").val());
        });
        return b;
    }
    //#endregion 扩展

    //#region 专题
    var jsonTopic = null;
    // Topic Row // 专题行
    function topicRow(o) {
        var count = $("#topicBox").children("tr").length + 1;
        var str = '<tr><td><select id="topicId" name="topicId" size="1"><option value="0">请选择</option>';
        if (jsonTopic != null) {
            for (var j = 0; j < jsonTopic.length; j++) {
                str += '<option value="' + jsonTopic[j].id + '"' + (o.topic_id == jsonTopic[j].id ? ' selected="selected"' : '') + '>' + jsonTopic[j].name + '</option>';
            }
        }
        str += '</select></td><td><input type="text" id="topicTitle_' + count + '" name="topicTitle" class="pw" value="' + o.title + '" /></td><td><input type="text" id="topicCover_' + count + '" name="topicCover" size="20" value="' + o.cover + '" /> <a href="javascript:;" onclick="imageSel(this)">选择</a></td><td><input type="text" id="topicIntro_' + count + '" name="topicIntro" class="pw" value="' + o.intro + '" /></td><td><input type="text" id="topicSort_' + count + '" name="topicSort" size="2" value="' + o.sort + '" /></td><td class="center"><a href="javascript:;" onclick="topicDele(this)">XXX</a></td></tr>';
        return str;
    }
    $("#topicAdd").click(function() {
        $("#topicBox").append(topicRow({ topic_id: 0, title: $("#name").val(), cover: $("#imageview").children("img").attr("src"), intro: $("#intro").val(), sort: $("#sort").val() }));
    });
    function topicDele(o) {
        var obj = $(o);
        if (GoodsId == 0) {
            obj.parent().parent().remove();
            return;
        } else {
            var id = obj.parent().parent().find("select[name=topicId]").val();
            $.get("Action.aspx?action=goodstopicdelete&id=" + GoodsId + "&topic=" + id + "&" + new Date(), function(data) {
                if (data == "-1") {
                    alert("参数错误！");
                    return;
                }
                if (data == "0") {
                    alert("删除失败！");
                    return;
                }
            });
        }
        obj.parent().parent().remove();
    }
    // 提交验证专题输入完整性
    function topicField(obj) {
        if (obj.val() == 0 || obj.val() == "") {
            obj.focus().css("background", "#ff0000");
            return false;
        } else {
            obj.css("background", "");
            return true;
        }
    }
    function topic() {
        var obj = $("#topicBox").children("tr");
        if (obj.length == 0) {
            return true;
        }
        var t = true;
        obj.each(function() {
            if (!topicField($(this).find("select"))) {
                t = false;
                return false;
            }
            obj.find("input").each(function() {
                if (!topicField($(0))) {
                    t = false;
                    return false;
                }
            });
            t = true;
            return t;
        });
        return t;
    }
    //#endregion 相关专题

    //#region 属性
    // bindAttr
    function bindAttr(cateid) {
        $.get("Action.aspx?action=getattrsbycategoryid&id=" + cateid + "&" + new Date(), function(data) {
            if (data == "null") {
                $("#attrBox").html("");
            } else {
                var list = eval(data);
                var str = "";
                for (var j = 0; j < list.length; j++) {
                    str += '<tr type="' + list[j].type + '">';
                    str += '<th class="w120"><label><input type="checkbox" id="sel_' + list[j].id + '" name="sel" value="1" />' + list[j].name + '：</label></th>';
                    str += '<td><input type="hidden" id="attrId_' + list[j].id + '" name="attrId" value="' + list[j].id + '" />';
                    str += '<input type="hidden" id="attrValue_' + list[j].id + '" name="attrValue" value="" />';
                    str += '<input type="hidden" id="attrEnable_' + list[j].id + '" name="attrEnable" value="1" />';
                    var val = list[j].values;
                    var k;
                    if (list[j].type == 1) {
                        // 单选
                        for (k = 0; k < val.length; k++) {
                            str += '<input type="radio" id="attr-value-' + list[j].id + '_' + val[k].id + '" name="attr-value-' + list[j].id + '" value="' + val[k].id + '" /><label for="attr-value-' + list[j].id + '_' + val[k].id + '">' + val[k].value + '</label>&nbsp;';
                        }
                    } else if (list[j].type == 2) {
                        // 复选
                        for (k = 0; k < val.length; k++) {
                            str += '<input type="checkbox" id="attr-value-' + list[j].id + '_' + val[k].id + '" name="attr-value-' + list[j].id + '" value="' + val[k].id + '" /><label for="attr-value-' + list[j].id + '_' + val[k].id + '">' + val[k].value + '</label>&nbsp;';
                        }
                    } else if (list[j].type == 3) {
                        // 下拉
                        str += '<select size="1" id="attr-value-' + list[j].id + '" name="attr-value-' + list[j].id + '">';
                        for (k = 0; k < val.length; k++) {
                            str += '<option value="' + val[k].id + '">' + val[k].value + '</option>';
                        }
                        str += '</select>';
                    }
                    str += '</td></tr>';
                }
                $("#attrBox").append(str);
            }
        });
    }
    // 属性检测
    function attr() {
        if ($("#attrBox").children("tr").length == 0)
            return true;

        var b = true;
        $("#attrBox").children("tr").each(function() {
            $(this).find("td input[name=attrEnable]").val($(this).find("th label input[type=checkbox]").attr("checked") ? "1" : "0");
            if ($(this).find("td input[name=attrEnable]").val() == "1") {
                var type = $(this).attr("type");
                var str = "";
                if (type == "1") {
                    if ($(this).find("td input[type=radio]:checked").length == 0) {
                        b = false;
                        $(this).css("background", "#FF0000");
                        return false;
                    } else {
                        str = $(this).find("td input[type=radio]:checked").val();
                        $(this).css("background", "");
                    }
                } else if (type == "2") {
                    if ($(this).find("td input[type=checkbox]:checked").length == 0) {
                        b = false;
                        $(this).css("background", "#FF0000");
                        return false;
                    } else {
                        $(this).find("td input[type=checkbox]:checked").each(function() {
                            str += (str == "" ? "" : ",") + $(this).val();
                        });
                        $(this).css("background", "");
                    }
                } else if (type == "3") {
                    str = $(this).find("td select").val();
                    if (str == "" || str == "0") {
                        b = false;
                        $(this).css("background", "#FF0000");
                        return false;
                    }
                    $(this).css("background", "");
                }
                //alert(str);
                $(this).find("td input[name=attrValue]").val(str.replace(/,/g, "|"));
            }
        });
        return b;
    }
    //#endregion 属性

    //    //#region 活动
    //    // Promote Row
    //    function promoteRow(o) {
    //        var count = $("#promoteBox").children("tr").length + 1;
    //        return '<tr><td>' + o.id + '<input type="hidden" id="promoteId_' + count + '" name="promoteId" value="' + o.id + '" /></td><td>' + o.name + '</td><td class="center"><a href="javascript:;" onclick="promoteDele(this)" title="删除记录">XXX</a></td></tr>';
    //    }
    //    function promoteDele(o) {
    //        var obj = $(o);
    //        if (GoodsId == 0) {
    //            obj.parent().parent().remove();
    //            return;
    //        } else {
    //            var id = obj.parent().parent().find("input[type=hidden]").val();
    //            $.get("Action.aspx?action=goodspromotedelete&id=" + GoodsId + "&promote=" + id + "&" + new Date(), function(data) {
    //                if (data == "-1") {
    //                    alert("参数错误！");
    //                    return;
    //                }
    //                if (data == "0") {
    //                    alert("删除失败！");
    //                    return;
    //                }
    //            });
    //        }
    //        obj.parent().parent().remove();
    //    }
    //    function promoteSel(s) {
    //        $.get("Action.aspx?action=getpromote&id=" + s + "&" + new Date(), function(data) {
    //            if (data == "0") {
    //                alert("有错误发生！");
    //                return false;
    //            }
    //            if (data == "null") {
    //                alert("没有选择任何活动！");
    //                return false;
    //            }
    //            var obj = eval(data);
    //            for (var j = 0; j < obj.length; j++) {
    //                $("#promoteBox").append(promoteRow(obj[j]));
    //            }
    //            return true;
    //        });
    //    }
    //    var aBox = $("#promoteAdd").wBox({
    //        title: "选择活动",
    //        requestType: "iframe",
    //        iframeWH: { width: 400, height: 300, resize: false },
    //        target: "AjaxPromote.aspx"
    //    });
    //    //#endregion

    //#region 赠品
    //Gift Row
    function giftRow(o) {
        var count = $("#giftBox").children("tr").length + 1;
        return '<tr><td>' + o.id + '<input type="hidden" id="giftId_' + count + '" name="giftId" value="' + o.id + '" /></td><td>' + o.name + '</td><td class="center"><a href="javascript:;" onclick="giftDele(this)" title="删除记录">XXX</a></td></tr>';
    }
    function giftDele(o) {
        var obj = $(o);
        if (GoodsId == 0) {
            obj.parent().parent().remove();
            return;
        } else {
            var id = obj.parent().parent().find("input[type=hidden]").val();
            $.get("Action.aspx?action=goodsgiftdelete&id=" + GoodsId + "&gift=" + id + "&" + new Date(), function(data) {
                if (data == "-1") {
                    alert("参数错误！");
                    return;
                }
                if (data == "0") {
                    alert("删除失败！");
                    return;
                }
            });
        }
        obj.parent().parent().remove();
    }
    function giftSel(s) {
        $.get("Action.aspx?action=getgift&id=" + s + "&" + new Date(), function(data) {
            if (data == "0") {
                alert("有错误发生！");
                return false;
            }
            if (data == "null") {
                alert("没有选择任何文章！");
                return false;
            }
            var obj = eval(data);
            for (var j = 0; j < obj.length; j++) {
                $("#giftBox").append(giftRow(obj[j]));
            }
            return true;
        });
    }
    var aBox = $("#giftAdd").wBox({
        title: "选择赠品",
        requestType: "iframe",
        iframeWH: { width: 400, height: 300, resize: false },
        target: "AjaxGift.aspx"
    });
    //#endregion

    //#region 文章
    // Article Row
    function artiRow(o) {
        var count = $("#artiBox").children("tr").length + 1;
        return '<tr><td>' + o.id + '<input type="hidden" id="artiId_' + count + '" name="artiId" value="' + o.id + '" /></td><td>' + o.title + '(<i>' + o.alias + '</i>)</td><td class="center"><a href="javascript:;" onclick="artiDele(this)" title="删除记录">XXX</a></td></tr>';
    }
    function artiDele(o) {
        var obj = $(o);
        if (GoodsId == 0) {
            obj.parent().parent().remove();
            return;
        } else {
            var id = obj.parent().parent().find("input[type=hidden]").val();
            $.get("Action.aspx?action=goodsarticledelete&id=" + GoodsId + "&arti=" + id + "&" + new Date(), function(data) {
                if (data == "-1") {
                    alert("参数错误！");
                    return;
                }
                if (data == "0") {
                    alert("删除失败！");
                    return;
                }
            });
        }
        obj.parent().parent().remove();
    }
    function artiSel(s) {
        $.get("Action.aspx?action=getarticle&id=" + s + "&" + new Date(), function(data) {
            if (data == "0") {
                alert("有错误发生！");
                return false;
            }
            if (data == "null") {
                alert("没有选择任何文章！");
                return false;
            }
            var obj = eval(data);
            for (var j = 0; j < obj.length; j++) {
                $("#artiBox").append(artiRow(obj[j]));
            }
            return true;
        });
    }
    var aBox = $("#artiAdd").wBox({
        title: "选择文章",
        requestType: "iframe",
        iframeWH: { width: 400, height: 300, resize: false },
        target: "AjaxArticle.aspx"
    });
    //#endregion 相关文章

    //#region 相关产品
    function prodDele(o) {
        var obj = $(o);
        if (GoodsId == 0) {
            obj.parent().parent().remove();
            return;
        } else {
            var id = obj.parent().parent().find("input[type=hidden]").val();
            $.get("Action.aspx?action=goodsproddelete&id=" + GoodsId + "&prod=" + id + "&" + new Date(), function(data) {
                if (data == "-1") {
                    alert("参数错误！");
                    return;
                }
                if (data == "0") {
                    alert("删除失败！");
                    return;
                }
            });
        }
        obj.parent().parent().remove();
    }
    function prodRow(o) {
        var count = $("#prodBox").children("tr").length + 1;
        return '<tr><td>' + o.id + '<input type="hidden" id="prodId_' + count + '" name="prodId" value="' + o.id + '" /></td><td>' + o.name + '<a href="' + o.cover + '" target="_blank"><img src="../images/image.png" alt="" /></a></td><td class="center"><a href="javascript:;" onclick="prodDele(this)" title="删除记录">XXX</a></td></tr>';
    }
    var aBox = $("#prodAdd").wBox({
        title: "选择相关产品",
        requestType: "iframe",
        iframeWH: { width: 400, height: 300, resize: false },
        target: "AjaxGoods.aspx"
    });
    function prodSel(s) {
        $.get("Action.aspx?action=getgoods&id=" + s + "&" + new Date(), function(data) {
            if (data == "0") {
                alert("有错误发生！");
                return false;
            }
            if (data == "null") {
                alert("没有选择任何产品！");
                return false;
            }
            var obj = eval(data);
            for (var j = 0; j < obj.length; j++) {
                $("#prodBox").append(prodRow(obj[j]));
            }
            return true;
        });
    }
    //#endregion

    //#region 验证正版必须选择品牌
    $("#lince").click(function() {
        var brandId = $("#brand").val();
        if (isNaN(brandId))
            brandId = 0;
        if ($(this).attr("checked") && parseInt(brandId) < 1) {
            alert("请先选择品牌！");
            $(this).attr("checked", false);
        }
        return true;
    });
    //#endregion

    //#region Tag验证
    $("#keys").blur(function() {
        tag("keys");
    });
    // Tag检测
    function getArrTags(tags) {
        try {
            if (!tags) { return new Array(); }
            tags = tags.replace(/(\s+)|(　)|(；)|(：)|(，)|(、)|(,)|(;)|(:)|(，)|(。)/g, " ");
            tags = $.trim(tags);
            tags = tags.split(" ");

            // 删除重复
            var newArray = [];
            var provisionalTable = {};
            for (var i = 0, item; (item = tags[i]) != null; i++) {
                if (!provisionalTable[item]) {
                    newArray.push(item);
                    provisionalTable[item] = true;
                }
            }
            return newArray;
        } catch (e) {
            alert("err: " + e);
        }
        return null;
    }
    function tag(id) {
        var obj = $("#" + id);
        var txt = obj.val();  // .toLowerCase().trim()
        if (txt == "") {
            obj.css("background", "#ff0000");
            return false;
        }

        var len = strLen(txt);
        if (len === 0) {
            obj.focus().css("background", "#ff0000");
            return false;
        }
        var tags = getArrTags(txt);
        if (tags == null) {
            obj.val("").focus().css("background", "#ff0000");
            return false;
        }

        var tn = 0;
        for (var i = 0; i < tags.length; i++) {
            if (!isNull(tags[i]) > 0) {
                if (tags[i].length < 2) {
                    alert("单个标签最少2个字");
                    obj.focus().css("background", "#ff0000");
                    return false;
                }
                if (strLen(tags[i]) > 12) {
                    alert("单个标签长度过长");
                    obj.focus().css("background", "#ff0000");
                    return false;
                }
                tn++;
            }
        }
        if (tn > 6) {
            alert("标签个数最多6个");
            return false;
        }
        obj.val(tags.join(","));
        try {
        } catch (e) {
            alert("err: " + e);
        }
        obj.css("background", "");
        return true;
    }
    //#endregion

    function loadSpecValue(vid) {
        $('#value_' + vid + '').attr("checked", true);
        var valueId = $('#value_' + vid + '').attr("value");
        var text = $('#value_' + vid + '').attr("text");
        var color = $('#value_' + vid + '').attr("color");
        if (color == 1) {
            if ($('#value_' + vid + '').attr("checked")) {
                // 添加绑定图片控件
                var s = $('#value_' + vid + '').parent().children("div").html();
                s += '<div id="valueimg_' + valueId + '" class="valueimg"><span class="imgs"></span> ' + text + ' ';
                s += '<a href="javascript:;" onclick="openBind(' + valueId + ');">绑定图片</a>';
                s += '<input type="hidden" id="valueImage_' + valueId + '" name="valueImage" value="" />';
                s += '</div>';
                $('#value_' + vid + '').parent().children("div").html(s);
            } else {
                // 移除绑定图片控件
                $("#valueimg_" + valueId).remove();
            }
        }

        var str = "";
        $("#specBox").children("tr").each(function() {
            //var specId = $(this).attr("specid");
            var str2 = "";
            $(this).find("td input[name=valueId]:checked").each(function() {
                str2 += (str2 == "" ? "" : ",") + $(this).attr("value");
            });

            if (str == "") {
                str += str2;
            } else {
                if (str2 != "") {
                    str += "|" + str2;
                }
            }
        });

        if (str == "") {
            return;
        }

        // 组合结果
        var arr = str.split("|");
        if (arr.length == 1) {
            createItem(str);
        } else {
            var t = "";
            var t1 = arr[0].split(",");
            var t2 = arr[1].split(",");
            for (var j = 0; j < t1.length; j++) {
                for (var k = 0; k < t2.length; k++) {
                    t += (t == "" ? "" : ",") + t1[j] + "|" + t2[k];
                }
            }
            createItem(t);
        }
        return;
    }

    $(document).ready(function() {
        // 加载上传的文件
        getSWFUpload();

        //#region edit-load // 修改商品时加载
        if (GoodsId != 0) {
            var categoryId = $("#category").val();
            // 绑定属性
            bindAttr(categoryId);
            // 商品图片
            $.get("Action.aspx?action=loadimages&id=" + GoodsId + "&" + new Date(), function(data) {
                if (data != "0" && data != "-" && data != "null") {
                    $("#images").val(data);
                    var json = eval(data);
                    var str = '';
                    for (var j = 0; j < json.length; j++) {
                        str += '<img alt="view[' + j + ']" onclick="imageSetDefault(this)" src="' + json[j].path + '50/' + json[j].filename + json[j].fileext + '" />';
                        if (json[j].iscover == 1) {
                            $("#cover").val(json[j].id);
                            $("#imageview").html('<img alt="view" src="' + json[j].path + '200/' + json[j].filename + json[j].fileext + '" />');
                        }
                    }
                    $("#imagelist").html(str);
                }
            });

            //#region 加载规格
            $.get("Action.aspx?action=loadspecs&id=" + GoodsId + "&" + new Date(), function(data) {
                var j = 0;
                //alert(data);
                if (data != "0" && data != "-" && data != "null") {
                    // 加载规格
                    var spec = eval(data);
                    var str = "";
                    for (j = 0; j < spec.length; j++) {
                        str += specRow(spec[j]);
                    }
                    $("#nospec").css("display", "none");
                    $("#specBox").css("display", "").html(str);
                    $("#specCtrl").text("取消规格");
                    // 开始选择规格值
                    $.get("Action.aspx?action=loaditems&id=" + GoodsId + "&" + new Date(), function(data1) {
                        if (data1 != "0" && data1 != "-" && data1 != "null") {
                            var json = eval(data1);
                            var idlist = ",";
                            for (j = 0; j < json.length; j++) {
                                //alert("color: "+idlist.indexOf("," + json[j].color_id + ","));
                                if (idlist.indexOf("," + json[j].color_id + ",") == -1) {
                                    idlist += json[j].color_id + ",";
                                    // 选择颜色值
                                    loadSpecValue(json[j].color_id);

                                    var colorId = json[j].color_id;
                                    // 加载颜色类值绑定的图片
                                    $.ajax({
                                        url: "Action.aspx?action=loadvalueimages&id=" + GoodsId + "&vid=" + colorId + "&" + new Date(),
                                        async: false, // 必须使用同步操作
                                        success: function(data2) {
                                            if (data2 != "0" && data2 != "-" && data2 != "null") {
                                                var imgs = eval(data2);
                                                var ids = "", html = "", iswap = 0;
                                                for (var k = 0; k < imgs.length; k++) {
                                                    if (("," + ids + ",").indexOf("," + imgs[k].image_id + ",") == -1) {
                                                        ids += (ids == "" ? "" : "|") + imgs[k].image_id;

                                                        if (html != "") html += " ";
                                                        if (imgs[k].iswap) {
                                                            html += '<img src="' + imgs[k].path + '50/' + imgs[k].filename + imgs[k].fileext + '" alt="WAP" imgid="' + imgs[k].image_id + '" class="wap" onclick="selwap(this)" />';
                                                            iswap = imgs[k].image_id;
                                                        } else {
                                                            html += '<img src="' + imgs[k].path + '50/' + imgs[k].filename + imgs[k].fileext + '" alt="WAP" imgid="' + imgs[k].image_id + '" onclick="selwap(this)" />';
                                                        }
                                                    }
                                                }
                                                $("#valueimg_" + colorId).children("input[name=valueImage]").val(colorId + "#" + iswap + "#" + ids);
                                                $("#valueimg_" + colorId).children("span").html(html);
                                                //alert(colorId + "#" + iswap + "#" + ids + "#" + html);
                                            }
                                        }
                                    });
                                }
                                // showTips(json[j].color);
                                // 选择尺寸值
                                //alert("size: " + idlist.indexOf("," + json[j].size_id + ","));
                                if (idlist.indexOf("," + json[j].size_id + ",") == -1) {
                                    idlist += json[j].size_id + ",";
                                    if (json[j].size_id != "0") {
                                        loadSpecValue(json[j].size_id);
                                        //showTips(json[j].size);
                                    }
                                }
                            }
                            // 绑定Item List
                            var itemhtml = "";
                            for (j = 0; j < json.length; j++) {
                                // [id], [color_id], [color], [size_id], [size], [itemno], [store], [market], [price], [cost], [weight], [hits], [trades]
                                itemhtml += "<tr>";
                                itemhtml += '<td class="center">' + json[j].color + '</td>';
                                if (json[j].size_id != 0) itemhtml += '<td class="center">' + json[j].size + '</td>';
                                itemhtml += '<td><input name="itemNo" readonly="readonly" type="text" size="12" id="itemNo_' + j + '" value="' + json[j].itemno + '" /></td>';
                                itemhtml += '<td><input name="itemStore" type="text" size="5" id="itemStore_' + j + '" value="' + json[j].store + '" /></td>';
                                itemhtml += '<td><input name="itemMarket" type="text" size="5" id="itemMarket_' + j + '" value="' + json[j].market + '" /></td>';
                                itemhtml += '<td><input name="itemPrice" type="text" size="5" id="itemPrice_' + j + '" value="' + json[j].price + '" /></td>';
                                itemhtml += '<td><input name="itemCost" type="text" size="5" id="itemCost_' + j + '" value="' + json[j].cost + '" /></td>';
                                itemhtml += '<td><input name="itemWeight" type="text" size="5" id="itemWeight_' + j + '" value="' + json[j].weight + '" />(g)</td>';
                                itemhtml += '<td><input type="hidden" id="itemId_' + j + '" name="itemId" value="' + json[j].id + '" />';
                                itemhtml += '<input type="hidden" value="' + json[j].color_id + '" id="itemColor_' + j + '" name="itemColor" />';
                                itemhtml += '<input type="hidden" value="' + json[j].color + '" id="itemColorName_' + j + '" name="itemColorName" />';
                                itemhtml += '<input type="hidden" value="' + json[j].size_id + '" id="itemSize_' + j + '" name="itemSize" />';
                                itemhtml += '<input type="hidden" value="' + json[j].size + '" id="itemSizeName_' + j + '" name="itemSizeName" />';
                                itemhtml += '<img src="../images/delete.gif" onclick="deleteItem(this)"></td>';
                                itemhtml += "</tr>";
                            }
                            $("#itemBox tbody").html(itemhtml); // 清空
                        }
                    });
                    // 绑定颜色选择的图片

                    // 绑定Item内容
                }
            });
            //#endregion

            //#region 填充属性表单
            $.get("Action.aspx?action=loadgoodsvalues&id=" + GoodsId + "&" + new Date(), function(data) {
                if (data != "0" && data != "-" && data != "null") {
                    // alert(data);
                    var json = eval(data);
                    for (var j = 0; j < json.length; j++) {
                        //$('#attrValue_' + json[j].attr_id + '').val(json[j].value_id);
                        if (json[j].type == 1 || json[j].type == 2) {
                            $('#attr-value-' + json[j].attr_id + '_' + json[j].value_id + '').attr("checked", true);
                        }
                        //alert($('#attr-value-' + json[j].attr_id + '_' + json[j].value_id + '').attr("checked"));
                        if (json[j].type == 3) {
                            $('#attr-value-' + json[j].attr_id + '').val(json[j].value_id);
                        }
                        $('#sel_' + json[j].attr_id + '').attr("checked", true);
                    }
                }
            });
            //#endregion

            //#region 相关
            //            // 参加活动
            //            $.get("Action.aspx?action=goodspromote&id=" + GoodsId + "&" + new Date(), function(data) {
            //                if (data != "0" && data != "-" && data != "null") {
            //                    var d = eval(data);
            //                    for (var j = 0; j < d.length; j++) {
            //                        $("#promoteBox").append(giftRow(d[j]));
            //                    }
            //                }
            //            });
            // 赠送礼品
            $.get("Action.aspx?action=goodsgift&id=" + GoodsId + "&" + new Date(), function(data) {
                if (data != "0" && data != "-" && data != "null") {
                    var d = eval(data);
                    for (var j = 0; j < d.length; j++) {
                        $("#giftBox").append(giftRow(d[j]));
                    }
                }
            });
            // 商品相关专题
            $.get("Action.aspx?action=goodstopic&id=" + GoodsId + "&" + new Date(), function(data) {
                if (data != "0" && data != "-" && data != "null") {
                    var d = eval(data);
                    for (var j = 0; j < d.length; j++) {
                        $("#topicBox").append(topicRow(d[j]));
                    }
                }
            });
            // 商品相关文章
            $.get("Action.aspx?action=goodsarticle&id=" + GoodsId + "&" + new Date(), function(data) {
                if (data != "0" && data != "-" && data != "null") {
                    var obj = eval(data);
                    for (var j = 0; j < obj.length; j++) {
                        $("#artiBox").append(artiRow(obj[j]));
                    }
                }
            });
            // 商品相关商品
            $.get("Action.aspx?action=goodsprod&id=" + GoodsId + "&" + new Date(), function(data) {
                if (data != "0" && data != "-" && data != "null") {
                    var obj = eval(data);
                    for (var j = 0; j < obj.length; j++) {
                        $("#prodBox").append(prodRow(obj[j]));
                    }
                }
            });
            //#endregion

            //#region 加载Extra
            $.get("Action.aspx?action=loadextras&id=" + GoodsId + "&" + new Date(), function(data) {
                if (data != "0" && data != "-" && data != "null") {
                    //alert(data);
                    var json = eval(data);
                    for (var j = 0; j < json.length; j++) {
                        // extraAdd(extType, { id: 0, title: extTitle, iswap: isWap, value: "[{ title:'', image:'', intro:'' }]" });
                        extraAdd(json[j].type, { id: json[j].id, title: json[j].title, iswap: json[j].iswap, value: json[j].content });
                    }
                }
            });
            //#endregion
        }
        //#endregion
    });
    //-->
</script>
<script type="text/javascript">
    //#region 表单验证
    function chkForm() {
        setTimeout("chkPost()", 50);
        return false;
    }
    // 通用判断字段是否填写或选择
    function chkField(id) {
        var obj = $("#" + id);
        if (obj.val() == 0 || obj.val() == "") {
            obj.focus().css("background", "#ff0000");
            return false;
        } else {
            obj.css("background", "");
            return true;
        }
    }
    function chkPost() {
        // 基本信息验证
        if (!chkField("category") || !chkField("model") || !chkField("serialid") || !chkField("name") || !chkField("alias") || !tag("keys") || !chkField("intro") || !chkField("comment") || !chkField("unit")) {
            return;
        }
        if (!extra() || !attr() || !topic()) {
            return;
        }
        document.getElementById("form1").submit();
    }
    //#endregion
</script>
</body>
</html>
