$(function() {
	
	boutonHover(".hoverAjouter", "images/bouton_ajouter_credit_on.png");
	boutonHover(".hoverSimuler", "images/bouton_simuler_petit_on.png");
	boutonHover(".hoverSuivant", "images/bouton_suivant_on.png");
	boutonHover(".hoverPrecedent", "images/bouton_precedent_on.png");
	boutonHover(".hoverTerminer", "images/bouton_valider2.png");
	
	createMultiSelect('#selectTelephone', '#tel_');
	createMultiSelect('#selectPatrimoine', '#div_');
	createMultiSelect('#selectFinancier', '#div_');
	
	createMultiSelectCo('#selectRevenuEmprunteur', '.in4' , '#div_4');
	createMultiSelectCo('#selectRevenuCoEmprunteur', '.in6', '#div_4');
	
	createMultiSelect('#selectCharges', '#div_');

	$('#selectBanquesConsultees').multiSelect({
		selectAll : false,
		noneSelected:'Sélectionnez',
		oneOrMoreSelected : '$'
	});
	
	$('.chiffre').blur(function() {
		
		tosp(this);
		
	});
	$('.chiffre2').blur(function() {
		
		tosp2(this);
		
	});
});  


function coEmprunteur(action) {
	
	if(action == 'show') {
		$('.coEmprunteur').show();
		$('.coEmprunteurField').css('border','1px solid #CCCCCC');
	} else {
		$('.coEmprunteurField').css('border','none');
		$('.coEmprunteur').hide();
	}
	
}

function createMultiSelect(exprSelect, exprCible) {
	$(exprSelect).multiSelect({
		selectAll : false,
		noneSelected:'Sélectionnez',
		oneOrMoreSelected : '$'
	}, function(ckBox) {
		if($(ckBox).attr('checked')) {
			$(exprCible+ckBox.val()).show();
		} else {
			$(exprCible+ckBox.val()).hide();
		}
		if(exprSelect == '#selectPatrimoine') {
			size = $("input[name='patrimoine[]']:checked").size();
			if(size == 0 || (size == 1 &&
					$("input[name='patrimoine[]']:checked:first").val() == 0)) {
				$('#div_20').hide();
			} else {
				$('#div_20').show();
			}	
		}
		if(exprSelect == '#selectFinancier') {
			size = $("input[name='financier[]']:checked").size();
			if(size == 0 || (size == 1 &&
					$("input[name='financier[]']:checked:first").val() == 0)) {
				$('#div_30').hide();
			} else {
				$('#div_30').show();
			}	
		}
		if(exprSelect == '#selectCharges') {
			size = $("input[name='charges[]']:checked").size();
			if(size == 0 || (size == 1 &&
					$("input[name='charges[]']:checked:first").val() == 0)) {
				$('#div_50').hide();
			} else {
				$('#div_50').show();
			}	
		}
	});	
}

function createMultiSelectCo(exprSelect, exprCible, divCible) {
	$(exprSelect).multiSelect({
		selectAll : false,
		noneSelected:'Sélectionnez',
		oneOrMoreSelected : '$'
	}, function(ckBox) {
		
		var name = "revenuEmprunteur";
		if(exprSelect == '#selectRevenuEmprunteur') {
			name = "revenuCoEmprunteur";
		}
		
		if($(ckBox).attr('checked')) {
			$(divCible+ckBox.val()).show();
			$(exprCible+ckBox.val()).show();
		} else {
			autreCkBox = $("input[name='"+name+"[]']").get(ckBox.val()-1);
			if(!$(autreCkBox).attr('checked')) {
				$(divCible+ckBox.val()).hide();
			}
			$(exprCible+ckBox.val()).hide();
		}
		
	});	
}

var cptCreditImmo = 1;
var cptCreditConso = 1;
var cptCreditAutre = 1;


