var checked = false;

//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);
}
var html;
//$(document).ready(function (){initServerList();});

function initServerList(id,area,server)
{
	if (!area)area='1';
	if (!server)server='1';
	if (id)
	{
		oAreaId = _('area_id_'+id);
		oServerId = _('server_id_'+id);	
		
	}else{
		oAreaId = _('area_id');
		oServerId = _('server_id');		
	}
	if (oAreaId != null)
	{
		oAreaId.options.length = 0;
			        
		for (var i=0; i<serverList.length; i++) 
		{
			oAreaId.options.add(new Option(serverList[i].name,serverList[i].id));
			if (area == serverList[i].id) 
				oAreaId.options[i].selected = true;
		}	
		areaChanged(area, id,server);
	}
}

//init the server list
function areaChanged(value,type,server)
{
	if (!server)server='1';
	if(type)
	{
		oAreaId = _('area_id_'+type);
		oServerId = _('server_id_'+type);
	}else{
		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 (server == area.sub[i].id) 
        		oServerId.options[i].selected = true;
        }
    }
}

function initSend(player_name){
	//$('#dhtmlgoodies_floating_window0').show();
	//showHideWindowTab('','floatingWindowTab2');
	chooseTab(2);
	$('#send_player_name').val(player_name);
}

//处理答题第一步点击按钮
function dologin()
{
	var account = _('account').value;
	var pwd = _('pwd').value;
	var code = _('code').value;
	$.get
	(
	   'local_db_app.php?do=login',
	   { 
		account: account,
		pwd:pwd,
		code:code
	   },
	   function(data){
			if(data==1){
				alert('Log in successfully！');
				$('#show_login').hide('slow');
				$('#login_ok').show('slow');
				top.location.reload();
			}
			else if(data=='900025'){
				changeCode();
				alert('Validation code error.');
			}
			else alert('Username or password error.');
	   }
	);
}

//处理充值第一步点击按钮
function doPayment()
{
	hideAll()
	$.get
	(
	   'index.php?do=login',
	   { 
		flag: 'payment'
	   },
	   function(data){ if(data!=1)showlogin(500,590); else server_pop('payment');} 
	);
}

//处理老玩家第一步点击按钮
function doOld()
{
	hideAll()
	$.get
	(
	   'index.php?do=login',
	   { 
		flag: 'old'
	   },
	   function(data){ if(data!=1)showlogin(500,590); else server_pop('old');} 
	);
}



//处理提交
function doSubmit()
{
	var action = _('action').value
	if (action=='test')
	{	
		var param = 
		{ 
			action: action,
			code: _('check_code').value,
			area_id: _('area_id').value,
			server_id: _('server_id').value,
			tm1:$('input[@name=tm1][@checked]').val(),
			tm2:$('input[@name=tm2][@checked]').val(),
			tm3:$('input[@name=tm3][@checked]').val()
		};
	}else {
		var param = 
		{ 
			action: action,
			code: _('check_code').value,
			area_id: _('area_id').value,
			server_id: _('server_id').value
		};
	}
	$.get
	(
	   'index.php?do=submit',
	   param,
	   function(data) 
	   {
			hideAll();
			alert(data);
			if (/新手礼包/.test(data))
			{
				window.location.href='http://my.91.com/intro/';
			}			
		} 
	);
}

function changeCode()
{

	_('code_img').src='index.php?do=code&'+Math.random();
}

function loadMessage(page)
{
	$('#msg_show').html('loading...');
	var param;
	$.get
	(
	   'message.php?p='+page,
	   param,
	   function(data) 
	   {
		   	$('#msg_show').hide();
			$('#msg_show').html(data);
			$('#msg_show').fadeIn('slow');	
	   } 
	);
}

//收件箱
function loadMessageBox(page)
{
	$('#msg_show_contents').html('loading...');
	var param;
	$.get
	(
	   'message.php?do=box&p='+page,
	   param,
	   function(data) 
	   {
			$('#msg_show_contents').hide();
			$('#msg_show_contents').html(data);
			$('#msg_show_contents').fadeIn('slow');			
	   } 
	);
}

