var listThemesLabel = new Array();
var listThemesId = new Array();

var listObjectivesLabel = new Array();
var listObjectivesId = new Array();

var listGeographicalScopesLabel = new Array();
var listGeographicalScopesId = new Array();

var listPartnerCountriesLabel = new Array();
var listPartnerCountriesId = new Array();

var listHydrographicalScopesLabel = new Array();
var listHydrographicalScopesId = new Array();

var separatorCaracters = new Array();
separatorCaracters[0] = ' ';
separatorCaracters[1] = ':';
separatorCaracters[2] = "'";
separatorCaracters[3] = ",";
separatorCaracters[4] = ".";
separatorCaracters[5] = "-";

var items = new Array();
var words = new Array();

var tableRules = new Array();






//=====================================================
// AUTOCOMPLETION
//=====================================================

var linkFocused = -1;
var xmlhttp;
function hideAutoCompleteDiv()
{
	document.getElementById('AUTOCOMPLETE').style.display = 'none';
}




function autoComplete(event, input){
	switch(event.keyCode){
		case 40:
			linkFocused = 0;
			if( document.getElementById( 'LINK_0' ) ){
				document.getElementById( 'LINK_0' ).focus();
			}
			linkFocused = 0;
			break;
		default:
			var div = document.getElementById('AUTOCOMPLETE');
			var lastWord = getLastWord(input);
			autoCompleteRequest(lastWord);

	}	

}


function autoCompleteRequest(text){
	if(text.length >= 2){
		var url = 'ajax/search_word.php?text=' + text + '&ms=' +new Date().getTime();;
		ObjetAJAX(url,autoCompleteCallBack);	
	} else {
		document.getElementById('AUTOCOMPLETE').style.display = 'none';
	}
}

function autoCompleteCallBack(){
	if (xmlhttp.readyState==4) {
		if (xmlhttp.status==200) {
			var div = document.getElementById('AUTOCOMPLETE');
			var input = document.getElementById('SEARCH');

			if( xmlhttp.responseText != '\n0'){
				div.innerHTML=xmlhttp.responseText;
				div.style.left = getX(input) + 'px';
				div.style.top = (getY(input) + 24) + 'px';
				div.style.display = 'block';
			} else {
				div.style.display = 'none';
			}
		}
	} 	
}	


function getLastWord(input){
	var text = input.value;
	var lastIndex = -1;
	// Chercher l'index du séparateur le plus éloigné possible du début de la chaine
	for(var i = 0; i < separatorCaracters.length; i++){
		var index = text.lastIndexOf(separatorCaracters[ i ]);
		if(index > lastIndex){
			lastIndex = index;
		}
	}
	
	if(lastIndex == -1){
		return text;
	} else {
		text = text.substr((lastIndex+1),(text.length - lastIndex));
		return text;
	}
}


function addWord(word){
	var input = document.getElementById('SEARCH');
	var text = input.value;
	
	if(text.lastIndexOf(' ') == -1){
		text = word ;
	} else {
		text = text.substr(0 , text.lastIndexOf(' '));
		text = text + ' ' + word ;
	}

	text = text + ' '; 	// Ajouter un espace après le mot inséré
	input.value = text;
	document.getElementById('AUTOCOMPLETE').style.display = 'none';
	getItems(input);
	input.focus();
	
	getItems(document.getElementById('SEARCH'));
	getWords(document.getElementById('SEARCH'));

	fillUserKeywordsField();
	fillUserKeywordsList();
	updateRule();
}


function changeSelection(event){
	switch(event.keyCode){
		case 40:
			linkFocused = linkFocused + 1;
			var id = 'LINK_' + linkFocused;

			if( document.getElementById( id ) ){
				var link = document.getElementById( id );
			} else {
				linkFocused = 0;
				id = 'LINK_0';
				var link = document.getElementById( id );
			}
			link.focus();
			break;
		case 38:
			linkFocused = linkFocused - 1;
			if(linkFocused < 0){
				linkFocused = 0;
			}
			var id = 'LINK_' + linkFocused;

			if( document.getElementById( id ) ){
				var link = document.getElementById( id ).focus();
			}
			link.focus();
			break;			
		default:
	}
}



function popupSynonym(action){
	var param = words.join(":");
	var search = document.getElementById('SEARCH').value;
	popupCentrer('popup/pop_search_synonyms.php?words=' + param + '&search=' + search + '&action=' + action, 'PopSynonym', 500,400);
}




function popupCentrer(page,nom,largeur,hauteur){
	var top = ((screen.height-hauteur)/2)-18;
	var left = (screen.width-largeur)/2;
	var options = 'top='+top+', left='+left+', width='+largeur+', height='+hauteur+', scrollbars = yes';
	
	fenetre = window.open(page,nom,options);
	fenetre.focus();
}





//=====================================================
// FONCTIONS UTILITAIRES
//=====================================================


/**
 * Supprime un élément d'un tableau
 * @param Array table Le tableau
 * @param String elem L'élément à supprimer
 * @return Array Le tableau avec l'élément à supprimer en moins
 */
function removeFromTable(table, elem){
	var temp = new Array();
	var iTemp = 0;

	for(i=0; i < table.length; i++){
		if(table[i] != elem){
			
			temp[iTemp] = table[i];
			iTemp++;
		}
	}
	return temp;
}

/**
 * Supprime les espaces en trop d'une chaine de caractère
 * @param String str La chaine de caractère à traiter
 * @return String La même chaine sans les espaces en début et fin de chaine.
 */
function trim(str) {
	return str.replace(/^\s+/, "").replace(/\s+$/, "");
}

