﻿/********************
* 根据ID获取jQuery对象
* elmId : 元素ID
********************/
function $j(elmId) { return $("#" + elmId); }

/** 获取单选空间值***/
function $rv(elmName) {
var obj = document.getElementsByName(elmName);
for (i = 0; i < obj.length; i++) {
if (obj[i].checked) {
return obj[i].value;
}
}
return "";
}
//检查email格式
function ce(src, batch) {
    if (batch == null)
        batch = false;
    if (batch) {
        var ptn = /\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*(;\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*)*/;
        return ptn.test(src);
    } else {
        var ptn = /\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
        return ptn.test(src);
    }
}

/********************
* 显示验证码
********************/
function showVerifyCode(elmId, msgElmId, imgId, chgLnkId) {
    if (elmId == null)
        elmId = "spVerCode";
    if (msgElmId == null)
        msgElmId = "spVerCodeMsg";
    if (imgId == null)
        imgId = "imgVerCode";
    if (chgLnkId == null)
        chgLnkId = "spChgVerCode";
    var jImg = $j(elmId);
    var jMsg = $j(msgElmId);
    var jChgLnk = $j(chgLnkId);
    if (jImg.html() == "") {
        jMsg.html("正在加载验证码...");
        jMsg.show();
        jImg.html("<img src='/Base/ValidCodeK.aspx' style='display: ;' id='" + imgId + "' alt='验证码' />");
    }
    var jVerCode = $j(imgId);
    jVerCode.load(function() {
        jMsg.hide();
        jVerCode.show();
        jChgLnk.show();
    });
}
/********************
* 更换验证码
********************/
function changeVerCode(elmId, msgElmId) {
    if (elmId == null)
        elmId = "imgVerCode";
    if (msgElmId == null)
        msgElmId = "spVerCodeMsg";
    var jImg = $j(elmId);
    var jMsg = $j(msgElmId);
    jMsg.html("正在刷新验证码...").show();
    jImg.attr({ src: "/Base/ValidCodeK.aspx?x=" + Math.random(), alt: "验证码" });
    jImg.hide();
    jImg.load(function() {
        jMsg.hide();
        jImg.show();
    });
}
/********************
* 显示正在处理的图标
* src : 触发事件的源对象
* show : 显示/隐藏
********************/
function showProc(src, show) {
    var oImg = $j("imgProc");
    if (show == null)
        show = true;
    if (show) {
        $(src).hide();
        if (oImg.length > 0)
            oImg.remove();
        $("<img src='/Images/img/processing.gif' id='imgProc' alt='正在处理' />").insertAfter(src);
    } else {
        $(src).show();
        oImg.remove();
    }
}
/********************
* 放大字体
* el : 放大/缩小
********************/
function enlarge(el, elmId) {
    if (el == null)
        el = true;
    if (elmId == null) {
        elmId = "Content";
    }
    var o = $j(elmId);
    var fontSize = parseInt(o.css("font-size"));
    var newFontSize = (el ? fontSize * 1.2 : fontSize / 1.2);
    o.css("font-size", newFontSize + "px");
}


//新闻评论
function NewsCommentsPost(nid, gid,src) {
    showProc(src);
    if (nid == null || nid == 0) {
        alert("很抱歉，操作失败！");
        return false;
    }
    var content = document.getElementById("txtContent").value;
    var code = document.getElementById("txtCode").value;
    $.post("/ajax.ashx?action=NewsComments", {
        _code: code,
        _content: content,
        _nid: nid,
        _gid: gid
    },
        function(msg) {
            alert(msg);
            showProc(src, false);
            window.location.href = window.location.href;
        });
    }

//新闻搜索
function NewsSearch(keyname,cidname) {
    var key = document.getElementById(keyname).value;
    var cid = document.getElementById(cidname).value;
    if (key == null || key == "")
    {alert("请输入搜索关键字！");return false;}
    window.location.href = "/news/news-S" + cid + "-K" + key + ".shtml";
}

//产品评论
function ProductCommentsPost(pid, gid, src) {
showProc(src);
if (pid == null || pid == 0) {
    alert("很抱歉，操作失败！");
    return false;
}
var content = document.getElementById("txtContent").value;
var code = document.getElementById("txtCode").value;
$.post("/ajax.ashx?action=ProductComments", {
    _code: code,
    _content: content,
    _pid: pid,
    _gid: gid
},
    function(msg) {
        alert(msg);
        showProc(src, false);
        window.location.href = window.location.href;
    });
}

//产品搜索
function ProductSearch(keyname, cidname,mid) {
    var key = document.getElementById(keyname).value;
    var cid = document.getElementById(cidname).value;
    if (key == null || key == "")
    { alert("请输入搜索关键字！"); return false; }
    window.location.href = "/Product/Pr-S" + cid + "-K" + key + "-M" + mid + ".shtml";
}

//会员中心登陆（需验证码）
function SignIn(src) {
    showProc(src);
    var username = document.getElementById("txtUsername").value;
    var password = document.getElementById("txtPassword").value;
    var code = document.getElementById("txtCode").value;
    $.post("/ajax.ashx?action=SignIn", {
        _code: code,
        _username: username,
        _password: password
    },
    function(msg) {
        if (msg == "ok")
        { window.location.href = "/Users"; }
        else {
            alert(msg);
            showProc(src, false);
        }
    });
}

