var test = 1;


//fonction qui créé lea requête d'interrogation
function doSubmitForm(frm)
{
	try
	{
	if(frm["noField-phrase-input"].value.length > 0)
		frm["noField-phrase"].value = quote(frm["noField-phrase-input"].value)
	else if(frm["noField-phrase"].value.length > 0)
		frm["noField-phrase"].value = '';
	}
	catch(e)	 {}
	creationChamp(frm);	
		
	doMakeQuery(frm.query);
}

function creationChamp(frm)
{
	var champsSelect=new Array();
	var htmlChamp = "";
	for (var x=1;x<4;x++)
	{
		if (Element.visible("champ"+x)==true)
		{
			var champ = document.getElementById("selectChamp"+x+"Name").value;
			if(frm["text"+x].value.length > 0)
			{							
				var text = document.getElementById("text"+x).value;	
				champsSelect[x] = new Array();			
				champsSelect[x]["nomChamp"]=champ;
				champsSelect[x]["text"]=text;
			}
		}
	}	

	for (var i=1;i<champsSelect.length;i++)
	{
		if (champsSelect[i])
		{
			var champ = champsSelect[i]["nomChamp"];
			var text = champsSelect[i]["text"];
		

			for (var j=1;j<champsSelect.length;j++)
			{
				if (j != i && champ == champsSelect[j]["nomChamp"])
				{
					text = text+"+"+ champsSelect[j]["text"];
					champsSelect[j]="";
					 
				}
			}
			htmlChamp = htmlChamp + "<input type='text' name='"+champ+"' value='"+text+"'>";
			htmlChamp = htmlChamp + "<input type='text' name='"+champ+"Modifier' value='+|+'>";
		}
	}
	document.getElementById("search").innerHTML = htmlChamp;
}

function rechercher ()
{
//Ici pour faire des vérifications sur le formulaire
// if (document.formulaire.elements['noField-and' ].value == ""|document.formulaire.elements['noField-and' ].value == " "|document.formulaire.elements['noField-and' ].value == "  "|document.formulaire.elements['noField-and' ].value == "   "|document.formulaire.elements['noField-and' ].value == "    "|document.formulaire.elements['noField-and' ].value == "     "|document.formulaire.elements['noField-and' ].value == "      "|document.formulaire.elements['noField-and' ].value == "       ")
// {
//	Element.show('champ_vide');
// }
// else
// {
	 envoiRecherche();
	 return true;
// }
}

function envoiRecherche()
{
	Element.show("loader");		
	var query = document.getElementById('query').value;
	var url = "http://philidor.cmbv.fr/jlbweb/jlbWeb?html=cmbv/result";	
	o_options = new Object();
	document.formulaire.submit();
	
	
//	 new Ajax.Request(url,{
//	 method: 'post',
//	 asynchronous: true,
//	 encoding: 'UTF-8',
//	 parameters: { query : $F('query'),nomBase : $F('nomBase')},
//	 onSuccess: afficherReponse,
//	 onComplete:afficherReponse,
//	 onFailure: afficherProbleme,
//	 on404:afficherProbleme
//	 }); 
	return false;
}

function afficherReponse(requete) 
{
	Element.hide("loader");	
	location.href="http://philidor.cmbv.fr/jlbweb/jlbWeb?html=cmbv/liste_weboeu";
} 

function afficherProbleme(requete) 
{ 
	document.getElementById('query').value = "Il y a eu un problème";
	Element.hide("loader");		
}
		
function annuler ()
{
	document.formulaire.elements['recspl'].value = "";
	Element.hide("loader");		
}	

//Fonction qui permet de rajouter les options de recherches
i = 1;
function plusOptions()
{
	c = i%2;
	if (c > 0)
	{
		Element.show("options");
		Element.show("options1");
		Element.show("options2");
		Element.show("options3");
		document.getElementById('txtOption').innerHTML = "<img src='http://philidor.cmbv.fr/jlbweb/images/minus.gif' border='0'><img src='http://philidor.cmbv.fr/jlbweb/images/vide.gif' border='0' width='3'>Moins d'option";		
	}
	else
	{
		Element.hide("options");
		Element.hide("options1");
		Element.hide("options2");
		Element.hide("options3");
		document.getElementById('txtOption').innerHTML = "<img src='http://philidor.cmbv.fr/jlbweb/images/plus.gif' border='0'><img src='http://philidor.cmbv.fr/jlbweb/images/vide.gif' border='0' width='3'>Plus d'option";
	}
	i++;
}