//=====================================================
// THEMES
//=====================================================
function addTheme(){
	var list = document.getElementById('THEME_LIST');
	if(list.value != '-1'){
		// récupère les paramètres du theme
		var id = list.options[ list.selectedIndex ].value;
		var text = list.options[ list.selectedIndex ].text;
		
		// recréer la combobox avec les options sauf celle que l'on choisie
		var chainOptionThemes = '<select id="THEME_LIST" name="THEME_LIST" class="searchSelect">';
		for ( i = 0; i < list.options.length; i++ ) {
			if ( id != list.options[ i ].value ) {
				chainOptionThemes += '<option value="' + list.options[ i ].value + '">' + list.options[ i ].text + '</option>';	
			}
		}
		chainOptionThemes += '</select> ';
		chainOptionThemes += '<a href="#"><img src="design/images/add.png" border="0"  onclick="addTheme();"/></a><br>';
		
		// redefinie la combobox
		var listSelect = document.getElementById('DIV_THEME_LIST');
		listSelect.innerHTML = chainOptionThemes;
		
		// Ajouter l'élément sélectionné dans les tableaux	
		listThemesLabel.push(text);
		listThemesId.push(id);

		// Afficher 
		printListThemes();
		
		list.selectedIndex = 0;
	}
}

function printListThemes(){
	var div = document.getElementById('THEMES');
	var html = '<table class="searchSelectTable" border="0" cellspacing="0" cellpadding="0">';
	var style;
	for(i=0; i < listThemesLabel.length; i++)	{
		if( i % 2 == 0){
			style = 'row1';
		} else {
			style = 'row2';
		}
		html = html + '<tr class="'+style+'"><td>';
		html = html + listThemesLabel[i] + '<input type="hidden" name="THEME_ID_'+i+'" id="THEME_ID_'+i+'" value="' + listThemesId[i] + '" />';
		html = html + '</td><td align="right"><a href="#" onclick="removeTheme(\''+ listThemesLabel[i] + '\',\'' + listThemesId[i] + '\');"> <img src="design/images/remove.png" border="0"  /></a>'
		html = html + '</td></tr>';	
	}
	html = html + '</table>';
	div.innerHTML = html;
	

}

function removeTheme(label, id){
	var list = document.getElementById('THEME_LIST');
	
	// recréer la combobox avec les options en ajoutant l'option déselectionnée
	var chainOptionThemes = '<select id="THEME_LIST" name="THEME_LIST" class="searchSelect">';
	var themeAdded = false;
	
	chainOptionThemes += '<option value="' + list.options[ 0 ].value + '">' + list.options[ 0 ].text + '</option>';	
	// détermine a quel endroit ajouter l'option dans la combobox (en fonction de l'ordre alphabétique)
	if ( label.toLowerCase() < list.options[ 1 ].text.toLowerCase() ) {
		chainOptionThemes += '<option value="' + id + '">' + label + '</option>';	
		themeAdded = true;
	}
	for ( i = 1; i < list.options.length-1; i++ ) {
		chainOptionThemes += '<option value="' + list.options[ i ].value + '">' + list.options[ i ].text + '</option>';	
		if ( ( label.toLowerCase() > list.options[ i ].text.toLowerCase() ) && ( label.toLowerCase() < list.options[ i + 1 ].text.toLowerCase() ) && ( themeAdded == false ) ) {
			chainOptionThemes += '<option value="' + id + '">' + label + '</option>';
			themeAdded = true;
		}
	}
	chainOptionThemes += '<option value="' + list.options[ list.options.length-1 ].value + '">' + list.options[ list.options.length-1 ].text + '</option>';	
	if ( themeAdded == false ) {
		chainOptionThemes += '<option value="' + id + '">' + label + '</option>';
	}
	chainOptionThemes += '</select> ';
	chainOptionThemes += '<a href="#"><img src="design/images/add.png" border="0"  onclick="addTheme();"/></a><br>';
	
	// redefinie la combobox
	var listSelect = document.getElementById('DIV_THEME_LIST');
	listSelect.innerHTML = chainOptionThemes;
		
	// Supprimer l'id et le label des tableau
	listThemesLabel = removeFromTable(listThemesLabel,label);
	listThemesId = removeFromTable(listThemesId,id);
	printListThemes();	// Raffraichir
}


//=====================================================
// Objective
//=====================================================
function addObjective(){
	var list = document.getElementById('OBJECTIVE_LIST');
	if(list.value != '-1'){
		var id = list.options[ list.selectedIndex ].value;
		var text = list.options[ list.selectedIndex ].text;
		
		// recréer la combobox avec les options sauf celle que l'on choisie
		var chainOptionObjective = '<select id="OBJECTIVE_LIST" name="OBJECTIVE_LIST" class="searchSelect">';
		for ( i = 0; i < list.options.length; i++ ) {
			if ( id != list.options[ i ].value ) {
				chainOptionObjective += '<option value="' + list.options[ i ].value + '">' + list.options[ i ].text + '</option>';	
			}
		}
		chainOptionObjective += '</select> ';
		chainOptionObjective += '<a href="#"><img src="design/images/add.png" border="0"  onclick="addObjective();"/></a><br>';
		
		// redefinie la combobox
		var listSelect = document.getElementById('DIV_OBJECTIVE_LIST');
		listSelect.innerHTML = chainOptionObjective;
		
		// Ajouter l'élément sélectionné dans les tableaux	
		listObjectivesLabel.push(text);
		listObjectivesId.push(id);

		// Afficher 
		printListObjectives();
		
		
		list.selectedIndex = 0;
	}
}

