﻿var UserID = "";
var Pwd = "";
var NickName = "";
var lastLoginTime = "";
//判断用户是否登录
function checklogin() {
    var allcookie = document.cookie;
    var cookieArr = allcookie.split(';');
    for (var i = 0; i < cookieArr.length; i++) {
        var cookieStart = cookieArr[i].indexOf("ifox_userid=");
        if (cookieStart > 0) {
            UserID = cookieArr[i].substring(cookieArr[i].indexOf('=') + 1);
        }
        cookieStart = cookieArr[i].indexOf("ifox_userpwd=");
        if (cookieStart > 0) {
            Pwd = cookieArr[i].substring(cookieArr[i].indexOf('=') + 1);
        }
        cookieStart = cookieArr[i].indexOf("ifox_nickname");
        if (cookieStart > 0) {
            NickName = decodeURIComponent(cookieArr[i].substring(cookieArr[i].indexOf('=') + 1));
        }
        cookieStart = cookieArr[i].indexOf("ifox_lastLoginTime");
        if (cookieStart > 0) {

            lastLoginTime = cookieArr[i].substring(cookieArr[i].indexOf('=') + 1);
        }
    }
    if (UserID != "") {
        return true;
    }
    else {
        return false;
    }
}
//设置评论登录状态
function setUserState() {
    var sHTML = "";
    if (checklogin()) {
        //输出登陆后的页面
        sHTML = "用户名：" + NickName + "&nbsp;&nbsp;<input type='button' value='退出' id='btnlogin' onclick='logout(1);' style='height:20px;'/>";
    }
    else {
        sHTML = "<div class='fR' style='margin: 0px 20px 0px 0px; float: left; width: 400px;'>";
        sHTML += "<a href='http://ucenter.cnuuu.com/Sendpass.aspx' target='_blank' style='margin: 4px 0pt 0pt 12px; background: transparent none repeat scroll 0% 0%;text-decoration: underline; color: rgb(51, 51, 51); font-weight: normal; font-size: 12px; float: right;display:inline;'>忘记密码</a>";
        sHTML += "用户名 <input id='useremail' type='text' style='width:65px;' onkeydown='pwdfocus();'/> 密码 <input id='userepwd' type='password' style='width:65px;' onkeydown='keyDown();'/>&nbsp;&nbsp;";
        sHTML += "<input type='button' value='登陆' id='btnlogin' onclick='go2Login(1);' style='height:20px;'/>&nbsp;<input type='button' value='注册' onclick='goReg();' style='height:20px;'/>";
        sHTML += "</div>";
        sHTML += "<input id='guestchk' type='checkbox' name='guestchk' onclick='Setguestname(this);' style='float:left;margin-top:4px;border: 0px;' /><div style='float:left;margin:0px 5px 0px 5px;' height:18px;>匿名</div> <input type='text' id='guestname' style='width:65px;display:none;float:left'/>";
    }
    document.getElementById("cnuuulogin").innerHTML = sHTML;
}
//设置首页登录状态
function setUserStateTop() {
    var sHTML = "";
    if (checklogin()) {
        //输出登陆后的页面
        sHTML = "<div class='fR' style='width:auto; margin:-3px -10px 0px 0px; '>用户名：" + NickName + "&nbsp;&nbsp;<input type='button' value='退出' id='btnlogin' onclick='logout(2);' style='height:20px;'/></div>";
    }
    else {
        //输出未登陆后的页面
        sHTML = "<a href='http://ucenter.cnuuu.com/Sendpass.aspx' target='_blank' style='float:right;margin:-8px 0px 0px 12px;background:none;text-decoration: underline;'>忘记密码</a> ";
        sHTML += "<div class='fR' style='width:360px; margin:-3px -70px 0px 0px; '>";
        sHTML += "用户名<input id='useremail' type='text' style='width:65px;' onkeydown='pwdfocus();'/> 密码<input id='userepwd' type='password' style='width:65px;' onkeydown='keyDown();'/>&nbsp;&nbsp;";
        sHTML += "<input type='button' value='登陆' id='btnlogin' onclick='go2Login(2);' style='height:20px;'/>&nbsp;<input type='button' value='注册' onclick='goReg();' style='height:20px;'/></div><form target='_blank' id='reform' method='POST'></form>";
    }
    document.getElementById("cnuuulogin").innerHTML = sHTML;
}
//新首页登录状态
function setUserStateTopNew() {
    var sHTML = "";
    if (checklogin()) {
        //输出登陆后的页面
        sHTML = "<div style='width:auto; margin:0px 0px 0px 600px; '>用户名：" + NickName + "&nbsp;&nbsp;<input type='button' value='退出' id='btnlogin' onclick='logout(6);' style='height:20px;'/></div>";
    }
    else {
        //输出未登陆后的页面
        sHTML = "<label>用户名：</label><input type='text' id='useremail' class='te' onkeydown='pwdfocus();'/> ";
        sHTML += "<label>密码：</label><input id='userepwd' type='password' onkeydown='keyDown();' class='te' /> ";
        sHTML += "<a onclick='go2Login(6);'><img src='/medias/images/bg/btn_01.jpg' width='46' height='20' id='img'/></a> ";
        sHTML += "<u><a href='http://ucenter.cnuuu.com/Register.aspx' target='_blank' style='background:none;text-decoration: underline;'>注册</a></u> <u><a href='http://ucenter.cnuuu.com/Sendpass.aspx' target='_blank' style='background:none;text-decoration: underline;'>找回密码</a></u>";
    }
    document.getElementById("cnuuulogin").innerHTML = sHTML;
}

