// Set the order criteria
function setOrder(orderCriteria, orderDirection, idForm) {
   	$("#orderCriteria").val(orderCriteria);
    if (orderCriteria == 2) {
	   	$("#orderNameDirection").val(orderDirection);
	}
	else {
	   	$("#orderLocalityDirection").val(orderDirection);
	}	
    $("#"+idForm).submit();
}



// Stat click Client
function sendClickClient(siteCode, idCli, input, numRub, siteCodeRub, idMar, isFoundBySignaletique, isFoundBySituation, isFoundByService, isFoundByContenu, pageIndex, pageCode) {
	if (document.images) {
		(new Image()).src="/web/ajaxStats.do?type=statTypeClient&siteCode="+siteCode+"&idCli="+idCli+"&input="+input+"&numRub="+numRub+"&siteCodeRub="+siteCodeRub+"&idMar="+idMar+"&isFoundBySignaletique="+isFoundBySignaletique+"&isFoundBySituation="+isFoundBySituation+"&isFoundByService="+isFoundByService+"&isFoundByContenu="+isFoundByContenu+"&page="+pageIndex+"&pageCode="+pageCode+"&time="+(new Date()).getTime();
		var now = new Date();
		var exitTime = now.getTime()+25;
	    while (true) {
	        now = new Date();
	        if (now.getTime() > exitTime) return;
	    }
	}
  	return true;
}

// Stat click Client
function saveClientStat(idCli, numPage, position, userInput, idProduit, idRubrique, indicator, situationIndicator, type, pageCode) {
	$.post(
		"/ed/saveClientStat.do", 
		{ 
			idCli:idCli,
			numPage:numPage,
			position:position,
			userInput:userInput,
			idProduit:idProduit,
			idRubrique:idRubrique,
			indicator:indicator,
			situationIndicator:situationIndicator,
			type:type,
			pageCode:pageCode
		}
	);
	
	var now = new Date();
	var exitTime = now.getTime()+25;
    while (true) {
        now = new Date();
        if (now.getTime() > exitTime) return;
    }
	
  	return true;
}

// Stat click Produit
function saveProduitStat(idPro, idCli, numPage, position, userInput, idProduit, idRubrique, indicator, situationIndicator, type, pageCode) {
	$.post(
		"/ed/saveProduitStat.do", 
		{ 
			idPro:idPro,
			idCli:idCli,
			numPage:numPage,
			position:position,
			userInput:userInput,
			idProduit:idProduit,
			idRubrique:idRubrique,
			indicator:indicator,
			situationIndicator:situationIndicator,
			type:type,
			pageCode:pageCode
		}
	);
	
	var now = new Date();
	var exitTime = now.getTime()+25;
    while (true) {
        now = new Date();
        if (now.getTime() > exitTime) return;
    }
	
  	return true;
}

// For display list of activities
function viewActivities(siteCode, searchDiv, zoneDiv, closeDiv, contentDiv, libFindActivity, libFindActivityAfter, inputSearch, imgPath, letter) {
	$.ajax({
		 type: "GET",
	     url: "/web/ajaxSearch.do",
	     data: "siteCode="+siteCode+"&letter="+letter,
	     success: function(resp) {
	     	$("#"+contentDiv).html(resp);
	     	onComplete: $("#"+inputSearch).hide();
	     				$("#"+searchDiv).css("background-image", "url("+imgPath+")");
	     				$("#"+closeDiv).show();
	     				$("#"+zoneDiv).show();
	     				$("#"+libFindActivity).hide();
	     				$("#"+libFindActivityAfter).show();
     				
	   	 }
	});
}

// For close list of activities
function closeActivities(searchDiv, zoneDiv, closeDiv, libFindActivity, libFindActivityAfter, inputSearch, imgPath) {
	$("#"+searchDiv).css("background-image", "url("+imgPath+")");
	$("#"+closeDiv).hide();
	$("#"+zoneDiv).hide();
	$("#"+inputSearch).show();
	$("#"+libFindActivity).show();
	$("#"+libFindActivityAfter).hide();
}