function printListObjectives(){
	var div = document.getElementById('OBJECTIVES');
	var html = '<table class="searchSelectTable" border="0" cellspacing="0" cellpadding="0">';
	var style;
	for(i=0; i < listObjectivesLabel.length; i++)	{
		if( i % 2 == 0){
			style = 'row1';
		} else {
			style = 'row2';
		}
		html = html + '<tr class="'+style+'"><td>';
		html = html + listObjectivesLabel[i] + '<input type="hidden" name="OBJECTIVE_ID_'+i+'" id="OBJECTIVE_ID_'+i+'" value="' + listObjectivesId[i] + '" />';
		html = html + '</td><td align="right"><a href="#" onclick="removeObjective(\''+ listObjectivesLabel[i] + '\',\'' + listObjectivesId[i] + '\');"> <img src="design/images/remove.png" border="0"  /></a>'
		html = html + '</td></tr>';	
	}
	html = html + '</table>';
	div.innerHTML = html;
}

function removeObjective(label, id){
	var list = document.getElementById('OBJECTIVE_LIST');
	
	// recréer la combobox avec les options en ajoutant l'option déselectionnée
	var chainOptionObjective = '<select id="OBJECTIVE_LIST" name="OBJECTIVE_LIST" class="searchSelect">';
	var themeAdded = false;
	
	chainOptionObjective += '<option value="' + list.options[ 0 ].value + '">' + list.options[ 0 ].text + '</option>';	
	// détermine a quel endroit ajouter l'option dans la combobox (en fonction de l'ordre alphabétique)
	if ( label.toLowerCase() < list.options[ 1 ].text.toLowerCase() ) {
		chainOptionObjective += '<option value="' + id + '">' + label + '</option>';	
		themeAdded = true;
	}
	for ( i = 1; i < list.options.length-1; i++ ) {
		chainOptionObjective += '<option value="' + list.options[ i ].value + '">' + list.options[ i ].text + '</option>';	
		if ( ( label.toLowerCase() > list.options[ i ].text.toLowerCase() ) && ( label.toLowerCase() < list.options[ i + 1 ].text.toLowerCase() ) && ( themeAdded == false ) ) {
			chainOptionObjective += '<option value="' + id + '">' + label + '</option>';
			themeAdded = true;
		}
	}
	chainOptionObjective += '<option value="' + list.options[ list.options.length-1 ].value + '">' + list.options[ list.options.length-1 ].text + '</option>';	
	if ( themeAdded == false ) {
		chainOptionObjective += '<option value="' + id + '">' + label + '</option>';
	}
	chainOptionObjective += '</select> ';
	chainOptionObjective += '<a href="#"><img src="design/images/add.png" border="0"  onclick="addObjective();"/></a><br>';
	
	// redefinie la combobox
	var listSelect = document.getElementById('DIV_OBJECTIVE_LIST');
	listSelect.innerHTML = chainOptionObjective;
	
	// Supprimer l'id et le label des tableau
	listObjectivesLabel = removeFromTable(listObjectivesLabel,label);
	listObjectivesId = removeFromTable(listObjectivesId,id);
	printListObjectives();	// Raffraichir
}



//=====================================================
// Geographical Scope
//=====================================================
function addGeographicalScope(){
	var list = document.getElementById('GEOGRAPHICAL_SCOPE_LIST');
	if(list.value != '-1'){
		var id = list.options[ list.selectedIndex ].value;
		var text = list.options[ list.selectedIndex ].text;
		
		// recréer la combobox avec les options sauf celle que l'on choisie
		var chainOptionGeographicalScope = '<select id="GEOGRAPHICAL_SCOPE_LIST" name="GEOGRAPHICAL_SCOPE_LIST" class="searchSelect">';
		for ( i = 0; i < list.options.length; i++ ) {
			if ( id != list.options[ i ].value ) {
				chainOptionGeographicalScope += '<option value="' + list.options[ i ].value + '">' + list.options[ i ].text + '</option>';	
			}
		}
		chainOptionGeographicalScope += '</select> ';
		chainOptionGeographicalScope += '<a href="#"><img src="design/images/add.png" border="0"  onclick="addGeographicalScope();"/></a><br>';
		
		// redefinie la combobox
		var listSelect = document.getElementById('DIV_GEOGRAPHICAL_LIST');
		listSelect.innerHTML = chainOptionGeographicalScope;
		
		// Ajouter l'élément sélectionné dans les tableaux	
		listGeographicalScopesLabel.push(text);
		listGeographicalScopesId.push(id);

		// Afficher 
		printListGeographicalScopes();
		
		
		list.selectedIndex = 0;
	}
}

function printListGeographicalScopes(){
	var div = document.getElementById('GEOGRAPHICAL_SCOPES');
	var html = '<table class="searchSelectTable" border="0" cellspacing="0" cellpadding="0">';
	var style;
	for(i=0; i < listGeographicalScopesLabel.length; i++)	{
		if( i % 2 == 0){
			style = 'row1';
		} else {
			style = 'row2';
		}
		html = html + '<tr class="'+style+'"><td>';
		html = html + listGeographicalScopesLabel[i] + '<input type="hidden" name="GEOGRAPHICAL_SCOPE_ID_'+i+'" id="GEOGRAPHICAL_SCOPE_ID_'+i+'" value="' + listGeographicalScopesId[i] + '" />';
		html = html + '</td><td align="right"><a href="#" onclick="removeGeographicalScope(\''+ listGeographicalScopesLabel[i] + '\',\'' + listGeographicalScopesId[i] + '\');"> <img src="design/images/remove.png" border="0"  /></a>'
		html = html + '</td></tr>';	
	}
	html = html + '</table>';
	div.innerHTML = html;
}

