﻿//全局变量
var IntervalTime = 5000;
var MaxTry = 40;
var TryCount = 0;
var IsLogined = false;
var KugooID = 0;
var NickName = "";
var IntervalID = -1;
function GetUserCookie() {
    var MyCookie = document.cookie.toLowerCase();
    re = /kugooid=(\d+)&/g;
    if (re.test(MyCookie)) {
        IsLogined = true;
        KugooID = RegExp.$1;
        re2 = /nickname=([^&]+)/g;
        re2.test(MyCookie);
        NickName = unescape(RegExp.$1).replace("+", " ").replace("+", " ");
        return KugooID;
    }
}
function ShowAllDiv() {
    var str = "";
    str += "<span id='LoginedDiv2' style='display:none;'></span>";
    str += "<span id='UnLoginedDiv2' style='display:none;'><form name='loginform' method='post' action='' onsubmit='return LoginCheck(this);'>\
                    <table border='0' cellspacing='2' cellpadding='0' style='float: left; margin: 5px 10px;'>\
                      <tr>\
                        <td width='70' height='25' align='center'>酷狗帐号:</td>\
                        <td width='90' align='left'>\
                          <input name='UserName' type='text' size='15' />\
                        </td>\
                        <td width='50' align='center'>密码:</td>\
                        <td width='90' align='left'>\
                          <input name='UserPwd' type='password' size='15' />\
                        </td>\
                        <td width='63' align='center'>\
                          <input name='Submit' type='submit' value='' style='border:0;width:53px;height:19px;background:url(http://image3.kugou.com/entpic/images/sub_23.gif);' />\
                        </td>\
                      </tr>\
                    </table>\
                  </form></span>";
    document.write(str);
}
function ShowLogined() {
    var LoginedDiv = document.getElementById("LoginedDiv2");
    var UnLoginedDiv = document.getElementById("UnLoginedDiv2");
    if (IsLogined) {
        //LoginedDiv.innerHTML = LoginedDiv.innerHTML.replace("{KugooID}",KugooID).replace("{NickName}",NickName);
        LoginedDiv.style.display = "block";
        UnLoginedDiv.style.display = "none";
    } else {
        LoginedDiv.style.display = "none";
        UnLoginedDiv.style.display = "block";
    }
}
GetUserCookie();
ShowAllDiv();
ShowLogined();

function TryReadCookie() {
    TryCount++;
    if (TryCount < MaxTry) {
        GetUserCookie();
        if (IsLogined) {
            window.clearInterval(IntervalID);
            ShowLogined();
        }
    }
    else {
        window.clearInterval(IntervalID);
    }
}
if (!IsLogined) {
    IntervalID = window.setInterval(TryReadCookie, IntervalTime);
}
function LoginCheck(form) {
    if (form.UserName.value.length == 0 || form.UserPwd.value.length == 0) {
        alert('请先填用户名和密码！');
        return false;
    }
    form.action = "http://www.kugou.com/user/LoginCheck2.aspx?url=" + location.href;
    return true;
}