function addCredit(element, type) {
	cpt = 0;
	if(type == 'immo' && cptCreditImmo <= 2)
		cpt = cptCreditImmo++;
	
	if(type == 'conso' && cptCreditConso <= 2)
		cpt = cptCreditConso++;
	
	if(type == 'autre' && cptCreditAutre <= 2)
		cpt = cptCreditAutre++;
	
	if(cpt != 0) {
		
		var anneeOpt = '<option value=""></option>';
		
		for(i=2009; i<=2060; i++) {
			anneeOpt += '<option vaue="'+i+'">'+i+'</option>';
		}
		var select = '<select name="mois_restant_credit_'+type+'_'+cpt+'" class="auto">'+
						'<option value=""></option>'+
						'<option value="Janvier">Janvier</option>'+
						'<option value="Février">Février</option>'+
						'<option value="Mars">Mars</option>'+
						'<option value="Avril">Avril</option>'+
						'<option value="Mai">Avril</option>'+
						'<option value="Juin">Juin</option>'+
						'<option value="Juillet">Juillet</option>'+
						'<option value="Août">Août</option>'+
						'<option value="Septembre">Septembre</option>'+
						'<option value="Octobre">Septembre</option>'+
						'<option value="Novembre">Novembre</option>'+
						'<option value="Décembre">Décembre</option>'+
					'</select> '+ 
					'<select name="anne_restant_credit_'+type+'_'+cpt+'" class="auto">'+
					anneeOpt+
					'</select>';
		
		var credit = '<div class="clear"></div>'+ 
					'<div class="form_left_3">&nbsp;</div>'+ 
					'<div class="form_center_5"><input id="i52_1'+cpt+'" name="mensualite_credit_'+type+'_'+cpt+'" type="text" class="chiffre"/>&euro; / mois</div>'+
					'<div class="form_right">'+select+'</div>';
		
		$(element).parent().before(credit);
	}
}

function typeAchat() {
	
	var res = 4;
	
	var bien = $("select[name='type_bien']").val();
	var etat = $("select[name='etat_bien']").val();
	
	if(bien == 'Maison' || bien == 'Appartement' || bien == 'Autres') {
		if(etat == 'Neuf') {
			res = 3;
		} else {
			res = 4;
		}
		
	}
	
	if(bien == "Terrain + construction") {
		res = 1;
	}
	
	$("input[name='TypeAchatF']").val(res);
	
	
}

function sumPretPro() {
	
	projet = sp('#montant_projet');
	apport = sp('#montant_apport');
	$('#montant_financement').val(projet - apport);
	tosp('#montant_financement');
	
}

function calcFrais(to) {
	
	var val = 0;
	
	if(to == 'logement') {
		var aq = sp('input[name=\'montant_estime_acqisition\']');
		var cons = sp('input[name=\'montant_construction\']');
		
		aq = isNaN(aq) ? 0 : aq;
		cons = isNaN(cons) ? 0 : cons;
		
		val = aq + cons;
	}
	
	if(to == 'terrain') {
		val = sp('input[name=\'prix_terrain\']');
	}
	
	if(to == 'PretPth') {
		val = sp('input[name=\'montant_rembourser\']');
		$("input[name='penalite_remboursement']").val(
				Math.round((sp('input[name=\'montant_rembourser\']')*3)/100));
	}
	
	if(to == 'travaux') {
		val = sp('input[name=\'montant_travaux\']');
	}
	
	if(isNaN(val)) {
		val = 0;
	}
	$('input[name=\''+to+'\']').val(val);
	
	validerF(document.calcFrN);
	
}

function calcTotal() {
	var total = 0;
	
	var tmpVal = sp("input[name='montant_estime_acqisition']");
	total +=  isNaN(tmpVal) ? 0 : tmpVal;
	
	tmpVal = sp("input[name='prix_terrain']");
	total +=  isNaN(tmpVal) ? 0 : tmpVal;
	
	tmpVal = sp("input[name='montant_construction']");
	total +=  isNaN(tmpVal) ? 0 : tmpVal;
	
	tmpVal = sp("input[name='montant_travaux']");
	total +=  isNaN(tmpVal) ? 0 : tmpVal;
	
	tmpVal = sp("input[name='frais_notaire']");
	total +=  isNaN(tmpVal) ? 0 : tmpVal;
	
	tmpVal = sp("input[name='montant_rembourser']");
	total +=  isNaN(tmpVal) ? 0 : tmpVal;
	
	tmpVal = sp("input[name='penalite_remboursement']");
	total +=  isNaN(tmpVal) ? 0 : tmpVal;
	
	tmpVal = sp("input[name='apport']");
	total -=  isNaN(tmpVal) ? 0 : tmpVal;
	
	tmpVal = sp("input[name='frais_garantie']");
	total +=  isNaN(tmpVal) ? 0 : tmpVal;
	
	
	$("input[name='budget_total_estime']").val(total);
	tosp("input[name='budget_total_estime']");
	
}

