﻿/*各子系统根目录地址*/
var ksshopURL = document.getElementById("ksshopURL").value;  //商城系统
var paurl=document.getElementById("paurl").value;

/**********初始化页面xmlhttp访问对象********/
var xmlHttp = false;
var xmlHttp0 = false;
var xmlHttp1 = false;
var pagetyle;
var pagetyleLogin;
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 loin = document.getElementById("sysinfotip");
var nologin = document.getElementById("nologin");
var strCookie = new String();
strCookie = unescape(document.cookie);
strTT = "fzShop=";
//根据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', paurl+"/sso/TestPage.aspx?cookieID=fzShop&style=setCookie&isValidate=false&appid=KS0401&webServiceURL="+ksshopURL+"/EvaluateWeb/KSShopUser.asmx/UpdateUserLoginTimes?UumsID=");
     document.getElementsByTagName("head")[0].appendChild(v);
}


/*******登录系统后初始化开始********/
UserTip.innerHTML = "&nbsp;<img src=\""+ksshopURL+"/App_Themes/Default/Images/common/ajax-loader.gif\" border=\"0\">";

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 flag = CheckInput(_username, _password);
    if (flag) {
       /*2010.4.24修改（因为+号被自动转成空格）*/
        /*对用户名进行编码*/
        _username=encodeURIComponent(_username);
        /*对密码进行编码*/
        _password=encodeURIComponent(_password);
            var nologinHtml = nologin.innerHTML;
            nologin.innerHTML = "<img src=\""+ksshopURL+"/App_Themes/Default/Images/common/ajax-loader.gif\" border=\"0\">";
            var _appid = "KS0401";
            var _cookiesid = "fzShop";
            var _style = "login";
            var loginstate;
            var loginstr = document.createElement("script");
            loginstr.setAttribute('type', 'text/javascript');
            var logurl = paurl + "/SSO/logininterface.aspx?username=" + _username + "&password=" + _password + "&style=" + _style + "&cookieID=" + _cookiesid + "&appID=" + _appid+"&webServiceURL="+ksshopURL+"/EvaluateWeb/KSShopUser.asmx/UpdateUserLoginTimes?UumsID=";
            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;
    }
    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 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;
}
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() {
    //UUMS新用户注册
    var uumsreg = document.getElementById("uumsreg");
    var findpwd=document.getElementById("findpwd");
    if (uumsreg != null) {
        findpwd.href=paurl + GetPageURL(findpwd.href);
        uumsreg.href = paurl + GetPageURL(uumsreg.href) + "?backurl=" + ksshopURL;
    }
}
var GetPageURL = function(str) {
    var strurl;
    var m = str.indexOf("#");
        strurl = str.substring(m + 1, str.length);
        return strurl;
}

//用户登录显示
SubjectListenerLogin("login");
//初始化页面各系统地址
InItPageUrl();

//改变页面中播放的flv视频
function ChageFLV(objFLV, objtitle, objName) {
    document.getElementById("flvtitle").innerHTML = objtitle;
    document.getElementById("flvname").innerHTML = objName;
	var swfHtml = "<OBJECT codeBase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0\"";
    swfHtml +=" height=\"160\" width=\"175\" classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\">";
    swfHtml += "<PARAM NAME=\"allowScriptAccess\" VALUE=\"sameDomain\"/>";
    swfHtml += "<PARAM NAME=\"movie\" VALUE=\""+objFLV+"\"/>";
    swfHtml += "<PARAM NAME=\"quality\" VALUE=\"high\"/><PARAM NAME=\"bgcolor\" VALUE=\"#F0F0F0\"/>";
    swfHtml += "<PARAM NAME=\"menu\" VALUE=\"false\"/>";
    swfHtml +="<embed src=\"" + objFLV + "\" quality=\"high\" width=\"175\" height=\"160\" align=\"middle\" allowScriptAccess=\"sameDomain\" type=\"application/x-shockwave-flash\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\"></embed>";
    swfHtml +="</object>";
    //modify 20090121
    document.getElementById("voide").innerHTML = swfHtml;
}

////enter键绑定查询按钮
//document.onkeydown=keydown;
//function  keydown(e) 
//{    
//    //使用document.getElementById获取到按钮对象   
//    var button = document.getElementById('btnSearch');
//    var theEvent = window.event || e;
//    var code = theEvent.keyCode || theEvent.which;
//     if(code == 13)        
//     { 
//       if(document.all)
//       {      
//       button.click();            
//       event.returnValue = false;    
//       }
//       else
//       { 
//        Search_Click();
//        e.preventDefault();  
//       }     
//     }       
//}





