$(document).ready(function(){
    $("tbody tr:even").css("background-color","#E5E5E5");
    $("tbody tr:odd").css("background-color","#D5D5D5");
});

function cart_add(item_id){
    $("#info").addClass("error");
    $("#info").text("正在添加产品...");
    $("#info").css("display","");
    succ = false;
    $.getJSON(WEB_ROOT+"/ajax/cart.php",{act:"cart_add",id:item_id},function(json){
        $("#info").attr('class','');
        $("#info").addClass(json.cls);
        $("#info").text(json.text);
        succ = true;
    });
    $("#info").ajaxComplete(function(event,request, settings){
        if(!succ){
            $(this).addClass("error");
            $(this).text("添加产品失败！");
        }
    });
}
function cart_del(item_id){
    $("#info").addClass("error");
    $("#info").text("正在删除...");
    $("#info").css("display","");
    $.getJSON(WEB_ROOT+"/ajax/cart.php",{"act":"cart_del","id":item_id},function(json){
        $("#info").attr('class','');
        $("#info").addClass(json.cls);
        $("#info").text(json.text);
        $("#item_"+item_id).css('display','none');
        //$("#cart_sum").load(WEB_ROOT+"/ajax/cart.php",{"act":"cart_sum"});
        $.getJSON(WEB_ROOT+"/ajax/cart.php",{"act":"cart_sum"},function(json){
            $.each(json,function(i,n){
                if(i==json.length-1) { // json 最后一条数据为 整个购物车的 总合计
                    $("#cart_sum_weight").text(n.sum_weight);
                    $("#cart_sum_money").text(n.sum_money);
                    return false;
                }
                $("#provider_"+n.provider_id+" span[name='sum_weight']").text(n.sum_weight);
                $("#provider_"+n.provider_id+" span[name='sum_money']").text(n.sum_money);

                //alert(n.sum_weight);
            });
            //alert(json.length);
        });

        succ = true;
    });
    $("#info").ajaxComplete(function(event,request, settings){
        if(!succ){
            $(this).addClass("error");
            $(this).text("操作失败，请联络管理员！");
        }
    });
}
function cart_modiQuantity(item_id,quantity){
    $("#info").addClass("error");
    $("#info").text("正在更新数量...");
    $("#info").css("display","");
    $.getJSON(WEB_ROOT+"/ajax/cart.php",{"act":"cart_modiQuantity","id":item_id,"quantity":quantity},function(json){
        $("#info").attr('class','');
        $("#info").addClass(json.cls);
        $("#info").text(json.text);
        // 原来的总重量 = 单重 * 新数量
        weight_sum = $("#item_"+item_id+"_weight").text() * quantity;
        $("#item_"+item_id+"_weight_sum").text(weight_sum);
        $.getJSON(WEB_ROOT+"/ajax/cart.php",{"act":"cart_sum"},function(json){
            $.each(json,function(i,n){
                if(i==json.length-1) { // json 最后一条数据为 整个购物车的 总合计
                    $("#cart_sum_weight").text(n.sum_weight);
                    $("#cart_sum_money").text(n.sum_money);
                    return false;
                }
                $("#provider_"+n.provider_id+" span[name='sum_weight']").text(n.sum_weight);
                $("#provider_"+n.provider_id+" span[name='sum_money']").text(n.sum_money);

                //alert(n.sum_weight);
            });
            //alert(json.length);
        });
        
        succ = true;
    });
    $("#info").ajaxComplete(function(event,request, settings){
        if(!succ){
            $(this).addClass("error");
            $(this).text("操作失败，请联络管理员！");
        }
    });
}
function cart_item_over(tr_id){
    scolor = $("#"+tr_id).css('background-color');
    $("#"+tr_id).attr('scolor',scolor);
    $("#"+tr_id).css('background-color','#FAFA00');
}
function cart_item_out(tr_id){
    scolor = $("#"+tr_id).attr('scolor');
    $("#"+tr_id).css('background-color',scolor);
}
// 淘宝地址分解
function tbaddress(){
    tbstr = "杨敏 ，13392882121 ，0755-28276831 ，广东省 深圳市 龙岗区 布吉街道乐民路51号国税布吉分局202室 ，518112 ";
    tbstr = $("textarea[name='tbaddr']").text();
    //tbstr = tbstr.replace(" ","");
    tbarr = tbstr.split("，");
    tbarr1 = tbarr[3].split(" ");
    sheng = tbarr1[0];
    tbarr[3] = tbarr[3].replace(sheng, "");
    tbarr[3] = tbarr[3].replace(" ","","g");
    
    $("input[name='address']")[0].value = tbarr[3];
    $("input[name='zipcode']")[0].value = tbarr[4];
    $("input[name='real_name']")[0].value = tbarr[0];
    $("input[name='mobile']")[0].value = tbarr[1];
    $("input[name='phone']")[0].value = tbarr[2];
    if(sheng.indexOf("省")!=-1) $("select[name='province']")[0].value = sheng.replace("省", "");
    if(sheng.indexOf("市")!=-1) $("select[name='province']")[0].value = sheng.replace("市", "");
    show_express($("select[name='province']")[0].value);
    //$("OPTION:contains('"+sheng+"')").attr("selected","selected");
}
function show_express(dest){
    
    $(".source~td span").each(function(){
        src = this.name;
        provider_id = this.id;
        $("#"+provider_id).load(WEB_ROOT+"/ajax/fare.php",{"act":"express_get","provider_id":provider_id,"dest":dest,"src":src});
    });
    $("#cart_sum_express").text(0);
    $("#cart_tsum_money").text(0);
    $("#express").css("display","");
    $("#data .error").css("display","none");
}

