
function is_dept_num(str) {return str.match(/^\d{2,3}$/);}
function is_cp(str) {return str.match(/^\d{5}$/);}
function is_word(str) {return str.match(/^\D+$/);}




function showCoupDeCoeur() {
    var container = $("#lesCoeur");

        container.css("width", 106 * container.find(".annonceCoeur").size());
        $("#Log").show();
    }


function buildSearchUrlBien(form, prefix) {

    var input_min, input_max, budget_min, budget_max;
    var url = '/trouver_logement/' + prefix;
    //if (form.type_transaction.value == 'location') {
    if ($(form).find('input[name=type_transaction]:checked').val()=='location') {
        url += 'location';
        budget_min = form.budget_min[1];
        budget_max = form.budget_max[1];
    }
    else {
        url += 'achat';
        budget_min = form.budget_min[0];
        budget_max = form.budget_max[0];
    }
    if (!budget_min && !budget_max) {
        budget_min = form.budget_min;
        budget_max = form.budget_max;
    }

    var type_bien = $(form).find("[name='type_bien']:checked");
    if (type_bien.length) {
        url += '-' + $.map(type_bien, function(input) {
                return input.value;
                }).join('-');
    }
    url = url.replace('autre', 'terrain-parking-immeuble-autre');

    var type_agebien = $(form).find("[name='type_agebien']:checked");
    url += $.map(type_agebien, function(input) {
            return '-' + input.value;
            }).join('');
  url += '/';

    var localisations = trimTab(form.localisation.value.replace(/ /g, '+').toUpperCase().split(','));
    var locVilles = [];
    var locCp = [];
    var locDept = [];
    for (i=0; i < localisations.length; i++){
        if (is_dept_num(localisations[i])){
            locDept.push(localisations[i] + "_" + DEPARTEMENTS[localisations[i]].replace(/ /g, '_'));
        }else if (is_cp(localisations[i])){
            locCp.push(localisations[i]);
        }else if (is_word(localisations[i])){
            locVilles.push(localisations[i]);
        }
    }
    if (locVilles.length) {
        url += 'v-' + locVilles.join('-') + '/';
    }
    if (locCp.length) {
        url += 'cp-' + locCp.join('-') + '/';
    }
    if (locDept.length) {
        url += 'd-' + locDept.join('-') + '/';
    }
//    if (form.alentours.checked) {
//        url += 'alentours/';
//    }

    input_min = Number(form.surface_min.value);
    input_max = Number(form.surface_max.value);
    if (input_max == '0'){
        url += 's-' + input_min + '-/';
    } else if (input_min || input_max) {
        url += 's-' + input_min + '-' + input_max + '/';
    }
    if ($('input#maisonAL').attr('checked')) {
      input_min = Number(form.surface_terrain_min.value);
      input_max = Number(form.surface_terrain_max.value);
    } else {
      input_min = 0;
      input_max = 0;
    }
    if (input_max == '0'){
        url += 'st-' + input_min + '-/';
    } else if (input_min || input_max) {
        url += 'st-' + input_min + '-' + input_max + '/';
    }
    input_min = Number(budget_min.value);
    input_max = Number(budget_max.value);
    if (input_max == '0'){
        url += 'b-' + input_min + '-/';
    } else if (input_min || input_max) {
        url += 'b-' + input_min + '-' + input_max + '/';
    }
    var pieces = $.map(
            $(form).find("[name='pieces']:checked"),
            function(input) {
            return input.value;
            }).join('-');
    if (pieces) {
        url += 'p-' + pieces + '/';
    }

    return url.toLowerCase();
}

function searchBiens(url){
    form = document.forms['recherche' + ACHAT_LOCATION];
    if (!form) { form = document.forms.recherche;}

    if (!url){
      url = buildSearchUrlBien(form, '');
      if (APPROCHANTS) {
          url += 'approchant/';
      }
      url += 'page-1/';
    }
    document.location.href = url;
}

function compterEtLancerBien(){
    form = document.forms['recherche' + ACHAT_LOCATION];
    if (!form) { form = document.forms.recherche;}

    url = buildSearchUrlBien(form, 'count/');
    $.getJSON(url, function(count){
        if (count[0] > 0) {
          if (count.length > 2 && count[2] !== ""){
            searchBiens(count[2]);
          } else {
            searchBiens(null);
          }
        } else {
        $("a#lien_tb_no_result").click();
        }
    });
}

function switch_achat_location(achat_location) {
  if (achat_location == 'achat') {
    $('[class=slider-budget-achat]').show();
    $('[class=slider-budget-location]').hide();

  } else if (achat_location == 'location') {
    $('[class=slider-budget-achat]').hide();
    $('[class=slider-budget-location]').show();
  }
//  $('[class^=slider-budget-]').toggle();
  $('#lien_liste_biens').attr('href', '/trouver_logement/' + achat_location + '/page-1/');
}

Array.prototype.unique = function () {
	var r = [];
	o:for(var i = 0, n = this.length; i < n; i++)
	{
		for(var x = 0, y = r.length; x < y; x++)
		{
			if(r[x]==this[i])
			{
				continue o;
			}
		}
		r[r.length] = this[i];
	}
	return r;
};

function get_villes() {
  var villes = [];
  $("input[name=ville]:checked").each(function (i, e){
    villes.push(e.value);
  });
  villes = villes.unique().toString();

  $("input[name=localisation]").attr('value', villes);
  tb_remove();
}

function init_search(code_postal) {
  if ( $("#Achat") ^ $("#location") ) {
    if ( $("#Achat") ) { $("#Achat").attr("checked", "checked"); }
    if ( $("#Location") ) { $("#Location").attr("checked", "checked"); }
    $("div[class=type_transaction]").hide();
  } else { 
    $("#Achat").attr("checked", "checked"); 
  }

  if( $("[name=type_transaction]:checked")[0].value == "location") {
    $(".slider-budget-location").show();
    $(".slider-budget-achat").hide();
  } else {
    $(".slider-budget-location").hide();
    $(".slider-budget-achat").show();
    $("#Achat").attr("checked", "checked");
  }
  $('#lien_liste_biens').attr('href', '/trouver_logement/' + $("[name=type_transaction]:checked")[0].value + '/page-1/');
  checkTerrain('');
  // cas des agences a paris
  if (code_postal.match('^75')) {
    $("#appartementAL").attr("checked", "checked");
  
  }
}

