$('#reg_username').blur(function(){
	$('#reg_username_result').html('<img src=\'images/generic/wait.gif\' alt=\'\' />');
	$.post("classes/ajax/reg_handler.php?id=reg_username", {
		username: $('#reg_username').val()
	}, function(response){
		setTimeout("finishAjax('reg_username_result', '"+escape(response)+"')", 500);
	});
	return false;
});

$('#reg_password').blur(function(){
	$('#reg_password_result').html('<img src=\'images/generic/wait.gif\' alt=\'\' />');
	$.post("classes/ajax/reg_handler.php?id=reg_password", {
		password: $('#reg_password').val()
	}, function(response){
		setTimeout("finishAjax('reg_password_result', '"+escape(response)+"')", 500);
	});
	return false;
});

$('#reg_confirm_password').blur(function(){
	$('#reg_confirm_password_result').html('<img src=\'images/generic/wait.gif\' alt=\'\' />');
	$.post("classes/ajax/reg_handler.php?id=reg_confirm_password", {
		password: $('#reg_password').val(), 
		confirm_password: $('#reg_confirm_password').val()
	}, function(response){
		setTimeout("finishAjax('reg_confirm_password_result', '"+escape(response)+"')", 500);
	});
	return false;
});

$('#reg_email').blur(function(){
	$('#reg_email_result').html('<img src=\'images/generic/wait.gif\' alt=\'\' />');
	$.post("classes/ajax/reg_handler.php?id=reg_email", {
		email: $('#reg_email').val()
	}, function(response){
		setTimeout("finishAjax('reg_email_result', '"+escape(response)+"')", 500);
	});
	return false;
});

$('#reg_confirm_email').blur(function(){
	$('#reg_confirm_email_result').html('<img src=\'images/generic/wait.gif\' alt=\'\' />');
	$.post("classes/ajax/reg_handler.php?id=reg_confirm_email", {
		email: $('#reg_email').val(), 
		confirm_email: $('#reg_confirm_email').val()
	}, function(response){
		setTimeout("finishAjax('reg_confirm_email_result', '"+escape(response)+"')", 500);
	});
	return false;
});

$('#reg_bot').blur(function(){
	$('#reg_bot_result').html('<img src=\'images/generic/wait.gif\' alt=\'\' />');
	$.post("classes/ajax/reg_handler.php?id=reg_bot", {
		reg_bot: $('#reg_bot').val()
	}, function(response){
		setTimeout("finishAjax('reg_bot_result', '"+escape(response)+"')", 500);
	});
	return false;
});


function finishAjax(id, response) {
  $('#'+id).html(unescape(response));
  $('#'+id).fadeIn();
}


$('#open_register_click').click(function(){
	$('#logreg').fadeOut('slow');
	$.blockUI({ 
		message: $('#register-window'),
		focusInput: true, 
		css: { 
			width: '50%', 
			left: '25%',
			right: '25%',
			padding: 0, 
			margin: 0, 
			cursor: 'null', 
			backgroundColor:'transparent', 
			border: '0px solid #000'
		}
	}); 
});


$('#registercancel').click(function(){
	$.unblockUI();
	$('#logreg').fadeIn('slow');
	return false;
});

$('#register').click(function(){
	var cstring = /succeed/;
	var username = $('#reg_username_result').html();
	var password = $('#reg_password_result').html();
	var c_password = $('#reg_confirm_password_result').html();
	var email = $('#reg_email_result').html();
	var c_email = $('#reg_confirm_email_result').html();
	var regbot = $('#reg_bot_result').html();
	
	if(username.search(cstring) !=-1 && password.search(cstring) !=-1 && c_password.search(cstring) !=-1 && email.search(cstring) !=-1 && c_email.search(cstring) !=-1 && regbot.search(cstring) !=-1)
	{
		submit_handler();
	}
	return false;						  					  
});							  
							  
function submit_handler() {
	var reg_username = $('#reg_username').val();
	var reg_password = $('#reg_password').val();
	var reg_email = $('#reg_email').val();
	var dataString = 'reg_username='+ reg_username + '&reg_password=' + reg_password + '&reg_email=' + reg_email;
	$.ajax({
		type: "POST",
		url: "classes/ajax/register_handler.php",
		data: dataString,
		success: function(reply) {
			if(!reply) {
				// all ok ...
				$.unblockUI();
				$.blockUI({
					timeout: 5000, 
					message: ('<div class=\"dialog\"><div class=\"content\"><h1>Register</h1><div class=\"t\"></div>Your account has been created. You can now login.</div><div class=\"b\"><div></div></div></div>'),
					css: { 
						padding: 0, 
						margin: 0, 
						width: '50%', 
						left: '25%',
						right: '25%',
						cursor: 'null', 
						backgroundColor:'transparent', 
						border: '0px solid #000'
					}
				});
				$('#logreg').fadeIn('slow');
				return false;
			} else {
				//alert(reply);
				return false;
			}
		}
	});
}
