function getSquareMeterPrice_table( property )
{
 if( $("#form_zip_code").val() == "" || $("#form_town").val() == "" )
 {
  $("#square_meter_price_contener").empty();
  $("#square_meter_price_contener").html( "<em>Aucune ville sélectionnée.</em>" );
  $("form#form_estimate_start").find("input[name=square_meter_price]").val( '' );
 }
 else
 {
  var data = { action_form: "square_meter_price",
               city: $("#form_town").val(),
               zip_code: $("#form_zip_code").val(),
               property: $("#form_estimate_start input[type=radio]:checked").val() };
  
  $.post( "/system/user/estimate.html", 
          data, 
          function( data )
          {
           if( data.status == "true" )
           {
            $("#square_meter_price_contener").empty();
            $("#square_meter_price_contener").html( data.html );
												
												if (data.square_meter_price != null) {
												 $("#form_estimate_start input[type=submit]").removeAttr('disabled');
													$("#form_estimate_start div p em").empty()
												} else {
												 $("#form_estimate_start input[type=submit]").attr('disabled', 'disabled');
             $("#form_estimate_start div p em").html('Etant donné qu\'aucun prix n\'est présent, nous ne pouvons pas estimer automatiquement votre bien. Pour une estimation, veuillez <a href="http://www.etude-immobiliere.com/nous-contacter.html">nous contacter</a>')
												}
												
            $("form#form_estimate_start").find("input[name=square_meter_price]").val( data.square_meter_price );
           }
           else
            alert( "Une erreur est survenue, merci de réessayer plus tard." );
          }, "json");
  $("#square_meter_price_contener").html();
 }
}


function validStartForm()
{
 if( $("form#form_estimate_start").validForm() )
 {
  return true;
 }
 else
 {
  alert( "Merci de complèter les champs manquants ou invalides, inscrits en rouge dans le formulaire." );
  
  return false;
 }
}