function removeGeographicalScope(label, id){
	var list = document.getElementById('GEOGRAPHICAL_SCOPE_LIST');
	
	// recréer la combobox avec les options en ajoutant l'option déselectionnée
	var chainOptionGeographicalScope = '<select id="GEOGRAPHICAL_SCOPE_LIST" name="GEOGRAPHICAL_SCOPE_LIST" class="searchSelect">';
	var themeAdded = false;
	
	chainOptionGeographicalScope += '<option value="' + list.options[ 0 ].value + '">' + list.options[ 0 ].text + '</option>';	
	// détermine a quel endroit ajouter l'option dans la combobox (en fonction de l'ordre alphabétique)
	if ( label.toLowerCase() < list.options[ 1 ].text.toLowerCase() ) {
		chainOptionGeographicalScope += '<option value="' + id + '">' + label + '</option>';	
		themeAdded = true;
	}
	for ( i = 1; i < list.options.length-1; i++ ) {
		chainOptionGeographicalScope += '<option value="' + list.options[ i ].value + '">' + list.options[ i ].text + '</option>';	
		if ( ( label.toLowerCase() > list.options[ i ].text.toLowerCase() ) && ( label.toLowerCase() < list.options[ i + 1 ].text.toLowerCase() ) && ( themeAdded == false ) ) {
			chainOptionGeographicalScope += '<option value="' + id + '">' + label + '</option>';
			themeAdded = true;
		}
	}
	chainOptionGeographicalScope += '<option value="' + list.options[ list.options.length-1 ].value + '">' + list.options[ list.options.length-1 ].text + '</option>';	
	if ( themeAdded == false ) {
		chainOptionGeographicalScope += '<option value="' + id + '">' + label + '</option>';
	}
	chainOptionGeographicalScope += '</select> ';
	chainOptionGeographicalScope += '<a href="#"><img src="design/images/add.png" border="0"  onclick="addGeographicalScope();"/></a><br>';
	
	// redefinie la combobox
	var listSelect = document.getElementById('DIV_GEOGRAPHICAL_LIST');
	listSelect.innerHTML = chainOptionGeographicalScope;
	
	// Supprimer l'id et le label des tableau
	listGeographicalScopesLabel = removeFromTable(listGeographicalScopesLabel,label);
	listGeographicalScopesId = removeFromTable(listGeographicalScopesId,id);
	printListGeographicalScopes();	// Raffraichir
}


//=====================================================
// Partner countries
//=====================================================
function addPartnerCountry(){
	var list = document.getElementById('PARTNER_COUNTRY_LIST');
	if(list.value != '-1'){
		var id = list.options[ list.selectedIndex ].value;
		var text = list.options[ list.selectedIndex ].text;
		
		// recréer la combobox avec les options sauf celle que l'on choisie
		var chainOptionPartnerCountry = '<select id="PARTNER_COUNTRY_LIST" name="PARTNER_COUNTRY_LIST" class="searchSelect">';
		for ( i = 0; i < list.options.length; i++ ) {
			if ( id != list.options[ i ].value ) {
				chainOptionPartnerCountry += '<option value="' + list.options[ i ].value + '">' + list.options[ i ].text + '</option>';	
			}
		}
		chainOptionPartnerCountry += '</select> ';
		chainOptionPartnerCountry += '<a href="#"><img src="design/images/add.png" border="0"  onclick="addPartnerCountry();"/></a><br>';
		
		// redefinie la combobox
		var listSelect = document.getElementById('DIV_COUNTRY_LIST');
		listSelect.innerHTML = chainOptionPartnerCountry;
		
		// Ajouter l'élément sélectionné dans les tableaux	
		listPartnerCountriesLabel.push(text);
		listPartnerCountriesId.push(id);

		// Afficher 
		printListPartnerCountries();
		
		
		list.selectedIndex = 0;
	}
}

function printListPartnerCountries(){
	var div = document.getElementById('PARTNER_COUNTRIES');
	var html = '<table class="searchSelectTable" border="0" cellspacing="0" cellpadding="0">';
	var style;
	for(i=0; i < listPartnerCountriesLabel.length; i++)	{
		if( i % 2 == 0){
			style = 'row1';
		} else {
			style = 'row2';
		}
		html = html + '<tr class="'+style+'"><td>';
		html = html + listPartnerCountriesLabel[i] + '<input type="hidden" name="PARTNER_COUNTRY_ID_'+i+'" id="PARTNER_COUNTRY_ID_'+i+'" value="' + listPartnerCountriesId[i] + '" />';
		html = html + '</td><td align="right"><a href="#" onclick="removePartnerCountry(\''+ listPartnerCountriesLabel[i] + '\',\'' + listPartnerCountriesId[i] + '\');"> <img src="design/images/remove.png" border="0"  /></a>'
		html = html + '</td></tr>';	
	}
	html = html + '</table>';
	div.innerHTML = html;
}

