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;
        }
    }
}
//init the server list
function areaChanged2(value, type) 
{
	oAreaId = _('area_id2');
	oServerId = _('server_id2');

    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){}	
	try
	{
		oAreaId = _('area_id2');
		oServerId = _('server_id2');
		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;
		}
		
		areaChanged2(1,0);		
	}
	catch(e){}	
	
}
function changeCode()
{
	_('_code').src='index.php?do=code&'+Math.random();
}
function Login()
{
	
	
	if ( _('account').value == '' ||  _('passwd').value == '' || _('code').value == '') 
    {
		alert('Please fill in all the field!');
		return false;
	} 
	$.getJSON
	(
	 	'sendgift.php?do=checkLogin',
		{code:_('code').value,area_id:$('#area_id').val(),server_id:$('#server_id').val(),account: _('account').value,passwd: _('passwd').value},
		function(data)
		{
			if(data['txt']==0)
			{
				changeCode();
				_('code').value ='';
				alert(data['text']);
				return;
			}
			else
			{
				if(data['txt']=='1')
				{
					window.location.reload();
					return;
					
				}
				alert(data['text']);
				return;
			}
		}
	)
}
function dosubmit()
{
	
	var lb= $("input[@type=radio][@name=lb][@checked]").val();
	var lbgw= $("input[@type=radio][@name=lbgw][@checked]").val();
	if ( _('player_name').value == '' ||  _('area_id2').value == '' || _('server_id2').value == '' || lb==undefined || lbgw==undefined) 
    {
		alert('Please fill in all the field!');
		return false;
	} 
	$('#lingqi').attr('disabled','disabled');
	$.getJSON
	(
	 	'sendgift.php?do=submit',
		{player_name:_('player_name').value,area_id:$('#area_id2').val(),server_id:$('#server_id2').val(),lb:lb,lbgw:lbgw},
		function(data)
		{
			if(data['txt']==0)
			{
				$('#lingqi').attr('disabled','');
				alert(data['text']);
				return;
			}
			else
			{
				if(data['txt']=='1')
				{
					$('#lingqi').attr('disabled','');
					alert(data['text']);
					window.location.reload();
					return;
					
				}
				$('#lingqi').attr('disabled','');
				alert(data['text']);
				return;
			}
		}
	)
}
$(function(){		
		   initServerList();
})
function doLogout()
{
	$.get('index.php?do=logout',function(data){window.location.reload();});	
}
