﻿jQuery(document).ready(function () {
    jQuery('#mycarousel').jcarousel();
});
var ddd = 1;
function codeSend() {
    $("#imgVerify").attr("src", "/VerifyPassword.aspx?d=" + ddd);
    ddd++;
}
(function ($) {

    $().ready(function () {

        //  按钮事件

        $("#addCart").live("click", function () {
            var n = $('#inputNum').val();
            var code = $('#HiddenCode').val();
            if (n == "" || n == "0") {
                var strHtml = "<font style='color:red;'>Error:<br> Shopping parameters entered incorrectly.</font>";
                $.modal(strHtml, { opacity: 40, minHeight: 80, minWidth: 300, maxHeight: 430, maxWidth: 800, focus: true, overlayClose: true, autoResize: true });
            }
            else {
                $.ajax({
                    type: "POST",
                    url: "/ASHX/ShoppingCart.ashx",
                    data: "productCode=" + code + "&productNum=" + n,
                    success: function (msg) {
                        var txtHtml = "";
                        if (msg == "1") {
                            txtHtml = "<font style='color:red;'>Error:<br>Add to cart error, please refresh the page and try again.</font>";
                            $.modal(txtHtml, { opacity: 40, minHeight: 80, minWidth: 300, maxHeight: 430, maxWidth: 800, focus: true, overlayClose: true, autoResize: true });
                        }
                        else if (msg == "2") {
                            var thisURL = document.URL;
                            location.href = "/Customer/login.aspx?url=" + thisURL;
                        }
                        else {
                            location.href = "/Shopping/ShoppingCart.aspx";
                        }


                        //     alert("Data Saved: " + msg);


                    }
                });
            }
        });

        $("#lowerNum").bind("click", function () {

            var p_num = $.trim($("#inputNum").val());
            var p_n = 0;
            if (p_num != "" && parseInt(p_num) != 1) {
                p_n = parseInt((p_num) - 1);
                $("#inputNum").attr("value", p_n);
            }
            else {
                p_n = 1;
                $("#inputNum").attr("value", 1);
            }

        });

        //start div_showImg
        $("#mycarousel").find("a").bind("click",function(){

           var txtImg=$(this).find("img").attr("title");
       
            $("#div_showImg").find("img").attr("src",txtImg);



        });
        // end

        $("#AddNum").bind("click", function () {

            var p_num = $.trim($("#inputNum").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));
                }
            }
            else {
                p_n = 1;
                $("#inputNum").attr("value", 1);
            }

        });

        // start

        using(["messager", "validatebox"], function () {

            $('#TextEmail').validatebox({ required: "true", missingMessage: "Can not be empty!", validType: "email", missingMessage: "Please enter a valid email address." });
            $('#TextContent').validatebox({ required: "true", missingMessage: "Can not be empty!", validType: "length[1,800]", invalidMessage: "The character should be between 1 and 800 in length." });
            $('#TextCode').validatebox({ required: "true", missingMessage: "Can not be empty!" });

            $("#sendSubmit").live("click", function () {

                if (!$('#TextEmail').validatebox("isValid") && !$('#TextContent').validatebox("isValid") && $('#TextCode').validatebox("isValid")) {
                    return false;
                }
                else {
                    var stremail = $("#TextEmail").val();
                    var strcontent = $("#TextContent").val();
                    var strcode = $("#TextCode").val();
                    var proCode = $("#HiddenCode").val();
                    strcontent = strcontent.replace(String.fromCharCode(10), "<br/>");
                    strcontent = strcontent.replace("<", "&lt;");
                    strcontent = strcontent.replace(">", "&gt;");
                    //
                    $.ajax({
                        type: "POST",
                        url: "/ASHX/Review.ashx",
                        data: "email=" + stremail + "&content=" + strcontent + "&code=" + strcode + "&prouctCode=" + proCode,
                        success: function (msgs) {
                     
                            msg = msgs.split("{{@}}")[0];
                            if (msg == "no") {
                                $.modal("Submission error", { opacity: 40, minHeight: 50, minWidth: 300, maxHeight: 450, maxWidth: 800, focus: true, overlayClose: true, autoResize: true });
                            }
                            else {
                                if (msg == "0") {

                                    $.modal("Successfully posted", { opacity: 40, minHeight: 50, minWidth: 300, maxHeight: 450, maxWidth: 800, focus: true, overlayClose: true, autoResize: true });
                                    
                                    var strhtml="<dl> <dd><p> <span class='chengse'>["+msgs.split("{{@}}")[1]+"]</span> "+strcontent+"</p></dd>  </dl>";    
                                  
                                    $("#div_reviews").prepend(strhtml);
                                      
                                    codeSend();

                                    $("#TextEmail").val("");
                                    $("#TextContent").val("");
                                    $("#TextCode").val("");

                                }
                                else if (msg == "1") {
                                    $.modal("Enter the verification code is wrong", { opacity: 40, minHeight: 50, minWidth: 300, maxHeight: 450, maxWidth: 800, focus: true, overlayClose: true, autoResize: true });
                                    codeSend();
                                }
                                else {
                                    $.modal("Submission error", { opacity: 40, minHeight: 50, minWidth: 300, maxHeight: 450, maxWidth: 800, focus: true, overlayClose: true, autoResize: true });

                                    codeSend();
                                }

                            }

                        }
                    });
                    //
                }

            });



        });

        //end
    });
})(jQuery);