function removePartnerCountry(label, id){
	var list = document.getElementById('PARTNER_COUNTRY_LIST');
	
	// recréer la combobox avec les options en ajoutant l'option déselectionnée
	var chainOptionPartnerCountry = '<select id="PARTNER_COUNTRY_LIST" name="PARTNER_COUNTRY_LIST" class="searchSelect">';
	var themeAdded = false;
	
	chainOptionPartnerCountry += '<option value="' + list.options[ 0 ].value + '">' + list.options[ 0 ].text + '</option>';	
	// détermine a quel endroit ajouter l'option dans la combobox (en fonction de l'ordre alphabétique)
	if ( label.toLowerCase() < list.options[ 1 ].text.toLowerCase() ) {
		chainOptionPartnerCountry += '<option value="' + id + '">' + label + '</option>';	
		themeAdded = true;
	}
	for ( i = 1; i < list.options.length-1; i++ ) {
		chainOptionPartnerCountry += '<option value="' + list.options[ i ].value + '">' + list.options[ i ].text + '</option>';	
		if ( ( label.toLowerCase() > list.options[ i ].text.toLowerCase() ) && ( label.toLowerCase() < list.options[ i + 1 ].text.toLowerCase() ) && ( themeAdded == false ) ) {
			chainOptionPartnerCountry += '<option value="' + id + '">' + label + '</option>';
			themeAdded = true;
		}
	}
	chainOptionPartnerCountry += '<option value="' + list.options[ list.options.length-1 ].value + '">' + list.options[ list.options.length-1 ].text + '</option>';	
	if ( themeAdded == false ) {
		chainOptionPartnerCountry += '<option value="' + id + '">' + label + '</option>';
	}
	chainOptionPartnerCountry += '</select> ';
	chainOptionPartnerCountry += '<a href="#"><img src="design/images/add.png" border="0"  onclick="addPartnerCountry();"/></a><br>';
	
	// redefinie la combobox
	var listSelect = document.getElementById('DIV_COUNTRY_LIST');
	listSelect.innerHTML = chainOptionPartnerCountry;
	
	// Supprimer l'id et le label des tableau
	listPartnerCountriesLabel = removeFromTable(listPartnerCountriesLabel,label);
	listPartnerCountriesId = removeFromTable(listPartnerCountriesId,id);
	printListPartnerCountries();	// Raffraichir
}


//=====================================================
// Hydrographical Scope
//=====================================================
function addHydrographicalScope(){
	var list = document.getElementById('HYDROGRAPHICAL_SCOPE_LIST');
	if(list.value != '-1'){
		var id = list.options[ list.selectedIndex ].value;
		var text = list.options[ list.selectedIndex ].text;
		
		// recréer la combobox avec les options sauf celle que l'on choisie
		var chainOptionHydrographicalScope = '<select id="HYDROGRAPHICAL_SCOPE_LIST" name="HYDROGRAPHICAL_SCOPE_LIST" class="searchSelect">';
		for ( i = 0; i < list.options.length; i++ ) {
			if ( id != list.options[ i ].value ) {
				chainOptionHydrographicalScope += '<option value="' + list.options[ i ].value + '">' + list.options[ i ].text + '</option>';	
			}
		}
		chainOptionHydrographicalScope += '</select> ';
		chainOptionHydrographicalScope += '<a href="#"><img src="design/images/add.png" border="0"  onclick="addHydrographicalScope();"/></a><br>';
		
		// redefinie la combobox
		var listSelect = document.getElementById('DIV_HYDROGRAPHICAL_LIST');
		listSelect.innerHTML = chainOptionHydrographicalScope;
		
		// Ajouter l'élément sélectionné dans les tableaux	
		listHydrographicalScopesLabel.push(text);
		listHydrographicalScopesId.push(id);

		// Afficher 
		printListHydrographicalScopes();
		
		
		list.selectedIndex = 0;
	}
	return false;
}

function printListHydrographicalScopes(){
	var div = document.getElementById('HYDROGRAPHICAL_SCOPES');
	var html = '<table class="searchSelectTable" border="0" cellspacing="0" cellpadding="0">';
	var style;
	for(i=0; i < listHydrographicalScopesLabel.length; i++)	{
		if( i % 2 == 0){
			style = 'row1';
		} else {
			style = 'row2';
		}
		html = html + '<tr class="'+style+'"><td>';
		html = html + listHydrographicalScopesLabel[i] + '<input type="hidden" name="HYDROGRAPHICAL_SCOPE_ID_'+i+'" id="HYDROGRAPHICAL_SCOPE_ID_'+i+'" value="' + listHydrographicalScopesId[i] + '" />';
		html = html + '</td><td align="right"><a href="#" onclick="removeHydrographicalScope(\''+ listHydrographicalScopesLabel[i] + '\',\'' + listHydrographicalScopesId[i] + '\');"> <img src="design/images/remove.png" border="0"  /></a>'
		html = html + '</td></tr>';	
	}
	html = html + '</table>';
	div.innerHTML = html;
}

