﻿/*各子系统根目录地址*/
var ksshopURL = "http://shop.kingsunsoft.com";  //商城系统
var SysncURL = "http://study.kingsunsoft.com";   //同步系统
var BBSURL = "http://bbs.kingsunsoft.com";   //论坛系统
var ReSourceURL = "http://res.kingsunsoft.com";  //资源系统
var FzVoteURL = "http://www.kingsunsoft.com/fzvote";  //调查系统
var FZwordURL="http://word.kingsunsoft.com";
/***********/

/**********初始化页面xmlhttp访问对象********/
var xmlHttp = false;
var xmlHttp0 = false;
var xmlHttp1 = false;
var pagetyle;
var pagetyleLogin;
var announcelist = document.getElementById("noticelist");
//var logintip = document.getElementById("labLogin");
var member = document.getElementById("member");
var logout = document.getElementById("logout");
var UserTip = document.getElementById("UserTip");
/***/
try {
    xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e) {
    try {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    catch (e2) {
        xmlHttp = false;
    }
}
if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
    xmlHttp = new XMLHttpRequest();
}
/***/
try {
    xmlHttp0 = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e) {
    try {
        xmlHttp0 = new ActiveXObject("Microsoft.XMLHTTP");
    }
    catch (e2) {
        xmlHttp0 = false;
    }
}
if (typeof XMLHttpRequest != 'undefined' && !xmlHttp0) {
    xmlHttp0 = new XMLHttpRequest();
}
/***/
try {
    xmlHttp1 = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e) {
    try {
        xmlHttp1 = new ActiveXObject("Microsoft.XMLHTTP");
    }
    catch (e2) {
        xmlHttp1 = false;
    }
}
if (typeof XMLHttpRequest != 'undefined' && !xmlHttp1) {
    xmlHttp1 = new XMLHttpRequest();
}
/***********判断cookies是否存在***********/
var pageUrl="";
//var pageUrl2="";
pageUrl ="http://"+window.location.host;  //门户根目录地址
//pageUrl2 = window.location.port;
//if (pageUrl2 != "" && pageUrl2 != "80") {
//    pageUrl = pageUrl + pageUrl2;
//}
var loin = document.getElementById("sysinfotip");
var nologin = document.getElementById("nologin");
var strCookie = new String();
strCookie = unescape(document.cookie);
strTT = "fzWebSite=";
//根据cookies来判断用户是否登录
if (strCookie.indexOf(strTT) >= 0) {
    nologin.style.display = 'none';
    loin.style.display = '';
    //存在
    //
    var url = "/DefaultView.aspx?type=webchecklogin";
    url = url + "&random=" + Math.random();
    xmlHttp1.open("GET", url, true);
    xmlHttp1.onreadystatechange = callBack_WebCheckLogin;
    xmlHttp1.send(null);
}
else 
{
    loin.style.display = 'none';
    nologin.style.display = '';
    //不存在
//用户登录验证    
 var v = document.createElement("script");
     v.setAttribute('type', 'text/javascript');
     v.setAttribute('src', pageUrl+"/fzuums/sso/TestPage.aspx?cookieID=fzWebSite&style=setCookie&isValidate=false&appid=KS0502");
     document.getElementsByTagName("head")[0].appendChild(v);
}

var SetSearchUrl = function(selectid, keyword) {
    var URL;
    switch (selectid) {
        case "1":
            URL =  ksshopURL + "/Search.aspx?firstid=0&secondid=0&keyword=" + escape(keyword);
            break;
        case "2":
            URL = SysncURL + "/SynCourse/SearchSyncClass.aspx?st=1&p=1&tt=" + escape(keyword);
            break;
        case "3":
            URL = ReSourceURL + "/Source/SerachSource.aspx?st=0&state=all&w="+escape(keyword);
            break;
        case "4":
            URL = BBSURL + "/search.aspx";
            break;
    }
    return URL;
}
/*******登录系统后初始化开始********/
announcelist.innerHTML = "<img src=\"/App_Themes/Mehu/images/index/loading.gif\" border=\"0\" height=\"150\">";
//logintip.innerHTML = "<img src=\"/App_Themes/Mehu/images/index/loginloading.gif\" border=\"0\">";
UserTip.innerHTML = "&nbsp;<img src=\"/App_Themes/Mehu/images/index/loginloading.gif\" border=\"0\">";

