﻿//
//----------------------------------------------------------------------
//
//                          钱文田   2007-02-12
//
//             登录页面用到的JS, 包含登录检查, USB KEY 登录
//
//----------------------------------------------------------------------
//

// 页面初始化
window.onload = function()
{
    // 加载安全登录组件
    initSafeLogin();

    // 加载浮动和弹出新闻
    loadNews('NewsPop.aspx?region=' + region);
}

// 设置当前选项卡
function showMenu(index)
{
    // 索引错误
    if (index != 1 && index != 2 && index != 3)
        return;
        
    // 此选项卡已经隐藏
    if (table1.rows[0].cells[(index - 1) * 2].style.display == "none")
        return;

    for(var i=1; i<=3; i++)
    {
        document.getElementById("menu" + i).style.display = "none";
        document.getElementById("btn_login" + i).disabled = true;
    }
    document.getElementById("menu" + index).style.display = "";
    document.getElementById("btn_login" + index).disabled = false;
}

// 隐藏选项卡
function hideMenu(index)
{
    if (index != 1 && index != 2 && index != 3)
        return;
    
    for(var i=1; i<=3; i++)
    {
        var table = document.getElementById("table" + i);
        table.rows[0].cells[(index - 1) * 2].style.display = "none";
        table.rows[0].cells[(index - 1) * 2 + 1].style.display = "none";
    }
}

// 移动证书登录检查    
function check_login1()
{
    if (!document.getElementById("safe_login"))
        return false;
        
    if (isNull(safe_login.LastResultCode))
    {
        showMessage("info", "您还没有安装安全登录组件, 请先安装!", "", function(){});
        return false;
    }
    
    var tbx_password1 = document.getElementById("tbx_password1"); 
    if (tbx_password1.value == "")
    {
        showMessage("info", "密码不能为空, 请输入!", "", function(){tbx_password1.select()});
        return false;
    }

    if (!safe_login.ZJReadKeySerial())
    {
        showMessage("info", "系统没有检测到 USB KEY, 请先插入 USB KEY!");
        return false;
    }
    
    var signvalue = safe_login.ZJKEYSignData(document.getElementById("hid_random").value, tbx_password1.value);
    
    var lastResutCode = safe_login.LastResultCode;
    
    switch(lastResutCode)
    {
        case 0:
            document.getElementById("hid_sign").value = signvalue;
            return true;
            
        case -13:
            showMessage("info", "您输入的 USB KEY 密码有误, 请重新输入!", "", function(){tbx_password1.select()});
            return false;
            
        default:
            showMessage("info", "未知的系统错误, 错误代码为: " + lastResutCode, "", function(){tbx_password1.select()});
            return false;                    
    }

    return true;
}

// 文件证书登录检查
function check_login2()
{
    if (!document.getElementById("safe_login"))
        return false;

    if (isNull(safe_login.LastResultCode))
    {
        showMessage("info", "您还没有安装安全登录组件, 请先安装!", "", function(){});
        return false;
    }

    var ddl_complist = document.getElementById("ddl_complist");
    if (ddl_complist.selectedIndex == -1)
    {
        showMessage("info", "您还没有安装文件证书, 请先申请!", "", function(){});
        return false;
    }

    var tbx_password2 = document.getElementById("tbx_password2"); 
    if (tbx_password2.value == "")
    {
        showMessage("info", "密码不能为空, 请输入!", "", function(){tbx_password2.select()});
        return false;
    }

    var signvalue = safe_login.ZJCIDSignData(document.getElementById("hid_random").value, ddl_complist.options[ddl_complist.selectedIndex].value, tbx_password2.value);
    
    var lastResutCode = safe_login.LastResultCode;
    
    switch(lastResutCode)
    {
        case 0:
            document.getElementById("hid_sign").value = signvalue;
            return true;
            
        case -14:
            showMessage("info", "您输入的证书密码有误, 请重新输入!", "", function(){tbx_password2.select()});
            return false;
            
        default:
            showMessage("info", "未知的系统错误, 错误代码为: " + lastResutCode, "", function(){tbx_password2.select()});
            return false;                    
    }
}

