// JavaScript Document
var boolComboBox = false;
var nlZIndex = 1000;
$(document).ready(function(){

//$(".ConteneurCombo").children("input").val("");
$(".ConteneurComboRadio").children("input#distance").val("5");
$(".ConteneurComboRadio").children("input#resultats").val("10");

$(".ConteneurCombo").children('.selectTitle').click(function(){
$(".selectTitle").next("div").hide();

$(".ConteneurComboRadio").css("position","static");
$(".ConteneurCombo").css("position","static");

if (!boolComboBox) {	
	nlZIndex = nlZIndex+1
	$(this).next("div").css("zIndex",nlZIndex);
	$(this).parent('.ConteneurCombo').css("position","relative")

	$(this).next("div").show();	

	boolComboBox = true;
}
else{
	$(this).next("div").hide();
	boolComboBox = false;
}

return false;
});



$(".ConteneurComboRadio").children('.selectTitle').click(function(){
$(".selectTitle").next("div").hide();
$(".ConteneurComboRadio").css("position","inherit");
$(".ConteneurCombo").css("position","inherit");

if (!boolComboBox) {	
	nlZIndex = nlZIndex+1
	$(this).next("div").css("zIndex",nlZIndex);
	$(this).parent('.ConteneurComboRadio').css("position","relative")

	$(this).next("div").show();	

	boolComboBox = true;
}
else{
	$(this).next("div").hide();
	boolComboBox = false;
}

return false;
});



$('.selectTitle').hover(function(){
	
	$(this).children(".selectTitleRight").attr("class","selectTitleRight-o");
},
function()
{
	$(this).children(".selectTitleRight-o").attr("class","selectTitleRight");
}
);




$(".ConteneurCombo").children(".comboListe").children('.comboChoix').click(Selectionner);


$(".ConteneurComboRadio").children(".comboListeRadio").children('.comboChoix').click(function(){
	comboChoix = $(this).attr("rel");
	texteTitre = $(this).children("a").attr("title");
		
	if($(this).parent('.comboListeRadio').next("input").val() !=""){
		$(this).parent(".comboListeRadio").children(".comboChoix").children("a").attr("class","desactive")				
		$(this).parent('.comboListeRadio').next("input").val("");
		
		valueCombo = $(this).attr("rel");
		titreCombo = $(this).children("a").attr("title");
		$(this).children("a").attr("class","active");			
		$(this).parent('.comboListeRadio').next("input").val(valueCombo);
	}
	else{
		$(this).parent(".comboListeRadio").children(".comboChoix").children("a").attr("class","desactive")	
		valueCombo = $(this).attr("rel");
		titreCombo = $(this).children("a").attr("title");
		$(this).children("a").attr("class","active");			
		$(this).parent('.comboListeRadio').next("input").val(valueCombo);
	}
	
	valeurPremierElement = $(this).parent('.comboListeRadio').children("div:first").children("a").attr("title");
	
	if($(this).parent('.comboListeRadio').next("input").val() != $(this).parent('.comboListeRadio').children("div:first").attr("rel")){
		$(this).parent("div.comboListeRadio").prev("div").children(".selectTitleLeft").children(".selectTitleRight").children(".titreComboBox").replaceWith("<div class='titreComboBox'>"+ titreCombo +"</div>")
	}
	else{
		$(this).parent("div.comboListeRadio").prev("div").children(".selectTitleLeft").children(".selectTitleRight").children(".titreComboBox").replaceWith("<div class='titreComboBox'>"+valeurPremierElement+"</div>")
	}
	if (!boolComboBox){		
		boolComboBox = true;		
	}
	return false;		
});



$(".comboListe").hoverIntent(
										
    	function() { $(this).show(); },
    	function() { $(this).hide(); boolComboBox = false;}	  	
 );
 
 $(".comboListeRadio").hoverIntent(
										
    	function() { $(this).show(); },
    	function() { $(this).hide(); boolComboBox = false;}	  	
 );
 
 



$('#plusInfo').children("a").click(function() 
{
 $("#DIV_MOVE").css("margin-top","120px");
  $('#suiteForm').slideToggle(400,
   function() {
	  if($("#suiteForm").css("display") == "none")
	  {
		  $("#DIV_MOVE").css("margin-top","0px");
	  }
  });
	   if($(this).parent("div").attr("id")=="plusInfo-o")
	   {
			$(this).parent("div").attr("id","plusInfo");
	   }
	   else
	   {
		   $(this).parent("div").attr("id","plusInfo-o");
	   }
  
 
});


$("#btnEnvoyer").hover(function(){
	$(this).attr("id","btnEnvoyer-o");
}, function()
{
$(this).attr("id","btnEnvoyer");	
}

);


});


function checkTexteBlur(spValue, spTexte, spDiv)
{
	if(spValue == "")
	{
		document.getElementById(spDiv).value =spTexte;	
	}	
}

function checkTexteFocus(spValue,spTexte, spDiv)
{
	if(spValue == spTexte)
	{
		document.getElementById(spDiv).value ="";	
	}	
}