function setouttop(id) {
    if (oHttpReq.readyState == 4) {
        if (oHttpReq.status == 200) {
            var re
            re = oHttpReq.responseText;
            if (re != "false") {
                if (id == 1) {
                    //输出未登陆后的页面pinglun
                    sHTML = "<div class='fR' style='margin: 0px 20px 0px 0px; float: left; width: 400px;'>";
                    sHTML += "<a href='http://ucenter.cnuuu.com/Sendpass.aspx' target='_blank' style='margin: 4px 0pt 0pt 12px; background: transparent none repeat scroll 0% 0%;text-decoration: underline; color: rgb(51, 51, 51); font-weight: normal; font-size: 12px; float: right;display:inline;'>忘记密码</a>";
                    sHTML += "用户名 <input id='useremail' type='text' style='width:65px;' onkeydown='pwdfocus();'/> 密码 <input id='userepwd' type='password' style='width:65px;' onkeydown='keyDown();'/>&nbsp;&nbsp;";
                    sHTML += "<input type='button' value='登陆' id='btnlogin' onclick='go2Login(1);' style='height:20px;'/>&nbsp;<input type='button' value='注册' onclick='goReg();' style='height:20px;'/>";
                    sHTML += "</div>";
                    sHTML += "<input id='guestchk' type='checkbox' name='guestchk' onclick='Setguestname(this);' style='float:left;margin-top:4px;border: 0px;' /><div style='float:left;margin:0px 5px 0px 5px;' height:18px;>匿名</div> <input type='text' id='guestname' style='width:65px;display:none;float:left'/>";
                    if (parent.document.getElementById("frame_contentzc")) {
                        parent.document.frames('frame_contentzc').location.reload();
                    }
                }
                else if (id == 2) {
                    //输出未登陆后的页面index
                    sHTML = "<a href='http://ucenter.cnuuu.com/Sendpass.aspx' target='_blank' style='float:right;margin:-8px 0px 0px 12px;background:none;text-decoration: underline;'>忘记密码</a> ";
                    sHTML += "<div class='fR' style='width:360px; margin:-3px -70px 0px 0px; '>";
                    sHTML += "用户名<input id='useremail' type='text' style='width:65px;' onkeydown='pwdfocus();'/> 密码<input id='userepwd' type='password' style='width:65px;' onkeydown='keyDown();'/>&nbsp;&nbsp;";
                    sHTML += "<input type='button' value='登陆' id='btnlogin' onclick='go2Login(2);' style='height:20px;'/>&nbsp;<input type='button' value='注册' onclick='goReg();' style='height:20px;'/></div><form target='_blank' id='reform' method='POST'></form>";
                }
                else if (id == 3) {
                    //输出未登陆后的页面list                   
                    sHTML = "<div class='list-new-r-01'><div style='height:40px'></div><div style='height: 30px; width: 240px; margin: auto;'><div class='list-new-r-bt'><a href='javascript:go3Login(3)' TabIndex='3'>登 录</a></div>用户名：<input type='text' id='useremail' onkeydown='pwdfocus();'TabIndex='1' style='width: 120px;' />&nbsp;&nbsp; </div><div style='height: 30px; width: 240px; margin: auto;'><div class='list-new-r-bt'><a href='http://ucenter.cnuuu.com/Sendpass.aspx' target='_blank'>忘记密码</a></div>密&nbsp;&nbsp;&nbsp;&nbsp;码：<input type='password' id='userepwd' onkeydown='keyDown();'TabIndex='2' style='width: 120px;' /></div><div style='height: 30px; width: 230px; margin: auto;'><div class='list-new-r-bt'style='margin-right:21px'><a href='http://ucenter.cnuuu.com/Register.aspx' target='_blank'>注 册</a></div><input type='checkbox' name='checkbox' id='checkbox' style='float: left; margin: 0px 0px 0px 19px' /><div style='float: left; height: 23px; text-align: left; line-height: 175%; margin-left: 5px;'>下次自动登录</div></div>"

                    if (parent.frame_content) {
                        parent.frame_content.location.reload();
                    }
                }
                else if (id == 6) {
                    //输出未登陆后的页面
                    sHTML = "<label>用户名：</label><input type='text' id='useremail' class='te' onkeydown='pwdfocus();'/> ";
                    sHTML += "<label>密码：</label><input id='userepwd' type='password' onkeydown='keyDown();' class='te' /> ";
                    sHTML += "<a onclick='go2Login(6);'><img src='/medias/images/bg/btn_01.jpg' width='46' height='20' id='img'/></a> ";
                    sHTML += "<u><a href='http://ucenter.cnuuu.com/Register.aspx' target='_blank' style='background:none;text-decoration: underline;'>注册</a></u> <u><a href='http://ucenter.cnuuu.com/Sendpass.aspx' target='_blank' style='background:none;text-decoration: underline;'>找回密码</a></u>";
                }
                if (id == 3) {
                    set_innerHTML("cnuuuloginlb", sHTML + re);
                }
                else {
                    set_innerHTML("cnuuulogin", sHTML + re);
                }

            }
        }
    }
}
function logout(id) {
    if (window.XMLHttpRequest) {
        oHttpReq = new XMLHttpRequest();
    }
    else {
        var MSXML = ['MSXML2.XMLHTTP.5.0', 'MSXML2.XMLHTTP.4.0', 'MSXML2.XMLHTTP.3.0', 'MSXML2.XMLHTTP', 'Microsoft.XMLHTTP'];
        for (var n = 0; n < MSXML.length; n++) {
            try {
                oHttpReq = new ActiveXObject(MSXML[n]);
                break;
            }
            catch (e) {
            }
        }
    }
    var myurl = "/apps/LoginSet.aspx?o=q&time=" + new Date();
    oHttpReq.open("POST", myurl, true);
    oHttpReq.onreadystatechange = function() { setouttop(id) }; ;
    oHttpReq.send(null);
}
function goReg() {
    var frm = document.getElementById("reform");
    frm.action = "http://ucenter.cnuuu.com/Register.aspx";
    frm.submit();
}