function check_express(express){
    base = express.value1;
    add = express.value2;
    p = express.parentNode.parentNode;
    
    sum_weight = $("#provider_"+p.id+" span[name=sum_weight]").text();
    sum_money = $("#provider_"+p.id+" span[name=sum_money]").text();
    if(sum_weight>1000){
        sum_express = parseFloat(base) + Math.ceil((sum_weight-1000)/1000)*add;
    }else{
        sum_express = parseFloat(base);
    }
    $("#provider_"+p.id+" span[name=sum_express]").text(sum_express);
    $("#cart_sum_express").text($("#cart_sum_express").text()/1+sum_express);
    $("#cart_tsum_money").text( parseFloat($("#cart_sum_express").text()) + parseFloat($("#cart_sum_money").text()) );
    //alert(base+":"+add);

}
function cart_order(){
    $("#cart_order_form").submit();
}

function order_pay(order_number){
    if(confirm("确定支付此订单吗？")){
        $.getJSON(WEB_ROOT+"/ajax/order.php",{"act":"order_pay","order_number":order_number},function(){
            window.location.reload(true);
        });
    }
}
function order_send(order_number,express_no){
        $("#state"+order_number).load(WEB_ROOT+"/ajax/order.php",{"act":"order_send","order_number":order_number,"express_no":express_no});
}
function cat_modi(id){
    html = "<td>"+$("#cat_"+id+" > td")[0].innerText+"<input type='hidden' name='id' value='"+id+"' /></td>\n\
            <td><input name='name' value='"+$("#cat_"+id+" > td")[1].innerText+"' /></td>\n\
            <td><input name='zone'' value='"+$("#cat_"+id+" > td")[2].innerText+"' /></td>\n\
            <td><input name='cost_discount' value='"+$("#cat_"+id+" > td")[3].innerText+"' size='6' /></td>\n\
            <td><input name='user_discount' value='"+$("#cat_"+id+" > td")[4].innerText+"' size='6' /></td>\n\
            <td width='300'><input name='remark' value='"+$("#cat_"+id+" > td")[5].innerText+"' size='50' /></td>\n\
            <td><input type='submit' value='修改' /></td>";
    $("#cat_"+id).html(html);
}
function fare_modi(id){
    html = "<td>"+$("#fare_"+id+" > td")[0].innerText+"<input type='hidden' name='id' value='"+id+"' /></td>\n\
            <td><input name='name' value='"+$("#fare_"+id+" > td")[1].innerText+"' /></td>\n\
            <td><input name='source' value='"+$("#fare_"+id+" > td")[2].innerText+"' /></td>\n\
            <td><input name='destination' value='"+$("#fare_"+id+" > td")[3].innerText+"' size='50' /></td>\n\
            <td><input name='base' value='"+$("#fare_"+id+" > td")[4].innerText+"' size='6' /></td>\n\
            <td><input name='add' value='"+$("#fare_"+id+" > td")[5].innerText+"' size='6' /></td>\n\
            <td><input type='submit' value='修改' /></td>";
    $("#fare_"+id).html(html);
}
