/* File Created: 三月 23, 2012 */

//设置运费承担
function SetFreight() {
    $("input[name='freight']").bind("click", function () {
        if ($("#buyer").attr("checked")) {

            $("#freightbuyer").show();
        } else {
            $("#freightbuyer").hide();
        }
    });
}

function RestoreFreight() {
    if ($("#lbFreightPayer").text() == 'buyer') {

        $("#buyer").attr("checked", true);
        $("#buyer").trigger("click");
    }
    else {
       $("#seller").attr("checked", true);
        $("#seller").trigger("click");
    }
}


function GetCidPathAjax(cid) {
    var Cidpath = '';
    $.ajax({
        type: "POST",
        url: "TaoBaoDataHandler.ashx?type=GetCidPath",
        data: { "Cid": cid },
        dataType: "json",
        async: false,
        success: function (data) {
            Cidpath = data.success_response.CidPath;
        }
    });
    return Cidpath;
}

 
  


//从选择的淘宝CSV商品中还原属性值
function CollectCsvFormSku(parent) {
    var inputs = $(parent).nextAll();
    $("#lbcids").text(GetCidPathAjax($(inputs[0]).val()));
    $("#txttitle").val($(inputs[1]).val());
    $("#txtnum").html($(inputs[2]).val());
    $("#txtprice").html($(inputs[3]).val());
    $("#lbinputstrs").text($(inputs[4]).val());
    $("#lbinputpids").text($(inputs[5]).val());
    if ($(inputs[6]).val() == '1') {
        $("#invoiceY").attr("checked", true);
    }
    else {
        $("#invoiceN").attr("checked", true);
    }
    if ($(inputs[7]).val() == '1') {
        $("#warrantyY").attr("checked", true);
    }
    else {
        $("#warrantyN").attr("checked", true);
    }
    if ($(inputs[8]).val() == 'buyer') {
       $("#buyer").attr("checked", true);
        $("#buyer").trigger("click");
        $("#txtems_fee").val($(inputs[9]).val())
        $("#txtpost_fee").val($(inputs[10]).val());
        $("#txtexpress_fee").val($(inputs[11]).val());
    }
    else {
       $("#seller").attr("checked", true);
       $("#seller").trigger("click");
       $("#buyer").trigger("click");
       $("#txtems_fee").val("0.00");
       $("#txtpost_fee").val("0.00");
       $("#txtexpress_fee").val("0.00");
    }
    $("#lbskuouterids").text($(inputs[12]).val());
    $("#lbskuquantities").text($(inputs[13]).val());
    $("#lbskuprice").text($(inputs[14]).val());
    $("#lbskuprop").text($(inputs[15]).val());
    $("#lbProps").text($(inputs[16]).val());
    RestoreCidSelect();
}

function ImporterCsv() {

    $.jBox("iframe:ImporterTaoBaoCSV.aspx", {
        title: "导入淘宝CSV",
        width: 600,
        height: 220,
        buttons: { '关闭': true }
    });
    // $("[name='jbox-iframe']").bind("load", function () { $(this).height($(this).contents().find(body).height() + 40) });
}
function ExportersCsv() {
    $.jBox("iframe:TaoBaoCsv.aspx", {
        title: "关联淘宝CSV",
        width: 800,
        height: 560,
        top:'3%',
        buttons: { '确定': 1, '取消': 0 },
        submit: function (v, h, f) {
            if (v == 1) {
                var checkradio = $("[name='jbox-iframe']").contents().find("input[name='tbcsv']:checked");
                if (checkradio.length <= 0) {
                    $.jBox.tip("请先选择需要关联的淘宝商品", 'error');
                    return false;
                }
                else {
                    CollectCsvFormSku(checkradio);
                    $.jBox.tip("正在关联数据", 'loading');
                    // 模拟2秒后完成操作
                    window.setTimeout(function () { $.jBox.tip('已关联成功。', 'success'); }, 2000);
                }


            }
        }
    });
    //$("[name='jbox-iframe']").bind("load", function () { $(this).height($(this).contents().find("#csvbody").height()+40) });
}






function RestoreCidSelect() {
    var cids = $("#lbcids").text();
    var cidarr = cids.split(',');

    if(cidarr[0]=="父类到子类列表"){
        cidarr[0]="-1";
    }
    $("#pid_0").val(cidarr[0]);
    $("#pid_0").trigger("change");
    for (i = 0; i + 1 < cidarr.length; i++) {
        $("#pid_" + cidarr[i]).val(cidarr[i + 1]);
        $("#pid_" + cidarr[i]).trigger("change");
    }
}

