// Fonctions d'écriture de la requête de recherche en full-text sur le Site DMT


// fonction permettant de filtrer les caractères accentués et d'intégrer un éventuel opérateur booléen
function replaceChars(entry) {
	
	// initialisation des variables à modifier
	vplus="+";
	vvirg=",";
	vet=" et ";
	vetmaj=" ET ";
	vdoubleblanc="  ";
	vou=" ou ";
	voumaj=" OU "
	vcote='"';

	veaigu="é";
	vegrave="è";
	vecirc="ê";
	vetrem="ë";
	ve="e";

	vagrave="à";
	vacirc="â";
	va="a";

	vicirc="î";
	vitrem="ï";
	vi="i";

	vugrave="ù";
	vucirc="û";
	vutrem="ü";
	vu="u";

	vocirc="ô";
	vo="o";

	vcedil="ç"
	vc="c"

	temp = "" + entry; // temporary holder
	
	// remplacer + par un espace
	while (temp.indexOf(vplus)>-1) {
		pos= temp.indexOf(vplus);
		temp = "" + (temp.substring(0, pos) + out + 
		temp.substring((pos + 1), temp.length));
	}

	// remplacer la virgule par un espace
	while (temp.indexOf(vvirg)>-1) {
		pos= temp.indexOf(vvirg);
		temp = "" + (temp.substring(0, pos) + out + 
		temp.substring((pos + 1), temp.length));
	}

	// remplacer la double cote par un espace
	while (temp.indexOf(vcote)>-1) {
		pos= temp.indexOf(vcote);
		temp = "" + (temp.substring(0, pos) + out + 
		temp.substring((pos + 1), temp.length));
	}

	// remplacer ' et ' par un espace
	while (temp.indexOf(vet)>-1) {
		pos= temp.indexOf(vet);
		temp = "" + (temp.substring(0, pos) + out + 
		temp.substring((pos + 4), temp.length));
	}

	// remplacer ' ET ' par un espace
	while (temp.indexOf(vetmaj)>-1) {
		pos= temp.indexOf(vetmaj);
		temp = "" + (temp.substring(0, pos) + out + 
		temp.substring((pos + 4), temp.length));
	}

	// remplacer ' ou ' par un espace
	while (temp.indexOf(vou)>-1) {
		pos= temp.indexOf(vou);
		temp = "" + (temp.substring(0, pos) + out + 
		temp.substring((pos + 4), temp.length));
	}

	// remplacer ' OU ' par un espace
	while (temp.indexOf(voumaj)>-1) {
		pos= temp.indexOf(voumaj);
		temp = "" + (temp.substring(0, pos) + out + 
		temp.substring((pos + 4), temp.length));
	}

	// remplacer é par e
	while (temp.indexOf(veaigu)>-1) {
		pos= temp.indexOf(veaigu);
		temp = "" + (temp.substring(0, pos) + ve + 
		temp.substring((pos + 1), temp.length));
	}

	// remplacer è par e
	while (temp.indexOf(vegrave)>-1) {
		pos= temp.indexOf(vegrave);
		temp = "" + (temp.substring(0, pos) + ve + 
		temp.substring((pos + 1), temp.length));
	}

	// et ainsi de suite pour tous les caractères accentués
	while (temp.indexOf(vecirc)>-1) {
		pos= temp.indexOf(vecirc);
		temp = "" + (temp.substring(0, pos) + ve + 
		temp.substring((pos + 1), temp.length));
	}

	while (temp.indexOf(vetrem)>-1) {
		pos= temp.indexOf(vetrem);
		temp = "" + (temp.substring(0, pos) + ve + 
		temp.substring((pos + 1), temp.length));
	}

	while (temp.indexOf(vagrave)>-1) {
		pos= temp.indexOf(vagrave);
		temp = "" + (temp.substring(0, pos) + va + 
		temp.substring((pos + 1), temp.length));
	}
	
	while (temp.indexOf(vacirc)>-1) {
		pos= temp.indexOf(vacirc);
		temp = "" + (temp.substring(0, pos) + va + 
		temp.substring((pos + 1), temp.length));
	}

	while (temp.indexOf(vicirc)>-1) {
		pos= temp.indexOf(vicirc);
		temp = "" + (temp.substring(0, pos) + vi + 
		temp.substring((pos + 1), temp.length));
	}

	while (temp.indexOf(vitrem)>-1) {
		pos= temp.indexOf(vitrem);
		temp = "" + (temp.substring(0, pos) + vi + 
		temp.substring((pos + 1), temp.length));
	}

	while (temp.indexOf(vugrave)>-1) {
		pos= temp.indexOf(vugrave);
		temp = "" + (temp.substring(0, pos) + vu + 
		temp.substring((pos + 1), temp.length));
	}

	while (temp.indexOf(vucirc)>-1) {
		pos= temp.indexOf(vucirc);
		temp = "" + (temp.substring(0, pos) + vu + 
		temp.substring((pos + 1), temp.length));
	}

	while (temp.indexOf(vutrem)>-1) {
		pos= temp.indexOf(vutrem);
		temp = "" + (temp.substring(0, pos) + vu + 
		temp.substring((pos + 1), temp.length));
	}

	while (temp.indexOf(vocirc)>-1) {
		pos= temp.indexOf(vocirc);
		temp = "" + (temp.substring(0, pos) + vo + 
		temp.substring((pos + 1), temp.length));
	}

	while (temp.indexOf(vcedil)>-1) {
		pos= temp.indexOf(vcedil);
		temp = "" + (temp.substring(0, pos) + vc + 
		temp.substring((pos + 1), temp.length));
	}

	// remplacer 2 espaces par un seul
	while (temp.indexOf(vdoubleblanc)>-1) {
		pos= temp.indexOf(vdoubleblanc);
		temp = "" + (temp.substring(0, pos) + out + 
		temp.substring((pos + 2), temp.length));
	}

	// effacer les blancs surnuméraires, en début de chaine
	while (''+temp.charAt(0)==' ') {
		temp = temp.substring(1, temp.length);
	}

	// effacer les blancs surnuméraires, en fin de chaine
	while (''+temp.charAt(temp.length-1)==' ') {
		temp = temp.substring(0, temp.length-1);
	}

		
	// en fonction du bouton radio, on modifie la chaine 'temp'	


	// OU booléen
	if (document.rechav.ope[0].checked == true) {
	out = " "; // replace this
	add = "$or$"; // with this
	out2 = "$" ;// replace this (2nd part)
	add2 = " " ; // with this

	// 1ere étape : remplacer " " par "$or$"
	while (temp.indexOf(out)>-1) {
		pos= temp.indexOf(out);
		temp = "" + (temp.substring(0, pos) + add + 
		temp.substring((pos + out.length), temp.length));
		}

	// 2e étape : remplacer "$" par " "
	while (temp.indexOf(out2)>-1) {
	pos= temp.indexOf(out2);
	temp = "" + (temp.substring(0, pos) + add2 + 
	temp.substring((pos + out.length), temp.length));
		}
	}

	// ET booléen
	if (document.rechav.ope[1].checked == true) {
	out = " "; // replace this
	add = "$and$"; // with this
	out2 = "$" ;// replace this (2nd part)
	add2 = " " ; // with this

	// 1ere étape : remplacer " " par "$and$"
	while (temp.indexOf(out)>-1) {
		pos= temp.indexOf(out);
		temp = "" + (temp.substring(0, pos) + add + 
		temp.substring((pos + out.length), temp.length));
	}

	// 2e étape : remplacer "$" par " "
	while (temp.indexOf(out2)>-1) {
	pos= temp.indexOf(out2);
	temp = "" + (temp.substring(0, pos) + add2 + 
	temp.substring((pos + out.length), temp.length));
		}
	}

	// dans tous les cas, la valeur est entourée de parenthèses
	temp="("+temp+")" ;

return temp;
}


