// JavaScript Document

	$(function(){
//		  $('#Dettes .menu-box ul li.second, #Aide-Endettement .menu-box ul li.second').remove();
//		  $('#Dettes .menu-box ul, #Aide-Endettement .menu-box ul').prepend('<li class="first" style="margin-bottom: 5px;margin-top: 35px;"><span class="icon">play</span><a href="Endettement.html"><strong class="title">solutions </strong> à l\'endettement</a></li>');
//		  
//		  
//		  $('#Aide-Endettement .menu-box ul li.third').remove();
//		  $('#Aide-Endettement .menu-box ul').append('<li class="second"  style="margin-top: 0px;"> <span class="icon">play</span> <a href="Dettes.html"> <strong class="title">mythes</strong> de l\'endettement </a> </li>');
		  
	
  $('.grass').html("<iframe src=\"http://www.facebook.com/plugins/like.php?href=" + encodeURIComponent(document.URL) +"&layout=button_count&show_faces=false&width=175&action=like&locale=fr_FR&font=arial&colorscheme=light&height=20\" scrolling=\"no\" frameborder=\"0\" style=\" background-color:transparent; border-color: initial; float: left; overflow-x: hidden; overflow-y: hidden; border-top-style: none; border-right-style: none; border-bottom-style:none; border-left-style : none; border-width: initial; width: 100px; height: 20px; \" allowtransparency=\"true\"/>");
 
	  
		  $('.innerPage .menu-box ul').append(myButton[randomnumber]);
 		
		  $('form#RESULTAT').submit(function() {
			  
			  isError = false
			  TheErrorMessage = '<span>Vérifiez le(s) champ(s) :'
			  TheDepenseTotal = 0
			  TheRevenuTotal = 0
			  $("form#RESULTAT input[type=text]").removeClass('error'); 
	
			 $('form#DEPENSES input[type=text]').each( function(){ 
			         ThisValue = $(this).val();
					 
			         if (isNumeric(ThisValue)){ 
					      TheDepenseTotal = TheDepenseTotal + parseInt(ThisValue) 
					 } else {
						 TheErrorMessage = TheErrorMessage + '<br/>' + removeSpecCarr('&#8226; ' + $(this).attr('ID'));
						 $(this).addClass('error');
					 	  isError = true
						 }
		     });
			 
			 $('form#REVENUS input[type=text]').each( function(){ 
			         ThisValue = $(this).val()
			         if (isNumeric(ThisValue)){ 
					      TheRevenuTotal = TheRevenuTotal + parseInt(ThisValue) 
					 } else {
						 TheErrorMessage = TheErrorMessage + '<br/>' + removeSpecCarr('&#8226; ' + $(this).attr('ID'));
						 $(this).addClass('error');
						// $(this).val('0')
						  isError = true
						 }
		     });
			 
			  TheErrorMessage = TheErrorMessage + '</span>' 
			 
		    if (isError) {
				    $('#taux_resultat').html(TheErrorMessage);
				} else {
					Big_DepenseTotal =  parseInt(TheDepenseTotal*100);
				   $('#taux_resultat').html('<br/><strong> Votre ratio d\'endettement est de <span>' + parseInt(Big_DepenseTotal/TheRevenuTotal) + '%<\/span><\/strong>');
				     }
			
			
			
			return false;
		  });		
		  
		  

		  $('form#autoEvaluation').submit(function() {
			  
			$("form#autoEvaluation div").hide('fast');
			arr = $("form#autoEvaluation input[type='radio']");
			arrChecked = $("form#autoEvaluation input[type='radio']:checked");
			
			var TotalQuestion = (arrChecked.length)
			var Question = (arr.length/2)
			var vrai=0;
			var faux=0;
						
			   $.each(arrChecked, function(value) {
				 if ($(this).val() == 'vrai') { vrai++ } else { faux++ }  
			   });
			   
			 if ( vrai + faux < Question ) { 
				 $('.erreur').html('Vous devez répondre à toutes les questions!');
			     return false ;
				} else {
					
						$('.erreur').html('');
						   if(vrai > 9){
							  $("#resultat10").show('slow');
						  } else if(vrai > 3){
							  $("#resultat4").show('slow');
						  } else if (vrai > 0){
							  $("#resultat1").show('slow');
						  } else {
							  $("#resultat0").show('slow');
						  } 
						
			
		              } 
			
			return false;
		  });		
	  
	 });	
	 
var randomnumber = Math.floor(Math.random()*2)
var myButton = new Array();
    myButton[0]= '<li class="second"> <span class="icon">play</span> <a href="Dettes.html"> <strong class="title">mythes</strong> de l\'endettement </a> </li>'; 
    myButton[1]= '<li class="third"> <span class="icon">play</span> <a href="Aide-Endettement.html"> <strong class="title">conseils</strong> <span class="mark">nos</span> </a> </li>';
	
function removeSpecCarr(string) {
 return string.split('_').join(' ').toUpperCase();
 //return 'hello';
}	
	// Ajustement des champs monétaires
