application.harvest = {
	list: {OAIPMH:[],CSW:[]},
	
	frequencyList : [
		{value:'DAILY', label:'Quotidien'},
		{value:'WEEKLY', label:'Hebdomadaire'},
		{value:'MONTHLY', label:'Mensuel'}
	],
	
	init: function() {
		application.harvest._makeList();
	},
	
	_makeList: function() {
		var result = $.ajax({
			url:application.baseUrl + '/harvest/getList', 
			async: false,
			type: "GET",
			error: function (request, status, error) {
				application.display.dialog.errorDialog(request.status);
		    },
		    success: function (result, text) {
				application.harvest.list = eval("(" + result + ")");
			}
			}).responseText;
		},
	
	update: function(rank,type) {
		application.harvest._updateValue(rank,type);
		
		$.ajax({
			url:application.baseUrl + '/harvest/update', 
			async: true,
			type: "GET",
			data: {hp: application.harvest._serializeHP(rank,type)}
			});
//		alert("Fonction indisponible en recette");
	},
	
	deleteHarvest: function(id) {
		$.ajax({
			url:application.baseUrl + '/harvest/delete', 
			async: true,
			type: "GET",
			data: {id: id}
			});
//		alert("Fonction indisponible en recette");
	},
	
	launch: function(id) {
		var result = $.ajax({
			url:application.baseUrl + '/harvest/launch', 
			async: false,
			type: "GET",
			data: {id: id}
			}).responseText;
			
		if (result == "OK") {
			application.display.dialog.alertDialog("Moisson","Moisson termin&eacute;e avec succ&egrave;s");
		} else {
			application.display.dialog.alertDialog("Moisson","Moisson en erreur");
		}
//		alert("Fonction indisponible en recette");
	},
	
	_updateValue: function(rank, type) {
		var hp = null;
		if (type == "OAIPMH")
			hp = application.harvest.list.OAIPMH[rank];
		else
			hp = application.harvest.list.CSW[rank];
			
		hp.id = $("#" + type + "_id_" + rank).val();
		hp.name = $("#" + type + "_name_" + rank).val();
		hp.url = $("#" + type + "_url_" + rank).val();
		hp.frequency = $("#" + type + "_frequency_" + rank).val();
		hp.login = $("#" + type + "_login_" + rank).val();
		hp.pwd = $("#" + type + "_pwd_" + rank).val();
	},
	
	_serializeHP: function(rank,type) {
		var hp = null;
		if (type == "OAIPMH")
			hp = application.harvest.list.OAIPMH[rank];
		else
			hp = application.harvest.list.CSW[rank];
			
		var result = '';
		if (hp) {
			result = (hp.id) ? hp.id : "''"; 
			result += "--!!--";
			result += (hp.name) ? hp.name : "''";
			result += "--!!--"
				+ hp.url + "--!!--"
				+ hp.frequency + "--!!--";
			result += (hp.login) ? hp.login : "''";
			result += "--!!--";
			result += (hp.pwd) ? hp.pwd : "''";
			result += "--!!--"
				+ type;
		}	
		return result;
	},
	
	addNew: function(type) {
		var newHp = {
			id: '',
			name: '',
			url: '',
			frequency: '',
			login: '',
			pwd: ''
		}
		if (type == "OAIPMH") 
			application.harvest.list.OAIPMH.push(newHp);
		else
			application.harvest.list.CSW.push(newHp);
//		alert("Fonction indisponible en recette");
	}
};