function SubjectListener(type) {
    pagetyle = type;
    var url = "/DefaultView.aspx?type=";
    url = url + type + "&number=5";
    url = url + "&random=" + Math.random();
    xmlHttp.open("GET", url, true);
    xmlHttp.onreadystatechange = callBack_Reluse;
    xmlHttp.send(null);
}
function callBack_Reluse(){
    var Result;
    if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
        Result = xmlHttp.responseText;
        if (pagetyle == "announcement") {
            //显示网站公告
            if (announcelist != null) {
                announcelist.innerHTML = "<ul>" + Result + "</ul>";
            }
        }
    }
}
function SubjectListenerLogin(type) {
    pagetyleLogin = type;
    var url = "/DefaultView.aspx?type=";
    url = url + type;
    url = url + "&random=" + Math.random();
    xmlHttp0.open("GET", url, true);
    xmlHttp0.onreadystatechange = callBack_ReluseLogin;
    xmlHttp0.send(null);
}

function callBack_ReluseLogin() {
    var Result;
    var labLogin = document.getElementById("labLogin");
    if (xmlHttp0.readyState == 4 || xmlHttp0.readyState == "complete") {
        Result = xmlHttp0.responseText;
        if (pagetyleLogin == "login") {
            var arrLoadName = new Array();
            arrLoadName = Result.split(",");
            if (labLogin != null || labLogin != undefined) {
                if (arrLoadName[0] == "true") {
                    GetMemberStr(arrLoadName[1]);
                }
               // labLogin.innerHTML = arrLoadName[1];
            }
            if (arrLoadName[0] == "true") {
                UserTip.innerHTML = arrLoadName[2] + " ！您可以：";
            }
            else if (arrLoadName[0] == "false") {
            //未登录
            UserTip.innerHTML = "免费的注册会员您可以：";
            }
        }
    }
}

function callBack_WebCheckLogin() {
    var checkResult;
    if (xmlHttp1.readyState == 4 || xmlHttp1.readyState == "complete") {
        checkResult = xmlHttp1.responseText;
        if (checkResult == "offline") {
            loin.style.display = 'none';
            nologin.style.display = '';
        }
        else if (checkResult == "online")
        {}
    }
}

/*获取登录后的帐号和退出字符串*/
var GetMemberStr = function(txtload) {
    var responseTXT = new Array();
    var m = txtload;
   // for (var i = 0; i < m.length; i++) {
    m = m.ReplaceAll("&nbsp;", "");
    m = m.ReplaceAll("[", ",");
    m = m.ReplaceAll("]", "");
    m = m.ReplaceAll("我的帐户", "会员中心");
    m = m.ReplaceAll("consume.aspx", "Member.aspx");
    m = m.ReplaceAll("href=\"#none\"", "");
    m = m.ReplaceAll("onclick", "href");
    m = m.ReplaceAll("window.parent.location", "window.location");
    m = m.ReplaceAll("退出", "退 出");
   // }
    responseTXT = m.split(",");
    member.innerHTML = responseTXT[1];   //会员中心
    logout.innerHTML = responseTXT[2];   //退出
}
/*******登录系统后初始化结束********/

/**********系统登录开始*******/
var CheckLogin = function() {
    var _username = document.getElementById("username").value;
    var _password = document.getElementById("password").value;
    var _inputCode = document.getElementById("keycodes").value;

    //输入性判断
    var flag = CheckInput(_username, _password, _inputCode);
    var codes = false;
    if (flag) {
        //验证码判断
        codes = validate(_inputCode);
		  /*2010.4.24修改（因为+号被自动转成空格）*/
        /*对用户名进行编码*/
        _username=encodeURIComponent(_username);
        /*对密码进行编码*/
        _password=encodeURIComponent(_password);
        if (codes) {
            var nologinHtml = nologin.innerHTML;
            nologin.innerHTML = "<img src=\"/App_Themes/Mehu/images/index/loading.gif\" border=\"0\">";
            var _appid = "KS0502";
            var _cookiesid = "fzWebSite";
            var _style = "login";
            var loginstate;
            var loginstr = document.createElement("script");
            loginstr.setAttribute('type', 'text/javascript');
            var logurl = pageUrl + "/fzuums/SSO/logininterface.aspx?username=" + _username + "&password=" + _password + "&style=" + _style + "&cookieID=" + _cookiesid + "&appID=" + _appid;
            logurl = logurl + "&random=" + Math.random();
            loginstr.setAttribute('src', logurl);
            document.getElementsByTagName("head")[0].appendChild(loginstr);
            if (document.getElementsByTagName("html")) {
                loginstr.onload = loginstr.onreadystatechange = function() {
                var state = "";
                if (document.all != undefined) {
                    state = loginstr.readyState;  // firefox里不兼容readyState
                }
                if (state == "" || state == "loaded" || state == "interactive" || state == "complete") {
                    if (loginresult.toLowerCase() == "false") {
                        alert("登录失败原因可能是：\n\n" + message.ReplaceAll("<br>", "\n"));
                        loin.style.display = 'none';
                        nologin.style.display = '';
                        nologin.innerHTML = nologinHtml;
                        // window.location.reload();
                    }
                    else if (loginresult.toLowerCase() == "securitykeyerr") {
                        alert("安全检验码出错，请重新登录！");
                        loin.style.display = 'none';
                        nologin.style.display = '';
                        nologin.innerHTML = nologinHtml;
                        //window.location.reload();
                    }
                    else if (loginresult.toLowerCase() == "success") {
                        loin.style.display = '';
                        nologin.style.display = 'none';
                        //已登录
                        SubjectListenerLogin("login");
                    }
                }

                }
            }

        }
    }
}
/*******登录输入项目检查**************/
var CheckInput = function(username, password, codes) {
    if (username.trim() == "") {
        alert("请输入用户名!");
        return false;
    }
    else if (checkLen(username.trim()) > 20) {
        alert("用户名已超过最大长度！");
        return false;
    }
    if (password.trim() == "") {
        alert("请输入密码！");
        return false;
    }
    else if (checkLen(password.trim()) > 20) {
        alert("密码已超过最大长度！");
        return false;
    }
    if (codes.trim() == "") {
        alert("请输入验证码！");
        return false;
    }
    else if (checkLen(codes.trim()) > 4) {
        alert("验证码已超过最大长度！");
        return false;
    }
    return true;
}
/******登录输入项目检查结束*************/


