$(document).ready(function() {
	$('#username-loading').hide();
	$('#username').blur(function(){
		if ($('#username').val() == '') {
		return false;
		} else {
		$('#username-loading').show();
		$.post('/ajax/?module=users&action=check&type=username', { username: $('#username').val() },
			function(response){
				$('#username-result').fadeOut();
				setTimeout("finishAjax('username-result', '"+escape(response)+"')", 400);
		});
		}		
	});
});

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

function submitForm() {
	// For your spammers/crackers, yes there is back-end
	// verification as well.
	var sname = $.trim($('input#name').val());
	var susername = $.trim($('input#username').val());
	var semail = $.trim($('input#reg_email').val());
	var semail2 = $.trim($('input#reg_email2').val());
	semail = semail.toLowerCase();
	semail2 = semail2.toLowerCase();
	var sbirth_month = $.trim($('select#birth_month').val());
	var sbirth_day = $.trim($('select#birth_day').val());
	var sbirth_year = $.trim($('select#birth_year').val());
	var spass1 = $.trim($('input#pass1').val());
	var spass2 = $.trim($('input#pass2').val());
	var scaptcha = $.trim($('input#captcha-response').val());
	var serror = '';
	if (sname == '') { serror = 'You must provide a name in order to register.';
	} else if (susername == '') { serror = 'You must provide a username in order to register.';
	} else if (semail == '') { serror = 'You must provide a valid email address in order to register.';
        } else if (semail != semail2) { serror = 'The email addresses you entered do not match, please try again.';
	} else if (sbirth_month > 12 || sbirth_month < 0) { serror = 'You must select a valid birth month and provide an accurate birth date to register.';
	} else if (sbirth_day > 31 || sbirth_day < 0) { serror = 'You must select a valid birth day and provide an accurate birth date to register.';
	} else if (sbirth_year > 2008 || sbirth_year < 0) { serror = 'You must select a valid birth year and provide an accurate birth date to register.';
	} else if (spass1 == '' || spass2 == '') { serror = 'You must enter a valid password in both password boxes in order to register.';
	} else if (spass1 != spass2) { serror = 'The passwords you entered do not match, please enter them again.';
	} else if (spass1.length < 6) { serror = 'The password you have entered is too short, it must be at least 6 characters in length.';
	} else if (scaptcha == '') { serror = 'You must enter the text in the Security Check box to verify you are human.';
	} else if ($('#terms').is(':checked') == false) { serror = 'You must agree to the terms of service in order to register.'; }
	if (serror == '') {$('form#r_form').submit();
	} else { $('div#report').html('<p class="report-alert">' + serror + '</p>'); return false; }
}