application.result.Row = function() {

	this.data = {};

	this.data.id = '';
	this.data.adhId = '';
	this.data.adhName = '';
	this.data.isUrlValide = true;
	this.data.isMoissonnable = false;
	this.data.thumbnailUrl = '';

	this.data.oaiId = new application.result.SingleValueField('');
	this.data.state = new application.result.SingleValueField('');
	this.data.title = new application.result.SingleValueField('');
	this.data.alternateTitle = new application.result.SingleValueField('');
	this.data.url = new application.result.SingleValueField('');
	this.data.description = new application.result.SingleValueField('');

	this.data.dataDate = new application.result.SingleValueField('');
	this.data.creationDate = new application.result.SingleValueField('');
	this.data.revisionDate = new application.result.SingleValueField('');
	this.data.publicationDate = new application.result.SingleValueField('');

	this.data.owner = new application.result.SingleValueField('');
	this.data.originator = new application.result.SingleValueField('');
	this.data.publisher = new application.result.SingleValueField('');
	this.data.contributor = new application.result.SingleValueField('');
	this.data.contact = new application.result.SingleValueField('');
	this.data.pointOfContact = new application.result.SingleValueField('');

	this.data.rights = new application.result.SingleValueField('');
	this.data.lang = new application.result.SingleValueField('');
	this.data.portail = new application.result.SingleValueField('');

	this.data.keywords = new application.result.MultipleValueField([]);
	this.data.themes = new application.result.MultipleValueField([]);
	this.data.nature = new application.result.MultipleValueField([]);
	this.data.niveauLecture = new application.result.MultipleValueField([]);
	this.data.geographie = new application.result.MultipleValueField([]);
	
	this.data.codeCommune = new application.result.SingleValueField('');
	this.data.nomCommune = new application.result.SingleValueField('');
	
	this.data.otherplaceNames = new application.result.MultipleValueField([]);
	
	this.data.importSourceType = new application.result.SingleValueField('');
	
	this.isEdited = false;
	this.isSelected = false;
	
	this.isEditable = true;
	
	this.data.dataDate.value = new Date();
	this.data.creationDate.value = new Date();
	this.data.revisionDate.value = new Date();
	this.data.publicationDate.value = new Date();
};

