
/** Ajoute des éléments au select des departements en fonction de la région sélectionnée **/
function findDepts(selectValue) {
    jQuery.ajax({
        type: 'POST',
	url: '/json-depts',
	data: {'id_region': selectValue},
	success: function(json) {
            depts = eval('(' + json + ')');
            var allOptions = jQuery('#departement');
            allOptions.empty();
		
            if(depts.length == 0) {
                jQuery('#departement').attr('disabled', 'disabled');
		return;
            }

            var anOptionBase = '<option value="0" selected:"selected">:: TOUS LES DEPARTEMENTS ::</option>';
            jQuery('#departement').append(anOptionBase);
            for(var key in depts) {
                var aDept = depts[key];
                var temp = aDept.split("#");
		var anOption = '<option value="'+temp[0]+'">'+temp[1]+'</option>';
                jQuery('#departement').append(anOption);
            }
            jQuery('#departement').removeAttr('disabled');
        },
        dataType: 'JSON'
    });
}


function findCategories(checkboxValue){
    jQuery.ajax({
        type: 'POST',
	url: '/json-category',
	data: {'type_search': checkboxValue},
	success: function(json) {
            categories = eval('(' + json + ')');
            var allOptions = jQuery('#categorie');
            allOptions.empty();

            var anOptionBase = '<option value="0" selected:"selected">:: TOUS LES SECTEURS ::</option>';
            jQuery('#categorie').append(anOptionBase);

            for(var key in categories) {
                var aCategory = categories[key];
                var temp = aCategory.split("#");
		var anOption = '<option value="'+temp[0]+'">'+temp[1]+'</option>';
                jQuery('#categorie').append(anOption);
            }
        },
        dataType: 'JSON'
    });

    var allOptions = jQuery('#secteur');
    allOptions.empty();
    jQuery('#secteur').attr('disabled', 'disabled');

    if(checkboxValue=='E'){
        jQuery('#region').attr('disabled', 'disabled');
        jQuery('#departement').attr('disabled', 'disabled');
        jQuery('#secteur').attr('disabled', 'disabled');
    }else {
        jQuery('#region').removeAttr('disabled');
    }
}


function findSectors(selectValue) {
    jQuery.ajax({
        type: 'POST',
	url: '/json-sector',
	data: {'id_category': selectValue},
	success: function(json) {
            sectors = eval('(' + json + ')');
            var allOptions = jQuery('#secteur');
            allOptions.empty();

            if(sectors.length == 0) {
                jQuery('#secteur').attr('disabled', 'disabled');
		return;
            }

            var anOptionBase = '<option value="0" selected:"selected">:: TOUTES LES ACTIVITES ::</option>';
            jQuery('#secteur').append(anOptionBase);

            for(var key in sectors) {
                var aSector = sectors[key];
                var temp = aSector.split("#");
                if(temp[0].substr(0, 2) == "**"){
                    continue;
                }
		var anOption = '<option value="'+temp[0]+'">'+temp[1]+'</option>';
                jQuery('#secteur').append(anOption);
            }
            var reg = document.getElementById("type_c").checked;
            if(reg){
                jQuery('#secteur').removeAttr('disabled');
            }
        },
        dataType: 'JSON'
    });
}


function switch_on(el){
    el.value = '';
    if(el.name == 'pwd'){
        el.type = 'password';
    }
}

function switch_off(el){
    if(el.value == '') {
        if(el.name == 'pwd'){
            el.type = 'text';
            el.value = 'Mot de passe';
        }else {
            el.value = 'Votre email';
        }
    }
    
}

// Bloque le formulaire de recherche
jQuery(document).ready(function(){
	
    jQuery('form#recherche').submit(function(){
		var somme = 0;
		
		if(jQuery('#region').length == 1 && jQuery('#region').val() != 0) {
			somme++;
		}
		if(jQuery('#categorie').val() != 0) {
			somme++;
		}
		if(jQuery('#reference').val() != '') {
			somme++;
		}

		if(somme <= 0) {
			alert('Veuillez affiner votre recherche');
			return false;
		}
		return true;
	});
});

