var Tasks = {};

function Task(id, name, edit) {
	this.name = name;
	this.id = id;
	this.edit = edit;
	this.documents = {};
	this.submitted = null;
}
Task.prototype = {
	info: function() {
		var self = this;
		if (this.edit) {
			Profiles[this.getId()] = this;
		}
		return this;
	},
	
	setUpQuickSearchs: function(userList, profileName, profileId, userIds) {
		var userTags = new Tags('nameSearch', 'Task[' + this.id + '][user_ids][]');
		userTags.addQuickSearch();
		if (userList) {
			for (var i in userList) {
				userTags.getQuickSearch().addOption(userList[i].userId, userList[i].fullname, QuickSearchTemplate(userList[i].fullname));
				if (userIds[userList[i].userId])
					userTags.addTag(userList[i].userId, userList[i].fullname);
			}
		}

		var self = this;
		var profileTags = new Tags('profileSearch');
		profileTags.addQuickSearch();
		profileTags.tagsOnClose(function(id, name) {
			self.getElt('profile_id').val('');
			self.getElt('profile_name').val('');
		});
		profileTags.setLimit(1);
	
		ajax("/login/search/list/ajax/true", null, function(response) {
			var json = response.json;
			var quickSearch = profileTags.getQuickSearch();
			quickSearch.setApproveFunction(function(id, name, profileName){
				profileTags.addTag(id, name);
				self.getElt('profile_id').val(id);
				self.getElt('profile_name').val(profileName);
			});
			
			var init = function(quickSearch, id, name, array, category) {
				for (var i = 0; i < array.length; i++) {
					var option = quickSearch.addOption(array[i][id], array[i][name] + ' ' + category, QuickSearchTemplate(array[i][name], category), category);
					if (category == profileName && profileId == array[i][id]) {
						quickSearch.approveOption(option);
					}
				}
			}
			
			init(quickSearch, 'brokerage_id', 'company_name', json.brokerage, 'Brokerage');
			init(quickSearch, 'institution_id', 'name', json.institution, 'Institution');
			init(quickSearch, 'investment_id', 'registeredname', json.investment, 'Registered Name');
			init(quickSearch, 'investor_id', 'name', json.investor, 'Investor');
			init(quickSearch, 'representative_id', 'name', json.representative, 'Representative');
			init(quickSearch, 'plant_id', 'name', json.plant, 'Plant');
			init(quickSearch, 'series_id', 'seriesname', json.series, 'Series');
		});
		
		return this;
	},
	
	updateStatus: function(elt) {
		ajax(HTMLPATH + '/task/updatestatus/ajax/true/id/' + this.id, {'status':elt.value});
	}
};
Task.prototype = jQuery.extend(Task.prototype, Profile.prototype);