function Selectionner(){
		nbrModifier =1;
		comboChoix = $(this).attr("rel");
		texteTitre = $(this).parent("div.comboListe").prev("div").children(".selectTitleLeft").children(".selectTitleRight").children(".titreComboBox").html();
		tabTitre = texteTitre.split("<");
		nlCompteur =0;
		numeroSelectionner = 0;
		if(tabTitre.length >0)
		{
			texteTitre = tabTitre[0];
		}
		
		if($(this).parent('.comboListe').next("input").val() !="")
		{
			valeurExistante = 	$(this).parent('.comboListe').next("input").val();
			tabValeurExistante = valeurExistante.split(";");
			nbrValeurExistante = tabValeurExistante.length;
			valeurModifier = $(this).parent('.comboListe').next("input").val();			
			newValeur = "";
			
			for(i=0; i<nbrValeurExistante;i++)
			{			
				if(tabValeurExistante[i] == comboChoix )
				{
					valeurModifier = valeurExistante.replace(comboChoix+";","")
					$(this).children("a").attr("class","desactive");
					nbrModifier = nbrValeurExistante-2;				
					texteModifier = nbrModifier + " s&eacute;lection(s)"
				
				}
									
			}
			
			if(valeurModifier == valeurExistante)
			{
			
				valeurModifier = valeurExistante + comboChoix+";"
				$(this).children("a").attr("class","active");
				
				nbrModifier = nbrValeurExistante;
				texteModifier = nbrModifier + " s&eacute;lection(s)"
				
			}			
			
		
			
			if(nbrModifier == 0)
			{
				texteModifier = "";	
				
				if($("#Region").val()!="")
				{
					$("#comboListeVille").children("div").children("a").attr("class","desactive");
					$("#Ville").val("");
					$("#titreComboBoxVille").children(".selectTitle").children(".selectTitleLeft").children(".selectTitleRight").children(".titreComboBox").replaceWith("<div class='titreComboBox'>MRC</div>")
					$("#comboListeVille").children(".comboChoix").css("display", "block");
				}
			}
			
			valueCombo = valeurModifier;
			nbrModifier = nbrModifier.toString();
			
			
		}
		else
		{
			textValue = $(this).parent('.comboListe').next("input").val();
			valueCombo = $(this).attr("rel") +";"+ textValue
			$(this).children("a").attr("class","active");			
			nbrModifier = nbrModifier.toString();
			texteModifier = "1 s&eacute;lection(s)";
			//$(this).parent("div.comboListe").prev("div").children(".selectTitleLeft").children(".selectTitleRight").children(".titreComboBox").replaceWith("<div class='titreComboBox'>"+texteTitre+"<span class='petitTexte'>1 s&eacute;lection(s)</span></div>")
		}
		$(this).parent("div.comboListe").prev("div").children(".selectTitleLeft").children(".selectTitleRight").children(".titreComboBox").replaceWith("<div class='titreComboBox'>"+texteTitre+"<span class='petitTexte'>" + texteModifier + "</span></div>")
		$(this).parent('.comboListe').next("input").val(valueCombo);
		
		alRegions = Elem("Region").value.split(";");
		if ($(this).parent('.comboListe').attr("id") == "comboListeRegion"){
			$("#comboListeVille").children(".comboChoix").css("display", "none");
			$("#comboListeVille").children(".comboChoix").each(  
				function(){ 
					for (i=0;i<alRegions.length-1;i++){
						if ($(this).attr("name") == "Ville_" + alRegions[i]){
							$(this).css("display", "block");
						}
					}
			  	} 
			); 
			if (alRegions.length-1 <= 0){
				$("#comboListeVille").children(".comboChoix").css("display", "block");
			}
		}

		nlVille = $(this).attr("rel");
		
		if ($(this).parent('.comboListe').attr("id") == "comboListeRegion" && $(this).children("a").attr("class") == "desactive"){			
			$("#comboListeVille").children(".comboChoix").each(  
				function(){ 
					
					if ($(this).attr("name") == "Ville_" + nlVille){
						if ($(this).children("a").attr("class") == "active"){
							nlCompteur++;
							$(this).children("a").attr("class","desactive")
							attrDelete = $(this).attr("rel")+";"							
							valComBox = $("#Ville").val();							
							newVal = valComBox.replace(attrDelete,"");
							$("#Ville").val(newVal)
							
							nomComBox = $(this).parent(".comboListe").prev(".selectTitle").children(".selectTitleLeft").children(".selectTitleRight").children(".titreComboBox").html();
							
							nomComBoxTab = nomComBox.split('<span class="petitTexte">');
							numSelec =  nomComBoxTab[1].split(" ");							
							titreComBox = nomComBoxTab[0]
							numeroSelectionner = numSelec[0];
							
						}
						else
						{
							
						}
					}
				} 
			);
		
			// Mise à jour du texte du combobox (nombre de sélections)
			newChiffre = numeroSelectionner-nlCompteur;		
			if(newChiffre == 0)
			{
				
				$("#comboListeVille").prev(".selectTitle").children(".selectTitleLeft").children(".selectTitleRight").children(".titreComboBox").replaceWith("<div class='titreComboBox'>"+titreComBox+"</div>")	
			}
			else
			{
				$("#comboListeVille").prev(".selectTitle").children(".selectTitleLeft").children(".selectTitleRight").children(".titreComboBox").replaceWith("<div class='titreComboBox'>"+titreComBox+"<span class=\"petitTexte\">"+newChiffre+" s&eacute;lection(s)</span></div>")
			} 	
			
			
		}
		
		if (!boolComboBox) {		
			boolComboBox = true;		
	}
	return false;	
}