application.result.Row.prototype = {
	clone: function(newUrl) {
		var newRow = new application.result.Row();
		
		//copy values
		newRow.data.adhId = this.data.adhId;
		newRow.data.isUrlValide = this.data.isUrlValide;
		newRow.data.isMoissonnable = this.data.isMoissonnable;
		newRow.data.thumbnailUrl = this.data.thumbnailUrl;
		newRow.isSelected = false;

		newRow.data.state.value = this.data.state.value;
		newRow.data.title.value = this.data.title.value;
		newRow.data.alternateTitle.value = this.data.alternateTitle.value;
		newRow.data.description.value = this.data.description.value;
		newRow.data.oaiId.value = this.data.oaiId.value;

		newRow.data.owner.value = this.data.owner.value;
		newRow.data.originator.value = this.data.originator.value;
		newRow.data.publisher.value = this.data.publisher.value;
		newRow.data.contributor.value = this.data.contributor.value;
		newRow.data.contact.value = this.data.contact.value;
		newRow.data.pointOfContact.value = this.data.pointOfContact.value;

		newRow.data.dataDate.value = this.data.dataDate.value;
		newRow.data.creationDate.value = this.data.creationDate.value;
		newRow.data.revisionDate.value = this.data.revisionDate.value;
		newRow.data.publicationDate.value = this.data.publicationDate.value;

		newRow.data.rights.value = this.data.rights.value;
		newRow.data.lang.value = this.data.lang.value;
		newRow.data.portail.value = this.data.portail.value;

		newRow.data.keywords.values = this.data.keywords.values.slice(0);
		newRow.data.themes.values = this.data.themes.values.slice(0);
		newRow.data.nature.values = this.data.nature.values.slice(0);
		newRow.data.niveauLecture.values = this.data.niveauLecture.values.slice(0);
		newRow.data.geographie.values = this.data.geographie.values.slice(0);

		newRow.data.codeCommune.value = this.data.codeCommune.value;
		newRow.data.nomCommune.value = this.data.nomCommune.value;

		newRow.data.otherplaceNames.values = this.data.otherplaceNames.values.slice(0);

		newRow.data.importSourceType.value = 'WEB_FORM';
		
		newRow.isEditable = true;
		
		newRow.data.id = '';
		newRow.data.url.value = newUrl;
		newRow.isEdited = true;
		
		return newRow;
	},
	
	setValue: function(propertyName, newValue) {
		if (typeof this.data[propertyName] == "string")
			this.data[propertyName] = newValue;
		else {
			if (this.data[propertyName].CLASSNAME == "SingleValueField"){
				if(propertyName=='owner' || propertyName=='originator' || propertyName=='publisher' || propertyName=='contributor' || propertyName=='contact' || propertyName=='pointOfContact'){
					this.videContact();
				}
				if(propertyName=='creationDate' || propertyName=='revisionDate' || propertyName=='publicationDate'){
					this.videDate();
				}
				
				if (typeof newValue == "object" && newValue.length && newValue.length > 0) {
					newValue = newValue[0];
				}
				
				this.data[propertyName].value = newValue;
				
			}
			
			else
				this.data[propertyName].values = newValue;
			this.data[propertyName].isDefaultValue = false;
		}
		this.isEdited = true;
	},
	
	getValue: function(propertyName) {
		if (typeof this.data[propertyName] == "string")
			return this.data[propertyName];
		else {
			if (this.data[propertyName].CLASSNAME == "SingleValueField")
				return this.data[propertyName].value;
			else
				return this.data[propertyName].values;
		}
	},
	
	getContact: function() {
		var value = '';
		var role = '';
		var viewValue = ''
				
		if (this.data.owner.value && this.data.owner.value.length > 0) {
			value = this.data.owner.value;
			viewValue =this.getViewValueContact('author');
			role = 'author';
		} else if (this.data.originator.value && this.data.originator.value.length > 0) {
			value = this.data.originator.value;
			viewValue =this.getViewValueContact('productor');
			role = 'productor';
		} else if (this.data.publisher.value && this.data.publisher.value.length > 0) {
			value = this.data.publisher.value;
			viewValue =this.getViewValueContact('publisher');
			role = 'publisher';
		} else if (this.data.contributor.value && this.data.contributor.value.length > 0) {
			value = this.data.contributor.value;
			viewValue =this.getViewValueContact('contrib');
			role = 'contrib';
		}  else if (this.data.contact.value && this.data.contact.value.length > 0) {
			value = this.data.contact.value;
			viewValue =this.getViewValueContact('pointOfContact');
			role = 'pointOfContact';
		}  else if (this.data.pointOfContact.value && this.data.pointOfContact.value.length > 0) {
			value = this.data.pointOfContact.value;
			viewValue =this.getViewValueContact('pointOfContact');
			role = 'pointOfContact';
		}
		
		return {
			value: value,
			lexiqueKey: role,
			viewValue: viewValue
		}
	},
	
	getViewValueContact : function(value){
		var lexique = application.getLexique('CI_RoleCode');
		var viewContact="";
		for (var i=0; i<lexique.length; i++) {
			if (value == lexique[i].key)
				viewContact=lexique[i].value;
		}
		return viewContact;
		
	},
	
	setContact: function(value, role) {
		
		this.videContact();
		
		if (role == 'author') {
			this.data.owner.value = value;
			this.data.owner.isDefaultValue = false;
		} else if (role == 'productor') {
			this.data.originator.value = value;
			this.data.originator.isDefaultValue = false;
		} else if (role == 'publisher') {
			this.data.publisher.value = value;
			this.data.publisher.isDefaultValue = false;
		} else if (role == 'contrib') {
			this.data.contributor.value = value;
			this.data.contributor.isDefaultValue = false;
		}  else if (role == 'contact') {
			this.data.contact.value = value;
			this.data.contact.isDefaultValue = false;
		}  else if (role == 'pointOfContact') {
			this.data.pointOfContact.value = value;
			this.data.pointOfContact.isDefaultValue = false;
		}
		this.isEdited = true;
	},
	
	videContact: function(){
		this.data.owner.value="";
		this.data.originator.value="";
		this.data.publisher.value="";
		this.data.contributor.value="";
		this.data.pointOfContact.value="";
		this.data.contact.value="";
	},
	
	getDate: function() {
		var value = '';
		//this.data.dataDate.value;
		var type = '';
		if (this.data.creationDate.value) {
			value=this.data.creationDate.value;
			type = 'creation';
		} else if (this.data.revisionDate.value) {
			type = 'revision';
			value=this.data.revisionDate.value;
		} else if ( this.data.publicationDate.value) {
			type = 'publication';
			value=this.data.publicationDate.value;
		}
		
		return {
			value: value,
			lexiqueKey: type
		};
	},
	
	setDate: function(value, type) {
		
		this.videDate();
		
		if (type == 'creation') {
			this.data.creationDate.value = value;
			this.data.creationDate.isDefaultValue = false;
		} else if (type == 'revision') {
			this.data.revisionDate.value = value;
			this.data.revisionDate.isDefaultValue = false;
		} else if (type == 'publication') {
			this.data.publicationDate.value = value;
			this.data.publicationDate.isDefaultValue = false;
		}
		this.isEdited = true;
	},
	
	videDate: function(){
		this.data.creationDate.value="";
		this.data.revisionDate.value="";
		this.data.publicationDate.value="";
	},
	
	isDefaultValue: function(propertyName) {
		if (this.data[propertyName]) {
			if (!this.data[propertyName].CLASSNAME)
				return false;
			else
				return this.data[propertyName].isDefaultValue;
		}
		return false;
	},
	
	serialize: function() {
		var state = this.data.state.value;
		var title = this.data.title.value;
		if (state == -1) {
			state = 0;
			title = '--BROUILLON--' + title;
		} 
	
		var url = this.data.url.value;
		if (url.indexOf("http://") == -1)
			url = "http://" + url;
	
		var result = 
			"[id]" + this.data.id + "-!-"
			+ "[oaiId]" + this.data.oaiId.value + "-!-"
			+ "[state]" + state + "-!-"
			+ "[moissonnable]" + this.data.isMoissonnable + "-!-"
			+ "[title]" + title + "-!-"
			+ "[alternateTitle]" + this.data.alternateTitle.value + "-!-"
			+ "[url]" + url + "-!-"
			+ "[description]" + this.data.description.value + "-!-"
			+ "[dataDate]" + application.serializeDate(this.data.dataDate.value) + "-!-"
			+ "[creationDate]" + application.serializeDate(this.data.creationDate.value) + "-!-"
			+ "[revisionDate]" + application.serializeDate(this.data.revisionDate.value) + "-!-"
			+ "[publicationDate]" + application.serializeDate(this.data.publicationDate.value) + "-!-"
			+ "[owner]" + this.data.owner.value + "-!-"
			+ "[originator]" + this.data.originator.value + "-!-"
			+ "[publisher]" + this.data.publisher.value + "-!-"
			+ "[contributor]" + this.data.contributor.value + "-!-"
			+ "[contact]" + this.data.contact.value + "-!-"
			+ "[pointOfContact]" + this.data.pointOfContact.value + "-!-"
			+ "[rights]" + this.data.rights.value + "-!-"
			+ "[lang]" + this.data.lang.value + "-!-"
			+ "[portail]" + this.data.portail.value + "-!-"
			+ "[keywords]" + application.serializeArray(this.data.keywords.values) + "-!-"
			+ "[themes]" + application.serializeArray(this.data.themes.values) + "-!-"
			+ "[nature]" + application.serializeArray(this.data.nature.values) + "-!-"
			+ "[niveauLecture]" + application.serializeArray(this.data.niveauLecture.values) + "-!-"
			+ "[geographie]" + application.serializeArray(this.data.geographie.values) + "-!-"
			+ "[nomCommune]" + this.data.nomCommune.value + "-!-"
			+ "[codeCommune]" + this.data.codeCommune.value + "-!-"
			+ "[otherPlaceNames]" + application.serializeArray(this.data.otherplaceNames.values);
		
		return result;
	}
};

application.result.SingleValueField = function(value) {
	this.CLASSNAME = "SingleValueField";
	this.isDefaultValue = false;
	this.value = value;
};

application.result.MultipleValueField = function(values) {
	this.CLASSNAME = "MultipleValueField";
	this.isDefaultValue = false;
	this.values = values;
};

application.result.MultipleValueField.prototype = {

	addValue: function(newValue) {
		if (!this.isPresent(newValue))
			this.values.push(newValue);
	},
	
	isPresent: function(value) {
		var isPresent = false;
		
		for (var i=0; i<this.values.length; i++) {
			if (this.values[i] == value) {
				isPresent = true;
				break;
			}
		}
		
		return isPresent;
	}

};
