var Emails = {};

function Email(id){
	this.id = id + 'email';
}
Email.prototype = {
	id: null,
	activeUser: null,
	
	toggleActivation: function(elt, id, active){
		if (!this.activeUser)
			this.activeUser = active;
		if ($(elt).html() == 'Cancel online access') {
			$('#' + this.id + ' div.password').hide();
			$('#' + this.id + ' div.password input.password').attr('disabled', true);
			$(elt).html('Allow online access');
			return;
		}
		
		if (this.activeUser == 'true') {
			$('#' + this.id + ' div.password').hide();
			$('#' + this.id + ' a.activationLink').html('Restore online access');
			ajax(HTMLPATH + '/user/deactivate/ajax/true/id/' + id);
			this.activeUser = 'false';
		}
		else if (this.activeUser == 'false') {
			$('#' + this.id + ' div.password').show();
			$('#' + this.id + ' a.activationLink').html('Disable online access');
			ajax(HTMLPATH + '/user/activate/ajax/true/id/' + id);
			this.activeUser = 'true';
		}
		else {
			$('#' + this.id + ' div.password').show();
			$(elt).html('Cancel online access');
			$('#' + this.id + ' div.password input.password').removeAttr('disabled');
		}
	},
	
	send: function() {
		var form = $('#' + this.id + ' input, #' + this.id + ' textarea, #' + this.id + ' hidden').serialize();
		var self = this;
		
		ajax('/login/email/send/ajax/true/', form, function(response){
			self.sendResponse(response);
		});
	},

	sendResponse: function(obj) {
		try {
			if (obj.error == 'true') {
				$('#' + this.id + ' div.error').html(obj.message);
			}
			else this.success(obj.message);
		}
		catch (e) {
			alert('Email.submitResponse error: ' + e);
		}
	},

	success: function(message) {
		var self = this;
		$('#' + this.id + ' span.success').html(message);
		setTimeout(function() {$('#' + self.id + ' span.success').html(''); }, 1500);
	}
};