﻿//项目共用的JS
//用于存放当前进行登录用户
var nowLoginUserName = "";
//用于存放登录后是否重新加载页面
var isLoginRefresh = "true";
//设置登录后是否重新加载页面
function setLoginRefresh(isRefresh)
{
	isLoginRefresh=isRefresh;
}
//检测用户cookie
function checkSession(){
    return getCookie("UkoubeiUserName");
}

//检查session是否存在，如果存在则返回true，否则返回false，并显示登录界面
function checkSession_Login(){
    if (checkSession() == null) {
        showLogin();
        return false;
    }
    else {
        return true;
    }
}

//显示登录层
function showLogin(){
    $("fullScreen4").style.display = "block";
    $("fullScreen4").style.height = document.documentElement.scrollHeight + "px";
    var divL = $("divLogin");
    $("txtUserName").value = "";
    $("txtPassword").value = "";
    
    divL.style.left = (document.documentElement.clientWidth - 400) / 2 + "px";
    divL.style.top = (document.documentElement.clientHeight - 240) / 2 + document.documentElement.scrollTop + "px";
    divL.style.display = "block";
}

//关闭登录层
function closeLogin(){
    $("fullScreen4").style.display = "none";
    $("divLogin").style.display = "none";
}

//登录
function doLogin(type){
    var txtUName = "";
    var txtPass = "";
    var isSaveLogin = "false";
    //彈出登錄框登錄
    if (type == 0) {
        txtUName = $F("txtUserName").strip();
        txtPass = $F("txtPassword").strip();
        if ($("checkIsSaveLogin").checked) {
            isSaveLogin = "true";
        }
    }
    else 
        if (type == 1) {
            txtUName = $F("txtIndexUserName").strip();
            txtPass = $F("txtIndexPassword").strip();
            if ($("checkIndexIsSaveLogin").checked) {
                isSaveLogin = "true";
            }
        }
    if (txtUName == "" || txtPass == "") {
        alert("用戶名和密碼都不能為空！");
        return false;
    }
    else {
        nowLoginUserName = txtUName;
        var url = "/AjaxHandler/Ajax_UserLogin.ashx?UserName=" + encodeValue(txtUName) + "&PassWord=" + encodeValue(txtPass) + "&IsSaveLogin=" + isSaveLogin + "&Random=" + Math.random();
        new Ajax.Request(url, {
            method: "get",
            onSuccess: completeLogin
        });
    }
}


//登录结果
function completeLogin(transport){
    var checkLoginResult = transport.responseText;
    if (checkLoginResult == "Reject") {
        alert("用戶名或者密碼錯誤！");
        return false;
    }
    else 
        if (checkLoginResult == "NoVal") {
            alert("該用戶還沒有激活，請通過註冊郵箱的驗證郵件進行激活！");
            return false;
        }
        else 
            if (checkLoginResult == "Error") {
                alert("發生了系統錯誤，登錄失敗！");
                return false;
            }
            else 
                if (checkLoginResult == "Pass") {
                    alert("登錄通過！");
					if (isLoginRefresh == "true") {
						window.location.href = window.location.href;
					}
					else
					{
						$("txtUserName").value="";
						$("txtPassword").value="";
						if($("spanLogin")!=null)
						{
						    $("spanLogin").innerHTML="歡迎您：" + nowLoginUserName + "&nbsp&nbsp<a href='javascript:doTopLogout()'>[安全退出]</a>&nbsp&nbsp|&nbsp&nbsp<a href='/user/UserMain.aspx?uName=" + nowLoginUserName + "'>進入個人專頁</a>";
						}
						closeLogin();
					}
                    return true;
                }
}


//打开居中层，同时背景置灰
function doCenter(cWidth, cHeight, strContent, strCommand, commandHeight){
    $("fullScreen3").style.display = "block";
    $("fullScreen3").style.height = document.documentElement.scrollHeight + "px";
    var divC = $("divCenter");
    var divContent = $("divCenterContent");
    var divCommand = $("divCenterCommand");
    
    divC.style.width = cWidth + "px";
    divC.style.height = cHeight + "px";
    divC.style.left = (document.documentElement.clientWidth - cWidth) / 2 + "px";
    divC.style.top = (document.documentElement.clientHeight - cHeight) / 2 + document.documentElement.scrollTop + "px";
    divContent.style.height = cHeight - commandHeight + "px";
    divContent.innerHTML = strContent;
    divCommand.innerHTML = strCommand;
    divC.style.display = "block";
}

//在居中层中打开一个元素对象
function doCenter_Object(cWidth, cHeight, objOpen){
    $("fullScreen3").style.display = "block";
    $("fullScreen3").style.height = document.documentElement.scrollHeight + "px";
    var divC = $("divCenter");
    var divContent = $("divCenterContent");
    var divCommand = $("divCenterCommand");
    
    divC.style.width = cWidth + "px";
    divC.style.height = cHeight + "px";
    divC.style.left = (document.documentElement.clientWidth - cWidth) / 2 + "px";
    divC.style.top = (document.documentElement.clientHeight - cHeight) / 2 + document.documentElement.scrollTop + "px";
    divContent.style.height = cHeight;
    divContent.appendChild(objOpen);
    divCommand.style.height = "0px";
    divC.style.display = "block";
}

//在居中层中打开页面
function openPage(url, width, height){
    strHtml = "<iframe src='" + url + "' frameborder=no scrolling=no width=" + width + " height='" + height + "'></iframe>";
    doCenter(width, height, strHtml, "", 0);
}

//关闭居中层
function closeCenter(){
    $("divCenterContent").innerHTML = "";
    $("divCenterCommand").innerHTML = "";
    $("fullScreen3").style.display = "none";
    $("divCenter").style.display = "none";
}

function iframeFitHeight(oIframe){//Iframe窗口自适应高度 兼容IE6.0 FF2.0以上  
    try {
        var oWin = oIframe.name ? window.frames[oIframe.name] : oIframe.contentWindow;
        oIframe.style.height = oWin.document.body.scrollHeight + "px";
    } 
    catch (e) {
    }
}

//进行url编码
function encodeValue(val){
    //注意encodeURIComponent是进行UTF-8编码的，因此无法在使用字符集gb2312的情况下使用
    var encodedVal;
    //    if (!encodeURIComponent) 
    //    {
    encodedVal = escape(val);
    /* fix the omissions */
    encodedVal = encodedVal.replace(/@/g, '%40');
    encodedVal = encodedVal.replace(/\//g, '%2F');
    encodedVal = encodedVal.replace(/\+/g, '%2B');
    //    }
    //    else 
    //    {
    //     encodedVal = encodeURIComponent(val);
    //     /* fix the omissions */
    //     encodedVal = encodedVal.replace(/~/g, '%7E');
    //     encodedVal = encodedVal.replace(/!/g, '%21');
    //     encodedVal = encodedVal.replace(/\(/g, '%28');
    //     encodedVal = encodedVal.replace(/\)/g, '%29');
    //     encodedVal = encodedVal.replace(/'/g,'%27');
    //     }
    return encodedVal;
}

