


$(function(){
	$("select#Region").change(function(){
		$.getJSON("/inc/inscription/qryMunListAjax.cfm",{regionid: $(this).val()}, function(j){
			var options = '';
			for (var i = 0; i < j.length; i++) {
				options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
			}
			$("#Ville").html(options);
			if (l == 'fr'){
				$('#Ville option:first').html('Choisir');
			}
			else{
				$('#Ville option:first').html('Choose');
			}
			$('#Ville option:first').attr('selected', 'selected');
		})
	})			
})

if (l == 'fr'){
	var pMaxArray = [{optionValue:50000, optionDisplay:'50 000 $'},
					{optionValue:100000, optionDisplay:'100 000 $'},
					{optionValue:150000, optionDisplay:'150 000 $'},
					{optionValue:200000, optionDisplay:'200 000 $'},
					{optionValue:250000, optionDisplay:'250 000 $'},
					{optionValue:300000, optionDisplay:'300 000 $'},
					{optionValue:400000, optionDisplay:'400 000 $'},
					{optionValue:500000, optionDisplay:'500 000 $'},
					{optionValue:600000, optionDisplay:'600 000 $'},
					{optionValue:700000, optionDisplay:'700 000 $'},
					{optionValue:800000, optionDisplay:'800 000 $'},
					{optionValue:900000, optionDisplay:'900 000 $'},
					{optionValue:1000000, optionDisplay:'1 000 000 $'}];
}
else {
	var pMaxArray = [{optionValue:50000, optionDisplay:'$50 000'},
					{optionValue:100000, optionDisplay:'$100 000'},
					{optionValue:150000, optionDisplay:'$150 000'},
					{optionValue:200000, optionDisplay:'$200 000'},
					{optionValue:250000, optionDisplay:'$250 000'},
					{optionValue:300000, optionDisplay:'$300 000'},
					{optionValue:400000, optionDisplay:'$400 000'},
					{optionValue:500000, optionDisplay:'$500 000'},
					{optionValue:600000, optionDisplay:'$600 000'},
					{optionValue:700000, optionDisplay:'$700 000'},
					{optionValue:800000, optionDisplay:'$800 000'},
					{optionValue:900000, optionDisplay:'$900 000'},
					{optionValue:1000000, optionDisplay:'$1 000 000'}];
}

