﻿// JScript 文件
// 站点根目录
var webPath = "http://www.simosolar.com/";

// 新闻根目录
var newsPath = "http://www.simosolar.com/"; // http://news.simosolar.com/

// 商务根目录
var busyPath = "http://b2b.simosolar.com/"; //http://b2b.simosolar.com/

// JScript 文件
var bbsHost = "http://bbs.simosolar.com/"; // http://bbs.simosolar.com/  localhost:38016   http://114.80.208.96:8081/

// 是否已登录
var isLogined = false;

// 用户ID
var uid = "0";

// 用户名
var uName = "";

// 用户密码
var pwd = "";

// 用户显示名
var showName = "游客";

// 是否管理员
var IsManage = false;

var numkey = Math.random();

// 更换验证码图片
function changeWeb()
{
    numkey = Math.round(numkey * 10000);
    $("#WebValidates").attr("src", webPath + "images/WebValidates.aspx?NumKey=" + numkey);
}

$(document).ready(function()
{
    // 滚动到页面顶部
    $("#toTop").click(function()
    {
        window.scrollTo(0, 0);
    });
    
    // 跳转到指定页数
    $("#btnPage").click(function ()
    {
        // 翻页事件
        PageTurn();
    });
    
    $("#txtPageNo").keydown(function (event)
    {
        // 获取键盘码（Get keyCode）
        var evt = (event) ? event : ((window.event) ? window.event : "");   //兼容IE和Firefox获得keyBoardEvent对象
        var key = evt.keyCode ? evt.keyCode : evt.which;                    //兼容IE和Firefox获得keyBoardEvent对象的键值
        
        if(key == 13)
        {
            // 翻页事件
            PageTurn();
            return false;
        }
    });
    
    // 更换验证码图片
    $("#WebValidates").click(function()
    {
        // 更换验证码图片
        changeWeb();
    });
    
    $(".btnLoginO").click(function()
    {
        var $Uname = $("#txtName");
        var $Pwd = $("#txtPwd");
        var $Code = $("#txtValiCode");
        
        uName = $Uname.val();
        pwd = $Pwd.val();
        var txtCode = $Code.val();
        
        if($.trim(uName) == "")
        {
            alert("登录失败：用户名不能为空！");
            $Uname.focus();
            return false;
        }
        else if($.trim(pwd) == "")
        {
            alert("登录失败：密码不能为空！");
            $Pwd.focus();
            return false;
        }
        else if($.trim(txtCode) == "")
        {
            alert("登录失败：验证码不能为空！");
            $Code.focus();
            return false;
        }
        
        numkey = Math.round(numkey * 10000);
        $.getJSON(webPath + "Ajax/Operator/ValiCode.ashx", {"code" : txtCode, "temp" : numkey}, function(validate)
        {
            if(validate[0].bFlag == "True")
            {
                $.getJSON(bbsHost + "eric-ashx/User_Login.ashx?jsoncallback=?",
                    {"username" : uName, "pwd" : pwd, "temp" : numkey }, function(data)
                {
                    // 当数据不为空时，将数据赋给容器，并显示
                    if(data.BFlag == "True")
                    {
                        isLogined = true;
                        
                        uid = data.Id;
                        pwd = data.Pwd;
                        showName = data.ShowName;
                        
                        if(data.IsManage == "True")
                        {
                            IsManage = true;
                        }
                        
                        $(".lblUName").text(unescape(unescape(showName)));
                        
                        alert("操作提示：登录成功！");
                        
                        // 将登录信息赋值到站点 Session
                        SetSessionValue();
                        
                        var url = unescape($.query.get("forword"));
                        
                        if(url == "")
                        {
                            url = busyPath + "Enterprise/index.aspx";
                        }
                        
                        window.location.href = url;
                    }
                    else
                    {
                        alert("登录失败：您输入的用户名或密码有误！");
                    }
                });
                
                // if(!isLogined) alert("登录失败：服务器忙，请稍后再试！");
            }
            else
            {
                alert("登录失败：您输入的验证码错误！\n若看不清楚，请点击验证码图片，更换验证码！");
                
                // 更换验证码图片
                changeWeb();
            }
        });
        
        return false;
    });
    
    $(".btnSearch").click(function ()
    {
        if($("#keyWord").val() != "")
        {
            window.open(busyPath + "Search/Search.aspx?keyWord=" + escape($("#keyWord").val()));
        }
        else
        {
            alert("搜索的关键词不能为空，请输入搜索的关键词！");
        }
    });
    
    // 记录蜘蛛爬行
    // numkey = Math.round(numkey * 10000);
    // $.getJSON(webPath + "Ajax/BOT/bot.ashx?jsoncallback=?", {"url": location.href, "temp" : numkey}, function(data)
    // {
        // alert(data.bFlag);
    // });
});

// 将登录信息赋值到站点 Session
function SetSessionValue()
{
    numkey = Math.round(numkey * 10000);
    $.getJSON(webPath + "Ajax/Operator/UserInfo.ashx",
        {"uid" : uid, "bflag" : IsManage, "uname" : uName,
        "pwd" : pwd, "sname" : showName, "temp" : numkey}, function(data)
    {
        // 当数据不为空时，将数据赋给容器，并显示
        if(data.BFlag == "True")
        {
            isLogined = true;
        }
    });
}

// 翻页事件
function PageTurn()
{
    var url = $("#hidUrl").val();
    var cur = $("#txtPageNo").val();
    var mPageNo = $("#maxValue").val();
    if(!isNaN(cur) && cur != "")
    {
        cur = parseInt(cur);
        mPageNo = parseInt(mPageNo);
        if(cur <= 1)
        {
            cur = 1;
        }
        else if(cur > mPageNo)
        {
            cur = mPageNo;
        }
        window.location.href = url + cur;
    }
    else alert("对不起，您输入的页码格式不正确！");
}

// 显示产品信息
function ShowPro(id, mid, pid)
{
    var hrefURL = busyPath + "Enterprise/";
    if(mid == "0")
    {
        hrefURL += "default/product.aspx?id=" + id + "&pid=" + pid;
    }
    else
    {
        hrefURL += "black/product.aspx?id=" + id + "&pid=" + pid;
    }
    window.open(hrefURL);
}

// 获取字符串长度
function getCharLength(value)
{
    var _tmp = value;
    var _length = 0;
    for (var i = 0; i < _tmp.length; i++) {
        if (_tmp.charCodeAt(i) > 255) {
            _length += 2;
        }
        else
        {
            _length++;
        }
    }
    return _length;
}

// 希萌主站
function ToWeb()
{
    window.location.href = webPath;
}

// 跳转到商务网首页
function ToBusy()
{
    window.location.href = busyPath + "Enterprise/index.aspx";
}

// 密码找回
function BackPwd()
{
    window.location.href = bbsHost + "?recoverpassword";
}

// 加入免费企业
function JoinUs()
{
    window.location.href = webPath + "Common/joinus.aspx";
}

// 用户注册
function Register()
{
    window.location.href = webPath + "Common/register.aspx";
}

// 用户登录
function LoginPage()
{
    window.location.href = webPath + "Common/login.aspx";
}

// 跳转到 BBS
function ToBBS()
{
    window.open(bbsHost);
}

// 暂未开放
function UnOpen()
{
    alert("此功能暂未开放！");
}