function calcMontantPret() {
	var total = 0;
	
	var tmpVal = sp("input[name='pret_taux_zero']");
	total +=  isNaN(tmpVal) ? 0 : tmpVal;
	
	tmpVal = sp("input[name='apport_personnel']");
	total +=  isNaN(tmpVal) ? 0 : tmpVal;
	
	tmpVal = sp('#montant_pret2');
	$("input[name='montant_pret']").val(tmpVal-total);
	tosp("input[name='montant_pret']");
}

function selectProjet(selProjet){
    showMontantAcq();
    selectNiveauAvance();
    
    if($('#votre_projet_immobilier').val() != 'Acquisition') {
    	$("input[name='montant_estime_acqisition']").val('');
    	$("input[name='logement']").val('0');
    	$("input[name='terrain']").val('0');
    	$("input[name='travaux']").val('1');
    	$("input[name='montant_estime_acqisition']").val('');
    	$("input[name='prix_terrain']").val('');
    	$("input[name='montant_construction']").val('');
    	$("input[name='montant_travaux']").val('');
    	$("input[name='frais_notaire']").val('');
    } else {
    	$("input[name='PretPth']").val('0');
    	$("input[name='montant_rembourser']").val('');
    	$("input[name='penalite_remboursement']").val('');
    	$("input[name='frais_garantie']").val('');
        	
    }
	$("input[name='budget_total_estime']").val('');
  }


function selectCivilite(val) {
	
	if(val=='civilite_4') {
		if($('#civilite_4').val() == 'Mme') {
			$('#div_402').show();
			$('#njf').show();
		} else {
			if($('#co_civilite').val() != 'Mme') {
				$('#div_402').hide();
			}
			$('#njf').hide();
		}
	}
	if(val=='co_civilite') {
		if($('#co_civilite').val() == 'Mme') {
			$('#div_402').show();
			$('#co_njf').show();
		} else {
			if($('#civilite_4').val() != 'Mme') {
				$('#div_402').hide();
			}
			$('#co_njf').hide();
		}
	}	
}

function selectPays(val) {
	
	if(val=='pays_4') {
		if($('#pays_4').val() != 'France') {
			$('#div_401').show();
			$('#ev').show();
		} else {
			if($('#co_pays').val() == 'France') {
				$('#div_401').hide();
			}
			$('#ev').hide();
		}
	}
	if(val=='co_pays') {
		if($('#co_pays').val() != 'France') {
			$('#div_401').show();
			$('#co_ev').show();
		} else {
			if($('#pays_4').val() == 'France') {
				$('#div_401').hide();
			}
			$('#co_ev').hide();
		}
	}	
}

function selectCategoriePro(val) {
	
	catProArray = ['Retraité','Congé parental','Sans activité','Recherche d\'emploi','Etudiant'];
	
	if(val=='categorie_professionnnelle') {
		if(!in_array($('#categorie_professionnnelle').val(), catProArray)) {
			$('#div_417').show();
			$('#ap').show();
		} else {
			if(in_array($('#co_categorie_professionnnelle').val(), catProArray)) {
				$('#div_417').hide();
			}
			$('#ap').hide();
		}
	}
	if(val=='co_categorie_professionnnelle') {
		if(!in_array($('#co_categorie_professionnnelle').val(), catProArray)) {
			$('#div_417').show();
			$('#co_ap').show();
		} else {
			if(in_array($('#categorie_professionnnelle').val(), catProArray)) {
				$('#div_417').hide();
			}
			$('#co_ap').hide();
		}
	}	
}

function in_array(string, array) {
	res = false;
	$.each(array, function (i, val) {
		res |= string == val ? true : false;
	});
	return res;
}

