﻿//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;
		oServerId.options.add(new Option("--", ""));
		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;
		oAreaId.options.add(new Option("--",""));
		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()
{
	_('v_code').src='index.php?do=code&'+Math.random();
}
function doLogin()
{
	//global play_namex;
	var vcode = $('#vcode').val();
	if(vcode =='' || vcode == null)
	{
		alert('请输入验证码');
		_('vcode').focus();
		return;
	}
	$.getJSON
	(
	'index.php?do=login',
	{vcode:vcode,area_id:$('#area_id').val(),server_id:$('#server_id').val()},
	function(data)
	{
		if(data['txt']=='0')
		{
			//top.location.reload();
			alert(data['text']);
			return;
		}
		else
		{
			//$('#prizelist').html(data['text']);
			play_namex = (data['play_name']);
			alert(data['text']);
			return;
		}
	}
	)
}

function _logout(){
	$.post(
	'index.php?do=Logout',
	function(data){
		top.location.reload();
	}
	)
}
function ajaxPage(url)
{
	$('#cclist').html('loading...').load(url);
}
function doVote(id)
{
	alert('Sorry， this event has ended. ');
	return false;
	$.getJSON
	(
	'showdetail.php?do=vote',
	{id:id},
	function(data)
	{
		if(data['txt']=='0')
		{
			alert(data['text']);
			return;
		}
		else
		{
			showDiv('#returnMsg');
			initServerList();
			return;
		}

	}
	)
}

function doVoteok(id)
{
	var vcode = $('#vcode').val();
	if($('#vcode').val() =='' || $('#area_id').val() == '' || $('#server_id').val()=='' || $('#account').val()=='' || $('#acpwd').val()=='')
	{
		alert('Please fill out all the field.');
		return;
	}
	$.getJSON
	(
	'showdetail.php?do=voteok',
	{vcode:vcode,id:id,area_id:$('#area_id').val(),server_id:$('#server_id').val(),account:$('#account').val(),acpwd:$('#acpwd').val()},
	function(data)
	{
		if(data['txt']=='0')
		{
			alert(data['text']);
			return;
		}
		else
		{
			alert(data['text'])
			closeDiv();
			return;
		}

	}
	)
}

function showDiv(info)
{
	var width = document.body.scrollWidth;
	var height = document.body.scrollHeight;
	$('#bg').width(width).height(height).css('opacity','0.3').show();
	$('#returnMsg').attr('class','tc_bg').css({top:getScrollXY(),left:width/2-100}).fadeIn();
}
function closeDiv()
{
	$('#returnMsg').hide('normal');
	$('#bg').fadeOut();
}
function getScrollXY() {
	var scrOfY = 0;
	if (typeof( window.pageYOffset ) == 'number') {
		scrOfY = window.pageYOffset;
	} else if (document.body && ( document.body.scrollLeft || document.body.scrollTop )) {
		scrOfY = document.body.scrollTop;
	} else if (document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop )) {
		scrOfY = document.documentElement.scrollTop;
	}
	return scrOfY+150;
}
function showlogin2()
{
	var width = document.body.scrollWidth;
	var height = document.body.scrollHeight;
	showlogin(getScrollXY(),width/2-100);
}
