var checked = false;

//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_' + type);
	oServerId = _('server_id_' + type);

    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;
        }
    }
}
	 

//process the load event
window.onload = function()
{
	for (var m = 1; m <= 3; m++)
	{
		try
		{
			oAreaId = _('area_id_' + m);
			oServerId = _('server_id_' + m);
			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, m);			
		}
		catch(e){}
	}
}


//处理新用户登录
function Newer()
{
	if ( _('account_1').value == '' ||  _('passwd_1').value == '' || _('code_1').value == '') 
    {
		alert('Please fill all the required information, including account, password and validate code.');
		return false;
	} 	
	
	
	$.get
	(
	   'index.php?do=newer',
	   { 
	   	account: _('account_1').value, 
	   	passwd: _('passwd_1').value,
		code: _('code_1').value,
		area_id: _('area_id_1').value,
		server_id: _('server_id_1').value
	   },
	   function(data) 
	   {
		   	if (/hour/.test(data) || /Tips/.test(data))
			{
				$('#Newer').html(data);
				$('#Newer').css({'margin-left': '30px'});
			}
			else
			{
			   	_('code_1').value = ""; _('_code_1').src='index.php?do=code&'+Math.random();
				alert(data);
			}
		} 
	);
}


//处理中间领取奖品
function Prize()
{
	if ( _('account_2').value == '' ||  _('passwd_2').value == '' || _('code_2').value == '') 
    {
		alert('Please fill all the required information, including account, password and validate code.');
		return false;
	} 
	
	
	$.getJSON
	(
	   'index.php?do=Prize',
	   { 
	   	account: _('account_2').value, 
	   	passwd: _('passwd_2').value,
		code: _('code_2').value,
		area_id: _('area_id_2').value,
		server_id: _('server_id_2').value
	   },
	   function(data) 
	   {
			if (data['stats'] != 'OK')
			{
				alert(data['text']);
		   		_('code_2').value = ""; 
				_('_code_2').src='index.php?do=code&'+Math.random();			
			}
			else
				$('#Prize').html(data['text']);
	   } 
	);			
}


//处理老用户登录
function Older()
{
	if ( _('account_3').value == '' ||  _('passwd_3').value == '' || _('code_3').value == '') 
    {
		alert('Please fill all the required information, including account, password and validate code.');
		return false;
	} 
	
	$.getJSON
	(
	   'index.php?do=older',
	   { 
	   	account: _('account_3').value, 
	   	passwd: _('passwd_3').value,
		code: _('code_3').value,
		area_id: _('area_id_3').value,
		server_id: _('server_id_3').value
	   },
	   function(data) 
	   {
			if (data['stats'] != 'OK')
			{
				alert(data['text']);
		   		_('code_3').value = ""; 
				_('_code_3').src='index.php?do=code&'+Math.random();			
			}
			else
				$('#Older').html(data['text']);
	   } 
	);		
}


function GetPrize(id)
{
	$.getJSON
	(
	   'index.php?do=GetPrize',
	   {id:id},
	   function(data) 
	   {
			if (data['stats'] != 'OK')
			{
				alert(data['text']);
		   		_('code_2').value = ""; 
				_('_code_2').src='index.php?do=code&'+Math.random();			
			}
			else
			{
				alert('You`ve claimed your prize successfully. Please login to the game for them.');
				
				$.getJSON
				(
				   'index.php?do=Prize',
				   function(data) {$('#Prize').html(data['text']);} 
				);					
			}
	   } 
	);		
}

//获取用户的排名数据
function GetRank()
{
	if ( _('account_1').value == '' ||  _('passwd_1').value == '' || _('code_1').value == '') 
    {
		alert('Please fill all the required information, including account, password and validate code.');
		return false;
	} 	
	
	
	$.get
	(
	   'rank.php?do=GetRank',
	   { 
	   	account: _('account_1').value, 
	   	passwd: _('passwd_1').value,
		code: _('code_1').value,
		area_id: _('area_id_1').value,
		server_id: _('server_id_1').value
	   },
	   function(data) 
	   {
		   	if (/Dear/.test(data))
			{
				$('#UserRank').html(data);
				$('#UserRank').css({'margin-left': '30px'});
			}
			else
			{
			   	_('code_1').value = ""; _('_code_1').src='index.php?do=code&'+Math.random();
				alert(data);
			}
		} 
	);		
}