function pwdfocus() {
    var e = event.srcElement;
    if (event.keyCode == 13) {
        document.getElementById("userepwd").focus();
    }
}
function keyDown() {
    var e = event.srcElement;

    if (event.keyCode == 13) {
        document.getElementById("btnlogin").click();
    }
}
var oHttpReq = null;
//返回登录信息
function gologin() {
    if (oHttpReq.readyState == 4) {
        if (oHttpReq.status == 200) {
            var re
            re = oHttpReq.responseText;
            if (re == "false") {
                alert("用户名或密码错误");
            }
            else {
                setUserState();
                set_innerHTML("cnuuulogin", document.getElementById("cnuuulogin").innerHTML + re);
                if (parent.document.getElementById("frame_contentzc")) {
                    parent.document.frames('frame_contentzc').location.reload();
                }
            }
        }
    }
}
//返回登录信息
function gologin2() {
    if (oHttpReq.readyState == 4) {
        if (oHttpReq.status == 200) {
            var re
            re = oHttpReq.responseText;
            if (re == "false") {
                alert("用户名或密码错误");
            }
            else {
                setUserStateTop();
            }
        }
    }
}
//返回登录信息
function gologin6() {
    if (oHttpReq.readyState == 4) {
        if (oHttpReq.status == 200) {
            var re
            re = oHttpReq.responseText;
            if (re == "false") {
                alert("用户名或密码错误");
            }
            else {
                setUserStateTopNew();
                set_innerHTML("cnuuulogin", document.getElementById("cnuuulogin").innerHTML + re);
            }
        }
    }
}
//返回登录信息
function gologin3() {
    if (oHttpReq.readyState == 4) {
        if (oHttpReq.status == 200) {
            var re
            re = oHttpReq.responseText;
            if (re == "false") {
                alert("用户名或密码错误");
            }
            else {
                setUserListRegisters();
                //window.location.reload();
                set_innerHTML("cnuuuloginlb", document.getElementById("cnuuuloginlb").innerHTML + re);
                if (parent.document.getElementById("frame_content")) {
                    parent.document.frames('frame_content').location.reload();
                }
            }
        }
    }
}
function login(useremail, userpwd, id) {
    if (window.XMLHttpRequest) {
        oHttpReq = new XMLHttpRequest();
    }
    else {
        var MSXML = ['MSXML2.XMLHTTP.5.0', 'MSXML2.XMLHTTP.4.0', 'MSXML2.XMLHTTP.3.0', 'MSXML2.XMLHTTP', 'Microsoft.XMLHTTP'];
        for (var n = 0; n < MSXML.length; n++) {
            try {
                oHttpReq = new ActiveXObject(MSXML[n]);
                break;
            }
            catch (e) {
            }
        }
    }
    var myurl = "/apps/LoginSet.aspx?email=" + useremail + "&pwd=" + userpwd + "&time=" + new Date();
    oHttpReq.open("POST", myurl, true);
    if (id == 1) {
        oHttpReq.onreadystatechange = gologin;
    }
    else if (id = 6) {
        oHttpReq.onreadystatechange = gologin6;
    }
    else {
        oHttpReq.onreadystatechange = gologin2;
    }
    oHttpReq.send(null);
}
function go2Login(id) {
    var useremail = document.getElementById("useremail").value;
    var userepwd = document.getElementById("userepwd").value;
    if (useremail == "" || userepwd == "") {
        alert("用户名或密码错误");
        return;
    }
    login(useremail, userepwd, id);
}