Array.prototype.inArray = function(array) {
for(var i=0; i<this.length;i++) {
if(this[i]==array){ return true;}
}
return false;
}; 

function SelectClass(className, Tag)
{
	var elts = document.getElementsByTagName(Tag);
	for (var j=0;j<elts.length;j++) 
	{
		if (elts[j].getAttribute('class') && elts[j].getAttribute('class').split(' ').inArray(className)) 
		{
			elts[j].className='pasSelectionne';
		}
	}
}

//fonction qui attribue un champ de recherche
function envoi_champ(champ)
{
 	SelectClass('selectionne', 'td');
	var valueChamp = "";
	var maSelection = document.getElementById('selectNum').value;
	if (maSelection==1) 
	{
		if (document.getElementById("text1"))
		{
			valueChamp = document.getElementById("text1").value;
		}
		codeChamp(1,champ,valueChamp);
		document.getElementById(champ).className='selectionne';
	}
	else if (maSelection==2) 
	{
		if (document.getElementById("text2"))
		{
			valueChamp = document.getElementById("text2").value;
		}
		codeChamp(2,champ,valueChamp);		
		document.getElementById(champ).className='selectionne';
	}
	else if (maSelection==3) 
	{
		if (document.getElementById("text3"))
		{
			valueChamp = document.getElementById("textt3").value;
		}
		codeChamp(3,champ,valueChamp);		
		document.getElementById(champ).className='selectionne';
	}
}
	
function ajouterChamp()
{
	if (Element.visible('champ1')==false)
	{
		Element.show('avance');Element.show('enlever');
		showChamp(1);
		colorSelect('selectChamp1Name');
	}
	else 
	{ 
		if (Element.visible('champ2')==false)
		{
			Element.hide('selectChamp1');
			showChamp(2);
			colorSelect('selectChamp2Name');
		}
		else
		{
			if (Element.visible('champ3')==false)
			{
				Element.hide('ajouter');Element.hide('selectChamp2');
				showChamp(3);
				colorSelect('selectChamp3Name');
			}
		}
	}
}

function enleverChamp()
{
	if (Element.visible('champ3')==true)
	{
		Element.show('ajouter');
		hideChamp(3);
		colorSelect('selectChamp2Name');
	}
	else 
	{ 
		if (Element.visible('champ2')==true)
		{
			Element.show('selectChamp1');
			hideChamp(2);
			colorSelect('selectChamp1Name');
		}
		else
		{
			if (Element.visible('champ1')==true)
			{
				Element.hide('avance');
				Element.hide('enlever');
				hideChamp(1);
			}
		}
	}
}

//code du nouveau champ avancé inséré
function codeChamp(Num,champ,valueChamp)
{
	if (champ.length > 0) {} else champ="";
	
	var valueChampIndex = "";
	if (valueChamp.length > 0) {valueChampIndex=valueChamp} else valueChampIndex="A";
	
	document.getElementById("champ"+Num).innerHTML = "<input type='text' name='' id='text"+Num+"' value='"+valueChamp+"' class='saisie' size='50px' style='border: solid 1px black;font-size:10;' OnFocus='javascript:selectChamp("+Num+");'><input type='hidden' id='selectChamp"+Num+"Name' value='"+champ+"'><a href=\"#\" Onclick=\"javascript:openIndex('"+Num+"','"+champ+"','"+valueChampIndex+"');\">&nbsp;&nbsp;Voir l'index</a>"; 
}


function showChamp(Num)
{
	codeChamp(Num,'','');
	Element.show('champ'+Num);
	selectChampColor(Num);
	document.getElementById('selectNum').value=Num;
	ajaxContext(Num);
}

function hideChamp(Num)
{
	Element.hide('champ'+Num);
	document.getElementById("champ"+Num).innerHTML = "";
	selectChampColor(Num-1);
	document.getElementById('selectNum').value=Num-1;
	ajaxContext(Num-1);
}

function ligneOver(element)
{
	element.style.background='#99B3AA';
}
function ligneOut(element)
{
	element.style.background='#EBE9E7';
}

function colorSelect(selectName)
{
	var id = document.getElementById(selectName).value;
	SelectClass('selectionne', 'td');
	if (id.length > 0)
	{
		document.getElementById(id).className='selectionne';
	}
	return false;
}

