﻿cn.liusoft.createNameSpace("page");
/// <reference path="../../jquery/alerts/jquery.alerts.js" />

/*点击扩展信息按钮时处理*/
page.clickExtInfoButtonHandler = function(extInfoContainerID, extInfoContainerClassName, hash1)
{
    document.getElementById(extInfoContainerID).className = extInfoContainerClassName;
    window.location.hash = hash1;
};


page.load = function() {

};
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("/__mall/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('', '/__mall/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("/__mall/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;
        }
    }
    
    cn.liusoft.importJS("cn.liusoft.ajax.HttpRequest");
    var xmlhttp = new cn.liusoft.ajax.HttpRequest("/__mall/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("更新失败，请重试！");
        }
    }
};
page.addProductToShoppingCart_onError = function(status, statusText, responseText)
{
    alert("发生错误，请重试！");
};

//打开另一个商品
page.changeToGetOtherProduct=function()
{
    var productID=document.getElementById("getOtherProduct").value;
    window.open("/__mall/goods-view/"+productID+".html");
}


/*添加商品到收藏*/
function addProductToFavorite(productID) {

    $.post("/__mall/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("发生错误，请重试！");
        }
    });

};
