﻿
$(function () {

    $().ready(function () {

        using(["messager", "validatebox", "numberbox"], function () {

            $.extend($.fn.validatebox.defaults.rules, {
                Input_num_Confrim: {
                    validator: function (value) {

                        var inputError = true;
                        var strNum = value;

                        if (parseInt(strNum) > 500) {
                            tipsWindown("Notice:", "text:For order more than 500 boxes, please contact us for better price at 7daysherbalslim@gmail.com", "250", "50", "true", "", "true", "classWindow");
                            //  inputError=false;
                        }
                        return inputError;
                    }
                    //	message:'Please enter at least {0} characters.'
                }
            });


            //  start


            $('#inputCode').validatebox({ required: "true", missingMessage: "Please input a security code of 16 numbers.", validType: "length[16,16]", invalidMessage: " Please input a security code of 16 numbers. " });
            $("#sendSubmit").live("click", function () {

                if ($('#inputCode').validatebox("isValid")) {

                    SetCookie("inputCode", $('#inputCode').val());

                    location.href = "fake_proof.aspx";

                }
            });

            //  产品

            $("div.home_con_01_right").find("div.pro_order_01").each(function () {
                $(this).find("input[name=inputNum]").validatebox({ required: "true", missingMessage: "Please input the quantity.", validType: "Input_num_Confrim" }).numberbox({ precision: "0" });

                $(this).find("a[name=subCart]").bind("click", function () {
                    var p_id = $(this).attr("id").split("_")[1];
                    $.ajax({
                        type: "POST",
                        url: "/ASHX/ProductCart.ashx",
                        data: "productNum=" + $.trim($("#inputNum_" + p_id).val()) + "&productCode=" + p_id,
                        success: function (msg) {

                            if (msg == "yes") {
                                /// <reference path="../Shopping/ShoppingCart.aspx" />

                                location.href = "/Shopping/ShoppingCart.aspx";
                            }
                            else {
                                tipsWindown("Error:", "text:Failed to add to Cart!", "250", "50", "true", "", "true", "classWindow");
                            }


                        }
                    });

                });

                //  加减按钮
                $(this).find("a[name=lowerNum]").bind("click", function () {

                    var n_id = $(this).attr("id")
                    var p_num = $.trim($("#inputNum_" + n_id.split("_")[1]).val());
                    var p_n = 0;

                    if (p_num != "" && parseInt(p_num) != 1) {
                        p_n = parseInt((p_num) - 1);
                        //  $("#inputNum").attr("value", p_n);
                        $("#inputNum_" + n_id.split("_")[1]).attr("value", p_n);
                    }
                    else {
                        $("#inputNum_" + n_id.split("_")[1]).attr("value", 1);
                        // $("#inputNum").attr("value", 1);
                    }

                });


                //  加减按钮
                $(this).find("a[name=AddNum]").bind("click", function () {
                    var n_id = $(this).attr("id")
                    var p_num = $.trim($("#inputNum_" + n_id.split("_")[1]).val());

                    var p_n = 0;
                    if (p_num != "") {
                        if (parseInt(p_num) != 500) {
                            p_n = parseInt(p_num) + 1;
                            // $("#inputNum").attr("value", (parseInt(p_num) + 1));
                            $("#inputNum_" + n_id.split("_")[1]).attr("value", (parseInt(p_num) + 1));

                        }

                    }
                    else {
                        p_n = 1;
                        $("#inputNum_" + n_id.split("_")[1]).attr("value", 1);

                    }

                });
                //  加减按钮


            });





        });

    });

    function movePrictur() {

        $("#left_b").bind({
            click: function () {


                if (select_num != 1) {
                    var mm = parseInt(select_num) - 1;

                    $("div.img_show li").removeClass("img_now");
                    $("div.img_show li").hide();
                    for (var i = mm * 4 - 3; i < mm * 4 + 1; i++) {
                        if ($("div.img_show li").eq(i).length > 0) {
                            $("div.img_show li").eq(i).show();
                        }

                    }
                    select_num = mm;
                }

            }
        });

        $("#right_b").bind({
            click: function () {


                var mm = parseInt(select_num) + 1;


                if ($("#li_img_" + (mm * 4 - 3)).length > 0) {
                    $("div.img_show li").removeClass("img_now");
                    $("div.img_show li").hide();
                    for (var i = mm * 4 - 3; i < mm * 4 + 1; i++) {
                        $("#li_img_" + i).show();

                    }

                    select_num = mm;
                }


            }
        });

    }


});



function SetCookie(name, value)  //    两个参数，一个是cookie的名子，一个是值
{
    var Days = 1;
    //   此 cookie 将被保存 1 天
    var exp = new Date();
    //   new Date("December 31, 9998");
    exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
    document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString();
}
function getCookie(name)//    取cookies函数
{
    var arr = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)"));
    if (arr != null) return unescape(arr[2]);
    return null;
}