function selectChamp(Num)
{
	colorSelect("selectChamp"+Num+"Name");
	document.getElementById('selectNum').value=Num;
	var momChampSelect = document.getElementById("selectChamp"+Num+"Name").value;
	selectChampColor(Num);
	plusChampShow(momChampSelect);
}

//Fonction pour mettre le carré de couleur sur le champ sélectionné
function selectChampColor(Num)
{
	if (Num == 1){	Element.show('selectChamp1');Element.hide('selectChamp2');Element.hide('selectChamp3');}
	if (Num == 2){	Element.show('selectChamp2');Element.hide('selectChamp1');Element.hide('selectChamp3');}
	if (Num == 3){	Element.show('selectChamp3');Element.hide('selectChamp1');Element.hide('selectChamp2');}
}

//fonction qui teste si on a appuyé sur "entrée" !
function testForEnter() 
{    
	if (event.keyCode == 13) 
	{        
		rechercher();
    }
}

//Fonction qui remet à Zero le champ Num du formulaire
function remiseDeb(Num)
{
	document.getElementById("noField-and").value="";
	SelectClass('selectionne', 'td');
	document.getElementById("champ"+Num).innerHTML = "";
	
	//codeChamp(Num,'');
	//document.getElementById("champ"+Num).innerHTML = "<input type='text' name='' id='text"+Num+"' class='saisie' size='50px' style='border: solid 1px black;font-size:10;' OnFocus='javascript:selectChamp("+Num+");'><input type='hidden' id='selectChamp"+Num+"Name' value=''>"; 
	//document.getElementById("tstitre").className='selectionne';
}

//Remise à zéro de tous les champs
function annuler()
{
	remiseDeb(1);
	remiseDeb(2);
	remiseDeb(3);
	document.getElementById("iframe1").innerHTML = "";
	document.getElementById("iframe2").innerHTML = "";
	document.getElementById("iframe3").innerHTML = "";
}

//fonction qui permet de mettre en context le nombre de champ ouvert à la recherche
function ajaxContext(Num)
{
	var url = "http://philidor.cmbv.fr/jlbweb/jlbWeb?html=cmbv/context&Num="+Num;	
	o_options = new Object();
	o_options = {method: 'get',onSucess:afficherProblemeContext,onComplete:afficherReponseContext,onFailure:afficherProblemeContext,on404:afficherProblemeContext,asynchronous:true,evalScripts:true};
	new Ajax.Request(url,o_options);
	return false;
}

function afficherReponseContext()
{
	
}

function afficherProblemeContext()
{
	
}

//fonction qui permet d'afficher les champs de recherche lors du chargement de la page
function loadShow(Num)
{
	if (Num>0)
	{
		
		for (var u=1; u<Num+1 ;u++)
		{
			ajouterChamp();
		}
	}
}

//Fonction qui permet d'afficher la liste des champs supplémentaires pour tstitres et tsnoms !
function plusChamp(id)
{
	if (Element.visible(id+"Liste")==true)
	{
		Element.hide(id+"Liste");
		document.getElementById(id+"Option").innerHTML = "<img src='http://philidor.cmbv.fr/jlbweb/images/plus.gif' border='0'>";
	}
	else
	{
		Element.show(id+"Liste");
		document.getElementById(id+"Option").innerHTML = "<img src='http://philidor.cmbv.fr/jlbweb/images/minus.gif' border='0'>";
	}
	plusChampShow();
}

//Fonction qui permet de laisser afficher le table si un des champs est sélectionné dans la recherche
function plusChampShow(champ)
{
	var essai=champ;
	if (essai=="inc" |  essai=="oeu" | essai=="incfr" | essai=="rec" | essai=="frag" | essai=="oeucit" | essai=="art" | essai=="titou" | essai=="tip" | essai=="ocn" | essai=="evnt" )
	{
		Element.show("tstitresListe");
		document.getElementById("tstitresOption").innerHTML = "<img src='http://philidor.cmbv.fr/jlbweb/images/minus.gif' border='0'>";
	}
	else if (essai=="aut" | essai=="autt" | essai=="ded" | essai=="cho" | essai=="ed" | essai=="intp" | essai=="nc" | essai=="pcn")
	{
		Element.show("tsnomsListe");
		document.getElementById("tsnomsOption").innerHTML = "<img src='http://philidor.cmbv.fr/jlbweb/images/minus.gif' border='0'>";		
	}
}

