var width = document.body.scrollWidth/2-100;
var height = document.body.scrollHeight/2-150;
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);
}
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(){
	
	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);
})
function showDiv(id,vid)
{
	$('#hid').val(id);
	$('#hvid').val(vid);
	$('#bg').width(document.body.scrollWidth).height(document.body.scrollHeight).css('opacity','0.3').show();
	$('#showaccount').css({top:height,left:width}).fadeIn();
	$('#vcode').val('');
	changeCode();
}
function checkinput()
{
	var user = $('#username').val();
	var a_id = $('#area_id').val();
	var area = $("select[@name='area_id'] option[@selected]").text();
	var s_id = $('#server_id').val();
	var server = $("select[@name='server_id'] option[@selected]").text();
	var vcode = $('#vcode').val();
	var id = $('#hid').val();
	if(user == '' || user == null)
	{
		alert('Please input your Account ID');
		_('username').focus();
		return;
	}
	if(vcode == '' || vcode == null)
	{
		alert('Please input validation code');
		_('vcode').focus();
		return;		
	}
	$.post
	(
		'index.php?do=post',
		{account:user,g_id:a_id,group:area,s_id:s_id,server:server,vcode:vcode,votenum:$('#vote_'+id).html(),v_id:$('#hvid').val()},
		function(data)
		{
			eval("var data="+data+";");
			if(data['txt']==0)
			{
				alert(data['text']);
				_('username').focus();
				return;
			}
			else
			{
				alert(data['text']);
				$('#vote_'+id).html(data['num']);		
				$('#showaccount').hide();
				$('#bg').fadeOut();
				return;
			}
		}
	)
}
function closeDiv()
{
	$('#showaccount').hide();
	$('#bg').fadeOut();
}
function goPage(id)
{
	location.href='page.php?id='+id;
}
function changeCode()
{
	_('_code').src='index.php?do=code&'+Math.random();
}