function changeExplaination( input )
{
 input = $(input);
 var tdExplaination = $(input).parent("td").next(".explaination");
 
 if( tdExplaination.length == 1 )
 {
  switch( $(input).attr("name") )
  {
   case 'see':
    switch( $(input).val() )
    {
     case '5': $(tdExplaination).html( "Face mer, rivière, golf, parc, montagne" ); break;
     case '4': $(tdExplaination).html( "Vue dégagée sur mer, parc, espaces verts" ); break;
     case '3': $(tdExplaination).html( "Pas de facteurs exceptionnels ni pénalisants" ); break;
     case '2': $(tdExplaination).html( "Maison, immeuble, densité de mitoyenneté" ); break;
     case '1': $(tdExplaination).html( "Sur voie férrée, 4 voies, zone industrielle..." ); break;
     default: $(tdExplaination).html( "" );
    }
    break;
   
   case 'sonorous':
    switch( $(input).val() )
    {
     case '5': $(tdExplaination).html( "Calme absolu" ); break;
     case '4': $(tdExplaination).html( "Légers bruits urbains" ); break;
     case '3': $(tdExplaination).html( "Proximité école, rue passante" ); break;
     case '2': $(tdExplaination).html( "Forte densité urbaine" ); break;
     case '1': $(tdExplaination).html( "4 voies, voie férrée, aéroport..." ); break;
     default: $(tdExplaination).html( "" );
    }
    break;
   
   case 'services':
    if( $(input).val() != "" )
     $(tdExplaination).html( "Transport, commerces, écoles, crèche, loisirs" );
    else
     $(tdExplaination).html( "" );
    break;
   
   case 'district':
    switch( $(input).val() )
    {
     case '5': $(tdExplaination).html( "Prisé" ); break;
     case '4': $(tdExplaination).html( "Réputé" ); break;
     case '3': $(tdExplaination).html( "Résidentiel" ); break;
     case '2': $(tdExplaination).html( "En développement" ); break;
     case '1': $(tdExplaination).html( "A rénover" ); break;
     default: $(tdExplaination).html( "" );
    }
    break;
   
   case 'adjoining':
    switch( $(input).val() )
    {
     case '5': $(tdExplaination).html( "Absence de mitoyenneté" ); break;
     case '4': $(tdExplaination).html( "Construction en limite de propriété" ); break;
     case '3': $(tdExplaination).html( "Construction adossée au garage" ); break;
     case '2': $(tdExplaination).html( "Mitoyenneté totale d'un côté" ); break;
     case '1': $(tdExplaination).html( "Mitoyenneté des deux côtés" ); break;
     default: $(tdExplaination).html( "" );
    }
    break;
   
   case 'garden':
    switch( $(input).val() )
    {
     case '5': $(tdExplaination).html( "Très paysager, beaux arbres, dense, piscine..." ); break;
     case '4': $(tdExplaination).html( "Diversité de plantations, qualité visuelle..." ); break;
     case '3': $(tdExplaination).html( "Belle pelouse légèrement arborée" ); break;
     case '2': $(tdExplaination).html( "Pas ou peu entretenu" ); break;
     default: $(tdExplaination).html( "" );
    }
    break;
   
   case 'age':
    switch( $(input).val() )
    {
     case '5': $(tdExplaination).html( "Dernières normes européennes de construction" ); break;
     case '4': $(tdExplaination).html( "Amélioration des normes et fin de l'amiante" ); break;
     case '3': $(tdExplaination).html( "Choc pétrolier : nouvelles normes d'isolation" ); break;
     case '2': $(tdExplaination).html( "1ères normes sanitaires : fin du plomb" ); break;
     case '1': $(tdExplaination).html( "Peu de normes en matière construction" ); break;
     default: $(tdExplaination).html( "" );
    }
    break;
   
   case 'quality':
    break;
   
   case 'heating':
    switch( $(input).val() )
    {
     case '5': $(tdExplaination).html( "Très basse consommation d'énergie" ); break;
     case '4': $(tdExplaination).html( "Basse consommation d'énergie" ); break;
     case '3': $(tdExplaination).html( "Moyenne consommation d'énergie" ); break;
     case '2': $(tdExplaination).html( "Forte consommation d'énergie" ); break;
     default: $(tdExplaination).html( "" );
    }
    break;
   
   case 'carcass':
    switch( $(input).val() )
    {
     case '5': $(tdExplaination).html( "Très bonne isolation phonique et thermique" ); break;
     case '4': $(tdExplaination).html( "Bonne isolation phonique et thermique" ); break;
     case '3': $(tdExplaination).html( "Isolation phonique et thermique normale" ); break;
     case '2': $(tdExplaination).html( "Isolation phonique et thermique basique" ); break;
     case '1': $(tdExplaination).html( "Isolation phonique et thermique faible" ); break;
     default: $(tdExplaination).html( "" );
    }
    break;
   
   case 'facade':
    switch( $(input).val() )
    {
     case '5': $(tdExplaination).html( "Très bonne isolation phonique et thermique" ); break;
     case '4': $(tdExplaination).html( "Bonne isolation phonique et thermique" ); break;
     case '3': $(tdExplaination).html( "Isolation phonique et thermique normale" ); break;
     case '2': $(tdExplaination).html( "Isolation phonique et thermique basique" ); break;
     case '1': $(tdExplaination).html( "Isolation phonique et thermique faible" ); break;
     default: $(tdExplaination).html( "" );
    }
    break;
   
   case 'roof':
    switch( $(input).val() )
    {
     case '5': $(tdExplaination).html( "Campagnonage, zinc, cuivre" ); break;
     case '4': $(tdExplaination).html( "Ardoise naturelle, tuile terre cuite, etc." ); break;
     case '3': $(tdExplaination).html( "Ardoise fibro, tuile béton (mécanique)" ); break;
     case '2': $(tdExplaination).html( "Couverture rustique (chaume, lose...)" ); break;
     case '1': $(tdExplaination).html( "Fibro amiante, schilgum" ); break;
     default: $(tdExplaination).html( "" );
    }
    break;
   
   case 'door_frame':
    switch( $(input).val() )
    {
     case '5': $(tdExplaination).html( "Aluminium+bois, double vitrage" ); break;
     case '4': $(tdExplaination).html( "Aluminium double vitrage" ); break;
     case '3': $(tdExplaination).html( "Bois double vitrage" ); break;
     case '2': $(tdExplaination).html( "PVC double vitrage" ); break;
     case '1': $(tdExplaination).html( "Simple vitrage (tout support)" ); break;
     default: $(tdExplaination).html( "" );
    }
    break;
   
   case 'facade_state':
    switch( $(input).val() )
    {
     case '5': $(tdExplaination).html( "Moins de 2 ans" ); break;
     case '4': $(tdExplaination).html( "Moins de 5 ans" ); break;
     case '3': $(tdExplaination).html( "Moins de 10 ans, traces visibles" ); break;
     case '2': $(tdExplaination).html( "Dégradation surface, fissures" ); break;
     case '1': $(tdExplaination).html( "Dégradation à risque, infiltrations" ); break;
     default: $(tdExplaination).html( "" );
    }
    break;
   
   case 'roof_state':
    switch( $(input).val() )
    {
     case '5': $(tdExplaination).html( "Contrôlée ou moins de 5 ans" ); break;
     case '4': $(tdExplaination).html( "Contrôlée ou moins de 10 ans" ); break;
     case '3': $(tdExplaination).html( "Contrôlée ou moins de 20 ans" ); break;
     case '2': $(tdExplaination).html( "Plus de 20 ans" ); break;
     case '1': $(tdExplaination).html( "A rénover" ); break;
     default: $(tdExplaination).html( "" );
    }
    break;
   
   case 'floor_state':
    switch( $(input).val() )
    {
     case '5': $(tdExplaination).html( "Très entretenu, sans usure" ); break;
     case '4': $(tdExplaination).html( "Entretien régulier, sans usure" ); break;
     case '3': $(tdExplaination).html( "Entretenu, avec légères usures" ); break;
     case '2': $(tdExplaination).html( "Peu entretenu, avec usures apparentes" ); break;
     case '1': $(tdExplaination).html( "A refaire" ); break;
     default: $(tdExplaination).html( "" );
    }
    break;
   
   case 'wall_and_ceiling_state':
    switch( $(input).val() )
    {
     case '5': $(tdExplaination).html( "Moins de 5 ans" ); break;
     case '4': $(tdExplaination).html( "Moins de 10 ans" ); break;
     case '3': $(tdExplaination).html( "Moins de 15 ans" ); break;
     case '2': $(tdExplaination).html( "Moins de 20 ans" ); break;
     case '1': $(tdExplaination).html( "Plus de 20 ans" ); break;
     default: $(tdExplaination).html( "" );
    }
    break;
   
   case 'toilet_state':
    switch( $(input).val() )
    {
     case '5': $(tdExplaination).html( "Très bon état" ); break;
     case '4': $(tdExplaination).html( "Bon état" ); break;
     case '3': $(tdExplaination).html( "État correct" ); break;
     case '2': $(tdExplaination).html( "Usures apparentes" ); break;
     case '1': $(tdExplaination).html( "A refaire" ); break;
     default: $(tdExplaination).html( "" );
    }
    break;
   
   case 'door_frame_state':
    switch( $(input).val() )
    {
     case '5': $(tdExplaination).html( "Moins de 10 ans" ); break;
     case '4': $(tdExplaination).html( "Moins de 20 ans" ); break;
     case '3': $(tdExplaination).html( "Moins de 30 ans" ); break;
     case '2': $(tdExplaination).html( "Plus de 30 ans" ); break;
     case '1': $(tdExplaination).html( "A refaire" ); break;
     default: $(tdExplaination).html( "" );
    }
    break;
   
   case 'sojourn':
    switch( $(input).val() )
    {
     case '5': $(tdExplaination).html( "Plus de 35% de la surface habitable" ); break;
     case '4': $(tdExplaination).html( "Entre 35 et 30% de la surface habitable" ); break;
     case '3': $(tdExplaination).html( "Entre 30 et 25% de la surface habitable" ); break;
     case '2': $(tdExplaination).html( "Entre 25 et 20% de la surface habitable" ); break;
     case '1': $(tdExplaination).html( "Moins de 20% de la surface habitable" ); break;
     default: $(tdExplaination).html( "" );
    }
    break;
   
   case 'corridor':
    switch( $(input).val() )
    {
     case '5': $(tdExplaination).html( "Moins de 5% de la surface habitable" ); break;
     case '4': $(tdExplaination).html( "Entre 5 et 10% de la surface habitable" ); break;
     case '3': $(tdExplaination).html( "Entre 10 et 15% de la surface habitable" ); break;
     case '2': $(tdExplaination).html( "Entre 15 et 20% de la surface habitable" ); break;
     case '1': $(tdExplaination).html( "Plus de 20% de la surface habitable" ); break;
     default: $(tdExplaination).html( "" );
    }
    break;
   
   case 'stowage':
    switch( $(input).val() )
    {
     case '5': $(tdExplaination).html( "Rangements très optimisés" ); break;
     case '4': $(tdExplaination).html( "Rangements optimisés" ); break;
     case '3': $(tdExplaination).html( "Rangements peu optimisés" ); break;
     case '2': $(tdExplaination).html( "Rangements très peu optimisés" ); break;
     case '1': $(tdExplaination).html( "Pas de rangements" ); break;
     default: $(tdExplaination).html( "" );
    }
    break;
   
   case 'garage':
    switch( $(input).val() )
    {
     case '5': $(tdExplaination).html( "Stationnements très confortables et sécurisés" ); break;
     case '4': $(tdExplaination).html( "Stationnement confortable et sécurisé" ); break;
     case '3': $(tdExplaination).html( "Stationnements très confortables" ); break;
     case '2': $(tdExplaination).html( "Stationnement confortable" ); break;
     case '1': $(tdExplaination).html( "Contrainte de stationnement" ); break;
     default: $(tdExplaination).html( "" );
    }
    break;
   
   case 'annex':
    if( $(input).val() != "" )
     $(tdExplaination).html( "Grenier, sous-sol, cave, dépendance, etc." );
    else
     $(tdExplaination).html( "" );
    break;
   
   case 'luminance':
    switch( $(input).val() )
    {
     case '5': $(tdExplaination).html( "Larges baies dans pièces de vie" ); break;
     case '4': $(tdExplaination).html( "Baies ou doubles fenêtres dans pièces de vie" ); break;
     case '3': $(tdExplaination).html( "Fenêtres standards dans pièces de vie" ); break;
     case '2': $(tdExplaination).html( "Petites ouvertures dans pièces de vie" ); break;
     case '1': $(tdExplaination).html( "Peu d'ouvertures dans pièces de vie" ); break;
     default: $(tdExplaination).html( "" );
    }
    break;
   
   case 'aspect':
    if( $(input).val() != "" )
     $(tdExplaination).html( "Pièce de vie principale et cuisine" );
    else
     $(tdExplaination).html( "" );
    break;
  }
 }
}