function go3Login(id) {
    var useremail = document.getElementById("useremail").value;
    var userepwd = document.getElementById("userepwd").value;
    var ischeckbox = document.getElementById("checkbox").checked;

    if (useremail == "" || userepwd == "") {
        alert("用户名或密码错误");
        return;
    }
    login1(useremail, userepwd, id, ischeckbox);
}

function login1(useremail, userpwd, id, chk) {
    if (window.XMLHttpRequest) {
        oHttpReq = new XMLHttpRequest();
    }
    else {
        var MSXML = ['MSXML2.XMLHTTP.5.0', 'MSXML2.XMLHTTP.4.0', 'MSXML2.XMLHTTP.3.0', 'MSXML2.XMLHTTP', 'Microsoft.XMLHTTP'];
        for (var n = 0; n < MSXML.length; n++) {
            try {
                oHttpReq = new ActiveXObject(MSXML[n]);
                break;
            }
            catch (e) {
            }
        }
    }
    var myurl = "/apps/LoginSet.aspx?email=" + useremail + "&pwd=" + userpwd + "&chk=" + chk + "&time=" + new Date();
    oHttpReq.open("POST", myurl, true);
    if (id == 1) {
        oHttpReq.onreadystatechange = gologin;
    }
    else if (id == 2) {
        oHttpReq.onreadystatechange = gologin2;
    }
    else if (id == 3) {
        oHttpReq.onreadystatechange = gologin3;
    }
    oHttpReq.send(null);
}
function refresh() {
    setUserState();
}
function SetPLData() {
    var obja = parent.document.getElementById("newsContent");
    var otxt;
    if (obja) {
        otxt = obja.innerHTML.toLowerCase();
        var firsti = otxt.indexOf("<label>") + 7;
        var lasti = otxt.indexOf("</label>");
        document.getElementById("pltitle").value = otxt.substr(firsti, lasti - firsti);
    }
    if (document.getElementById("guestname")) {
        if (document.getElementById("guestname").style.display == '' && document.getElementById("guestname").value != '') {
            document.getElementById("plguestname").value = document.getElementById("guestname").value;
        }
    }
    document.getElementById("plurl").value = parent.location.href;
}
//用于flash小游戏
function SetPLData2() {
    if (document.getElementById("guestname")) {
        if (document.getElementById("guestname").style.display == '' && document.getElementById("guestname").value != '') {
            document.getElementById("plguestname").value = document.getElementById("guestname").value;
        }
    }
    document.getElementById("pltitle").value = parent.title;
    document.getElementById("plurl").value = parent.location.href;
}

