$(document).ready(function() { 
	$('.help-item').hide();
	$('.error-item').hide();

	$('.help-item').css('display','none');
	$('.error-item').css('display','none'); 

	$('.help-form').bind('focus',function() {
		$(this).addClass('active');
		id = $(this).attr('id');

		newEl = '#help-' + id;
		
		//Position the help bubble
		pos = $(this).position();
		width = $(this).width();
		height = $(this).height();

		$(newEl).css( { "position": "absolute", "left": (pos.left + width) + "px", "top": (pos.top) + "px" });

		//Show the help bubble
		if (!$(this).hasClass('error'))
			$(newEl).show('fast');
	});

	$('.help-form').bind('blur',function() {
		$(this).removeClass('active');
		id = $(this).attr('id');
		$('#help-' + id).hide('fast');
	});
	
	$('#email2').bind('blur',function() {
		email = $('#email').val();
		$.get('lib/register-ajax.php',{action: 'email', e: email},function (data) {
			if (data==0 || $('#email').val()!=$('#email2').val()) {
				$('#email').addClass('error');
				$('#email2').addClass('error');
				newEl = '#error-email2';
				//Position the help bubble
				pos = $('#email2').position();
				width = $('#email2').width();
				height = $('#email2').height();

				$(newEl).css({"position": "absolute", "left": (pos.left + width) + "px", "top": (pos.top) + "px" });

				//Show the help bubble
				$(newEl).show('fast');
			}
			else {
				$('#email').removeClass('error');
				$('#email2').removeClass('error');
				$('#error-email2').hide();
			}
		});
	});
	
	$('#pass2').bind('blur',function() {
		pass = $('#pass2').val();
		$.get('lib/register-ajax.php',{action: 'pass', p: pass},function (data) {
			if (data==0 || $('#pass').val()!=$('#pass2').val()) {
				$('#pass').addClass('error');
				$('#pass2').addClass('error');
				newEl = '#error-pass2';
				//Position the help bubble
				pos = $('#pass2').position();
				width = $('#pass2').width();
				height = $('#pass2').height();

				$(newEl).css({"position": "absolute", "left": (pos.left + width) + "px", "top": (pos.top) + "px" });

				//Show the help bubble
				$(newEl).show('fast');
			}
			else {
				$('#pass').removeClass('error');
				$('#pass2').removeClass('error');
				$('#error-pass2').hide();
			}
		});
	});
	
	$('#username').bind('blur',function() {
		username = $(this).val();
		$.get('lib/register-ajax.php',{action: 'username', u: username},function (data) {
			if (data==0) {
				$('#username').addClass('error');
				newEl = '#error-username';
				//Position the help bubble
				pos = $('#username').position();
				width = $('#username').width();
				height = $('#username').height();

				$(newEl).css({ "position": "absolute", "left": (pos.left + width) + "px", "top": (pos.top) + "px" });

				//Show the help bubble
				$(newEl).show('fast');
			}
			else {
				$('#username').removeClass('error');
				$('#error-username').hide();
			}
		});
	});
	
	$('#phone').bind('blur',function() {
		if (!$(this).val()) {
			$(this).addClass('error');
			newEl = '#error-phone';
			//Position the help bubble
			pos = $('#phone').position();
			width = $('#phone').width();
			height = $('#phone').height();

			$(newEl).css({ "position": "absolute", "left": (pos.left + width) + "px", "top": (pos.top) + "px" });

			//Show the help bubble
			$(newEl).show('fast');
		}
		else {
			$(this).removeClass('error');
			$('#error-phone').hide();
		}
	});

	$('#realname').bind('blur',function() {
		if (!$(this).val()) {
			$(this).addClass('error');
			newEl = '#error-realname';
			//Position the help bubble
			pos = $('#realname').position();
			width = $('#realname').width();
			height = $('#realname').height();

			$(newEl).css({ "position": "absolute", "left": (pos.left + width) + "px", "top": (pos.top) + "px" });

			//Show the help bubble
			$(newEl).show('fast');
		}
		else {
			$(this).removeClass('error');
			$('#error-realname').hide();
		}
	});
	
	$('#country').bind('blur',function() {
		if ($(this).val()=='x') {
			$(this).addClass('error');
			newEl = '#error-country';
			//Position the help bubble
			pos = $('#country').position();
			width = $('#country').width();
			height = $('#country').height();

			$(newEl).css({ "position": "absolute", "left": (pos.left + width) + "px", "top": (pos.top) + "px" });

			//Show the help bubble
			$(newEl).show('fast');
		}
		else {
			$(this).removeClass('error');
			$('#error-country').hide();
		}
	});
	
	$('form.register').bind('submit',function() {
		if ($('#username').hasClass('error') || $('#country').hasClass('error') || $('#pass').hasClass('error') || $('#email').hasClass('error') || $('#realname').hasClass('error') || $('#phone').hasClass('error')) {
			alert('Please correct the problems with your information before submitting the form')
			return false;
		}
		if ($('#tc').is(':checked'))
			return true;
		else {
			alert('Please confirm you accept the terms and conditions before proceeding');
			return false;
		}
	});
});