var fauxDropDownCourrant = '';
var listeServicesSelectionnes = 2;

	function chgPrix(minMax, valeur){
		if(minMax == 'min'){
			
			var vieilleValeur = document.getElementById('prixMax').options[document.getElementById('prixMax').selectedIndex].value;

			var nbreElements = document.getElementById('prixMax').options.length;
			for (var i = 0; i < nbreElements; i++){
					document.getElementById('prixMax').options[0] = null;
			}
			var nbreOptions = 0;
			for (var i = 0; i < pMaxArray.length; i++){
				if (pMaxArray[i]['optionValue'] > valeur){
					document.getElementById('prixMax').options[nbreOptions] = new Option(pMaxArray[i]['optionDisplay'],pMaxArray[i]['optionValue']);
					if (pMaxArray[i]['optionValue'] == vieilleValeur){
						document.getElementById('prixMax').options[nbreOptions].selected = true;
					}
					nbreOptions++;
				}
			}
				document.getElementById('prixMax').options[nbreOptions] = new Option((lang == 'fr') ? 'Illimite' : 'No limit','');
				if ('' == vieilleValeur){
					document.getElementById('prixMax').options[nbreOptions].selected = true;
				}
		}
		else if(minMax == 'max'){
			
			var vieilleValeur = document.getElementById('prixMin').options[document.getElementById('prixMin').selectedIndex].value;
			var nbreElements = document.getElementById('prixMin').options.length;
			for (var i = 0; i < nbreElements; i++){
					document.getElementById('prixMin').options[0] = null;
			}
			var nbreOptions = 0;
			document.getElementById('prixMin').options[nbreOptions] = new Option('0$','0');
			nbreOptions++;
			for (var i = 0; i < pMaxArray.length; i++){
				if (pMaxArray[i]['optionValue'] < valeur || valeur == ''){
					document.getElementById('prixMin').options[nbreOptions] = new Option(pMaxArray[i]['optionDisplay'],pMaxArray[i]['optionValue']);
					if (pMaxArray[i]['optionValue'] == vieilleValeur){
						document.getElementById('prixMin').options[nbreOptions].selected = true;
					}
					nbreOptions++;
				}
			}
				
		}
	}
	


	
	function montrerFauxDropDown(type){
		if (typeof document.body.style.maxHeight === "undefined") {//if IE 6
			$("body","html").css({height: "100%", width: "100%"});
			$("html").css("overflow","hidden");
		}
			
		if (document.getElementById("X_HideSelect") === null) {//iframe to hide select elements in ie6
			$("body").append('<iframe id="X_HideSelect"></iframe><div id="overlay2"></div>');
		}
		
		var tousSelectionne = true;
		
		for (var i in servicesArray){
			if (servicesArray[i].selectionne == false && servicesArray[i].type == type){
				tousSelectionne = false;
			}
		}
		
		if (document.getElementById("servicesCatDiv" + type) === null) {//iframe to hide select elements in ie6
			var contenu = '<div id="servicesCatDiv' + type + '" style="position:absolute;padding:2px;width:250px;text-align:left;background-color:white;border-style:solid;border-width:1px;border-color:black;display:none;z-index:100">';
			contenu += '<input onclick="toggleServicesSelect(' + type + ')" id="servicesCatTous' + type + '" name="servicesCatTous' + i + '" type="checkbox" value=""' + ((tousSelectionne == true) ? 'checked="checked"' : '') + ' /> <label for="servicesCatTous' + type + '">' + ((lang == 'fr') ? 'Tous' : 'All') + '</label><br />';
			for (var i in servicesArray){
				if (servicesArray[i].type == type){
				contenu += '<input onclick="changeServiceSelection(' + i + ',' + type + ')" id="servicesCat' + i + '" name="GetServicesCat" type="checkbox" value="' + i + '"' + ((servicesArray[i].selectionne == true) ? ' checked="checked"' : '') + ' /> <label for="servicesCat' + i + '">' + servicesArray[i].nom + '</label><br />';
				}
			}

			contenu += '</div>';
			$("body").append(contenu);
		}
		fauxDropDownCourrant = type;
		$("#overlay2").mousedown(cacherFauxDropDown);
		//$("#servicesCloseBtn" + i).mousedown(cacherFauxDropDown);
						
		$("#overlay2").show();
		
		positionmenu('servicesCatDiv' + type,'servicesCatSelect' + type, 250, 165);
		$('#servicesCatDiv' + type).show();
		

		document.getElementById('servicesCatSelect' + type).style.visibility='hidden';
		document.getElementById('servicesCatSelect' + type).style.display='none';

		setTimeout(function() {document.getElementById('servicesCatDiv' + type).blur();},500) 
		return false;
	}

	
	function cacherFauxDropDown(){
		type = fauxDropDownCourrant;
		if (!document.getElementById('servicesCatSelect' + type).options[0]){
			document.getElementById('servicesCatSelect' + type).options[0] = new Option('','');
		}
	 	document.getElementById('servicesCatSelect' + type).options[0].text = '';
	 	document.getElementById('services').value = '';
	 	var nbreSelectedType = 0;
	 	var nbreSelected = 0;
	 	var tous = true;
		for (var i in servicesArray){
			
			if (servicesArray[i].selectionne == true){
				if (nbreSelected > 0){document.getElementById('services').value += ',';}
				document.getElementById('services').value +=  i;
				nbreSelected++;
				if (servicesArray[i].type == type) {
					if (nbreSelectedType > 0){document.getElementById('servicesCatSelect' + type).options[0].text += ',';}
					document.getElementById('servicesCatSelect' + type).options[0].text += servicesArray[i].nom;
					nbreSelectedType++;
				}
			}
			else if (servicesArray[i].type == type){
				tous = false;
			}
			//on update le champ caché qui contient la liste des services/commerces sélectionnés
		}

		if (tous == true){
			document.getElementById('servicesCatSelect' + type).options[0].text = (lang == 'fr') ? 'Tous' : 'All';
		}
		if (nbreSelectedType == 0) {
			if (lang == 'fr'){
				document.getElementById('servicesCatSelect' + type).options[0].text = 'Choisir';
			}
			else{
				document.getElementById('servicesCatSelect' + type).options[0].text = 'Choose';
			}
		}
		
		if (typeof document.body.style.maxHeight == "undefined") {//if IE 6
			$("body","html").css({height: "auto", width: "auto"});
			$("html").css("overflow","");
		}
		
		$('#overlay2,#X_HideSelect').remove();
		document.getElementById('servicesCatSelect' + type).style.visibility='visible';
		document.getElementById('servicesCatSelect' + type).style.display='inline';
		document.getElementById('servicesCatDiv' + type).style.display='none';
		//document.getElementById('X_HideSelect').style.display='none';
		//document.getElementById('overlay2').style.display='none';
		fauxDropDownCourrant = '';
		//$('#focumlaireRechercheCommerciale').submit();
	}
	
	
	function changeServiceSelection(id, type){
		var statut = document.getElementById('servicesCat' + id).checked;
		servicesArray[id].selectionne = statut;
		if (statut == false){
			document.getElementById('servicesCatTous' + type).checked = false;
		}
	}
	
	function toggleServicesSelect(type){
		estSelectionne = document.getElementById('servicesCatTous' + type).checked;
		for (var i in servicesArray){
			if (servicesArray[i].type == type){
				document.getElementById('servicesCat' + i).checked = estSelectionne;
				servicesArray[i].selectionne = estSelectionne;
			}
		}
	}
