/**
 * Encapsulate the user entity type. See the user database table.
 */ 
 
var User = {
	
	entityName: Entity.USER,

	/**
	 * Handle form serialization in order to encrypt passwords
	 */
	getData: function(context, entity) {		
		var data = $(context + Service.SCOPE_SEPARATOR + entity + Service.SCOPE_SEPARATOR + 'form').serialize();
		// Encrypt the password
		var parts = data.split('=');
		for (i = 0; i < parts.length; i++) {
			if (parts[i].endsWith(Service.SCOPE_SEPARATOR + 'password')) {
				var sub = parts[i + 1].split('&');
				//sub[0] = hex_md5(sub[0]);
				parts[i + 1] = sub.join('&');
				break;
			}
		}
		data = parts.join('=');
		return data;
	},
	
	create: function(context){
		var target = context + Service.SCOPE_SEPARATOR + 'usertypes';
		Service.lister(target, context, Entity.USERTYPE, null, null, '1', false);
	},

	edit: function(context, entityID){
		var selection = new Selection(this.entityName, entityID);
		var target = context + Service.SCOPE_SEPARATOR + 'usertypes';
		Service.lister(target, context, Entity.USERTYPE, null, null, '1', false, null, null, selection);
	},

	view: function(context, entityID) {
		var filter = new ListFilter(this.entityName, entityID);
		var target = context + Service.SCOPE_SEPARATOR + 'usertypes';
		Service.lister(target, context, Entity.USERTYPE, null, null, '', true, filter, Widget.FLAT_LIST);
 	},
	
	closeEditor: function(context) {
		var target = context + Service.SCOPE_SEPARATOR + 'usertypes';
		$(target).update('');
	},

	closeViewer: function(context) {
		var target = context + Service.SCOPE_SEPARATOR + 'usertypes';
		$(target).update('');
	},	
	
	validate: function(context){
		var message = '';
		var textMessage = '';
		var passwordMessage = '';
		var prefix = context + Service.SCOPE_SEPARATOR + Widget.EDITOR + Service.SCOPE_SEPARATOR + this.entityName + Service.SCOPE_SEPARATOR;
		var passwordValue;
		var passwordConfirmValue;
		// Must have values in all descriptor text fields
		var textInputElements = $(context + Service.SCOPE_SEPARATOR + this.entityName + Service.SCOPE_SEPARATOR + 'form').getInputs('text');			
		var idField = prefix + 'id';
		if ($F(idField).length == 0) {		
			for (i = 0; i < textInputElements.length; i++) {
				if ($(textInputElements[i]).id == prefix + 'password') passwordValue = $F($(textInputElements[i]).id);
				if ($(textInputElements[i]).id == prefix + 'password_confirm') passwordConfirmValue = $F($(textInputElements[i]).id);
				if ($(textInputElements[i]).value.length == 0) textMessage += $(textInputElements[i].id).previous('label').innerHTML + '\n';
			}
			
			if (message.length == 0 && (passwordValue != passwordConfirmValue)) {
				passwordMessage = 'Password and confirm password fields do not match';
			}
		}		
		else{
			for (i = 0; i < textInputElements.length; i++) {
				if ($(textInputElements[i]).id == prefix + 'password') passwordValue = $F($(textInputElements[i]).id);
				if ($(textInputElements[i]).id == prefix + 'password_confirm') passwordConfirmValue = $F($(textInputElements[i]).id);
				if ($(textInputElements[i]).value.length == 0) textMessage += $(textInputElements[i].id).previous('label').innerHTML + '\n';
			}
		}
		
		if(textMessage) message += 'Please enter a value in:\n' + textMessage;
		if(passwordMessage) message += passwordMessage;
		return message;
	}
	
}