function validTabToGoNext( input )
{
 var selected = $('#estimate_tabs').tabs('option', 'selected');
 var validated = true;
 input = $(input);
 
 getResultOfEstimate();
 
 switch( $(input).attr("name") )
 {
  case 'next_2':
   if( $("#tab-environnement").find("select[value='']").length > 0 )
    validated = false;
   else
   {
    $("#estimate_tabs").tabs('option', 'disabled', [2, 3, 4, 5]);
    $('#estimate_tabs').tabs('option', 'selected', 1);
   }
   break;
  case 'next_3':
   if( $("#tab-construction").find("select[value='']").length > 0 )
    validated = false;
   else
   {
    $("#estimate_tabs").tabs('option', 'disabled', [3, 4, 5]);
    $('#estimate_tabs').tabs('option', 'selected', 2);
   }
   break;
  case 'next_4':
   if( $("#tab-etat_general").find("select[value='']").length > 0 )
    validated = false;
   else
   {
    $("#estimate_tabs").tabs('option', 'disabled', [4, 5]);
    $('#estimate_tabs').tabs('option', 'selected', 3);
   }
   break;
  case 'next_5':
   if( $("#tab-habitabilite").find("select[value='']").length > 0 )
    validated = false;
   else
   {
    $("#estimate_tabs").tabs('option', 'disabled', [5]);
    $('#estimate_tabs').tabs('option', 'selected', 4);
   }
   break;
  case 'next_6':
   if( $("#tab-exposition").find("select[value='']").length > 0 )
    validated = false;
   else
   {
    $("#estimate_tabs").tabs('option', 'disabled', []);
    $('#estimate_tabs').tabs('option', 'selected', 5);
   }
   break;
  case 'finish':
   if( $("#tab-controle_technique").find("select[value='']").length > 0 )
    validated = false;
   break;
 }
 
 if( validated == false )
 {
  alert( "Vous devez remplir tous les champs avant de passer à l'étape suivante." );
 }
}