/******页面中搜索功能开始*********/
var SelectChoose = function() {
    var chooseid = document.getElementById("serachchoose");
    if (chooseid.value == "") return;
    switch (chooseid.value) {
        case "1":
            show('0');
            break;
        case "2":
            show('1');
            break;
        case "3":
            show('2');
            break;
        case "4":
            show('3');
            break;
    }
}
function show(objid) {
    switch (objid) {
        case "0":
            document.getElementById("menu0").style.display = '';
            document.getElementById("menu1").style.display = 'none';
            document.getElementById("menu2").style.display = 'none';
            document.getElementById("menu3").style.display = 'none';
            break;
        case "1":
            document.getElementById("menu0").style.display = 'none';
            document.getElementById("menu1").style.display = '';
            document.getElementById("menu2").style.display = 'none';
            document.getElementById("menu3").style.display = 'none';
            break;
        case "2":
            document.getElementById("menu0").style.display = 'none';
            document.getElementById("menu1").style.display = 'none';
            document.getElementById("menu2").style.display = '';
            document.getElementById("menu3").style.display = 'none';
            break;
        case "3":
            document.getElementById("menu0").style.display = 'none';
            document.getElementById("menu1").style.display = 'none';
            document.getElementById("menu2").style.display = 'none';
            document.getElementById("menu3").style.display = '';
            break;
        default:
            break;
    }
}
var setUrl = function() {
    var chooseid = document.getElementById("serachchoose");
    var keyword = document.getElementById('txtSerValue').value;
    if (keyword.trim() == '请输入您要搜索的关键字' || keyword.trim() == "") {
        //  document.getElementById('txtSerValue').value = '';
        document.getElementById('txtSerValue').style.color = '#91918f';
        alert("请输入您要搜索的关键字！");
        return false;
    }
    var url = SetSearchUrl(chooseid.value, keyword.trim());
    if (chooseid.value == '4') {
        //构造from提交
        CreateForm(url, keyword.trim());
    }
    else {
        window.open(url);
    }
}
//热点搜索
var HotUrl = function(sid, word) {
    //sid对应页面上搜索下拉框里各子系统的选中值
    var hoturl = SetSearchUrl(sid, word);
    //window.open(hoturl);
    return hoturl;
}

var CreateForm = function(action, keyword) {
    var f = document.createElement("<form action='" + action + "'   method='post' target='_blank'>");
    var i0 = document.createElement("<input type=\"hidden\" name=\"poster\" />");
    var i1 = document.createElement("<input type=\"hidden\" name=\"keyword\" value=\"" + keyword + "\"/>");
    var i2 = document.createElement("<input type=\"hidden\" name=\"type\" value=\"\" />");
    var i3 = document.createElement("<input id=\"keywordtype\" type=\"hidden\" name=\"keywordtype\" value=\"0\"/>");
    f.appendChild(i0);
    f.appendChild(i1);
    f.appendChild(i2);
    f.appendChild(i3);
    document.appendChild(f);
    f.submit();
}
/******页面中搜索功能结束*********/


/****去空格函数*********/
String.prototype.trim = function() {
    return this.replace(/(^\s*)|(\s*$)/g, "");
}

