//初始化xmlHttp
function getxmlHttp(){
    var xmlHttp = false;
    try {
        xmlHttp = new XMLHttpRequest();
    } catch (trymicrosoft) {
        try {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (othermicrosoft) {
            try {
                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (failed) {
                xmlHttp = false;
            }
        }
    }
    if (!xmlHttp)
        alert("初始化XMLHttpRequest失败!");
    return xmlHttp;
}
//初始化get参数
var _GET = new Array();
var u=window.location.toString();
u=u.split('?');
if(typeof(u[1]) == 'string'){
    u=u[1].split('&');
    for(i=0;i<u.length;i++)
    {
        s=u[i].split("=");
        eval('_GET["' + s[0] + '"]' + '="' + s[1]+'"');
    }
}

function loading(){
    var width=document.body.clientWidth;
    var height=document.body.clientHeight;
    document.getElementById("modalmask").style.width=width+"px";
    document.getElementById("modalmask").style.height=height+"px";
    document.getElementById("modaltext").style.left=(width-100)/2+"px";
    document.getElementById("modaltext").style.top=(height-52)/2+"px";
    document.getElementById("modalmask").style.visibility="visible";
    document.getElementById("modaltext").style.visibility="visible";
}

function hideloading(){
    document.getElementById("modalmask").style.visibility="hidden";
    document.getElementById("modaltext").style.visibility="hidden";
}

function login(){//登陆提示
    var main2="用户名：<input type=\"text\" id=\"username\" name=\"username\" style=\"width:200px\"/><br />\n 密　码：<input type=\"password\" id=\"password\" name=\"password\" style=\"width:200px\" onkeypress=\"javascript:{if (event.keyCode==13) login2();}\" /><br />\n <input type=\"button\" value=\"登陆\" onclick=\"login2()\" />";
    document.getElementById('typetitle').innerHTML="登陆";
    document.getElementById('typelist').innerHTML="登陆";
    document.getElementById('maincontent2').innerHTML=main2;
    changetitle("登陆");
}

function login2(){//登录检查
    var xmlHttp = getxmlHttp();
    var url = "login.php?username=" + escape(document.getElementById("username").value) + "&password=" + escape(document.getElementById("password").value);
    xmlHttp.open("GET", url, true);
    xmlHttp.onreadystatechange = function (){//获取登陆信息
        if (xmlHttp.readyState == 4 && xmlHttp.status == 200){
            response = xmlHttp.responseText.split("|&|");
            changetitle(response[0]);
            document.getElementById('maincontent2').innerHTML=response[1];
            getLoginMenu();
        }
        hideloading();
    };
    xmlHttp.send(null);
    loading();
}

function getLoginMenu(){//获取上面右边链接
    var xmlHttp = getxmlHttp();
    var url = "inc/getloginmenu.php";
    xmlHttp.open("GET", url, true);
    xmlHttp.onreadystatechange = function (){//获取右边链接
        if (xmlHttp.readyState == 4 && xmlHttp.status == 200){
            response=xmlHttp.responseText.split('|&|');
            document.getElementById('loginmenu').innerHTML=response[1];
            if (response[0]!="") addScript("loginjs",response[0]);
        }
        hideloading();
    };
    xmlHttp.send(null);
    loading();
}

function getTypeInfo(type,page) {//获取Articles目录
    var xmlHttp = getxmlHttp();
    if (!page) page="1";
    if (!type) type="jianjie";
    var url = "articles.php?type=" + escape(type)+"&page="+escape(page);
    xmlHttp.open("GET", url, true);
    xmlHttp.onreadystatechange = function (){//Articles目录
        if (xmlHttp.readyState == 4 && xmlHttp.status == 200){
            response=xmlHttp.responseText.split('|&|');
            title=response[0];
            loadid=response[1];
            types=response[2];
            changetitle(title);
            document.getElementById('typetitle').innerHTML=title;
            document.getElementById('typelist').innerHTML=types;
            if (loadid!=0) getArticlesInfo(loadid);
            if (document.getElementById('typelist').innerHTML=="暂无记录！") document.getElementById('maincontent2').innerHTML="暂无记录!";
        }
        hideloading();
    };
    xmlHttp.send(null);
    loading();
}


function getArticlesInfo(type,page) {//获取Articles内容
    var xmlHttp = getxmlHttp();
    if (type=="") type="1";
    var url = "getarticles.php?id=" + escape(type);
    xmlHttp.open("GET", url, true);
    xmlHttp.onreadystatechange = function (){//Articles内容
        if (xmlHttp.readyState == 4 && xmlHttp.status == 200){
            response=xmlHttp.responseText.split('|&|');
            changetitle(response[0]);
            document.getElementById('maincontent2').innerHTML=response[1];
            getLoginMenu();
        }
        hideloading();
    };
    xmlHttp.send(null);
    loading();
}

function getfilelist(page){//获取文件列表
    var xmlHttp = getxmlHttp();
    if (!page) page="1";
    document.getElementById('typetitle').innerHTML="资源分享";
    document.getElementById('typelist').innerHTML="<li onclick=getfilelist("+page+")><a href=\"javascript:void(0)\">返回资源列表</a></li>";
    if (page!=1) document.getElementById('typelist').innerHTML+="<li onclick=getfilelist(1)><a href=\"javascript:void(0)\">返回列表首页</a></li><br />";
    url = "fileshare.php?page="+page;
    xmlHttp.open("GET", url, true);
    xmlHttp.onreadystatechange = function (){//文件列表
        if (xmlHttp.readyState == 4 && xmlHttp.status == 200){
            response=xmlHttp.responseText;
            changetitle("资源分享");
            document.getElementById('maincontent2').innerHTML=response;
        }
        hideloading();
    };
    xmlHttp.send(null);
    loading();
}

function getfileid(id){//获取文件内容
    var xmlHttp = getxmlHttp();
    var url = "fileshow.php?id="+id;
    xmlHttp.open("GET", url, true);
    xmlHttp.onreadystatechange = function (){//文件内容
        if (xmlHttp.readyState == 4 && xmlHttp.status == 200){
            response=xmlHttp.responseText.split('|&|');
            changetitle(response[0]);
            document.getElementById('maincontent2').innerHTML=response[1];
        }
        hideloading();
    };
    xmlHttp.send(null);
    loading();
}

function gettask(){//浏览任务
    var xmlHttp = getxmlHttp();
    var url = "task/index.php";
    xmlHttp.open("GET", url, true);
    xmlHttp.onreadystatechange = function (){//内容列表
        var msg = document.getElementById("maincontent2");
        if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
            response=xmlHttp.responseText.split('|&|');
            changetitle("任务管理");
            document.getElementById("typetitle").innerHTML="任务管理";
            document.getElementById("typelist").innerHTML=response[0];
            msg.innerHTML = response[1];
        }
        hideloading();
    };
    xmlHttp.send(null);
    loading();
}

function getusertask(id){
    var xmlHttp = getxmlHttp();
    var url = "task/index.php?id="+id;
    xmlHttp.open("GET", url, true);
    xmlHttp.onreadystatechange = function (){//内容列表
        var msg = document.getElementById("maincontent2");
        if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
            response=xmlHttp.responseText.split('|&|');
            changetitle("任务管理");
            document.getElementById("typetitle").innerHTML="任务管理";
            document.getElementById("typelist").innerHTML=response[0];
            msg.innerHTML = response[1];
        }
        hideloading();
    };
    xmlHttp.send(null);
    loading();
}

