var rega = null;
var trn = null;
var kto = null;
var eto = null;
var pto = null;
var tint = 2000;
var un_check = 0;
var ps_check = 0;
var e_check = 0;

var loadIcon = '<div class="loadIcon"><img src="/imgs/waiting_ani.gif" title="loading..."></div>';

function checkRegDet(whichField)
{
	if (rega)
	{
		rega.abort();
		rega = null;
	}
    
	var text = '';

	if (whichField == 'un')
	{
		text = $('#new_username').val();
	}

	var randomnumber = Math.round(new Date().getTime());
      rega = $.ajax({
          type: 'GET',
          url: '/s/dyn_signup',
         data: 'w=' + whichField + '&data=' + encodeURIComponent(text) + '&rnd=' + randomnumber,
          success: function(data)
          {
			if (data.length > 10)
			{
				$('#tsu_un_msg').html('<span class="red">Gone</span>');
			}
			else
			{
				$('#tsu_un_msg').html('<span class="green">OK</span>');
				un_check = 1;
				readyCheck();
			}
		  }
      });

	

}

function readyCheck()
{
	if (un_check && ps_check && e_check)
	{
		$('#regsub').attr('disabled','');
	}
	else
	{
		$('#regsub').attr('disabled','disabled');
	}
}

	
$(document).ready(function()
{

	$("#tsu_submit").click(function(){
		$('#str_gmessage').html('Please login or register ! (Message saved)');
		$('#str_gmessage').slideDown("fast");
		$('#tsu_wrap').slideDown("fast");
		pageTracker._trackPageview("/t/hot?reg=talkregview");
	});





	var rg_un = /\W/;
	var rg_e = /^([\w\-]+(?:\.[\w\-]+)*)@((?:[\w\-]+\.)*\w[\w\-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	var rg_s = /\s+/;

	trn = setTimeout(function(){
	
	$('#new_username').keyup(function()
	{ 
		
		un_check = 0;
		readyCheck();
		
		if (rega)
		{
			rega.abort();
			rega = null;
		}

		if (kto)
		{
			clearTimeout(kto);
		}

		$('#tsu_un_msg').html('');
		$('#tsu_g_msg').html('&nbsp;');

		if ($(this).val().length < 5)
		{
			kto = setTimeout(function(){
			$('#tsu_g_msg').html('<span class="red">Username is too short. Five letters or more please</span>');
			},1000);
		}
		else if (rg_s.test($(this).val()))
		{
			$('#tsu_g_msg').html('<span class="red">No spaces in the username please</span>');
		}
		else if (rg_un.test($(this).val()))
		{
			$('#tsu_g_msg').html('<span class="red">Only letters and numbers in the username</span>');
		}
		else
		{
			kto = setTimeout(function(){
				checkRegDet('un');
			}, 1000);
		}
		
		

	// end username
	});
	
	
	$('#new_email').keyup(function()
	{ 
		e_check = 0;
		readyCheck();
		
		if (eto)
		{
			clearTimeout(eto);
		}

		$('#tsu_e_msg').html('');
		$('#tsu_g_msg').html('&nbsp;');

		if (rg_e.test($(this).val()))
		{
			eto = setTimeout(function(){
				$('#tsu_e_msg').html('<span class="green">OK</span>');
				e_check = 1;
				readyCheck();
			},2000);
		}
		else if ($(this).val().length > 0)
		{
			eto = setTimeout(function(){
				$('#tsu_g_msg').html('<span class="red">Please check your email address</span>');
			},2000);
		}

	// end email
	});
	
	
		
	$('#password1').keyup(function()
	{ 
		ps_check = 0;
		readyCheck();
		
		if (pto)
		{
			clearTimeout(pto);
		}

		$('#tsu_ps_msg').html('');
		$('#tsu_g_msg').html('&nbsp;');

		if ($(this).val().length > 0 && $(this).val().length < 6)
		{
			pto = setTimeout(function(){
			$('#tsu_g_msg').html('<span class="red">Password is too short</span>');
			},1000);
		}
		else if (rg_s.test($(this).val()))
		{
			$('#tsu_g_msg').html('<span class="red">No spaces in the password please</span>');
		}
		else if ($(this).val().length > 5)
		{
			$('#tsu_ps_msg').html('<span class="green">OK</span>');
			ps_check = 1;
			readyCheck();
		}
		

	// end password
	});
	
	
	
	
	
	
	
	
	// end trn timeout
	}, tint);
	
	// $('#regsub').attr('disabled','');

});


$('#rgn_cancel').live('click', function()   
{ 
	$('#reg_now_sta').css('display', 'none');
});






