/*
 * Comment utiliser cette fonctionnalité ?
 *
 * Dans votre page web vous avez besoin d'avoir une boite div avec :
 *	-> soit un id = "formDiv"
 *	-> soit un id = votre valeur et le préciser dans l'appel de la fonction question()
 *
 * l'appel de la fonction question dans son maximum (mais sans les détails de la fonction requete()) :
 *	onclick="question(
 *		'objet',					<- type     : obligatoire  : objet, documentation, personne, lieu
 *		requete(on vera plus tard),	<- requete  : obligatoire  : appel la fonction qui retourne une chaine de la question
 *		'+NUMINVENTAIRE',			<- trie     : optionnel	   : par defaut +NUMINVENTAIRE (+ croissant,- décroissant)
 *		1,							<- operator : optionnel	   : par defaut 1 (1=ET, ??=OU)
 *		2,							<- viewtype : optionnel	   : par defaut 2 (2=Mosaïque, 4=Tableau)
 *		64,							<- nombre   : optionnel	   : par defaut 64 le maximum (valeur possible : 8,16,32,64)
 *		'formDiv'					<- nomdiv   : optionnel	   : le nom de votre div si vous le souhaité (besoin d'une div avec se nom la dans la page (ou alors définnissez votre propre nom d'id))
 *	});"
 *
 *l'appel de la fonction requete :
 * requete('[~]','NUMINVENTAIRE','002.1.78*','CODEMUSEE','M0820')
 *
 *	le premier argument correspond à l'opérateur ET ou OU puis se suivent par alternance les noms de champs et leurs valeurs associées
 */

function MaQuestion(pIdDiv,pType){
	this.idDiv = pIdDiv;
	this.query = "";
	
	this.isObjet = function(){
		this.groupid	= "286";
		this.operator	= "1";
		this.tableid	= "32";
		this.viewtype	= "2";
		this.pagesize	= "64";
		this.sortfield	= "+NUMINVENTAIRE";
		this.formgroupid= "38";
		this.href		= "form_objet.asp";
	}
	
	this.isDocum = function(){
		this.groupid	= "286";
		this.operator	= "1";
		this.tableid	= "35";
		this.viewtype	= "2";
		this.pagesize	= "64";
		this.sortfield	= "+IDENTIFIANT";
		this.formgroupid= "41";
		this.href		= "form_documentation.asp";
	}
	
	this.isPers = function(){
		this.groupid	= "286";
		this.operator	= "1";
		this.tableid	= "33";
		this.viewtype	= "2";
		this.pagesize	= "64";
		this.sortfield	= "+ETATCIVIL";
		this.formgroupid= "42";
		this.href		= "form_perso.asp";
	}

	this.isLieu = function(){
		this.groupid	= "286";
		this.operator	= "1";
		this.tableid	= "35";
		this.viewtype	= "2";
		this.pagesize	= "64";
		this.sortfield	= "+IDENTIFIANT";
		this.formgroupid= "41";
		this.href		= "form_lieu.asp";
	}
	
	this.whatTypeIsIt = function(pType){
		switch(pType){
			case 'objet' 			: this.isObjet();	break;
			case 'Objet' 			: this.isObjet();	break;
			case 'OBJET' 			: this.isObjet();	break;
			case 'documentation'	: this.isDocum();	break;
			case 'Documentation'	: this.isDocum();	break;
			case 'DOCUMENTATION'	: this.isDocum();	break;
			case 'personne'			: this.isPers();	break;
			case 'Personne'			: this.isPers();	break;
			case 'PERSONNE'			: this.isPers();	break;
			case 'lieu'				: this.isLieu();	break;
			case 'Lieu'				: this.isLieu();	break;
			case 'LIEU'				: this.isLieu();	break;
		}
	}

	this.createForm = function(){
		var formText = '<form name="myFormAWP" method="post" action="http://www.alienor.org/alienorweb/public/reponse.asp" target="_blank">'
			+ '<input type="hidden" name="__groupid" value="'+this.groupid+'"/>'
			+ '<input type="hidden" name="__operator" value="'+this.operator+'"/>'
			+ '<input type="hidden" name="__tableid" value="'+this.tableid+'"/>'
			+ '<input type="hidden" name="__viewtype" value="'+this.viewtype+'"/>'
			+ '<input type="hidden" name="__query" value="'+this.query+'"/>'
			+ '<input type="hidden" name="__pagesize" value="'+this.pagesize+'"/>'
			+ '<input type="hidden" name="__sortfield" value="'+this.sortfield+'"/>'
			+ '<input type="hidden" name="__FormGroupId" value="'+this.formgroupid+'"/>'
			+ '<input type="hidden" name="__href" value="'+this.href+'" />'
			+ '</form>';
		return formText;
	}
	
	this.traitement = function(nom,valeur){
		var txt = "";
		switch(nom){
			case 'operator':
				if(valeur==1){
					txt = "[~]";
				}
				//la même condition pour le OU est à faire
			break;
		}
		return txt;
	}

	this.submit = function(){
		document.getElementById(this.idDiv).innerHTML = this.createForm();
		document.myFormAWP.submit();
	}
}


function question(pType,pQuery,pSortField,pOperator,pViewType,pPageSize,pIdDiv){
	if(pIdDiv==null || typeof(pIdDiv)=="undefined"){
		pIdDiv = "formDiv";
	}
	if(document.getElementById(pIdDiv)){
		if(pType!=null & typeof(pType)!="undefined"){
			if(pQuery!=null & typeof(pQuery)!="undefined"){
				var obj = new MaQuestion(pIdDiv,pType);
				obj.whatTypeIsIt(pType);
				if(pOperator!=null & typeof(pOperator)!="undefined"){
					obj.operator = pOperator;
				}
				obj.query = pQuery;
				if(pSortField!=null & typeof(pSortField)!="undefined"){
					obj.sortfield = pSortField;
				}
				if(pViewType!=null & typeof(pViewType)!="undefined"){
					obj.viewtype = pViewType;
				}
				if(pPageSize!=null & typeof(pPageSize)!="undefined"){
					obj.pagesize = pPageSize;
				}
				obj.submit();
			}else{
				//la variable pQuery est vide
				alert('Veuillez définir la question.');
			}
		}else{
			//la variable pType est vide
			alert('Veuillez définir le type de recherche (objet, documentation...).');
		}
	}else{
		//la variable pIdDiv référence l'id d'une balise qui n'existe pas
		alert('La div "'+pIdDiv+'" n\'existe pas');
	}
}

function requete(){
	var op = requete.arguments[0];
	var txt ="";
	for(i=1;i<requete.arguments.length;i++){
		txt += requete.arguments[i]
		if(i%2==0){
			if(i+1!=requete.arguments.length){
				txt += op;
			}
		}else{
			txt += "==";
		}
	}
	return txt;
}
