var ua = navigator.userAgent.toLowerCase();
var isIE = (ua.indexOf("msie") > -1),isIE7 = (ua.indexOf("msie 7") > -1),isOpera = (ua.indexOf("opera") > -1),isSafari = (ua.indexOf("webkit") != -1 || ua.indexOf("khtml") != -1),isGecko = (!isSafari && ua.indexOf("gecko") > -1);
var integral = '';
var gameType = '';
var games = ['dan'];

var uname = '';
var islogin = '';
var home = '';

//错误信息定义
var error = 
{
    'NOT_LOGIN': 'Please login first.',
    'BAD_WARE': 'Input incorrect. Please check it and try again.',
    'SELL_OUT': 'The prize is run out. Please try others.',
    'OUT_POINT': 'Sorry, your point is not enough to exchange for this prize. Please try others.',
    'DB_ERROR': 'Sorry, the exchange cannot be completed. Please contact our Customer Service.'
};

//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);
}

window.onload = function ()
{
	$.get
	(
		'tuya.php?do=getUser',
		function (data)
		{
			try{_('_flash_').SetVariable('uname',data.split('|')[1]);}catch(e){}
			uname = data.split('|')[1];
			
			try{_('_flash_').SetVariable('home',data.split('|')[0]);}catch(e){}
			home = data.split('|')[0];
			
			if (data.split('|')[1] == 'unlogin')
			{
				islogin = 0;
				try{_('_flash_').SetVariable('islogin', 0);}catch(e){}
			}
			else
			{
				islogin = 1;
				try{_('_flash_').SetVariable('islogin', 1);}catch(e){}
			}
			/*islogin = 0;
			try{_('_flash_').SetVariable('islogin', 0);}catch(e){}*/
			
			if (data.split('|')[1] != 'unlogin')
			{
				$('#information').html(data.split('|')[2]);
				$('#information').fadeIn();	
			}
		}
	);
}


function trim(s)
{
	return s.replace(/(^\s+)|(\s+$)/g, "");
}

function doTitle()
{
	var field = ['code', 'eggtitle'];
	
	if(_('eggtitle').value == '')
	{
		alert("Please input the egg's name before you start painting.");
		return false;
	}
	
	for (var i in field)
	{
		_(field[i]).value = trim(_(field[i]).value);
		if (_(field[i]).value == '')
		{
			alert('Please input all required information for us to deliver the prizes.');
			return false;
		}
	}
	
	$.post
	(
		'tuya.php?do=title',
		{
			code: _('code').value,
			eggtitle: _('eggtitle').value
		},
		function(ret)
		{
			if("OK" == ret)
			{
				$('#information').fadeOut();	
			}
			else
			{
				_('code').value = '';
				_('_code').src='tuya.php?do=code&'+Math.random();
				alert(ret);
			}
		}
	)
}

//显示登录框
function showLogin()
{
	$.get
    (
        'tuya.php?do=login',
        function(data)
        {
			if ( data.length <= 150 )
				alert(data);
			else
			{
				$('#information').html(data);
				initServerList();
				$('#information').fadeIn();	
			}
        } 
    );	
}

//登录处理
function doLogin()
{
	var param =
	{
		eggtitle: _('eggtitle').value,
		account: _('account').value,
		passwd: _('passwd').value,
		code: _('code').value,
		server_id: _('server_id').value,
		area_id: _('area_id').value
	}

	if ( param['eggtitle'] == '' || param['account'] == '' || param['code'] == '' || param['passwd'] == '')
	{
		alert('Please input all required information for us to deliver the prizes.');
		return false;
	}
	
    $.post
    (
        'tuya.php?do=login&stat=post', 
        param,
        function(data)
        {
			if (data.split('|')[0] == 'OK')	//积分提交成功
			{
				try{_('_flash_').SetVariable('uname',data.split('|')[1]);}catch(e){}
				uname = data.split('|')[1];
				
				try{_('_flash_').SetVariable('islogin', 0);}catch(e){}
				islogin = 1;
				
				try{_('_flash_').SetVariable('home', home);}catch(e){}
				
				_('information').style.display = 'none';
			}
			else if ( data.length <= 150 )
			{
				alert(data);
				try
				{
					_('code').value = '';
					_('_code').src='tuya.php?do=code&'+Math.random();					
				}catch(e){}
			}
        } 
    );	
}

//重置Flash
function flashout(swf)
{
	if(home != '')
		top.location.reload();
	
	_('flasharea').innerHTML = "<object name=\"_flash_\" classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" codebase=\"http:\/\/download.macromedia.com\/pub\/shockwave\/cabs\/flash\/swflash.cab#version=9,0,28,0\" width=\"630\" height=\"485\"><param name=\"movie\" value=\"images\/" + swf + ".swf\"><param name=\"wmode\" value=\"opaque\" \/><param name=\"quality\" value=\"high\"><embed src=\"images\/" + swf + ".swf\" quality=\"high\" pluginspage=\"http:\/\/www.adobe.com\/shockwave\/download\/download.cgi?P1_Prod_Version=ShockwaveFlash\" type=\"application\/x-shockwave-flash\" width=\"630\" height=\"485\" wmode=\"transparent\" id=\"_flash_\" name=\"_flash_\" /><\/object>";

	if (islogin != '')
		try{_('_flash_').SetVariable('islogin', islogin);}catch(e){}
		
	if (uname != '')
		try{_('_flash_').SetVariable('uname', uname);}catch(e){}	
		
	if (home != '')
	{
		try{_('_flash_').SetVariable('home', home);}catch(e){}	
	}
				
}


//初始化服务器列表
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){}	
}


//init the server list
function areaChanged(value, type) 
{
	try
	{
		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) 
	    {
	    	var oOptions = oAreaId.options;
	    	for (var i = 0; i < oOptions.length; i++)
	    	{
	    		if (oOptions[i].value == value)
	    			oOptions[i].selected = true;
	    	}
	    	
	    	
	        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 (type == area.sub[i].id) 
	        		oServerId.options[i].selected = true;
	        }
	    }		
	}catch(e){}
}