var game_opened = false;
function preSaveRegForm()
{
	var reg_inputs = $('#reg_left input');
	var that = this;
	var error;
	$.each(reg_inputs,function(i,val){
		if($(val).attr('type') == 'text' || $(val).attr('type') == 'password')
		{
			if(jQuery.trim($(val).val()) == '')
				that.error = 1;
		}
		if($(val).attr('type') == 'checkbox')
		{
			if($(val).attr('checked') != true)
			{
				that.error = 2;
			}
		}
	});
	
    if(undefined !== that.error)
     return false;
	if(undefined !== that.error){
		$('#modal_box_message')
		.html(lng.Get('reg_fill_the_fields'))
		.dialog('open');
		return false;
	}
	return true;			
}

function preLoginForm()
{
	var that = this;
	var error;
	
	$.each($('#login_form input[type != checkbox]'),function(i,val){
		if(jQuery.trim($(val).val()) == '')
			that.error = 1;
	});
	if(undefined !== that.error){
		$('#modal_box_message')
		.html(lng.Get('login_fill_the_fields'))
		.dialog('open');
		return false;
	}
	return true;	
	
}
function showLoginResponse(resp, status)
{
	if(resp.status == 'success')
	{
		//window.location.reload(true);
		$('#login').html(resp.data);

  var enter = wsp.getElementById('enter'); try{
   enter.src = enter.src.replace('menu06_', 'menu05_')
  }catch(e){};
 }else
	{
		var content;
		content = '<h3>'+lng.Get('login_box_error_title')+'</h3>';
		content += '<ul style="margin: 5px">';
		for(i in resp.data)
		{
			content += '<li style="margin:3px 0px 0px 3px;">'+resp.data[i]+'</li>';
		}
		content += '</ul>';

		var modalbox = $('#modal_box_message');
		if(resp.spec == 'height')
			$(modalbox).dialog( 'option', 'height', 250 )
		$(modalbox).html(content).dialog('open');
 }
}

function preForgot()
{
	if(jQuery.trim($('#login_email').val()) == '')
	{
		$('#modal_box_message')
			.html(lng.Get('forgot_box_error'))
			.dialog('open');
			
		return false;
	}
	return true;
}

function preChangePwd()
{
	var reg_inputs = $('#change_pwd_form input');
	var that = this;
	var error;
	$.each(reg_inputs,function(i,val){
		if(jQuery.trim($(val).val()) == '')
			that.error = 1;
	});
	
	if(undefined !== that.error){
		$('#modal_box_message')
		.html(lng.Get('forgot_box_empty_error'))
		.dialog('open');
		return false;
	}
	return true;
}

function getGameLink(obj)
{
 $.ajax({cache:false,
         async:false,
         type: 'POST',
         url: 'get_game.php',
         dataType: 'json',
         data: {},
         success: function(msg){
          if(msg.logged == true)
          { 
           if(msg.status == 'success')
           {
            if(msg.link.length != 0)
             $(obj).attr('href',msg.link).attr('target','_self');
           }
           else
//            Regf.LuminStart();
            $('#modal_box_message').html(lng.Get('load_game_error')).dialog('open');
          }
          else
           Regf.LuminStart();
         }, 
         error: function(){/* Regf.LuminStart(); */ alert(lng.Get('load_game_error'));}
        });
}

function check_username(obj)
{
	show_status(obj);
	show_error(obj);
	if(jQuery.trim($(obj).val()) != '')
	{
		show_status(obj,'status_loader');
		$.post('/reg_ajax.php?p=username',{username: $(obj).val()},function(data){
			show_status(obj,'status_'+data.status);
			if(data.status == 'error')
				show_error(obj,data.message);
			else
				show_error(obj);	
		},'json');
	}
}

function compare_pwds(pwd_obj, pwd_conf_obj)
{
	show_status(pwd_conf_obj);
	show_error(pwd_conf_obj);
	if($(pwd_obj).val() != $(pwd_conf_obj).val())
	{
		show_status(pwd_conf_obj,'status_error');
		show_error(pwd_conf_obj,'The passwords you entered do not match');
	}else
		show_status(pwd_conf_obj,'status_success');
	
}

function check_email(obj)
{
	show_status(obj);
	show_error(obj);
	if(jQuery.trim($(obj).val()) != '')
	{
		show_status(obj,'status_loader');
		$.post('/reg_ajax.php?p=email',{email: $(obj).val()},function(data){
			show_status(obj,'status_'+data.status);
			if(data.status == 'error')
				show_error(obj,data.message);
			else
				show_error(obj);	
		},'json');
	}
}

function show_status(obj,status)
{
	var status_obj = $(obj).nextAll('span.status:first');
	$(status_obj).hide();
	$(status_obj).removeClass().addClass('status');
	if(status)
	{
		$(status_obj).addClass(status).show();
	}
}

function show_error(obj,text)
{
	var error_obj = $(obj).nextAll('span.error_msg:first');
	$(error_obj).html('');
	$(obj).removeClass('border-red');
	if(text)
	{
		$(error_obj).html(text);
		$(obj).addClass('border-red');
	} 
}

function open_link(obj)
{
	$(obj).click();
}

