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 error = 
{
    'NOT_LOGIN': '您还没有登录，请登录后再进行奖品的兑换',
    'BAD_WARE': '您输入的物品编号不正确，请确认后再进行购买！',
    'SELL_OUT': '该奖品已兑换空，试试其它的吧！',
    'OUT_POINT': '很抱歉，您的积分还不够兑换该奖品，试试其它的吧！',
    'DB_ERROR': '很抱歉，我们在处理兑换时发生了错误，请与我们的客服联系！'
};

function mousePosition(e)
{
    var posx=0,posy=0;
    if(e==null) e=window.event;
    if(e.pageX || e.pageY)
    {
        posx=e.pageX; posy=e.pageY;
        }
    else if(e.clientX || e.clientY)
    {
        if(document.documentElement.scrollTop)
        {
            posx=e.clientX+document.documentElement.scrollLeft;
            posy=e.clientY+document.documentElement.scrollTop;
        }
        else
        {
            posx=e.clientX+document.body.scrollLeft;
            posy=e.clientY+document.body.scrollTop;
        }
    }
	
	return {
	 		x:posx,         
	   		y:posy		
		};
}


function mouseMove(ev)
{
    ev = ev || window.event;
    mousePos = mousePosition(ev);
}

document.onmousemove = mouseMove;


//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);
}


//换用户进行登录
function changeLogin()
{
	$.get
    (
        'integral.php?do=changeLogin',
        function(data)
        {
			if ( data.length <= 50 )
				alert(data);
			else
			{
				$('#information').html(data);
                iniserverlist();
				$('#information').fadeIn();	
			}
        } 
    );	
}


//用户玩游戏积分处理
function doIntegral(type)
{
	if (type == 'login')
	{
	     var param = 
	     {
	          account: _('account').value,
	          passwd: _('passwd').value,
	          code: _('code').value,
	          server_id: _('server_id').value,
	          area_id: _('area_id').value,
	       
			  type:type,
			  integral:integral
	     } 
	        
	     if ( param['account'] == '' || param['code'] == '' || param['passwd'] == '' )
	     {
	         alert('Please input all required information for us to send the prizes.');
	         return false;
	     }
	     
	 }
	 else
	 {
	 	var param = 
		{
			type:type,
			integral:integral,
			gameType:gameType
		};
	 }
	 	 	 	
     
    $.post
    (
        'integral.php?do=integral', 
        param,
        function(data)
        {
			if (data == 'OK')	//积分提交成功
			{
				alert('Points recorded successfully!');
				top.location.reload();	
			}
			else if ( data.length <= 50 )
			{
				alert(data);
				try
				{
					_('code').value = '';
					_('_code').src='?do=code&'+Math.random();					
				}catch(e){}
			}
			
			else
			{
				$('#information').html(data);
                iniserverlist();
				$('#information').fadeIn();	
			}
        } 
    );
}

//供Flash调用的函数
function end_game(num)
{
	integral = num;
	doIntegral('show');
}


//重置Flash
function flashout(swf)
{
	if (swf == 'dan')
		gameType = 0;
				
	_('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=\"resource\/images\/" + swf + ".swf\"><param name=\"wmode\" value=\"opaque\" \/><param name=\"quality\" value=\"high\"><embed src=\"resource\/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>";
}


function onFormSubmit() {
	if (document.getElementById('account').value == ''
	|| document.getElementById('code').value == ''
	|| document.getElementById('passwd').value == '') {

		alert('请填写您的完整资料！以上这些项都是必须填写的。');
		return false;
	} else if (!(/^[a-zA-Z0-9]{4,30}$/.test(document.getElementById('account').value))) {
		alert('您输入的帐号格式错误！');
		return false;
	}
	$.post(
		"question.php?do=Submit", 
		{
			account:document.getElementById('account').value,
			area_id:document.getElementById('area_id').value,
			server_id:document.getElementById('server_id').value,
			passwd:document.getElementById('passwd').value,
			code:document.getElementById('code').value
		},
		function(data){
			
			if(data=='ok')
			{
				top.location.reload();
				return false;
			}else if(data=='您还没有填写密码保护资料，请填写完整后继续参加活动。')
			{
			    alert(data);
				 
                window.open("https://reg.91.com/mylogin.aspx?flag=my");   
				return false;
			}else if(data=='ok2')
			{
			    alert("您还没有填写密码保护资料");
				top.location.reload();
				return false;
			}
			
			else
			{
				alert(data);			
				return false;
			}
			}		
	);	
}

//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{_('_flash_').SetVariable('cookies',unescape(document.cookie));}catch(e){}
}
function iniserverlist(){
    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){}
}