//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;
        }
    }
}
//process the load event
window.onload = function(){
    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 showDiv(id,vid){
    vote_id = vid;
    $('#showaccount').fadeIn();
    changeCode();
}

function checkinput()
{
    var account = $('#account').val();
    var area_id = $('#area_id').val();
    //var area = $("select[@name='area_id'] option[@selected]").text();
    var server_id = $('#server_id').val();
    //var server = $("select[@name='server_id'] option[@selected]").text();
    var code = $('#code').val();
    var id = $('#hid').val();
    if(account == '' || account == null){
        alert('Please input your Account ID');
        _('account').focus();
        return;
    }
    if(code == '' || code == null){
        alert('Please input validation code');
        _('code').focus();
        return;        
    }
    $.post(
        'display.php?do=vote',
        {
            account:account,
            server_id:server_id,
            area_id:area_id,
            vote_id:vote_id,
            code:code
        },
        function(data)
        {
            if(data == 'ok'){
                _('account').value='';
                alert("Vote successfully!");
                top.location.reload();
            }else{
                alert(data);
                try
                {
                    _('_code').value = '';
                    _('_code').src='index.php?do=code&'+Math.random();                    
                }catch(e){}
            }
        }
    )
}
function closeDiv()
{
    _('account').value='';
    _('_code').value = '';
    $('#showaccount').hide();
    //$('#bg').fadeOut();
}