function getResultOfEstimate()
{
 var result = 0;
 var nbSelect = 0;
 var environnementAvg = 0;
 var constructionAvg = 0;
 var etat_generalAvg = 0;
 var habitabiliteAvg = 0;
 var expositionAvg = 0;
 var globalAvg = 0;
 var nbTab = 0;
 
 var estimate = 0;
 var estimateFrom = 0;
 var estimateTo = 0;
 var capitalGainFrom = 0;
 var capitalGainTo = 0;
 
 
 // Environnement avg
 $("form#form_estimate #tab-environnement")
     .find("select[value!='']")
     .each( function( i, select )
            {
             if( $(select).val() != "" )
             {
              environnementAvg += parseInt( $(select).val() );
             }
            } );
 if( $("form#form_estimate #tab-environnement").find("select[value!='']").length > 0 )
 {
  environnementAvg = environnementAvg / $("form#form_estimate #tab-environnement").find("select[value!='']").length;
  nbTab++;
 }
 
 // Construction avg
 $("form#form_estimate #tab-construction")
     .find("select[value!='']")
     .each( function( i, select )
            {
             if( $(select).val() != "" )
             {
              constructionAvg += parseInt( $(select).val() );
             }
            } );
 if( $("form#form_estimate #tab-construction").find("select[value!='']").length > 0 )
 {
  constructionAvg = constructionAvg / $("form#form_estimate #tab-construction").find("select[value!='']").length;
  nbTab++;
 }
 
 // Etat general avg
 $("form#form_estimate #tab-etat_general")
     .find("select[value!='']")
     .each( function( i, select )
            {
             if( $(select).val() != "" )
             {
              etat_generalAvg += parseInt( $(select).val() );
             }
            } );
 if( $("form#form_estimate #tab-etat_general").find("select[value!='']").length > 0 )
 {
  etat_generalAvg = etat_generalAvg / $("form#form_estimate #tab-etat_general").find("select[value!='']").length;
  nbTab++;
 }
 
 // Habitabilite avg
 $("form#form_estimate #tab-habitabilite")
     .find("select[value!='']")
     .each( function( i, select )
            {
             if( $(select).val() != "" )
             {
              habitabiliteAvg += parseInt( $(select).val() );
             }
            } );
 if( $("form#form_estimate #tab-habitabilite").find("select[value!='']").length > 0 )
 {
  habitabiliteAvg = habitabiliteAvg / $("form#form_estimate #tab-habitabilite").find("select[value!='']").length;
  nbTab++;
 }
 
 // Exposition avg
 $("form#form_estimate #tab-exposition")
     .find("select[value!='']")
     .each( function( i, select )
            {
             if( $(select).val() != "" )
             {
              expositionAvg += parseInt( $(select).val() );
             }
            } );
 if( $("form#form_estimate #tab-exposition").find("select[value!='']").length > 0 )
 {
  expositionAvg = expositionAvg / $("form#form_estimate #tab-exposition").find("select[value!='']").length;
  nbTab++;
 }
 
 // Global avg
 if( nbTab > 0 )
  globalAvg = ( environnementAvg + constructionAvg + etat_generalAvg + habitabiliteAvg + expositionAvg ) / nbTab;
 
 
 $("img#estimate_house").removeClass( "color5" );
 $("img#estimate_house").removeClass( "color3" );
 $("img#estimate_house").removeClass( "color4" );
 $("img#estimate_house").removeClass( "color2" );
 $("img#estimate_house").removeClass( "color1" );
 
 if( $("form#form_estimate").find("select[value!='']").length > 0 )
 {
  if( globalAvg > 4.5 )
  {
   result = 10;
   $("img#estimate_house").addClass( "color5" );
  }
  else if( globalAvg > 3.5 )
  {
   result = 5;
   $("img#estimate_house").addClass( "color4" );
  }
  else if( globalAvg > 2.5 )
  {
   result = 0;
   $("img#estimate_house").addClass( "color3" );
  }
  else if( globalAvg > 1.5 )
  {
   result = -5;
   $("img#estimate_house").addClass( "color2" );
  }
  else
  {
   result = -10;
   $("img#estimate_house").addClass( "color1" );
  }
 }
 
 
 estimate = Math.ceil( $("input#form_habitable_surface").val() * $("input#form_square_meter_price").val() );
 estimate = Math.ceil( estimate + ( estimate * result / 100 ) );
 estimateFrom = Math.ceil( estimate - ( estimate * 2.5 / 100 ) );
 estimateTo = Math.ceil( estimate + ( estimate * 2.5 / 100 ) );
 $("span#estimate_from").html( estimateFrom );
 $("span#estimate_to").html( estimateTo );
 capitalGainFrom = estimateFrom - $("input#form_purchase_price").val();
 capitalGainTo = estimateTo - $("input#form_purchase_price").val();
 $("span#capital_gain_from").html( ( capitalGainTo < 0 ? capitalGainTo : capitalGainFrom ) );
 $("span#capital_gain_to").html( ( capitalGainTo < 0 ? capitalGainFrom : capitalGainTo ) );
 
 
 return result;
}