function removeHydrographicalScope(label, id){
	var list = document.getElementById('HYDROGRAPHICAL_SCOPE_LIST');
	
	// recréer la combobox avec les options en ajoutant l'option déselectionnée
	var chainOptionHydrographicalScope = '<select id="HYDROGRAPHICAL_SCOPE_LIST" name="HYDROGRAPHICAL_SCOPE_LIST" class="searchSelect">';
	var themeAdded = false;
	
	chainOptionHydrographicalScope += '<option value="' + list.options[ 0 ].value + '">' + list.options[ 0 ].text + '</option>';	
	// détermine a quel endroit ajouter l'option dans la combobox (en fonction de l'ordre alphabétique)
	if ( label.toLowerCase() < list.options[ 1 ].text.toLowerCase() ) {
		chainOptionHydrographicalScope += '<option value="' + id + '">' + label + '</option>';	
		themeAdded = true;
	}
	for ( i = 1; i < list.options.length-1; i++ ) {
		chainOptionHydrographicalScope += '<option value="' + list.options[ i ].value + '">' + list.options[ i ].text + '</option>';	
		if ( ( label.toLowerCase() > list.options[ i ].text.toLowerCase() ) && ( label.toLowerCase() < list.options[ i + 1 ].text.toLowerCase() ) && ( themeAdded == false ) ) {
			chainOptionHydrographicalScope += '<option value="' + id + '">' + label + '</option>';
			themeAdded = true;
		}
	}
	chainOptionHydrographicalScope += '<option value="' + list.options[ list.options.length-1 ].value + '">' + list.options[ list.options.length-1 ].text + '</option>';	
	if ( themeAdded == false ) {
		chainOptionHydrographicalScope += '<option value="' + id + '">' + label + '</option>';
	}
	chainOptionHydrographicalScope += '</select> ';
	chainOptionHydrographicalScope += '<a href="#"><img src="design/images/add.png" border="0"  onclick="addHydrographicalScope();"/></a><br>';
	
	// redefinie la combobox
	var listSelect = document.getElementById('DIV_HYDROGRAPHICAL_LIST');
	listSelect.innerHTML = chainOptionHydrographicalScope;
	
	// Supprimer l'id et le label des tableau
	listHydrographicalScopesLabel = removeFromTable(listHydrographicalScopesLabel,label);
	listHydrographicalScopesId = removeFromTable(listHydrographicalScopesId,id);
	printListHydrographicalScopes();	// Raffraichir
	return false;
}

/**
 * Récupère la liste des items
 */
function getItems( input )
{
	listItems = input.value;
		
	var regExp = new RegExp("[ ]+","g");
	if( trim(input.value).length > 0 ){
		items = trim(listItems).split( regExp );
		for(i = 0; i < items.length; i++){
			items[i] = trim(items[i]);
		}
	}
}


/**
 * Récupère la liste des words
 */
function getWords( input ) 
{
	listWords = input.value;

	/*
	do {
		listWordsInitial = listWords;
		listWords = listWords.replace("*"," ");
		listWords = listWords.replace("-"," ");
		listWords = listWords.replace("."," ");
	} while ( listWords != listWordsInitial );
	*/

	var regExp = new RegExp("[ ]+","g");

	if( trim(listWords).length > 0 ){
		words = trim(listWords).split( regExp );
		for(i = 0; i < words.length; i++){
			words[i] = trim(words[i]);
		}
	}
}

/**
 * Récupère la liste des items
 */
function fillUserKeywordsField()
{

	chainItems = '';
	if ( document.getElementById('SEARCH').value != '' ) {
		for (i = 0; i < items.length; i++) {
			if ( i == 0 ) {
				chainItems = items[i];
			} else {
				chainItems = chainItems + ' - ' + items[i];
			}
		}
	}
	document.getElementById('userKeywords').innerHTML = chainItems;
}
	
/**
 * Récupère la liste des items dans les checkboxs et ajoute ou reduit le nombre de boites de dialogue selon les parametres selectionnés
 */
function fillUserKeywordsList()
{
	chainSelect = '';
	countItems = 0;
	i = 0;
	
	if ( document.getElementById('SEARCH').value != '' ) {
		// rempli le tableau des items selectionnés dans les boites de dialogue et compte le nombre d'item
		var itemsUsed = new Array();
		stop = false;
		while ( document.getElementById('keyWord_' + i) ) {
			valueSelect = document.getElementById('keyWord_' + i).value;
			
			// si on a sélectionné une valeur dans la boite de dialogue on rempli le tableau avec la valeur et on incremente le nombre d'item
			if ( valueSelect != "selectKeyword") {
				exist = false;
				for (j = 0; j < items.length; j++) {
					if ( ( items[j] == document.getElementById('keyWord_' + i).value ) && ( stop == false ) ) {
						exist = true;
					}
				}
				if ( exist == true ) {
					itemsUsed[countItems] = document.getElementById('keyWord_' + i).value;
					countItems++;
				}
			} else {
				stop = true;
			}
			i++;
		}

		for ( i = 0; i < countItems; i++) {
			chainSelect += '<select name="keyWord_' + i + '" id="keyWord_' + i + '" onchange="fillUserKeywordsList();" >';
			chainSelect += '<option value="selectKeyword"> -- Select a keyword -- </option>';

			for (j = 0; j < items.length; j++) {
				exist = false;
				for (k = 0; k < i; k++) {
					if ( itemsUsed[k] == items[j] ) {
						exist = true;
					}
				}
				if ( exist == false ) {
					if ( document.getElementById('keyWord_' + i).value == items[j] ) {
						chainSelect += '<option selected value="' + items[j] + '">' + items[j] + '</option>';
					} else {
						chainSelect += '<option value="' + items[j] + '">' + items[j] + '</option>';
					}
				}
			}
			chainSelect += '</select>&nbsp;';
		}
		
		if ( countItems < items.length ) {
			chainSelect += '<select name="keyWord_' + i + '" id="keyWord_' + i + '" onchange="fillUserKeywordsList();" >';
			chainSelect += '<option value="selectKeyword"> -- Select a keyword -- </option>';
			for (j = 0; j < items.length; j++) {
				exist = false;
				for (k = 0; k < i; k++) {
					if ( itemsUsed[k] == items[j] ) {
						exist = true;
					}
				}
				if ( exist == false ) {
					chainSelect += '<option value="' + items[j] + '">' + items[j] + '</option>&nbsp;';
				}
			}
			chainSelect += '</select>&nbsp;';
		}
	}
	
	document.getElementById('conditionKeywords').innerHTML = chainSelect;
}

