var User = {
	loginAttempts: 0,
	removeAnchors: function(url) {
		url = url.replace(/#([a-zA-Z]+)$/gi, "");
		return url;
	},
	login: function() {
		var remHTML = $('#login').html();
		User.loginAttempts++;
		
		var username = $('#login_username').val();
		var password = $('#login_password').val();
		param = {
			'username': username,
			'password': password
		}
		
		$('#login').html('<div class="status">Logger ind ...</div>');
		
		$.post(url('/user/login'), param, function (data) {
			if (data.success) {
				document.location = User.removeAnchors(document.location.href);
			} else {
				$('#login').html(remHTML);
				$('#login_username').val(username);
				$('#login_password').val(password);
				
				var errors = data.errors;
				var content = '<strong>Fejl ved login</strong><br />';
				if (errors['missing_fields']) {
					content += '- Mangler at udfylde begge felter!<br />';
				}
				if (errors['nomatch']) {
					content += '- Dit brugernavn og kodeord findes ikke i databasen!<br />';
				}
				if (errors['notactive']) {
					content += '- Denne bruger mangler at blive aktiveret!<br />';
				}
				
				errorBox({
					'attach': $('#login'),
					'content': content,
					'id': 'login_'+User.loginAttempts,
					'offsetX': -18,
					'offsetY': 5
				});
			}
		}, "json");
	},
	logout: function() {
		$.post(url('/user/logout'), {}, function (data) {
			document.location = User.removeAnchors(document.location.href);
		}, "text");
	},
	register: function() {
		$('#user_create_errors').css('display', 'none');
		$('#user_create_status').html('<br />Opretter...');
		Layout.columns();
		var param = {}
		
		var name;
		var value;
		$('#user_create input').each(function (x, item) {	
			name = $(item).attr('id');
			value = $(item).val();
			param[name] = value;
		});

		var html = '';
		$.post(url('/user/register'), param, function (data) {
			$('#user_create_status').html('');
			if (data['success']) {
				$('#user_create').html('<div class="section">Din bruger er nu oprettet, vi har sendt dig en mail med din aktiveringskode. Efter du har brugt aktiveringskoden kan du logge ind og g&oslash;re brug af vores bruger muligheder</div>');
			} else {
				$('#user_create_errors').html('');
				html = '<strong>'+Translate.__('There was an error with your registration')+':</strong>';
			
				errors = data['errors'];
				html += '<ul>';
				
				if (errors['missing_fields']) {
					html += '<li>'+Translate.__("You haven't filled out all the requires fields")+'</li>';
				}
				if (errors['password_nomatch']) {
					html += '<li>'+Translate.__("The two passwords do not match")+'</li>';
				}
				if (errors['username_exists']) {
					html += '<li>'+Translate.__("Username allready exists, please choose another")+'</li>';
				}
				if (errors['username_malformed']) {
					html += '<li>'+Translate.__("The given username is in an incorrect format, please recheck")+'</li>';
				}
				if (errors['email_exists']) {
					html += '<li>'+Translate.__("Email allready exists, please choose another")+'</li>';
				}
				if (errors['email_malformed']) {
					html += '<li>'+Translate.__("The given email is in an incorrect format, please recheck")+'</li>';
				}
				if (errors['notocagree']) {
					html += '<li>'+Translate.__("It is required that you understand and accept the terms and conditions for use of this site")+'</li>';
				}
				
				html += '</ul>';
				
				$('#user_create_errors').html(html);
				$('#user_create_errors').show();
			}
		}, "json");
	}
}
