﻿$(function() {
    $(".chooseinfo a").click(function() {
        var cont = $(this).html();
        var spanid = $(this).parent().attr("forspan");
        $("#" + spanid).html(cont);
    })
    $("input[name=buynum]").keyup(function() {
        $(this).attr("onpaste", "return false");
        var tmptxt = $(this).val();
        tmptxt = tmptxt.replace(/[^0-9]/, '');
        if (parseInt(tmptxt) > parseInt($("#spanleavenum").html())) {
            tmptxt = $("#spanleavenum").html(); alert("最多只剩" + tmptxt + "件商品");
        }
        $(this).val(tmptxt);
    })
    $("#btn_buynow").click(function() {
        if (checkform()) {
            $("input[name=doaction]").val("buynow");
            $("input[name=doaction]").after("<input type='hidden' name='color' value='" + $("#mycolor").html() + "'/>");
            $("input[name=doaction]").after("<input type='hidden' name='size' value='" + $("#mysizer").html() + "'/>");
            $("#mysaveform").attr("action", "/user/CreateOrder");
            $("#mysaveform").submit();
        }
    })
})
function addtoshopcar() {
    if (checkform()) {
        $.ajax({
            type: "POST",
            url: "/ajax/shopcar?doaction=1&goodid="
                            + $("#goodid").val() + "&buynum="
                            + $("input[name=buynum]").val()
                            + "&color=" + $("#mycolor").html()
                            + "&size=" + $("#mysizer").html(),
            data: null,
            success: function(msg) {
                alert(msg);
            }
        });
    }
}
function checkform() {
    if ($("#mysizer").html() == "未选择") {
        alert("请选择尺码");
        return false;
    }
    if ($("#mycolor").html() == "未选择") {
        alert("请选择颜色");
        return false;
    }
    if ($("input[name=buynum]").val() == "") {
        alert("请输入购买数量");
        return false;
    }
    if (!isNaN($("input[name=buynum]").val())) {
        if (parseInt($("input[name=buynum]").val()) > parseInt($("#spanleavenum").html())) {
            alert("你输入的数量超过了库存量");
            return false;
        }
    }
    else {
        alert("购买数量只能是数字");
        return false;
    }
    return true;
}
