var integral = '';
//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 end_game(num)
{
	integral = num;
	doIntegral('show');
}
//用户玩游戏积分处理
function doIntegral(type)
{
	if (type == 'login')
	{
	     var param = 
	     {
	          account: _('account').value,
	          passwd: _('passwd').value,
	          code: _('code').value,
	          server_id: _('server_id').value,
	          area_id: _('area_id').value,
			  type:type,
			  integral:integral
	     } 	        
		 if(param['account'] == '' || param['account'] == null)
		 {
			 alert('please input your Account ID');
			 _('account').focus();
			 return;
		 }
		 if(param['passwd'] == '' || param['passwd'] == null)
		 {
			 alert('please input your password');
			 _('passwd').focus();
			 return;
		 }
		  if(param['code'] == '' || param['code'] == null)
		 {
			 alert('please input the validation code');
			 _('code').focus();
			 return;
		 }
	 }
	 else
	 {
	 	var param = 
		{
			type:type,
			integral:integral
		};
	 }
	 	 	 	
     
    $.post
    (
        'index.php?do=integral', 
        param,
        function(data)
        {
			if (data == 'OK')	//积分提交成功
			{
				alert('Points recorded successfully!');
				top.location.reload();	
			}
			else if ( data.length <= 150 )
			{
				if('LaJi Data' == data)
				{
					alert('Server error, please try again later.');
					location.reload();
					return;
				}
				else if ('0' == data)
				{
					return;
				}
				else
				{
					alert(data);				
					try
					{
						_('code').value = '';
						_('_code').src='index.php?do=code&'+Math.random();					
					}catch(e){}
				}
			}
			else
			{
				var width = document.body.scrollWidth;
				var height = document.body.scrollHeight;
				$('#bg').width(width).height(height).css('opacity','0.3').show();
				$('#login_info').html(data);
				initServerList();
				$('#login_info').css({top:'500px',left:width/2-100}).fadeIn();	
			}
        } 
    );
}
//换用户进行登录
function changeLogin()
{
	$.get
    (
        'index.php?do=changeLogin',
        function(data)
        {
			if ( data.length <= 150 )
				alert(data);
			else
			{
				var width = document.body.scrollWidth;
				$('#login_info').html(data);
				initServerList();
				$('#login_info').css({top:'500px',left:width/2-100}).fadeIn();	
			}
        } 
    );	
}
function changeCode()
{
	_('_code').src='index.php?do=code&'+Math.random();
}
function closeDiv()
{
	$('#login_info').hide();
	$('#bg').fadeOut();
}