//查找所有的SELECT和CHECKBOX赋上从API取得的商品属性
function RestoreSelectCheck() {
    var Props = $("#lbProps").text();
    if (Props != '') {
        var PropsArr = Props.split(';');
        for (var i = 0; i < PropsArr.length; i++) {
            var pids = PropsArr[i].split(':');
            var pid = $("input[name='pid_" + pids[0] + "'],select[id='pid_" + pids[0] + "']");
            if ($(pid).attr("type") == 'select-one') {
                $(pid).val(PropsArr[i]);
                $(pid).trigger("change");
            }
            else if ($(pid).attr("type") == 'checkbox') {
                var s = $(pid).attr("name");
                $("input[name='" + s + "']" + "[value='" + PropsArr[i] + "']").attr("checked", "true");
                //$("input[name='" + s + "']" + "[value='" + PropsArr[i] + "']").trigger("click");
            }
        }
    }
    UpdateSkuTable();

}


//查找所有的自定义文本框从API取得的商品属性
function RestoreInput() {
    var inputpids = $("#lbinputpids").text();
    var inputstrs = $("#lbinputstrs").text();
    var inputstrArr = inputstrs;
    var inputpidArr = inputpids;
    if (inputstrArr != '' && inputpidArr != '') {

        inputpidArr = inputpidArr.split(',');
        inputstrArr = inputstrArr.split(',');

        for (var i = 0; i < inputpidArr.length; i++) {

            var tstr = inputstrArr[i].split(';');
            var pid = $("#pid_" + inputpidArr[i]);
            if ($(pid).attr("type") == 'select-one') {
                if (inputpidArr[i] == '20000') {
                    var f = $(pid).find("option[text='" + tstr[0] + "']");
                    var f1 = $(pid).find("option[value='20000']");
                    var f2 = $(pid).find("option[value='20000_型号']");
                    if (f.length > 0) {
                        $(f).attr("selected", "true");
                        $(pid).trigger("change");
                        var ne = $("#pid_20000_span select");
                        if (ne.length > 0) {
                            var ops = $(ne).attr("id");
                            $("#" + ops).find("option[text='自定义']").attr("selected", true)
                            $("#" + ops).trigger("change");
                            $("#pid_20000_span input").val(tstr[2]);
                        }

                    }
                    else if (f1.length > 0) {
                        $(f1).attr("selected", "true");
                        $(pid).trigger("change");
                        $("#input_20000").val(tstr[0]);
                    }
                    else if (f2.length > 0) {
                        $(f2).attr("selected", "true");
                        $(pid).trigger("change");
                        $("#input_20000").val(tstr[0]);
                        $("#input_20000").trigger("blur");
                        $("#" + tstr[1]).val(tstr[2]);
                    }
                }
                else {
                    $(pid).val(inputpidArr[i]);
                    $(pid).trigger("change");
                    $("#input_" + inputpidArr[i]).val(tstr[0]);
                }

            }
            else {
                $("#input_" + inputpidArr[i]).val(tstr[0]);
            }

        }

    }

}

function InsertSku() {
    var skuprop = $("#lbskuprop").text();
    var skuprice = $("#lbskuprice").text();
    var skuouterids = $("#lbskuouterids").text();
    var skuquantities = $("#lbskuquantities").text();
    if (skuprop != '') {

        var skuq = skuquantities.split(',');
        var skutds = skuprop.split(',');
        var skup = skuprice.split(',');
        var skuoutid = skuouterids.split(',');
        for (i = 0; i < skutds.length; i++) {
            var itemprop = skutds[i].replace(';', '_');
            $("input[name='" + itemprop + "_p']").val(skup[i]);
            $("input[name='" + itemprop + "_q']").val(skuq[i]);
            $("input[name='" + itemprop + "_q']").trigger("blur");
            $("input[name='" + itemprop + "_sku']").val(skuoutid[i]);
        }
    }

}

function ShowCSV(num) {
    if (num == 0) {
        $("#ul_head li:eq(0)").attr("class", "menucurrent");
        $("#ul_head li:eq(1)").attr("class", "optionend");
        $("#div_0").css("display", "block");
        $("#div_1").css("display", "none");
    } else {
        $("#ul_head li:eq(0)").attr("class", "optionstar");
        $("#ul_head li:eq(1)").attr("class", "menucurrent");
        $("#div_0").css("display", "none");
        $("#div_1").css("display", "block");
    }
    
}