/**
 * Checks if the values entered in the advanced search form are correct
 */
function validAdvancedForm(fields, errorDivId) {
	var tab;
	if (fields.indexOf("|") == -1) {
		if (fields.length > 0) {
			tab = [fields];
		}
		else {
			return true;
		}
	}
	else {
		tab = fields.split("|");
	}
	
	for (var i=0; i<tab.length; i++) {
		if ($("#"+tab[i]).val() != "") {
			$("#"+errorDivId).hide();
			return true;
		}
	}
	$("#"+errorDivId).show();
	return false;
}

function setUpAutocompleteFields(siteCode, language) {
	// Desactivation de la fonctionnalite
	return;
	
	// Recuperation de la valeur du champ codPay si ce dernier existe
	var codPayId = '';
	if (arguments.length > 2) {
		codPayId = arguments[2];
	}
	
	/**
	 * For each element with a "autocomp" class
	 */
	$('.autocomp-text').each(function() {
		// name of the field
		var fieldName = $(this).attr("name");
		// effective width of the field
		var width = $(this).width();			
		
		/**
		 * Sets up the autocompletion options
		 * @see http://docs.jquery.com/Plugins/Autocomplete/
		 */
		$(this).autocomplete("/web/ajaxSearchAdv.do", {
			// The search will begin when the user enters at least 3 characters
			minChars:3,
			// The width of the suggestions list
			width:width,
			// The height of the suggestions list
			scrollHeight:300,
			// No cache is used to store the results
			cacheLength:0,
			// We allow the user to type values that are not in the suggestions list
			mustMatch:false,
			// The suggestion list will not be selected by default
			selectFirst:false,
			// Data type used for the AJAX call
			dataType:"text",
			// List of extra parameters that will be added to the URL of the AJAX call
			extraParams:{
				fieldName:function(){return fieldName;},
				siteCode:function(){return siteCode;},
				language:function(){return language;},
				codPay:function(){
					if (codPayId != '' && $('#'+codPayId).length > 0) {
						return $('#'+codPayId).val();
					}
					return '';
				}
			}
		});
	});
	
	
	/**
	 * For each element with a "autocomp" class
	 */
	$('.autocomp-with-id').each(function(){
		
		// name of the field
		var fieldName = $(this).attr("name");
		// id of the field
		var fieldId = $(this).attr("id");
		// effective width of the field
		var width = $(this).width();
		
		// reset the field id at the beginning of every key action
		synchronizeFieldWithId(fieldId);
		
		/**
		 * Sets up the autocompletion options
		 * @see http://docs.jquery.com/Plugins/Autocomplete/
		 */
		$(this).autocomplete("/web/ajaxSearchAdv.do", {
			// The search will begin when the user enters at least 3 characters
			minChars:3,
			// The width of the suggestions list
			width:width,
			// The height of the suggestions list
			scrollHeight:300,
			// No cache is used to store the reuslts
			cacheLength:0,
			// The only values that will be printed in the input field are the ones in the suggestions list
			mustMatch:false,
			// The suggestion list will not be selected by default
			selectFirst:false,
			// Data type used for the AJAX call
			dataType:"text",
			// Configure the results that will be displayed in the suggestions list
			formatItem:function(data, i, nb) {
				var chaine = new String(data);
				var infos = chaine.split("~");
				return infos[1];
			},
			// Configure the selected result in the input field
			formatResult:function(data, i, nb) {
				var chaine = new String(data);
				var infos = chaine.split("~");
				return infos[1];
			},
			// List of extra parameters that will be added to the URL of the AJAX call
			extraParams:{
				fieldName:function(){return fieldName;},
				siteCode:function(){return siteCode;},
				language:function(){return language;},
				codPay:function(){
					if (codPayId != '' && $('#'+codPayId).length > 0) {
						return $('#'+codPayId).val();
					}
					return '';
				}
			}
		});
		
		/**
		 * Function called when a result is selected in the autocompletion list
		 */
		$(this).result(function(event, data, formatted){
			var chaine = new String(data);
			var infos = chaine.split("~");
			$(this).next().val(infos[0]);
		});
	});
}

