﻿function $(id) { return document.getElementById(id); }
function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            oldonload();
            func();
        }
    }
}
function addBtn() {    
    if (!$('ibanner') || !$('ibanner_pic')) return;
    var picList = $('ibanner_pic').getElementsByTagName('a');
    if (picList.length == 0) return;
    var btnBox = document.createElement('div');
    btnBox.setAttribute('id', 'ibanner_btn');
    var SpanBox = '';
    for (var i = 1; i <= picList.length; i++) {
        var spanList = '<span class="normal">' + i + '</span>';
        SpanBox += spanList;
    }
    btnBox.innerHTML = SpanBox;
    $('ibanner').appendChild(btnBox);
    $('ibanner_btn').getElementsByTagName('span')[0].className = 'current';
    for (var m = 0; m < picList.length; m++) {
        var attributeValue = 'picLi_' + m
        picList[m].setAttribute('id', attributeValue);
    }
}
function moveElement(elementID, final_x, final_y, interval) {
    if (!document.getElementById) return false;
    if (!document.getElementById(elementID)) return false;
    var elem = document.getElementById(elementID);
    if (elem.movement) {
        clearTimeout(elem.movement);
    }
    if (!elem.style.left) {
        elem.style.left = "0px";
    }
    if (!elem.style.top) {
        elem.style.top = "0px";
    }
    var xpos = parseInt(elem.style.left);
    var ypos = parseInt(elem.style.top);
    if (xpos == final_x && ypos == final_y) {
        moveing = false;
        return true;
    }
    if (xpos < final_x) {
        var dist = Math.ceil((final_x - xpos) / 10);
        xpos = xpos + dist;
    }
    if (xpos > final_x) {
        var dist = Math.ceil((xpos - final_x) / 10);
        xpos = xpos - dist;
    }
    if (ypos < final_y) {
        var dist = Math.ceil((final_y - ypos) / 10);
        ypos = ypos + dist;
    }
    if (ypos > final_y) {
        var dist = Math.ceil((ypos - final_y) / 10);
        ypos = ypos - dist;
    }
    elem.style.left = xpos + "px";
    elem.style.top = ypos + "px";
    var repeat = "moveElement('" + elementID + "'," + final_x + "," + final_y + "," + interval + ")";
    elem.movement = setTimeout(repeat, interval);
}
function classNormal() {
    var btnList = $('ibanner_btn').getElementsByTagName('span');
    for (var i = 0; i < btnList.length; i++) {
        btnList[i].className = 'normal';
    }
}
function picZ() {
    var picList = $('ibanner_pic').getElementsByTagName('a');
    for (var i = 0; i < picList.length; i++) {
        picList[i].style.zIndex = '1';
    }
}
var autoKey = false;
function iBanner() {
    if (!$('ibanner') || !$('ibanner_pic') || !$('ibanner_btn')) return;
    $('ibanner').onmouseover = function() { autoKey = true };
    $('ibanner').onmouseout = function() { autoKey = false };
    var btnList = $('ibanner_btn').getElementsByTagName('span');
    var picList = $('ibanner_pic').getElementsByTagName('a');
    if (picList.length == 1) return;
    picList[0].style.zIndex = '2';
    for (var m = 0; m < btnList.length; m++) {
        btnList[m].onmouseover = function() {
            for (var n = 0; n < btnList.length; n++) {
                if (btnList[n].className == 'current') {
                    var currentNum = n;
                }
            }
            classNormal();
            picZ();
            this.className = 'current';
            picList[currentNum].style.zIndex = '2';
            var z = this.childNodes[0].nodeValue - 1;
            picList[z].style.zIndex = '3';
            if (currentNum != z) {
                picList[z].style.left = '650px';
                moveElement('picLi_' + z, 0, 0, 10);
            }
        }
    }
}