//fonction qui permet d'ouvrir la pop-up pour interroger les index
function openIndex(Num,idc,root)
{
	if (idc.length > 0)
	{
		var iframe = "<IFRAME NAME=\"lexique"+Num+"\" ID=\"lexique"+Num+"\" HEIGHT=\"500\" WIDTH=\"400\" SRC=\"http://philidor.cmbv.fr/jlbweb/jlbWeb?html=cmbv/index&idc="+idc+"&root="+root+"&zone=text"+Num+"\" scrollbar=\"no\" style=\"border:0px;background-color:white;\" scrolling=\"no\" marginheight=\"0\" marginwidth=\"0\" frameborder=\"0\">";
		iframe = iframe+"<LAYER NAME=\"lexique"+Num+"\" ID=\"lexique"+Num+"\" HEIGHT=\"500\" WIDTH=\"400\" SRC=\"http://philidor.cmbv.fr/jlbweb/jlbWeb?html=cmbv/index&idc="+idc+"&root="+root+"&zone=text"+Num+"\" scrollbar=\"no\" style=\"border:0px;background-color:white;\" scrolling=\"no\" marginheight=\"0\" marginwidth=\"0\" frameborder=\"0\">";
		iframe = iframe+"</LAYER></IFRAME>";
		document.getElementById("iframe"+Num).innerHTML = iframe;
		
		if (Num == 1){	Element.show('iframe1');Element.hide('iframe2');Element.hide('iframe3');}
		if (Num == 2){	Element.show('iframe2');Element.hide('iframe1');Element.hide('iframe3');}
		if (Num == 3){	Element.show('iframe3');Element.hide('iframe1');Element.hide('iframe2');}
		
		Element.hide("alertIndexVide");
	}
	else
	{
		Element.show("alertIndexVide");
	}	
}


//Pour les recherches "rapides" :
function doSubmitFormDirect(frm)
{
	doMakeQuery(frm.query);
}

function rechercherDirect ()
{
//Ici pour faire des vérifications sur le formulaire
// if (document.formulaire.elements['noField-and' ].value == ""|document.formulaire.elements['noField-and' ].value == " "|document.formulaire.elements['noField-and' ].value == "  "|document.formulaire.elements['noField-and' ].value == "   "|document.formulaire.elements['noField-and' ].value == "    "|document.formulaire.elements['noField-and' ].value == "     "|document.formulaire.elements['noField-and' ].value == "      "|document.formulaire.elements['noField-and' ].value == "       ")
// {
//	Element.show('champ_vide');
// }
// else
// {
	 envoiRechercheDirect();
	 return true;
// }
}

function envoiRechercheDirect()
{
	Element.show("loader");		
	//var query = document.getElementById('query').value;
	//alert(query);
	var url = "http://philidor.cmbv.fr/jlbweb/jlbWeb?html=cmbv/result";	
	o_options = new Object();
//	document.formulaire.submit();
	
	
	 new Ajax.Request(url,{
	 method: 'post',
	 asynchronous: true,
	 encoding: 'UTF-8',
	 parameters: { query : $F('query'),nomBase : $F('nomBase')},
	 onSuccess: afficherReponseDirect,
	 onComplete:afficherReponseDirect,
	 onFailure: afficherProblemeDirect,
	 on404:afficherProblemeDirect
	 }); 
	return false;
}

function afficherReponseDirect(requete) 
{
	if (test > 0)
	{
		var query = document.getElementById('noField-and').value
		test = 0;	
		loader();
		var url = "http://philidor.cmbv.fr/jlbweb/jlbWeb?html=cmbv/ajaxPrCMBV";					
		o_options = new Object();
		o_options = {method: 'post',postBody:'query='+query,encoding: 'UTF-8', onSucess:afficherProblemeDirect2,onComplete:afficherReponseDirect2,onFailure:afficherProblemeDirect2,on404:afficherProblemeDirect2,asynchronous:true,evalScripts:true};
		new Ajax.Updater('contenu',url,o_options);		
		return false;		
		
	}
} 

function afficherProblemeDirect(requete) 
{ 
	document.getElementById('query').value = "Il y a eu un problème";
	Element.hide("loader");		
}

function afficherReponseDirect2(requete) 
{ 
	Element.hide('loader');
	test = 1;
} 
function afficherProblemeDirect2(requete)
{ 
	Element.hide('loader');
	test = 1;
}