function rules(typeRule, tabItems) {
	this.typeRule = typeRule;
	this.tabItems = tabItems;
}
  
function addRule()
{
	tableItems = new Array();
	i = 0;
	while ( document.getElementById('keyWord_' + i) ) {
		if ( document.getElementById('keyWord_' + i).value != "selectKeyword" ) {
			tableItems[i] = document.getElementById('keyWord_' + i).value
		}
		i++;
	}

	if ( document.getElementById('conditionChoice').value == "withAllWords" ) {
		condition = "With all these words";
	} else if ( document.getElementById('conditionChoice').value == "withExactSentence" ) {
		condition = "With the exact sentence";
	}  else if ( document.getElementById('conditionChoice').value == "withoutWords" ) {
		condition = "Without the words";
	} else if ( document.getElementById('conditionChoice').value == "startsWith" ){
		condition = "Starts with";
	} else if ( document.getElementById('conditionChoice').value == "contains" ){
		condition = "Contains";
	} else if ( document.getElementById('conditionChoice').value == "endsWith" ){
		condition = "Ends with";
	} else {
		alert('Bad rule selected');
	}
	
	tableRules[tableRules.length] = new rules(condition, tableItems);
	
	chainRowRules = '';
	if ( tableRules.length > 0 ) {
		chainRowRules += '<table cellspacing="0px" id="tableRules" border="0" width="100%" align="center">';
		even = 0;
		for ( i = 0; i < tableRules.length; i++ ) {
			chainRowRules += '<tr><td class="informationRule' + even + '">' + tableRules[i].typeRule + '</td>';
			chainRowRules += '<input type="hidden" name="RULE_TYPE_' + i + '"  id="RULE_TYPE_' + i + '" value="' + tableRules[i].typeRule + '" />';
			chainRowRules += '<td class="informationKeywords' + even + '">';
			for ( j = 0; j < tableRules[i].tabItems.length; j++ ) {
				chainRowRules += tableRules[i].tabItems[j];
				if ( j + 1 < tableRules[i].tabItems.length ) {
					chainRowRules += ' - ';
				}
				chainRowRules += '<input type="hidden" name="RULE_ITEM_' + i + '_' + j + '"   id="RULE_ITEM_' + i + '_' + j + '" value="' + tableRules[i].tabItems[j] + '" />';
			}
			chainRowRules += '</td>';
			chainRowRules += '<td class="buttonRule' + even + '" style="vertical-align: middle; text-align: center; width: 105px"><input type="button" value="Remove rule" onclick="removeRule(' + i + ');return false;" /></td>';
			chainRowRules += '<td></td></tr>';
			even++;
			if ( even > 1 ) {
				even = 0;
			}
		}
		chainRowRules += '</table>';
	}
	document.getElementById('rowRules').innerHTML = chainRowRules;
	
	
	// remet les listes de select a zéro
	chainSelect = '<select name="keyWord_0" id="keyWord_0" onchange="fillUserKeywordsList();" >';
	chainSelect += '<option selected value="selectKeyword"> -- Select a keyword -- </option>';
	for (j = 0; j < items.length; j++) {
		chainSelect += '<option value="' + items[j] + '">' + items[j] + '</option>';
	}
	chainSelect += '</select>&nbsp;';
	document.getElementById('conditionKeywords').innerHTML = chainSelect;
	
	document.getElementById('conditionChoice').value = "selectRule";
}
  
function removeRule(idRule)
{
	for ( i = idRule; i < tableRules.length; i++ ) {
		tableRules[i] = tableRules[i+1];
	}
	tableRules.pop();

	chainRowRules = '';
	if ( tableRules.length > 0 ) {
		chainRowRules += '<table cellspacing="0px" id="tableRules" border="0" width="100%" align="center">';
		even = 0;
		for ( i = 0; i < tableRules.length; i++ ) {
			chainRowRules += '<tr><td class="informationRule' + even + '">' + tableRules[i].typeRule + '</td>';
			chainRowRules += '<input type="hidden" name="RULE_TYPE_' + i + '"  id="RULE_TYPE_' + i + '" value="' + tableRules[i].typeRule + '" />';
			chainRowRules += '<td class="informationKeywords' + even + '">';
			for ( j = 0; j < tableRules[i].tabItems.length; j++ ) {
				chainRowRules += tableRules[i].tabItems[j];
				if ( j + 1 < tableRules[i].tabItems.length ) {
					chainRowRules += ' - ';
				}
				chainRowRules += '<input type="hidden" name="RULE_ITEM_' + i + '_' + j + '"   id="RULE_ITEM_' + i + '_' + j + '" value="' + tableRules[i].tabItems[j] + '" />';
			}
			chainRowRules += '</td>';
			chainRowRules += '<td class="buttonRule' + even + '" style="vertical-align: middle; text-align: center; width: 105px"><input type="button" value="Remove rule" onclick="removeRule(' + i + ');return false;" /></td>';
			chainRowRules += '<td></td></tr>';
			even++;
			if ( even > 1 ) {
				even = 0;
			}
		}
		chainRowRules += '</table>';
	}

	document.getElementById('rowRules').innerHTML = chainRowRules;
}
  