// 普通登录检查
function check_login3()
{
    var tbx_username = document.getElementById("tbx_username");
    if (tbx_username.value.trim() == "")
    {
        showMessage("info", "用户名不能为空, 请输入!", "", function(){tbx_username.select()});
        return false;
    }

    var tbx_password3 = document.getElementById("tbx_password3"); 
    if (tbx_password3.value == "")
    {
        showMessage("info", "密码不能为空, 请输入!", "", function(){tbx_password3.select()});
        return false;
    }
                
    return true;
}

// 动态加载安全登录组件
function initSafeLogin()
{
    // 在静态加载安全登录组件, 若没有安装ActiveX有些延迟, 所以改成动态加载
    document.body.insertAdjacentHTML("afterBegin", "<object id='safe_login' classid='clsid:BABD0A45-CE1A-43EA-9E86-B7056C99F4C9' style='display:none'></object>");
    setTimeout(initSafeLogin_interval, 1);
}

// 检查是否已经加载完安全登录组件, 并且调用填充文件证书列表方法
function initSafeLogin_interval()
{
    if (safe_login.readyState != 4)
    {
        setTimeout(initSafeLogin_interval, 50);
        return;
    }
    
    if (isNull(safe_login.LastResultCode))
    {
        span_ukey.innerText = "您还没有安装安全登录组件, 请先安装";
        return;
    }
    
    fillCompList();
    
    readKeyID();
}

// 填充文件证书列表
function fillCompList()
{
    var allcomp = safe_login.ZJGetAllCID();
    if (allcomp != "")
    {
        var complist = allcomp.split("&");                
        var ddl_complist = document.getElementById("ddl_complist");
        ddl_complist.options.length = 0;
        for(var i=0; i<complist.length; i++)
        {
            ddl_complist.options.add(new Option(complist[i], complist[i]));
            if (window.curFileUserCode == complist[i])
            {
                ddl_complist.selectedIndex = i;
            }
        }
    }
}

// 读取移动证书的序列号
function readKeyID()
{
    if (safe_login.ZJReadKeySerial())
    {
        span_ukey.innerText = "系统已经检测到 USB KEY, 请输入密码登录";
    }
    else
    {
        span_ukey.innerText = "系统没有检测到 USB KEY, 请插入 USB KEY";
    }
    // window.setTimeout(readKeyID, 500);
}

//
//----------------------------------------------------------------------
//
//                          钱文田   2007-02-02
//
//                     浮动新闻和弹出新闻的弹出和显示
//
//----------------------------------------------------------------------
//

var _newsXmlHttp;

//
// 加载新闻
//
function loadNews(url)
{
    try
    {
	    _newsXmlHttp = new ActiveXObject("MSXML2.XMLHTTP");  
    }
    catch(e)
    {
	    _newsXmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
//	_newsXmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	_newsXmlHttp.onreadystatechange = loadNews_callback;
   	_newsXmlHttp.Open("GET", url, true);
   	_newsXmlHttp.Send();
}

//
// 加载新闻的回调函数
//
function loadNews_callback()
{    
    if(_newsXmlHttp.readyState == 4)
    {
        if(_newsXmlHttp.status == 200)
        {
            appendNews(_newsXmlHttp.responseText);
        }
    }
}

//
// 根据XML, 创建新闻
//
function appendNews(xml)
{    
	var dom = createXmlDom(xml);
	var rootNode = dom.childNodes[0];
	for(var i=0; i<rootNode.childNodes.length; i++)
	{
	    var newsNode = rootNode.childNodes[i];
	    switch(newsNode.getAttribute("typeid"))
	    {
	        case "1":
                var div = document.createElement("div");
                div.innerHTML = newsNode.getAttribute("html");
                div.style.position = "absolute";
                div.style.top = newsNode.getAttribute("y");
                div.style.left = newsNode.getAttribute("x");
                div.style.width = newsNode.getAttribute("width");
                // 不设置DIV高度, 让HTML自己去撑高度
                //div.style.height = newsNode.getAttribute("height");
                document.body.insertBefore(div, document.body.firstChild);
	            break;
	        case "2":
	            var ret = window.open("", "", "menu=no,status=no,resizable=no,scrollbars=no,top=" + newsNode.getAttribute("y") + ",left=" + newsNode.getAttribute("x") + ",width=" + newsNode.getAttribute("width") + ",height=" + newsNode.getAttribute("height"));
	            if (ret)
	            {
	                ret.document.title = newsNode.getAttribute("title");
	                ret.document.body.innerHTML = newsNode.getAttribute("html");
	            }
	            break;	        
	    } 
	}
}
