﻿function BOX_show(e,a)//显示
{
    if (document.getElementById(e) == null) {
        return;
    }
    
    if (!a) {
        var selects = document.getElementsByTagName('select');
        for (i = 0; i < selects.length; i++) {
            selects[i].style.visibility = "hidden";
        }
    }

    BOX_layout(e);
    window.onresize = function() { BOX_layout(e); } //改变窗体重新调整位置
    window.onscroll = function() { BOX_layout(e); } //滚动窗体重新调整位置
    document.onkeyup = function(event) {
        var evt = window.event || event;
        var code = evt.keyCode ? evt.keyCode : evt.which;
        //alert(code);

        if (code == 27) {
            BOX_remove(e);
        }
    }
}

function BOX_remove(e)//移除
{
    window.onscroll = null;
    window.onresize = null;
    document.getElementById('BOX_overlay').style.display = "none";
    document.getElementById(e).style.display = "none";

    var selects = document.getElementsByTagName('select');
    for (i = 0; i < selects.length; i++) {
        selects[i].style.visibility = "visible";
    }
}

function BOX_layout(e)//调整位置
{
    var a = document.getElementById(e);

    if (document.getElementById('BOX_overlay') == null)//判断是否新建遮掩层
    {

        var overlay = document.createElement("div");
        overlay.setAttribute('id', 'BOX_overlay');

        //overlay.onclick=function(){BOX_remove(e);};
        //a.parentNode.appendChild(overlay);
        document.body.appendChild(overlay);
    }

    document.getElementById('BOX_overlay').onclick = function() { BOX_remove(e); };
    //取客户端左上坐标，宽，高
    var scrollLeft = (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
    var scrollTop = (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);

    var clientWidth;
    if (window.innerWidth) {
        clientWidth = window.innerWidth;
        //clientWidth = ((Sys.Browser.agent === Sys.Browser.Safari) ? window.innerWidth : Math.min(window.innerWidth, document.documentElement.clientWidth));
    }
    else {
        clientWidth = document.documentElement.clientWidth;
    }

    var clientHeight;
    if (window.innerHeight) {
        clientHeight = window.innerHeight;
        //clientHeight = ((Sys.Browser.agent === Sys.Browser.Safari) ? window.innerHeight : Math.min(window.innerHeight, document.documentElement.clientHeight));
    }
    else {
        clientHeight = document.documentElement.clientHeight;
    }

    var bo = document.getElementById('BOX_overlay');
    bo.style.left = scrollLeft + 'px';
    bo.style.top = scrollTop + 'px';
    bo.style.width = '100%';
    bo.style.height = clientHeight + 'px';
    bo.style.display = '';

    //Popup窗口定位
    a.style.display = 'block'
    a.style.left = scrollLeft + ((clientWidth - a.offsetWidth) / 2) + 'px';
    a.style.top = scrollTop + ((clientHeight - a.offsetHeight) / 2) + 'px';
}

function HiddenButton(e) {
    e.style.visibility = 'hidden';
    e.previousSibling.style.visibility = 'visible'
}

function UserLogin(e) {
    BOX_show(e);
    document.getElementById('loginname').focus();
}

function dologin() {
    var name = $('#loginname').val();
    var pass = $('#password').val();
    var auto = $('#auto_login').val();

    if (name == '' || pass == '') {
        $('#box_error').html('用户名和密码都要填写。');
        return;
    }

    $('#box_error').html('正在登录...');
    var state = QuickLogin.CheckUser(name, pass, auto, location.pathname).value;
    if (state == null) {
        window.location.href = window.location.href;
    }
    else {
        $('#box_error').html(state);
    }
}

function iframeBox(title, url, w, h) {
    if (url.indexOf('?') > 0) {
        url = url + '&Random=' + Math.random();
    }
    else {
        url = url + '?Random=' + Math.random();
    }

    if (document.getElementById('iframe_box') == null) {
        var obj = document.createElement('div');
        obj.setAttribute('id', 'iframe_box');

        var str = [];
        str.push('<span><strong id="iframe_box_title"></strong><a href="javascript:BOX_remove(\'iframe_box\');">关闭</a></span>');
        str.push('<iframe id="my_iframe" src="' + url + '" frameborder="0"></iframe>');

        obj.innerHTML = str.join('');
        document.body.appendChild(obj);
    }
    else {
        document.getElementById('my_iframe').src = url;
    }

    document.getElementById('iframe_box_title').innerHTML = title;
    document.getElementById('my_iframe').width = w;
    document.getElementById('my_iframe').height = h;
    
    BOX_show('iframe_box');
}