function contactus(){//联系我们
    document.getElementById('typetitle').innerHTML="联系我们";
    document.getElementById('typelist').innerHTML="联系我们";
    document.getElementById('maincontent2').innerHTML="王飞： E-mail:fei@innlab.net Tel:15973377501 <br />刘鑫标： E-mail:liuxinbiao@innlab.net Tel:15886311597 <br />严晶涛： E-mail:yanjingtao@innlab.net Tel:13873370702 <br /> 实验室地址：湖南工业大学计通大楼401<br/>邮编：412008<br/>网站:<a href=\"http://www.innlab.net\">www.<strong>innlab</strong>.net</a>";
}

function addScript(id,url){//动态加载js
    var script = document.getElementById(id);
    var head = document.getElementsByTagName("head")[0];
    if (!script) {
        script = document.createElement("script");
        script.setAttribute("id", id);
        script.setAttribute("src", url);
        script.setAttribute("type","text/javascript");
        script.setAttribute("language","javascript");
        head.appendChild(script);
    }
    return script;
}

function deleteScript(id){//动态删除js
    var script = document.getElementById(id);
    var head = document.getElementsByTagName("head")[0];
    if (script){
        head.removeChild(script);
    }
}

function onbodyload()//页面打开执行
{
    changetitle("首页");
    getLoginMenu();
    if (_GET['type']) {
        if (_GET['type']=="file") getfilelist();
        else if (_GET['type']=="contactus") contactus();
        else if (_GET['type']=="task") gettask();
        else if (_GET['type']=="login") login();
        else getTypeInfo(_GET['type']);
    }else getTypeInfo("jianjie");
}

function changetitle(name)//更改标题栏
{
    document.title=name+" —— 湖南工业大学学生技术创新实验室";
}