$(document).ready( function() {
	
	$('div#register-button').children().each( function() {
//		$(this).hover(
//				function() {
//					$(this).css({'text-decoration' : 'underline', 'cursor' : 'pointer'});
//				},
//				function() {
//					$(this).css({'text-decoration' : ''});
//				}
//		);
		$(this).click( function() {
			var id = $(this).find('span').attr('id');
			register.call( this, id );
		});
		
	});
		
});

function register(id) {
	try {
		if( id === "cancel" ) {
			$('div.register-user').each( function() {
				$(this).find('input').val('');
				if( ! $('div.errors').hasClass('hidden') ) {
					$('div.errors').addClass('hidden');
				}
			});
			window.location = base_url;
		}
		else if( id === "register" ) {
			$.ajax({
				type: "POST",
				data: $('form#register-form').serializeArray(),
				dataType: "json",
				url: base_url + "/user/register_user",
				success: function(data) {
					if( data.v_err || data.d_err ) {
						var error = (data.v_err) ? data.v_err : data.d_err;
						$('div.errors').html('');
						$('div.errors').append( $.sprintf('%s', error) );
						$('div.errors').removeClass('hidden');
					}
					else {
						window.location = base_url + data.location;
					}
				},
				error: function() {
					alert('Register Failed');
				}
			});
		}
		else {
			throw "identityErr";
		}
	}
	catch( err ) {
		if( err === "identityErr" ) {
			document.write("Exception ! Identification Error");
		}
	}
}