setInterval('autoBanner()', 5000);
function autoBanner() {    
    if (!$('ibanner') || !$('ibanner_pic') || !$('ibanner_btn') || autoKey) return;
    var btnList = $('ibanner_btn').getElementsByTagName('span');
    var picList = $('ibanner_pic').getElementsByTagName('a');
    if (picList.length == 1) return;
    for (var i = 0; i < btnList.length; i++) {
        if (btnList[i].className == 'current') {
            var currentNum = i;
        }
    }
    if (currentNum == (picList.length - 1)) {
        classNormal();
        picZ();
        btnList[0].className = 'current';
        picList[currentNum].style.zIndex = '2';
        picList[0].style.zIndex = '3';
        picList[0].style.left = '650px';
        moveElement('picLi_0', 0, 0, 10);
    } else {
        classNormal();
        picZ();
        var nextNum = currentNum + 1;
        btnList[nextNum].className = 'current';
        picList[currentNum].style.zIndex = '2';
        picList[nextNum].style.zIndex = '3';
        picList[nextNum].style.left = '650px';
        moveElement('picLi_' + nextNum, 0, 0, 10);
    }
}
if (window.addEventListener) {
    window.addEventListener("load", addBtn, false);
    window.addEventListener("load", iBanner,false);
} else if (window.attachEvent) {
    window.attachEvent("onload", iBanner);
    window.attachEvent("onload", addBtn);
}


function switchmodTag(modtag, modcontent, modk) {
    for (i = 1; i < 3; i++) {
        if (i == modk) {

            Get(modtag + i).className = "menuOn1"; Get(modcontent + i).className = "slidingList";
        }
        else {

            Get(modtag + i).className = "menuNo1"; Get(modcontent + i).className = "slidingList_none";
        }
    }
}

function MyCheck(num) {
    if (num == 1) {
        Get("modd1").className = "menuOn";
        Get("modd2").className = "menuNo";
        Get("md2").style.display = "none";
        Get("md1").style.display = "block";
    } else {
        Get("md1").style.display = "none";
        Get("md2").style.display = "block";
        Get("modd2").className = "menuOn";
        Get("modd1").className = "menuNo";

    }
}

function Login() {
    var xmlHttp = CreateXmlHttp();
    xmlHttp.open("post", "Reg.ashx", true);
    xmlHttp.onreadystatechange = function() {
        if (xmlHttp.readyState == 4) {
            if (xmlHttp.status == 200) {
                if (xmlHttp.responseText.substr(0, 1) == "1") {
                    Get("md2").style.paddingTop = "20px";
                    Get("md2").innerHTML = xmlHttp.responseText.substr(1) + "，科技咨询服务协会欢迎您！<span style='color:red;cursor:pointer;' onclick='LogOut();'>注销</span>";

                } else {
                    alert("用户名或密码错误");
                }
            } else {

            }
        }
    }
    //定义传输的文件HTTP头信息
    xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    var postStr = 't=login&uid=' + Get('txtUID').value + "&pwd=" + Get('txtPWD').value;
    xmlHttp.send(postStr);
}

function LogOut() {
    var xmlHttp = CreateXmlHttp();
    xmlHttp.open("post", "Reg.ashx", true);
    xmlHttp.onreadystatechange = function() {
        if (xmlHttp.readyState == 4) {
            if (xmlHttp.status == 200) {
                if (xmlHttp.responseText == "1") {
                    Get("md2").style.paddingTop = "0px";
                    Get("md2").innerHTML = '<div class="huitext" style="padding-top: 20px;"> 用户名：<input id="txtUID" type="text" /><br /> 密 &nbsp; 码： <input id="txtPWD" type="password" size="12" /></div> <div style="text-align: center; padding-top: 15px;"> <input type="button" style="border: 0; width: 57px; height: 23px; background: url(images/about_28.gif) no-repeat;" onclick="Login();" />&nbsp;&nbsp;&nbsp;&nbsp; <input type="button" style="border: 0; width: 57px; height: 23px; background: url(images/about_30.gif) no-repeat;" /> </div> <div style="padding-top: 10px; text-align: right;"> <a class="zi" target="_blank" href="regis.html">现在注册</a></div>';

                } else {
                }
            } else {

            }
        }
    }
    //定义传输的文件HTTP头信息
    xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    var postStr = 't=logout';
    xmlHttp.send(postStr);
}

window.CreateXmlHttp = function() {
    if (window.XMLHttpRequest) {
        return new XMLHttpRequest();
    }
    else {
        return new ActiveXObject("Microsoft.XMLHttp");
            }
        }

        function Get(id) {
            return document.getElementById(id);
        }