function synchronizeFieldWithId(fieldId) {
	$("#"+fieldId).bind($.browser.opera ? "keypress" : "keydown", function() {
		$("#"+fieldId).next().val("");
	});
}

/* Fonction gérant le switch visuel entre l'onglet "Y aller" et "En partir" de la partie mappy sur les annuaires
 * @param int active : index de l'id de la div dont l'image background est active 
 * @param int inactive : index de l'id de la div dont l'image background est inactive 
 * @param string activeContent : le nom de l'id de la zone à afficher
 * @param string hideContent : le nom de l'id de la zone à cacher
 * @param string pathImgTabActive : le path de l'image active à afficher
 * @param string pathImgTabInactive : le path de l'image inactive à afficher
 * @param string boutonActif : le nom de l'id du bloc contenant le bouton calculer à afficher
 */
function switchTabsMappy(active,inactive,activeContent,hideContent, pathImgTabActive, pathImgTabInactive,boutonActif,boutonInactif) {	
	//la zone à cacher
	if ($("#"+hideContent).length) {
		$("#"+hideContent).hide();
	}

	//on affecte au valeur d arrivée (zone "en partir") les infos du client
	$("#paysArriveeP").val($("#paysDepartA").val());
    $("#villeArriveeP").val($("#villeDepartA").val()) ;
    $("#adresseArriveeP").val($("#adresseDepartA").val()) ;
	
	//on affecte au valeur de depart (zone "y aller") les infos du client
	$("#paysDepartA").val($("#paysArriveeP").val());
    $("#villeDepartA").val($("#villeArriveeP").val());
    $("#adresseDepartA").val($("#adresseArriveeP").val());
	
	//remise à vide valeurs renseigné par l utilisateur
    $("#paysArriveeA").val("");
    $("#villeArriveeA").val("") ;
    $("#adresseArriveeA").val("") ;	
	$("#paysDepartP").val("");
    $("#villeDepartP").val("");
    $("#adresseDepartP").val("");	
	
	//affectation de l image de fond
	$("#ongletMappy"+active).css("background-image", "url("+pathImgTabActive+")");
	$("#ongletMappy"+inactive).css("background-image", "url("+pathImgTabInactive+")");
	$("#"+activeContent).show();
	$("#"+boutonActif).show();
	$("#"+boutonInactif).hide();
}

/*
 * 
Fonction pour afficher/cacher les thèmes/rubriques du "sommaire menu" de la recherche par activité editus.lu
*/
function menuSearchActivity(textSee,textHide)
{
	 var span = $(".listFull");
	 for(i=0;i<span.length;i++) {
		 if ($(span[i]).is(":visible")){	
			 	$(span[i]).hide();
				$('#suiteDeroule').text(textSee);
		 }else{
			 	$(span[i]).show();
				$('#suiteDeroule').text(textHide);
		 	  }
	 }
}

/*
 * 
Fonction pour afficher/cacher le message de non résultat de la recherche par activité editus.lu
*/
function toggleSearchActivity(anId)
{
	var id = $("#"+anId);
	if ($(id).is(":visible"))
	{
		// Contenu visible, le cacher
		$(id).hide();
	}else{
		// Contenu visible, le cacher
		$(id).show();
	}
}

/*
 * 
Fonction pour afficher/cacher un élément
*/
function showHide(anId)
{
	var id = $("#"+anId);
	if ($(id).is(":visible"))
	{
		// Contenu caché, le rendre visible
		$(id).hide();
	}else{
		// Contenu visible, le cacher
		$(id).show();
	}
}
 