//头部登陆（无证码）
function HSignIn(src) {
    showProc(src);
    var username = document.getElementById("txtHUsername").value;
    var password = document.getElementById("txtHPassword").value;
    if (username == "" || password == "") {
        alert("请输入用户名和密码！");
        showProc(src, false);
        return false;
    }
    $.post("/ajax.ashx?action=HSignIn", {
        _username: username,
        _password: password
    },
    function(msg) {
        if (msg == "ok")
        { window.location.href = "/Users"; }
        else {
            alert(msg);
            showProc(src, false);
        }
    });
}

//获取用户名
function GetUser() {
    var objlgimg = document.getElementById("headlging");
    $.post("/ajax.ashx?action=GetUser",
    function(msg) {
            if (/^ok/.test(msg)) {
                var msgCrumbs = msg.split(',');
                objlgimg.innerHTML = "您好，&nbsp;&nbsp;" + msgCrumbs[1] + "&nbsp;&nbsp;欢迎您来到 福州嘉乐咖啡 &nbsp;&nbsp;<input name='loginout' class='LoginBtn' onclick='LoginOut(this)' type='button' value='退出'/>";
             }
    });
}

//退出登陆
function LoginOut(src) {
    showProc(src);
    $.post("/ajax.ashx?action=LoginOut",
    function(msg) {
    window.location.href = "/";
    });
}

//在线留言
function addfeedbook(src) {
showProc(src);
    var Title= $("#txtTitle").val();
    var CompName= $("#txtCompName").val();
    var Address=$("#txtAddress").val();
    var Name = $("#txtCName").val();
    var Tel=$("#txtTel").val();

    var Mobile=$("#txtMobile").val();
    var Fax=$("#txtFax").val();
    var Content=$("#txtContent").val();
    var code = $("#txtcode").val();
    if (Name == "")
    { alert("请输入联系人！"); return false; }

    if (Content == "")
    { alert("请输入留言内容！"); return false; }
    
    if(Tel==""&&Mobile=="")
    {
     alert("请输入联系电话或联系手机！"); return false;
    }
    if(code=="")
    {
     alert("请输入验证码！"); return false;
    }
    $.post("/ajax.ashx?action=feedbook", {
        _code: code,
        _name: Name,
        _content: Content,
        _mobile: Mobile,
        _compname:CompName,
        _address:Address,
        _tel:Tel,
        _fax:Fax,
        _title:Title
    },
    function(msg) {
        alert(msg);
        showProc(src, false);
        document.form1.reset();
    });
}

//添加收藏
function addfav(pid,title, photo, src) {
    showProc(src);
    $.post("/ajax.ashx?action=addfav", {
         _pid: pid,
        _title: title,
        _photo: photo
    },
    function(msg) {
        alert(msg);
        showProc(src, false);
    });
}

//删除收藏
function delfav(pid, src) {
    showProc(src);
    $.post("/ajax.ashx?action=delfav", {
        _pid: pid
    },
    function(msg) {
        alert(msg);
        showProc(src, false);
        window.location.href = window.location.href;
    });
}

//产品订单留言
function praddorder(src, pid, title) {
    var amount = document.getElementById("txtnub").value;
    var contact = document.getElementById("txtname").value;
    var mobile = document.getElementById("txtmobile").value;
    var email = document.getElementById("txtmail").value;
    var memo = document.getElementById("txtPrOrderMemo").value;

    if (amount == "")
    { alert("请输入您要购买的数量！"); return false; }
    if (contact == "")
    { alert("请输入您的姓名！"); return false; }
    if (mobile == "")
    { alert("请输入您的联系电话！"); return false; }
    if (email == "")
    { alert("请输入您的E-mail！"); return false; }
    if (!ce(email, true)) { alert("Email格式错误！"); return false; }
    showProc(src);

    $.post("/ajax.ashx?action=addproductorder", {
    _pid: pid,
    _title: title,
    _contact: contact,
    _amount: amount,
    _mobile: mobile,
    _email: email,
    _memo: memo
    },
    function(msg) {
        alert(msg);
        showProc(src, false);
    });
}

//找回密码
function chpassword(src) {
    var username = document.getElementById("txt_username").value;
    var question = document.getElementById("selQuestion").value;
    var answer = document.getElementById("txt_answer").value;
    var newpaasowrd = document.getElementById("txt_password").value;
    var paasowrdtwo = document.getElementById("txt_passwordre").value;
    var code = document.getElementById("txtCode").value;
    if (code == "")
    { alert("请输入验证码！"); return false; }
    if (username == "")
    { alert("请输入用户名！"); return false; }
    if (question == "")
    { alert("请选择您的问题类型！"); return false; }
    if (answer == "")
    { alert("请输入您的问题答案！"); return false; }
    if (newpaasowrd == "")
    { alert("请输入您的新密码！"); return false; }
    if (paasowrdtwo == "")
    { alert("请输入确认您的新密码！"); return false; }
    if (paasowrdtwo != newpaasowrd) {
        alert("两次输入的密码不一致！"); return false;
    }
    showProc(src);

    $.post("/ajax.ashx?action=chpass", {
    _username: username,
    _question: question,
    _answer: answer,
    _newpaasowrd: newpaasowrd,
    _code: code
    },
    function(msg) {
        alert(msg);
        showProc(src, false);
    });
}
//添加投票
function addvote(src) {
    showProc(src);
    var _vid;
    $("[name='vodeid']:checked").each(function() {
        _vid = $(this).val();
        $.post("/ajax.ashx?action=addvote&&t=" + Math.random(), { vid: _vid },
        function(msg) { });
    });
    window.open('/Vode.aspx');
    showProc(src, false);
}