function Setguestname(obj) {
    if (obj.checked) {
        document.getElementById("guestname").style.display = '';
    }
    else {
        document.getElementById("guestname").style.display = 'none';
    }
}

function islogout() {
    alert('请登录!');
}

//用于列表页 内容页登录状态
function setUserListRegisters() {
    var sHTML = "";
    if (checklogin()) {
        //输出登陆后的页面
        sHTML = "<div class='list-new-r-01z'><div style='height:40px'></div><div style='height:60px; width:200px; margin:auto;'>" + NickName + "&nbsp;&nbsp;欢迎您登陆犀牛网<br><br>上次登录时间:" + lastLoginTime + "<br><br><a href='http://bbs.cnuuu.com/Circle/CircleUser.aspx?nn=ts&uid=" + UserID + "'target='_blank'>[用户信息]</a>&nbsp;&nbsp;<a href='http://bbs.cnuuu.com' target='_blank'>[论坛]</a>&nbsp;&nbsp;<a href='javascript:logout(3);'>[登出]</a></div></div>"
    }
    else {
        //输出未登陆后的页面
        sHTML = "<div class='list-new-r-01'><div style='height:40px'></div><div style='height: 30px; width: 240px; margin: auto;'><div class='list-new-r-bt'><a href='javascript:go3Login(3)'>登 录</a></div>用户名：<input type='text' id='useremail' onkeydown='pwdfocus();' style='width: 120px;' />&nbsp;&nbsp; </div><div style='height: 30px; width: 240px; margin: auto;'><div class='list-new-r-bt'><a href='http://ucenter.cnuuu.com/Sendpass.aspx' target='_blank'>忘记密码</a></div>密&nbsp;&nbsp;&nbsp;&nbsp;码：<input type='password' id='userepwd' onkeydown='keyDown();' style='width: 120px;' /></div><div style='height: 30px; width: 240px; margin: auto;'> <input type='checkbox' style='margin-left: 35px;' id='checkbox' name='checkbox'> 下次自动登录     &nbsp;&nbsp;&nbsp;<a href='http://ucenter.cnuuu.com/Register.aspx' target='_blank'>注 册</a></div></div>"
    }
    document.getElementById("cnuuuloginlb").innerHTML = sHTML;

}


//执行script
var global_html_pool = [];
var global_script_pool = [];
var global_script_src_pool = [];
var global_lock_pool = [];
var innerhtml_lock = null;
var document_buffer = "";

