﻿// utility function to retrieve an expiration data in proper format;
function getExpDate(days, hours, minutes) {
    var expDate = new Date();
    if (typeof (days) == "number" && typeof (hours) == "number" && typeof (hours) == "number") {
        expDate.setDate(expDate.getDate() + parseInt(days));
        expDate.setHours(expDate.getHours() + parseInt(hours));
        expDate.setMinutes(expDate.getMinutes() + parseInt(minutes));
        return expDate.toGMTString();
    }
}

//utility function called by getCookie()
function getCookieVal(offset) {
    var endstr = document.cookie.indexOf(";", offset);
    if (endstr == -1) {
        endstr = document.cookie.length;
    }
    return unescape(document.cookie.substring(offset, endstr));
}

// primary function to retrieve cookie by name
function getCookie(name) {
    var arg = name + "=";
    var alen = arg.length;
    var clen = document.cookie.length;
    var i = 0;
    while (i < clen) {
        var j = i + alen;
        if (document.cookie.substring(i, j) == arg) {
            return getCookieVal(j);
        }
        i = document.cookie.indexOf(" ", i) + 1;
        if (i == 0) break;
    }
    return;
}

// store cookie value with optional details as needed
function setCookie(name, value, expires, path, domain, secure) {
    
    document.cookie = name + "=" + escape(value) +
            ((expires) ? "; expires=" + expires : "") +
            ((path) ? "; path=" + path : ";path=/") +
            ((domain) ? "; domain=" + domain : "") +
            ((secure) ? "; secure" : "");
}

// remove the cookie by setting ancient expiration date
function deleteCookie(name, path, domain) {
    if (typeof (getCookie(name)) != "undefined") {
        document.cookie = name + "=" +
                ((path) ? "; path=" + path : ";path=/") +
                ((domain) ? "; domain=" + domain : "") +
                "; expires=Thu, 01-Jan-90 00:00:01 GMT";
        //Init();
        window.location.href="index.html";
    }
}
///登陆初始化
function Init() {
    var username = getCookie("userID");
    var content = "";
    if (typeof (username) != "undefined") {
        content = "<div>会员：" + getCookie("userName") + "</div>";
        content += "<div>" + getCookie("loginTime") + " 已成功登录！</div>";
        content += "<div><input type=\"button\" onclick=\"javascript:location.href=\'messageList.html?message=user\';\" class=\"bx_wdtwbotton\" value=\"\" /> <input type=\"button\" onclick=\"javascript:location.href=\'message.html\';\" class=\"bx_wytwbotton\" value=\"\" /></div>";
        content += "<div><a href=\"#\" onclick=\"deleteCookie(\'userID\')\">安全退出</a> <a href=\"userPass.html\" target=\"_blank\">修改密码</a></div>";
    } else {
    content = " <div>用户： <input id=\"userName\" type=\"text\" style=\"width:150px\" \/><\/div>";
    content += " <div>密码：&nbsp;<input id=\"userPass\" type=\"password\" style=\"width:150px\" \/><\/div>";
    content += "<div><input type=\"button\" class=\"bx_dlbotton\" value=\"\" onclick=\"userLogin()\"/>";
    content += " <input type=\"button\" class=\"bx_zcbotton\" onclick=\"javascript:window.open('reg/useragreement.html')\" value=\"\" \/> <\/div>";
}
document.getElementById("userLogin").innerHTML = content;
}
///用户登陆
function userLogin() {
    if (userName.value == '') {
        alert("用户名不能为空");
        return;
    }
    if (userPass.value == '') {
        alert("密码不能为空");
        return;
    }
    var reg = new RegExp("[\\`,\\~,\#,\\$,\\%,\\^,\\+,\\*,\\\\,\\/,\\<,\\>,\\',\"]");
    if (reg.test(userName.value)) {
        alert("有非法字符，请重新输入");
        userName.value = '';
        return;
    }
    jQuery.ajax({
        type: "POST",
        url: "/html/bdgov/bxjy/messageService.asmx/userLogin",
        dataType: "json",
        data: "{userName:'" + userName.value + "',userPass:'" + userPass.value + "'}",
        contentType: "application/json",
        success: function(message) {
            if (typeof (message) != "undefined") {
                if (message == '') {
                    alert('用户名或密码错误');
                } else {
                    var result = jQuery.parseJSON(message);                    
                    setCookie("userID", result[1]);
                    setCookie("userName",result[0]);
                    setCookie("loginTime",result[3]);                    
                    document.getElementById("userLogin").innerHTML = result[2];
                }
            }
        },
        error: function(result, status) {
            if (status == 'error') {
                alert('error');
            }
        }
    });
}

//初始化回复率排行
function setReplyRate()
{
    jQuery.ajax({
        url: "/html/bdgov/bxjy/messageService.asmx/GetDepartmentReplyRate",
        type: "POST",
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        data: "{}",
        success: function(jsonStr){
            //var obj=jQuery.parseJSON(jsonStr);
            document.getElementById("depReplyRate").innerHTML=jsonStr;                             
        },
        error: function(x, e) { 
            alert("回复率排行错误");
        }
    });
}
///地址栏接受参数
function QueryString() {
    var name, value, i;
    var str = location.href;
    var num = str.indexOf("?");
    str = str.substr(num + 1);
    var arrtmp = str.split("&");
    for (i = 0; i < arrtmp.length; i++) {
        num = arrtmp[i].indexOf("=");
        if (num > 0) {
            name = arrtmp[i].substring(0, num);
            value = arrtmp[i].substr(num + 1);
            this[name] = value;
        }
    }
}     
