//document.getElementById
function _(element) 
{
    if (arguments.length > 1) 
    {
        for (var i = 0, elements = [], length = arguments.length; i < length; i++)
          elements.push($(arguments[i]));
        return elements;
    }
    else
        return document.getElementById(element);
}

//init the server list
function areaChanged(value, type) 
{
	oAreaId = _('area_id');
	oServerId = _('server_id');

    var area = serverList[0];
    for (var i=0; i<serverList.length; i++) 
    {
        if (serverList[i].id == value) 
		{
        	area = serverList[i];
        	break;
        }
    }
            
    if (area) 
    {
        oServerId.options.length = 0;
        for (var i=0; i<area.sub.length; i++) 
        {
        	oServerId.options.add(new Option(area.sub[i].name, area.sub[i].id));
        	if ('1' == area.sub[i].id) 
        		oServerId.options[i].selected = true;
        }
    }
}

//初始化服务器列表
function initServerList()
{
	try
	{
		oAreaId = _('area_id');
		oServerId = _('server_id');
		oAreaId.options.length = 0;
	        
		for (var i=0; i<serverList.length; i++) 
		{
			oAreaId.options.add(new Option(serverList[i].name,serverList[i].id));
			if ('1' == serverList[i].id) 
				oAreaId.options[i].selected = true;
		}
		
		areaChanged(1,0);		
	}
	catch(e){}	
}

function changeCode()
{
	_('_code').src='index.php?do=code&'+Math.random();
}
$(function()
		   {
			   initServerList();
		   }
)
function doLogin()
{
	var vcode = $('#vcode').val();
	if(vcode =='' || vcode == null)
	{
		alert('请输入验证码');
		_('vcode').focus();
		return;		
	}
	$.getJSON
	(
	 	'index.php?do=login',
		{vcode:vcode,area_id:$('#area_id').val(),server_id:$('#server_id').val()},
		function(data)
		{
			if(data['txt']=='0')
			{
				alert(data['text']);
				return;
			}
			else
			{
				alert(data['text']);
				return;
			}
		}
	)
}
function _logout(){
    $.post(
        'index.php?do=Logout',
        function(data){
           top.location.reload();
        }
    )
}
function obj$(/*string*/id)
{
	return document.getElementById(id);
}

function SetToCenter(oId, width, height)
{
    try{
	    var obj = obj$(oId);
	    var bodyWidth = 0;
	    var bodyHeight = 0;
	    if (!document.all){
            bodyWidth = window.innerWidth;
            bodyHeight = window.innerHeight;
        }
        else{
            bodyWidth = document.documentElement.clientWidth;
            bodyHeight = document.documentElement.clientHeight;
            if(bodyWidth==0)
	            bodyWidth = document.body.clientWidth;
            if(bodyHeight==0)
	            bodyHeight = document.body.clientHeight;
        }
	    var marginTop = Math.abs(bodyHeight - height) / 2 + GetBrowserDocument().scrollTop;
	    var marginLeft = Math.abs(bodyWidth - width) / 2 + GetBrowserDocument().scrollLeft;
	    obj.style.left = marginLeft + "px";
	    obj.style.top = marginTop + "px";
        //GetBrowserDocument().style.overflow = "hidden";

        return {"l":marginLeft,"t":marginTop};
    }
    catch(e){}
}