function loadMessageSendBox(page)
{
	$('#msg_show_contents').html('loading...');
	var param;
	$.get
	(
	   'message.php?do=sendbox&p='+page,
	   param,
	   function(data) 
	   {
			$('#msg_show_contents').hide();
			$('#msg_show_contents').html(data);
			$('#msg_show_contents').fadeIn('slow');			
	   } 
	);
}

function loadSendInput(){
	$('#msg_show_contents').hide();
	$('#msg_show_contents').html($('#tmp_send_input').html());	
	$('#msg_show_contents').fadeIn('slow');
}

function loadSuggesInput(){
	$('#msg_show_contents').hide();
	$('#msg_show_contents').html($('#tmp_suggest_input').html());	
	$('#msg_show_contents').fadeIn('slow');
}

//处理提交建议
function doSuggest()
{
	var suggest_msg = _('suggest_msg').value;

	if(!suggest_msg){
		alert('input error!');
		return false;
	}
	var param = 
	{ 
		suggest_msg: suggest_msg
	};
	$.get
	(
	   'message.php?do=suggest',
	   param,
	   function(data) 
	   {
			if (/OK/.test(data))
			{
				_('suggest_msg').value='';
				alert('Your suggestion has submitted successfully.');
			}else alert(data);	
		} 
	);
}


//处理提交留言
function domsg()
{
	var msg = _('msg_content').value;
	var sid = _('sid').value;

	if(!msg){
		alert('input error!');
		return false;
	}
	var param = 
	{ 
		msg: msg,
		sid:sid
	};
	$.get
	(
	   'message.php?do=submit',
	   param,
	   function(data) 
	   {
			if (/OK/.test(data))
			{
				chooseTab(1);
				loadMessage();
				alert('message submit be ok!');
			}else alert(data);	
		} 
	);
}

//处理提交站内信息
function doPostMsg()
{
	var title = _('send_title').value;
	var player_name = _('send_player_name').value;
	var msg = _('send_msg').value;
	var server_id = _('server_id').value;

	if(!msg){
		alert('input error!');
		return false;
	}
	var param = 
	{ 
		msg: msg,
		title:title,
		player_name:player_name,
		server_id:server_id
	};
	$.get
	(
	   'message.php?do=send',
	   param,
	   function(data) 
	   {
			if (/OK/.test(data))
			{
				loadMessageBox(1);
				alert('message send be ok!');
			}else alert(data);	
		} 
	);
}


//用户提交照片处理 
function upload()
{	
	var fields = ['account', 'passwd', 'code',  'photo', 'title'];
	var status = true;
	
	for (var m in fields)
	{
		try
		{
			if (_(fields[m]).value == '')
				status = false;			
		}catch(e){status = true;}

	}
	
	if (!status)
	{
		alert('You should fill out all the field.');
		return false;
	}
		
	
	try{
		if ( _('photo').value != '' && !/(\.jpg|\.gif)$/.test(_('photo').value.toLowerCase()))	
		{
			alert('The image formate is incorrect, it can only be jpg or gif.');
			return false;
		}		
	}catch(e){status = true;}

	if (status)
		_('submit_form').submit();	
}




function chooseTab(id){
	//初始化全部
	$('#tab').show();
	$('#Tab0').css('z-index',0);
	$('#Tab1').css('z-index',1);
	$('#Tab2').css('z-index',2);
//	$('#Tab3').css('z-index',3);
	$('#Tab0').attr("class","floatingWindowTab_inactive"); 
	$('#Tab1').attr("class","floatingWindowTab_inactive"); 
	$('#Tab2').attr("class","floatingWindowTab_inactive"); 
	$('#Tab3').attr("class","floatingWindowTab_inactive");
	$('#img0').attr("src","./images/tab_right_inactive.gif");
	$('#img1').attr("src","./images/tab_right_inactive.gif");
	$('#img2').attr("src","./images/tab_right_inactive.gif");
	//$('#img3').attr("src","./images/tab_right_inactive.gif");
	//突出所选
	$('#Tab'+id).css('z-index',100);
	$('#Tab'+id).attr('class','floatingWindowTab_active');
	$('#img'+id).attr('src','./images/tab_right_active.gif');
	
	if (id==0)loadMessageBox(1);
	else if (id==1)loadMessageSendBox(1);
	else if (id==2)loadSendInput();
	else loadSuggesInput();
}
	
	
	
	
	
	
	
	
	
	
	