/*****替换字符串在所有相同的字符*********/
String.prototype.ReplaceAll = function(stringToFind, stringToReplace) {
    var result = this;
    var index = result.indexOf(stringToFind);
    while (index != -1) {
        result = result.replace(stringToFind, stringToReplace);
        index = result.indexOf(stringToFind);
    }
    return result;
}

/*********检查输入的字符的长度***********/
function checkLen(str) {
    var x = 0;
    for (i = 0; i < str.length; i++) {
        if (str.charCodeAt(i) > 128) {
            x = x + 2;
        } else {
            x = x + 1;
        }
    }
    return x;
}
/****************/

/****初始化页面地址******/
var InItPageUrl = function() {
    //商城连接地址
    var ksshopindex = document.getElementById("ksshopindex");
    if (ksshopindex != null) {
        ksshopindex.href = ksshopURL;
    }
    for (q = 0; q < 6; q++) {
        var ksshopurl = document.getElementById("ksshop" + q);
        if (ksshopurl != null) {
            //  ksshopurl.href = ksshopURL + GetPageURL(ksshopurl.href);
            ksshopurl.href = HotUrl('1', ksshopurl.innerHTML);
        }
    }
    
    //调查系统连接地址
    var fzvote = document.getElementById("fzvote");
    if (fzvote != null) {
        fzvote.href = FzVoteURL + GetPageURL(fzvote.href);
    }
    //同步系统连接地址
    var sysncindex = document.getElementById("sysncindex");
    if (sysncindex != null) {
        sysncindex.href = SysncURL;
    }
    for (k = 0; k < 6; k++) {
        var sysncurl = document.getElementById("sysnc" + k);
        if (sysncurl != null) {
            //  sysncurl.href = SysncURL + GetPageURL(sysncurl.href);
            sysncurl.href = HotUrl('2', sysncurl.innerHTML);
        }
    }
    //论坛连接地址
    var bbsindex = document.getElementById("bbsindex");
    if (bbsindex != null) {
        bbsindex.href = BBSURL;
    }
    for (j = 0; j < 7; j++) {
        var bbs = document.getElementById("bbs" + j);
        if (bbs != null) {
            bbs.href = BBSURL + GetPageURL(bbs.href);
        }
    }
    //教育资源连接地址
    var resouceindex = document.getElementById("resouceindex");
    if (resouceindex != null) {
        resouceindex.href = ReSourceURL;
    }
    for (h = 0; h < 7; h++) {
        var resources = document.getElementById("resources" + h);
        if (resources != null) {
            //resources.href = ReSourceURL  + GetPageURL(resources.href);
            resources.href = HotUrl('3', resources.innerHTML);
        }
    }
    //UUMS新用户注册
    var uumsreg = document.getElementById("uumsreg");
    if (uumsreg != null) {
        uumsreg.href = pageUrl + GetPageURL(uumsreg.href) + "?backurl=" + pageUrl;
    }

    //各系统根目录地址
    for (g = 0; g < 6; g++) {
        var sysrooturl = document.getElementById("rooturl" + g);
        var rootURL;
        if (sysrooturl != null) {
            switch (g) {
                case 0:
                    rootURL = ksshopURL;
                    break;
                case 3:
                    rootURL = pageUrl;
                    break;
                case 1:
                    rootURL = SysncURL;
                    break;
                case 2:
                    rootURL = ReSourceURL;
                    break;
                case 4:
                    rootURL = BBSURL;
                    break;
                case 5:
                    rootURL = FZwordURL;
                    break;
            }
            sysrooturl.href = rootURL + GetPageURL(sysrooturl.href);
        }
        else {
            break;
        }
    }

}
var GetPageURL = function(str) {
    var strurl;
    var m = str.indexOf("#");
        strurl = str.substring(m + 1, str.length);
        return strurl;
}
/*******初始化页面地址结束**************/
/*******设置页面默认按钮****************/
document.onkeydown = keyDown;
function keyDown(e) {
    var theEvent = window.event || e;
    var code = theEvent.keyCode || theEvent.which;
    if (code == 13) {
        var loginButton = document.getElementById("loginbtn");
        var _username = document.getElementById("username");
        var _password = document.getElementById("password");
        if (_username != null && _password != null) {
            if (_username.value.trim() != "" && _password.value.trim() != "") {
                if (loginButton != null) {
                    if (document.all)
                        loginButton.click();
                    else {
                        CheckLogin(); 
//                        var evt = document.createEvent("MouseEvents");
//                        evt.initEvent("click", true, true);
//                        loginButton.dispatchEvent(evt);
                    }   
                }
            }
        }
    }
}
/***********************/
//系统公告显示
SubjectListener("announcement");
//用户登录显示
SubjectListenerLogin("login");
//初始化页面各系统地址
InItPageUrl();