function selectSituation(){//s1
    if(( document.getElementById('situation_actuelle').value == 'Propriétaire' || $('#situation_actuelle').val() == 'Usage mixte du bien') 
    		&& document.getElementById('usage_projet').value == 'Résidence principale' ){//s2
      document.getElementById('div_12').style.display = 'block'; 
    }//e2
    else {//s3
      document.getElementById('div_12').style.display = 'none'; 
    }//e3

    if( document.getElementById('situation_actuelle').value == 'Locataire'){//s4
      document.getElementById('div_14').style.display = 'block';
    }//e4
    else {//s5
      document.getElementById('div_14').style.display = 'none'; 
    }//e5
  }//e1

  function labelSetFields(id1, id2){
    if( document.getElementById(id1).checked == 1 ){
      document.getElementById(id1).checked = 0;
      document.getElementById(id2).style.display = "none";
    }
    else {
      document.getElementById(id1).checked = 1;
      document.getElementById(id2).style.display = "block";
    }
  }
 
  function ckboxSetFields(id1, id2){
    if( document.getElementById(id1).checked == 1 ){
      document.getElementById(id2).style.display = "block";
    }
    else {
      document.getElementById(id2).style.display = "none";
    }
  }

  function labelSetFields2(id1, id2, id3, id4){
    if( document.getElementById(id1).checked == 1 ){
      document.getElementById(id1).checked = false;
      document.getElementById(id4).style.display = "none";
    }
    else {
      document.getElementById(id1).checked = true;
      if(id2){ document.getElementById(id2).checked = false; }
      if(id3){ document.getElementById(id3).style.display = "block"; }
      document.getElementById(id4).style.display = "block";

    }
  }

  function ckboxSetFields2(id1, id2, id3, id4){
    if( document.getElementById(id1).checked == 1 ){
      if(id2){ document.getElementById(id2).checked = false; }
      if(id3){ document.getElementById(id3).style.display = "block"; }
      document.getElementById(id4).style.display = "block";
    }
    else {
      document.getElementById(id4).style.display = "none";
    }
  }

  function labelClearFields(id0, id1, first, last){
    var id;
    if( document.getElementById(id1).checked == false ){
      document.getElementById(id1).checked = true;
      if(id0){ document.getElementById(id0).style.display = "none"; }
        for(id = first; id <= last; id ++){
          document.getElementById("div_"+id).style.display = "none";
          document.getElementById("ck"+id).checked = false;
        }
    }
    else {
      document.getElementById(id1).checked = false;
    }
  }

  function ckboxClearFields(id0, id1, first, last){
    var id;
    if( document.getElementById(id1).checked == 1 ){
      if(id0){ document.getElementById(id0).style.display = "none"; }
        for(id = first; id <= last; id ++){
          document.getElementById("div_"+id).style.display = "none";
          document.getElementById("ck"+id).checked = false;
        }
    }
  }
  
  
    function showHidebis(id1, value, id2, action){
	/*Les valeurs hide et show ont été échangé car sinon les noms de banques n'apparaissait pas quand oui était coché*/	
	if ( action == "hide" ){
      if ( document.getElementById(id1).value == value ){
        document.getElementById(id2).style.display = "block";
      }
      else {
        document.getElementById(id2).style.display = "none";    
      }
    }

    if ( action == "show" ){
      if ( document.getElementById(id1).value == value ){
        document.getElementById(id2).style.display = "none";
      }
      else {
        document.getElementById(id2).style.display = "block";    
      }
    }
  }
  
  

  function showHide(id1, value, id2, action){
	if ( action == "show" ){
      if ( document.getElementById(id1).value == value ){
        document.getElementById(id2).style.display = "block";
      }
      else {
        document.getElementById(id2).style.display = "none";    
      }
    }

    if ( action == "hide" ){
      if ( document.getElementById(id1).value == value ){
        document.getElementById(id2).style.display = "none";
      }
      else {
        document.getElementById(id2).style.display = "block";    
      }
    }
  }
  
  function showHideConso(id1){
    switch( parseInt(document.getElementById(id1).value) ) {
      case 2:
        document.getElementById('pret_conso_2').style.display = "block";
        document.getElementById('pret_conso_3').style.display = "none";
        break;
      case 3:
        document.getElementById('pret_conso_2').style.display = "block";
        document.getElementById('pret_conso_3').style.display = "block";
        break;
      default:
        document.getElementById('pret_conso_2').style.display = "none";
        document.getElementById('pret_conso_3').style.display = "none";
        break;
    }
  }

  function calculate_revenus(id1, id2, id3){
    document.getElementById(id3).value = document.getElementById(id1).value * document.getElementById(id2).value;
  }

  function sum_values(id1, first, last){
    var sum = 0;
    var ii;
    for(ii = first; ii <= last; ii ++){
    	//var val = Math.floor(parseInt(document.getElementById("in"+ii).value.replace(' ','')));
        var val = Math.floor(sp('#in'+ii));
      //sum += isNaN(val) ? 0 : val;
      sum += val;
      }
    document.getElementById(id1).value = sum;
    tosp('#'+id1);
  }



  function selectTypeBien(selTB){//s_stb1
    showHide('type_bien', 'Terrain + construction', 'div_11' ,'hide');
    showMontantAcq();

    if( selTB.value == "Maison" || selTB.value == "Appartement" ){
      document.getElementById('niv_av_4').style.display = "block";
    }
    else {
      document.getElementById('niv_av_4').style.display = "none";
      document.getElementById('niveau_avancement').selectedIndex = 0;
    }

    if( selTB.value == "Terrain + construction" ){
      document.getElementById('niv_av_5').style.display = "block";
    }
    else {
      document.getElementById('niv_av_5').style.display = "none";
    }
  }//e_stb1

  function selectNiveauAvance(){//s_sna1
    selNA = document.getElementById('niveau_avancement');

    if( (selNA.value == "Vous débutez vos recherches" || selNA.value == "Vous êtes en recherche active") &&
         document.getElementById('votre_projet_immobilier').value == "Acquisition" ){
      //document.getElementById('div_16').style.display = "block";
    }
    else {
      document.getElementById('div_16').style.display = "none";
    }

    if( selNA.value == "Vous avez trouvé et pensez signer prochainement" ){
      document.getElementById('div_15a').style.display = "block";
    }
    else {
      document.getElementById('div_15a').style.display = "none";
    }

    if( selNA.value == "Vous avez signé une promesse d'achat" || selNA.value == "Vous avez signé un contrat de construction" ){
      document.getElementById('div_15').style.display = "block";
    }
    else {
      document.getElementById('div_15').style.display = "none";
    }
  }//e_sna1

  function showMontantAcq(){//s_sma1
    document.getElementById('div_17').style.display = ( document.getElementById('votre_projet_immobilier').value == "Acquisition" && document.getElementById('type_bien').value != "Terrain + construction" ? "block" : "none" );

    document.getElementById('div_18').style.display = ( document.getElementById('votre_projet_immobilier').value == "Acquisition" && document.getElementById('type_bien').value == "Terrain + construction" ? "block" : "none" );

    document.getElementById('div_19').style.display = ( document.getElementById('votre_projet_immobilier').value == "Acquisition" && document.getElementById('type_bien').value == "Terrain + construction" ? "block" : "none" );

    document.getElementById('div_110').style.display = ( document.getElementById('votre_projet_immobilier').value == "Rachat de prêt immobilier" ? "block" : "none" );

    document.getElementById('div_111').style.display = ( document.getElementById('votre_projet_immobilier').value == "Rachat de prêt immobilier" ? "block" : "none" );

    document.getElementById('div_112').style.display = ( document.getElementById('votre_projet_immobilier').value == "Rachat de prêt immobilier" ? "none" : "block" );

    document.getElementById('div_113').style.display = ( document.getElementById('votre_projet_immobilier').value == "Rachat de prêt immobilier" ? "block" : "none" );

    document.getElementById('div_114').style.display = ( document.getElementById('votre_projet_immobilier').value == "Rachat de prêt immobilier" ? "none" : "block" );
   
    document.getElementById('div_15b').style.display = ( document.getElementById('votre_projet_immobilier').value == "Rachat de prêt immobilier" ? "none" : "block" );
     }//e_sma1

