$(document).ready(function() {
	$(".geo").each(function() {
		var latitude = $(this).find("span.latitude").html();
		var longitude = $(this).find("span.longitude").html();
		if (latitude && longitude) {
 			var url = 'http://maps.google.com/' + '?ll='+latitude+','+longitude + '&q='+latitude+','+longitude;
			$(this).append('<a href="'+url+'" class="map" target="map">Voir sur une carte</a>');
		}
	});
});

/* ************************************************************** SIMULATION */
$(document).ready(function() {
	function checkNumber(input, min, max, msg) {
	    msg = msg + " (" + input.value + ") doit &ecirc;tre un nombre ";
	    var str = input.value;
	    var expression = /^\d+\.?\d*$/;
	    if (!expression.test(str)){
			$(input).parent("div").append("<strong>" + msg + "</strong>");
	 		return false;
		}
	    var num = parseFloat(str)
	    if (num < min || max < num) {
			$(input).parent("div").append("<strong>" + msg + " entre " + min + " et " + max + ".</strong>");
	        return false;
	    }
	    return true;
	}

	function initInput(input){
		if (input.value == null || input.value.length == 0)
			input.value = 0; 
		input.value = input.value.replace(',','.');
	}

	function isFormValid(form) {
		$(".formBlock strong").remove();
		initInput(form.FV);
		initInput(form.IR);
		initInput(form.PMT);
		initInput(form.PV);
		initInput(form.NP);
		a = checkNumber(form.FV, -100000000, 100000000, "La valeur &agrave; &eacute;ch&eacute;ance");
		b = checkNumber(form.IR, 0.01, 99, "Le taux d'int&eacute;r&ecirc;t annuel");
		c = checkNumber(form.PMT, 0, 10000000, "L'&eacute;pargne mensuelle");
		d = checkNumber(form.PV, 0, 99999999, "Le d&eacute;pot initial");
		e = checkNumber(form.NP, 0, 99, "La dur&eacute;e de l'investissement");
		return a && b && c && d && e;
	}

	function round_decimals(number, decimals) {
		var a = number * Math.pow(10, decimals)
		var b = Math.round(a)
		var c = b / Math.pow(10, decimals)
		return (c)
	}

	function futureValue(PMT, IR, NP, PV) {
	  var FV = (-(-PV-PMT*((1-Math.pow(1+(IR/100/12),-(NP*12)))/(IR/100/12)))/(Math.pow(1+(IR/100/12),-(NP*12))));
	  return round_decimals(FV, 2)
	}

	function calculateFutureValue(form) {    
	    form.FV.value = 0;
	    if ( !isFormValid(form) )
			return;
		var PMT = parseFloat(form.PMT.value);
		var IR = parseFloat(form.IR.value);
		var NP = parseFloat(form.NP.value);
		var PV = parseFloat(form.PV.value);
	    form.FV.value = futureValue(PMT, IR, NP, PV);
	}

	function calculateRequiredDeposits(form) {
		form.PMT.value = 0;
	    if ( !isFormValid(form) )
	        return;
		var FV = parseFloat(form.FV.value);
		var IR = parseFloat(form.IR.value);
		var NP = parseFloat(form.NP.value);
		var PV = parseFloat(form.PV.value);
		var PMT = (FV - futureValue(0, IR, NP, PV)) * (IR/100/12) / (Math.pow(1 + (IR/100/12), (NP*12)) - 1);
		form.PMT.value = round_decimals(PMT, 2);
	}

	function calculateRequiredDuration(form) {
		form.NP.value = 0;
		if ( !isFormValid(form) )
			return;
		var PMT = parseFloat(form.PMT.value);
		var FV = parseFloat(form.FV.value);
		var IR = parseFloat(form.IR.value);
		var PV = parseFloat(form.PV.value);
		var NP = Math.ceil(((Math.log((PMT-((0-FV)*(IR/100/12)))/((PV*(IR/100/12))+PMT)))/(Math.log(1+(IR/100/12))))/12);
		form.NP.value = round_decimals(NP, 0);
	}

	function calculateRequiredDurationInFine(form) {
		initInput(form.M);
		if (checkNumber(form.M, -100000000, 100000000, "La valeur du bien")){
			form.PV.value = parseFloat(form.M.value) * 0.20;
			form.FV.value = form.M.value;
			if ( !isFormValid(form) )
				return;
			calculateRequiredDuration(form);
		}
	}

	$(".formBlock form").toggle();
	$(".formBlock h3").click(function(){
		$(this).next("form").toggle();
	});
	$("#capital2Term input").keyup(function(){
		calculateFutureValue(this.form);
	});
	$("#paymentsTime input").keyup(function(){
		calculateRequiredDuration(this.form);
	});
	$("#paymentsAmount input").keyup(function(){
		calculateRequiredDeposits(this.form);
	});
	$("#paymentsTimeInFine input").keyup(function(){
		calculateRequiredDurationInFine(this.form);
	});
});