// Fonction permettant de construire la Query
function construireRequete() {
	req="" ;

	// traitement du champ 'terme'
	req=document.rechav.terme.value;

	if(req!="") {
		req=replaceChars(document.rechav.terme.value);
	}

	//traitement de la collection
	vcollec = document.rechav.collec.value ;
	if (vcollec!="") {
		if (req == "") {
			req="([reference]="+vcollec+")" ; }
		else {
			req=req+" and ([reference]="+vcollec+")";
		}
	}	

	return req;
}


// Fonction construisant la requête passée au moteur Notes					
function executerRecherche() {

	BaseHref="/inrs-pub/inrs01.nsf/";
	SearchView="inrs01_dmtfulltext_view";
	SearchOrder="4";
	if (document.rechav.ope[2].checked == true) {
		SearchWV="False";}
	  else {
		SearchWV="True"
       }
	SearchFuzzy="False";
	Start="1";
	Max="all";
	Count="300";
	

		StrRequete = construireRequete();

		if(StrRequete==""){
		alert("Vous devez saisir au moins un critère de recherche");
		}
			else {
		urlRecherche = BaseHref + SearchView+"/?SearchView=" 
					+ "&Query="+StrRequete
					+ "&SearchMax="+Max
					+ "&Start="+Start
					+ "&SearchOrder="+SearchOrder
					+ "&SearchWV="+SearchWV
					+ "&SearchFuzzy="+SearchFuzzy
					+ "&Count="+Count;

		window.location.href = urlRecherche;
		}

}