function updateRule() {
	
	for ( i = 0; i < tableRules.length; i++ ) {
		sub = false;
		for ( j = 0; j < tableRules[i].tabItems.length; j++ ) {
			find = false;
			for ( k = 0; k < items.length; k++ ) {
				if ( items[k] == tableRules[i].tabItems[j] ) {
					find = true;
				}
			}
			if ( find == false ) {
				sub = true;
			}
		}
		if ( sub == true ) {
			for ( j = i; j < tableRules.length; j++ ) {
				tableRules[i] = tableRules[i+1];
			}
			tableRules.pop();
			i--;
		}
	}

	chainRowRules = '';
	if ( tableRules.length > 0 ) {
		chainRowRules += '<table cellspacing="0px" id="tableRules" border="0" width="100%" align="center">';
		even = 0;
		for ( i = 0; i < tableRules.length; i++ ) {
			chainRowRules += '<tr><td class="informationRule' + even + '">' + tableRules[i].typeRule + '</td>';
			chainRowRules += '<input type="hidden" name="RULE_TYPE_' + i + '"  id="RULE_TYPE_' + i + '" value="' + tableRules[i].typeRule + '" />';
			chainRowRules += '<td class="informationKeywords' + even + '">';
			for ( j = 0; j < tableRules[i].tabItems.length; j++ ) {
				chainRowRules += tableRules[i].tabItems[j];
				if ( j + 1 < tableRules[i].tabItems.length ) {
					chainRowRules += ' - ';
				}
				chainRowRules += '<input type="hidden" name="RULE_ITEM_' + i + '_' + j + '"   id="RULE_ITEM_' + i + '_' + j + '" value="' + tableRules[i].tabItems[j] + '" />';
			}
			chainRowRules += '</td>';
			chainRowRules += '<td class="buttonRule' + even + '" style="vertical-align: middle; text-align: center; width: 105px"><input type="button" value="Remove rule" onclick="removeRule(' + i + ');return false;" /></td>';
			chainRowRules += '<td></td></tr>';
			even++;
			if ( even > 1 ) {
				even = 0;
			}
		}
		chainRowRules += '</table>';
	}

	document.getElementById('rowRules').innerHTML = chainRowRules;
}


function printListFromItems(id,onchange){
	var str = '<select class="searchSelect" name="' + id + '" id="' + id + '" onchange="' + onchange + '">';
	str = str + '<option value="0">-- Select choice --</option>'
	for(i = 0; i < items.length; i++){
		str = str + '<option value="' + items[i] + '">' + items[i] + '</option>';
	}
	str = str + '</select>';
	return str;
}

function loadTables(chain){
	
	var lists = chain.split("|SeparatedParameter|"); 
	var listCategory;
	var listItems;
	var listTypeRules = new Array();
	
	// parcours chaque liste 
	for ( i = 0; i < lists.length; i++ ) {
		listCategory = lists[i].split("|SeparatedList|"); 
		// parcours chaque objet d'un type de catégorie

		for ( j = 0; j < listCategory.length; j++ ) {
			if ( !( ( listCategory.length == 1 ) && ( listCategory[j] == '' ) ) ) {
				if ( ( i >=0 ) && ( i < 10 ) ) {
					document.getElementById('DIV_CRITERIAS').style.display = 'block';
				}
				if ( i == 0 ) {
					listThemesId[j] = listCategory[j];
				} else if ( i == 1 ) {
					listThemesLabel[j] = listCategory[j];
				} else if ( i == 2 ) {
					listObjectivesId[j] = listCategory[j];
				} else if ( i == 3 ) {
					listObjectivesLabel[j] = listCategory[j];
				} else if ( i == 4 ) {
					listGeographicalScopesId[j] = listCategory[j];
				} else if ( i == 5 ) {
					listGeographicalScopesLabel[j] = listCategory[j];
				} else if ( i == 6 ) {
					listPartnerCountriesId[j] = listCategory[j];
				} else if ( i == 7 ) {
					listPartnerCountriesLabel[j] = listCategory[j];
				} else if ( i == 8 ) {
					listHydrographicalScopesId[j] = listCategory[j];
				} else if ( i == 9 ) {
					listHydrographicalScopesLabel[j] = listCategory[j];
				} else if ( i == 10 ) {
					document.getElementById('DIV_RULES').style.display = 'block';
					listTypeRules[j] = listCategory[j];
				} else if ( i == 11 ) {
					listItems = listCategory[j].split("|SeparatedItem|"); 
					listItemsRule = new Array();
					for ( k = 0; k < listItems.length; k++ ) {
						listItemsRule[k] = listItems[k];
					}
					
					tableRules[j] = new rules(listTypeRules[j], listItemsRule);
				} else if ( i == 12 ) {
					document.getElementById('DIV_CRITERIAS').style.display = 'block';
					
				}
			}
		}
	}
	
	getItems(document.getElementById('SEARCH'));
	getWords(document.getElementById('SEARCH'));

	fillUserKeywordsField();
	fillUserKeywordsList();
	updateRule();
}

function resetFilter() {
	for ( i = listThemesLabel.length - 1; i >= 0; i--) {
		removeTheme(listThemesLabel[i], listThemesId[i]);
	}
	for ( i = listObjectivesLabel.length - 1; i >= 0; i--) {
		removeObjective(listObjectivesLabel[i], listObjectivesId[i]);
	}
	for ( i = listGeographicalScopesLabel.length - 1; i >= 0; i--) {
		removeGeographicalScope(listGeographicalScopesLabel[i], listGeographicalScopesId[i]);
	}
	for ( i = listHydrographicalScopesLabel.length - 1; i >= 0; i--) {
		removeHydrographicalScope(listHydrographicalScopesLabel[i], listHydrographicalScopesId[i]);
	}
	for ( i = listPartnerCountriesLabel.length - 1; i >= 0; i--) {
		removePartnerCountry(listPartnerCountriesLabel[i], listPartnerCountriesId[i]);
	}
}