function set_innerHTML(obj_id, html, time) {
    if (innerhtml_lock == null) {
        innerhtml_lock = obj_id;
    }
    else if (typeof (time) == "undefined") {
        global_lock_pool[obj_id + "_html"] = html;
        window.setTimeout("set_innerHTML('" + obj_id + "', global_lock_pool['" + obj_id + "_html']);", 10);
        return;
    }
    else if (innerhtml_lock != obj_id) {
        global_lock_pool[obj_id + "_html"] = html;
        window.setTimeout("set_innerHTML('" + obj_id + "', global_lock_pool['" + obj_id + "_html'], " + time + ");", 10);
        return;
    }

    function get_script_id() {
        return "script_" + (new Date()).getTime().toString(36)
          + Math.floor(Math.random() * 100000000).toString(36);
    }

    document_buffer = "";

    document.write = function(str) {
        document_buffer += str;
    }
    document.writeln = function(str) {
        document_buffer += str + "\n";
    }

    global_html_pool = [];

    var scripts = [];
    html = html.split(/<\/script>/i);
    for (var i = 0; i < html.length; i++) {
        global_html_pool[i] = html[i].replace(/<script[\s\S]*$/ig, "");
        scripts[i] = { text: '', src: '' };
        scripts[i].text = html[i].substr(global_html_pool[i].length);
        scripts[i].src = scripts[i].text.substr(0, scripts[i].text.indexOf('>') + 1);
        scripts[i].src = scripts[i].src.match(/src\s*=\s*(\"([^\"]*)\"|\'([^\']*)\'|([^\s]*)[\s>])/i);
        if (scripts[i].src) {
            if (scripts[i].src[2]) {
                scripts[i].src = scripts[i].src[2];
            }
            else if (scripts[i].src[3]) {
                scripts[i].src = scripts[i].src[3];
            }
            else if (scripts[i].src[4]) {
                scripts[i].src = scripts[i].src[4];
            }
            else {
                scripts[i].src = "";
            }
            scripts[i].text = "";
        }
        else {
            scripts[i].src = "";
            scripts[i].text = scripts[i].text.substr(scripts[i].text.indexOf('>') + 1);
            scripts[i].text = scripts[i].text.replace(/^\s*<\!--\s*/g, "");
        }
    }

    var s;
    if (typeof (time) == "undefined") {
        s = 0;
    }
    else {
        s = time;
    }

    var script, add_script, remove_script;

    for (var i = 0; i < scripts.length; i++) {
        var add_html = "document_buffer += global_html_pool[" + i + "];\n";
        add_html += "document.getElementById('" + obj_id + "').innerHTML = document_buffer;\n";
        script = document.createElement("script");
        if (scripts[i].src) {
            script.src = scripts[i].src;
            if (typeof (global_script_src_pool[script.src]) == "undefined") {
                global_script_src_pool[script.src] = true;
                s += 2000;
            }
            else {
                s += 10;
            }
        }
        else {
            script.text = scripts[i].text;
            s += 10;
        }
        script.defer = true;
        script.type = "text/javascript";
        script.id = get_script_id();
        global_script_pool[script.id] = script;
        add_script = add_html;
        add_script += "document.getElementsByTagName('head').item(0)";
        add_script += ".appendChild(global_script_pool['" + script.id + "']);\n";
        window.setTimeout(add_script, s);
        remove_script = "document.getElementsByTagName('head').item(0)";
        remove_script += ".removeChild(document.getElementById('" + script.id + "'));\n";
        remove_script += "delete global_script_pool['" + script.id + "'];\n";
        window.setTimeout(remove_script, s + 10000);
    }

    var end_script = "if (document_buffer.match(/<\\/script>/i)) {\n";
    end_script += "set_innerHTML('" + obj_id + "', document_buffer, " + s + ");\n";
    end_script += "}\n";
    end_script += "else {\n";
    end_script += "document.getElementById('" + obj_id + "').innerHTML = document_buffer;\n";
    end_script += "innerhtml_lock = null;\n";
    end_script += "}";
    window.setTimeout(end_script, s);
}
