﻿cn.liusoft.createNameSpace("page");

/*点击扩展信息按钮时处理*/
page.clickExtInfoButtonHandler = function(extInfoContainerID, extInfoContainerClassName, hash1) {
    document.getElementById(extInfoContainerID).className = extInfoContainerClassName;
    window.location.hash = hash1;
};


page.load = function() {

/*-----设定控件当焦点进入离开时的事件----*/
$("#ui_txtReplyContent").focus(function() {
    if ($(this).val() == "在此填写您的问题，问题被默认回复到本页面，请您记得回来查看！") {
        $(this).val("");
    }
}).blur(function() {
    if ($(this).val() == "") {
        $(this).val("在此填写您的问题，问题被默认回复到本页面，请您记得回来查看！");
    }
})


$("#ui_txtName").focus(function() {
    if ($(this).val() == "联系姓名") {
        $(this).val("");
    }
}).blur(function() {
    if ($(this).val() == "") {
        $(this).val("联系姓名");
    }
})

$("#ui_txtTel").focus(function() {
    if ($(this).val() == "联系电话") {
        $(this).val("");
    }
}).blur(function() {
    if ($(this).val() == "") {
        $(this).val("联系电话");
    }
})

$("#ui_txtQQ").focus(function() {
    if ($(this).val() == "联系QQ") {
        $(this).val("");
    }
}).blur(function() {
    if ($(this).val() == "") {
        $(this).val("联系QQ");
    }
})
/*-----end设定控件当焦点进入离开时的事件----*/

//设置checkbox控件的click事件
$("#chk").click(function() {
    if ($(this).attr("checked") == true) {
        $("#btn").fadeTo("slow", 0, function() {
            $(this).attr("class", "input-big");
        })
        $(".row-3").show("3000");
        // $(".row-3").css("display", "block");
        $(".reply").height("180px");
        $("#btn").fadeTo("slow", 1);
        $("#btn").show("slow");
    } else {
        $("#btn").fadeTo("slow", 0, function() {
            $(this).attr("class", "input-small");
        })
        $(".row-3").hide("3000");
        // $(".row-3").css("display", "none");
        $(".reply").height("150px");
        $("#btn").fadeTo("slow", 1);
        $("#btn").show("slow");
    }

});


//鼠标滑过时改变样式
$("#chk-text").mouseover(function() {
    $(this).css("text-decoration", "underline");
}).mouseout(function() {
    $(this).css("text-decoration", "none");
});

};
cn.liusoft.dom.addLoadedEventListener("page.load()");


//提交评论
page.addComment = function() {
    
    var errorInfo = "请您修改以下错误：";
    var replayContent = document.getElementById("ui_txtReplyContent").value;
    if (replayContent.length < 5 || replayContent == "在此填写您的问题，问题被默认回复到本页面，请您记得回来查看！") {
        errorInfo += "\n留言内容少于5个字符！";
    }

    if (document.getElementById("chk").checked == true) {
        var replyName = document.getElementById("ui_txtName").value;
        if (replyName.length < 2 || replyName == "联系姓名") {
            errorInfo += "\n联系姓名输入有误！";
        }

        var replyTel = document.getElementById("ui_txtTel").value;
        var replyQQ = document.getElementById("ui_txtQQ").value;

        if (replyQQ == "联系QQ" && replyTel == "联系电话") {
            errorInfo += "\n联系电话或联系QQ至少应输入一个！";
        } else {
            if (replyTel.length < 8 && replyQQ.length < 7 && replyQQ != "联系QQ" && replyTel != "联系电话") {
                errorInfo += "\n联系电话输入有误！";
                errorInfo += "\n联系QQ输入有误！";
            } else {

                if (replyTel.length < 8 && replyQQ == "联系QQ") {
                    errorInfo += "\n联系电话输入有误！";
                }

                if (replyQQ.length < 7 && replyQQ == "联系电话") {
                    errorInfo += "\n联系QQ输入有误！";
                }
            }
        }

    }

    if (errorInfo != "请您修改以下错误：") {
        alert(errorInfo);
        return;
    }

    if (replyName == undefined) {
        replyName = "";
    }

    if (replyQQ == "联系QQ" || replyQQ == undefined) {
        replyQQ = "";
    }
    if (replyTel == "联系电话" || replyTel == undefined) {
        replyTel = "";
    }



    cn.liusoft.importJS("cn.liusoft.ajax.HttpRequest");
    var xmlhttp = new cn.liusoft.ajax.HttpRequest("/product/ajaxaction.aspx", page.addComment_onComplete, page.addComment_onError);

    xmlhttp.addFormData("AjaxAction", "添加评论");
    xmlhttp.addFormData("replayContent", escape(replayContent));
    xmlhttp.addFormData("replyName", replyName);
    xmlhttp.addFormData("replyTel", replyTel);
    xmlhttp.addFormData("replyQQ", replyQQ);
    xmlhttp.addFormData("subjectType", "0");
    xmlhttp.addFormData("typeLinkTableID", typeLinkTableID);

    xmlhttp.doCallBack();
}