function adjustNumeric(num, allowNegative, allowDecimal, maxDecimal, maxUnit) {
	if (num === '' || num === undefined || num === null) return '';

	// On force le type en string.
	str = num.toString();

	// On prend note si le nombre est negatif
	neg = 0;
	if (str.charAt(0) === '-') {
		neg = 1;
	}

	// On strip toutes les chars AUTRES que les chiffres, le point et la virgule. 
	str2 = str;
	str = '';
	for (k = 0; k < str2.length; k++) {
		if (
			((str2.charAt(k) === ',' || str2.charAt(k) === '.') && allowDecimal)
			|| str2.charAt(k) === '0' || str2.charAt(k) === '1'
			|| str2.charAt(k) === '2' || str2.charAt(k) === '3'
			|| str2.charAt(k) === '4' || str2.charAt(k) === '5'
			|| str2.charAt(k) === '6' || str2.charAt(k) === '7'
			|| str2.charAt(k) === '8' || str2.charAt(k) === '9'
		) {
			str = str + str2.charAt(k);
		}
	}
	
	// Si, apres la transformation, la string ne contient plus de chars, alors inutile de continuer plus loin.
	if (str.length == 0) {
		return('');
	}

	// Si on permet les decimales, on supprime toutes les points et virgules que contient la string SAUF le/la dernier(e).
	if (allowDecimal) {
		str2 = str;
		str = '';
		sep = false;
		for (j = str2.length-1; j >= 0; j--) {
			if (str2.charAt(j) === ',' || str2.charAt(j) === '.') {
				if (!sep) {
					str = str2.charAt(j) + str;
				}
				sep = true;
			} else {
				str = str2.charAt(j) + str;
			}
		}
	}
	
	// On change le point pour une virgule
	str2 = str;
	str = '';
	for (k = 0; k < str2.length; k++) {
		if (str2.charAt(k) === ',') {
			str = str + '.';
		} else {
			str = str + str2.charAt(k);
		}
	}

	// Si, apres la transformation, la string ne contient plus de chars, alors inutile de continuer plus loin.
	if (str.length == 0) {
		return('');
	}

	// On ajuste le nombre de décimales authorisées selon maxDecimal
	if (allowDecimal) {
		a = str.split('.');
		x = a[0];
		y = a[1];
		z = '';
		if (y !== undefined) {
			if (y.length < maxDecimal) {
				diff = 1 * maxDecimal - y.length;
				fill = '';
				for (var d = 1; d <= diff; d++) {
					fill = fill + '0';
				}
				z = str + fill;
			} else {
				if (y.length > maxDecimal) {
					z = x + '.' + y.substr(0, maxDecimal);
				}
			}
		
		// S'il n'y avait pas de décimales, on les ajoute selon maxDecimal
		} else {
			fill = '';
			for (var d = 1; d <= maxDecimal; d++) {
				fill = fill + '0';
			}
			z = str + '.' + fill;
		}
		if (z.length > 0) {
			str = z;
		}
	}

	// On ajuste le nombre d'unités authorisées selon maxUnit
	if (maxUnit > 0) {
		a = str.split('.');
		x = a[0];
		y = a[1];
		z = '';
		if (x !== undefined && x.length > maxUnit) {
			z = x.substr(0, maxUnit) + '.' + y;
		}
		if (z.length > 0) {
			str = z;
		}
	}

	// On sépare les milles par une espace
	a = str.split('.');
	if (typeof(a[1]) === 'undefined') {
		a = str.split('.');
	}

	x = a[0];
	y = a[1]; 
	z = '';
	
	if (typeof(x) != 'undefined') {
		for (i = x.length-1; i >= 0; i--) {
			if (x.charAt(i) != '.' && x.charAt(i) != ' ') {
				z += x.charAt(i);
			}
		}

		x = '';

		for (i = z.length-1; i >= 0; i--) {
			if (i%3 == 0) {
	 			x += z.charAt(i) + ' ';
	 		} else {
				x += z.charAt(i);
			}
		}

		if (typeof(y) == 'undefined') { 
			y = '00';
		}

		if (y.length == 1) {
			y = y + '0';
		}

		if (allowDecimal) { 
			x = x.slice(0, (x.length-1)) + ',' + y;
		} else {
			x = x.slice(0, (x.length-1));
		}

		str = x;
	}

	// On trim les zéros et espaces inutiles à gauche
	tocut = 0;
	for (k = 0; k < str.length; k++) {
		if (str.charAt(k+1) != ',' && (str.charAt(k) === '0' || str.charAt(k) === ' ')) {
			tocut++;
		} else {
			break;
		}
	}
	if (tocut > 0) {
		str = str.substr(tocut, 1*str.length-tocut);
	}

	// Si le nombre était negatif et qu'on permet qu'il le soit, on lui remet son signe
	if (neg && allowNegative) {
		str = '-' + str;
	}

 	return str;
}

function string2Float(inputString) {
	if (inputString === '' || inputString === undefined || inputString === null) return 0;
	inputString = sanitizeString(inputString)
	newFloat = parseFloat(inputString);
	if (!isNumeric(newFloat) || isNaN(newFloat)) {
		newFloat = 0;
	}
	return newFloat;
}

function sanitizeString(inputString) {
	if (inputString === '' || inputString === undefined || inputString === null) return '';
	inputString = inputString.replace(/\s+/g, '');
	inputString = inputString.replace(',', '.');
	return inputString;
}

function isNumeric(strString) {
	if (strString === '' || strString === undefined || strString === null) return false;
	var strValidChars = "-0123456789., ";
	var strChar;
	var blnResult = true;
	if (strString.length == 0) {
		return true;
	}
	for (i = 0; i < strString.length && blnResult == true; i++) {
		strChar = strString.charAt(i);
		if (strValidChars.indexOf(strChar) == -1) {
			blnResult = false;
		}
	}
	return blnResult;
}


function validate(field,idfield,idvalue) {
	
   var valid = "0123456789"
   var ok = "yes";
   var temp;
   
	for (var i=0; i<field.value.length; i++) {
	  temp = "" + field.value.substring(i, i+1);
	  if (valid.indexOf(temp) == "-1") ok = "no";
	}
	
	if (ok == "no") {
	  alert("Veuillez entrer des caractères\nnumériques seulement (0123456789)");
	  field.focus();
	  field.select();
	  document.getElementById(idfield).value = ""
    }
}

 


	 