page.addComment_onComplete = function(responseText, responseXML) {
    eval(responseText);

    if (isOK) {
        if (commentID > 0) { //没有填写联系资料
            tb_show('', '/comment/commentContactInfo-edit.aspx?id=' + commentID + '&KeepThis=true&TB_iframe=true&height=185&width=370', false);
            return;
        } else {
            document.getElementById("ui_txtReplyContent").value = "在此填写您的问题，问题被默认回复到本页面，请您记得回来查看！";
            document.getElementById("ui_txtName").value = "联系姓名";
            document.getElementById("ui_txtTel").value = "联系电话";
            document.getElementById("ui_txtQQ").value = "联系QQ";
            window.location.reload();
        }
        alert("恭喜，您的问题已成功提交！");
    }
    else {
        if (commentID == -1) {
            alert("提交问题超时！");
        } else if (commentID == -2) {
            alert("请您一分钟后再进行提交！");
        } else {
            alert("提交失败，请重试！");
        }
    }
};
page.addComment_onError = function(status, statusText, responseText)
{
    alert("发生错误，请重试！");
};


//提交留言
page.addGuestBook=function()
{ 


    var replayContent=document.getElementById("ui_txtGuestBookContent").value;
    if(replayContent.length<5)
    {
        alert("您至少应该输入5个字符!");
        return;
    }
    
    cn.liusoft.importJS("cn.liusoft.ajax.HttpRequest");
    var xmlhttp = new cn.liusoft.ajax.HttpRequest("/product/ajaxaction.aspx",page.addGuestBook_onComplete,page.addGuestBook_onError);
    
    xmlhttp.addFormData("AjaxAction","添加留言");
    xmlhttp.addFormData("replayContent",escape(replayContent));
    xmlhttp.addFormData("subjectType","2");

    xmlhttp.doCallBack();
}

page.addGuestBook_onComplete = function(responseText, responseXML)
{
    eval(responseText);
    
    if(isOK)
    {
        document.getElementById("ui_txtGuestBookContent").value = "";
        alert("感谢您的留言，我们将会尽快给您最满意的回复！");
    }
    else
    {
        alert("提交失败，请重试！");
    }
};
page.addGuestBook_onError = function(status, statusText, responseText)
{
    alert("发生错误，请重试！");
};


/*添加商品到购物车*/
page.addProductToShoppingCart = function(isSale)
{

    if(isSale!="True")
    {
        alert("商品已售完，请您关注！");
        return;
    }
    
    if(currentItem_IsOnSale!=1)
    {
        alert("商品已售完，请您关注！");
        return;
    }
    if(isNaN(document.getElementById("ui_productCount").value))
    {
        alert("商品数量必须为英文数字！");
        return;
    }else{
        if(parseInt(document.getElementById("ui_productCount").value)<1)
        {
            alert("商品数量至少得大于0！");
            return;
        }
    }
    
//    document.getElementById("shopping-cart_add-item_tips").innerHTML = "正在添加...";
//    document.getElementById("shopping-cart_add-item_tips").style.display = "block";
    
    cn.liusoft.importJS("cn.liusoft.ajax.HttpRequest");
    var xmlhttp = new cn.liusoft.ajax.HttpRequest("/my/shoppingcart/ajaxaction.aspx",page.addProductToShoppingCart_onComplete,page.addProductToShoppingCart_onError);
    
    xmlhttp.addFormData("AjaxAction","添加商品到购物车");
    xmlhttp.addFormData("ProductID",currentItem_ProductID);
    xmlhttp.addFormData("ProductCount",parseInt(document.getElementById("ui_productCount").value));

    xmlhttp.doCallBack();
};
page.addProductToShoppingCart_onComplete = function(responseText, responseXML) {
    eval(responseText); /* var isOK = true; var shoppingCartItemCount = 4; var productItemCount = 3; */

    if (isOK) {
        document.getElementById("shopping-cart_product-count").innerHTML = shoppingCartItemCount;
        document.getElementById("shopping-cart_product-item-count").innerHTML = productItemCount;

        show_control_shoppingcart_add_addToShoppingCart(shoppingCartItemCount, productItemCount, productTotalMoney);

    }
    else {
        if (result == 1) {
            alert("您只能选购一件礼品！");
        }
        else if (result == 2) {
            alert("您还需要再购买" + money + "元的商品才能选择该礼品！");
        }
        else {
            alert("更新失败，请重试！");
        }
        //        window.setTimeout(
        //        function(){document.getElementById("shopping-cart_add-item_tips").innerHTML = "";},500);
    }
};
page.addProductToShoppingCart_onError = function(status, statusText, responseText)
{
    alert("发生错误，请重试！");
};

//打开另一个商品
page.changeToGetOtherProduct=function()
{
    var productID=document.getElementById("getOtherProduct").value;
    window.open("/product-view/"+productID+".html");
}




/*添加商品到收藏*/
function addProductToFavorite(productID) {

    $.post("/my/shoppingcart/ajaxaction.aspx", { AjaxAction: "添加商品到收藏夹", ProductID: productID }, function(data, textStatus) {
        if (textStatus == "success") {
            eval(data); /* var isOK = true;*/
            if (isOK) {
                if (result == 1 || result == 2) {
                    show_control_shoppingcart_add_addToFavorite();
                } else {
                    alert("添加失败，请您重试！");
                }
            }
            else {
                if (login == false) {
                    alert("您需要登录后才能收藏此产品！");
                }
            }
        } else {
            alert("发生错误，请重试！");
        }
    });

};

