/******************************************* Formats *********************************************/

// Si le jour ou le mois n'est composé que de 1 chiffre, on complète automatiquement à 2 chiffres
function format_day_month(textfield_object) {
   
   var txt_jour_mois = textfield_object.value;
   var int_jour_mois = parseInt(txt_jour_mois, 10); // jour ou mois
   
   if (int_jour_mois < 10) {
      txt_jour_mois = '0' + int_jour_mois;
      textfield_object.value = txt_jour_mois;
   }
   
}

function calculduree(d1,d2)
{
    if (d2<d1)
  {
    var msg = "Difference entre " + d1 + " et " + d2 + "";
    msg+= "semaine :"+ ((d2-d1)/604800000).toString().match(/^\d+/)+ "";
    msg+= "ou jour :" + ((d2-d1)/86400000).toString().match(/^\d+/)+ "";
    msg+= "ou heures :" + ((d2-d1)/3600000).toString().match(/^\d+/)+ "";
    msg+= "ou minutes :" + ((d2-d1)/60000).toString().match(/^\d+/)+ "";
    msg+= "ou secondes :" + ((d2-d1)/1000).toString().match(/^\d+/)+ "";
    alert (msg);
  }
  else
    alert("mauvaise entrée de données")
}

// Si l'année n'est composée que de 1 ou 2 chiffres, on complète automatiquement à 4 chiffres
function format_year(textfield_object) {
   
   var txt_annee = textfield_object.value;
   var int_annee = parseInt(txt_annee, 10); // année
   
   if (int_annee < 100) {
      int_annee += 1900;
      txt_annee = '' + int_annee;
      textfield_object.value = int_annee;
   }
   
}


// Si le champ texte à droite de la case à cocher est rempli, on cose la case associée
function format_checkbox_textfield(checkbox_object, textfield_object) {
   
   checkbox_object.checked = (eltform_get_textfield_value(textfield_object) != "");
   
}

// Positionne un champ texte à une certaine valeur
function format_set_value(textfield_name, textfield_value) {
	
   textfield_name.value = textfield_value;
   
}

// Positionne un champ texte à une valeur vide
function format_del_value(textfield_name) {
	
   textfield_name.value = '';
   
}

// Retourne un montant arrondi à deux chiffres après la virgule
function format_euros_cents(textfield_value) {
	
	var textfield_value_format = 0;
	
	textfield_value = textfield_value.toString();
	if ((textfield_value != '') && !isNaN(textfield_value)) {
   	textfield_value_format = Math.round(textfield_value*100)/100;
   	textfield_value_format = textfield_value_format.toDecimals(2);
   }
   
   return textfield_value_format;
   
}

// Retourne un montant arrondi à deux chiffres après la virgule si 1 ou 2 décimales, n'arrondit pas si plus
function format_euros_cents2(textfield_value) {
	
	var textfield_value_format = 0;
	
	textfield_value = textfield_value.toString();
	if ((textfield_value != '') && !isNaN(textfield_value)) {
		
		var tempval = textfield_value.split('.');
		var entpart = tempval[0]; // partie entière
		var dectpart = tempval[1]; // partie décimale
		
		if (dectpart > 99) { // 123.45678
	   	textfield_value_format = textfield_value;
		} else { // 123 ou 123.4 ou 123.45
	   	textfield_value_format = Math.round(textfield_value*100)/100;
	   	textfield_value_format = textfield_value_format.toDecimals(2);
		}
   }
   
   return textfield_value_format;
   
}

// Retourne un montant avec un espace pour séparer les milliers
function format_euros_mills(textfield_value) {
	
	if (isNaN(textfield_value)) {
		// erreur: n'est pas un nombre
		return false;
	}
	
	if (textfield_value == parseInt(textfield_value, 10))  {
		// c'est un entier, à convertir en float
		textfield_value = format_euros_cents(textfield_value);
	}
	textfield_value = textfield_value.toString();
	
	if (isNaN(textfield_value.split(' ').join(''))) {
		// erreur: n'est pas un nombre
		return false;
	}

	var textfield_value_format = new Array();
	var tempval = textfield_value.split('.');
	textfield_value = tempval[0].split(' ').join('');
	textfield_value = textfield_value.split('').reverse() ;
	
	var i = 0;
	while (i<textfield_value.length) {
		textfield_value_format.push((textfield_value[i+2]?textfield_value[i+2]:'') + (textfield_value[i+1]?textfield_value[i+1]:'') + textfield_value[i]);
		i = i + 3;
	}
	
	textfield_value_format = textfield_value_format.reverse().join(' ') + (tempval[1] ? tempval[1].length>0 ? '.' + tempval[1] : '' : '');
	return textfield_value_format;
	
}

// Retourne un montant avec un espace pour séparer les milliers
function format_euros_mills2(textfield_value) {
	
	if (isNaN(textfield_value)) {
		// erreur: n'est pas un nombre
		return false;
	}
	
	if (textfield_value == parseInt(textfield_value, 10))  {
		// c'est un entier, à convertir en float
		textfield_value = format_euros_cents2(textfield_value);
	}
	textfield_value = textfield_value.toString();
	
	if (isNaN(textfield_value.split(' ').join(''))) {
		// erreur: n'est pas un nombre
		return false;
	}

	var textfield_value_format = new Array();
	var tempval = textfield_value.split('.');
	textfield_value = tempval[0].split(' ').join('');
	textfield_value = textfield_value.split('').reverse() ;
	
	var i = 0;
	while (i<textfield_value.length) {
		textfield_value_format.push((textfield_value[i+2]?textfield_value[i+2]:'') + (textfield_value[i+1]?textfield_value[i+1]:'') + textfield_value[i]);
		i = i + 3;
	}
	
	textfield_value_format = textfield_value_format.reverse().join(' ') + (tempval[1] ? tempval[1].length>0 ? '.' + tempval[1] : '' : '');
	return textfield_value_format;
	
}

// Retourne un montant sans espace pour séparer les milliers
function format_euros_number(textfield_value) {
	
	textfield_value_format = textfield_value.replace(/ /, '');
	return textfield_value_format;
	
}

// Retourne un nombre (jour, mois...) sur deux chiffres (digits)
function format_two_digits(textfield_value) {
	
	if (textfield_value == parseInt(textfield_value, 10))  {
		// c'est un entier, à convertir en entier
		// NB: bien indiquer en base 10, pour éviter bug du zéro devant
		textfield_value = parseInt(textfield_value, 10);
	}
	
	if ((textfield_value >= 0) && (textfield_value <= 9)) {
	   textfield_value_format = '0' + textfield_value;
	} else if (textfield_value > 99) { // impossible...
	   textfield_value_format = '';
	} else {
	   textfield_value_format = textfield_value;
	}
	
	return textfield_value_format;
	
}

// Retourne le nom du client de manière à l'afficher tout en majuscule
function format_client_name(name_field) {
	
	element_nom_client = document.getElementById(name_field);
	valeur_nom_client = element_nom_client.value;
	
	if (valeur_nom_client != '') {
		valeur_nom_client = valeur_nom_client.toUpperCase();
		element_nom_client.value = valeur_nom_client;
	}
	
}

/*************************************************************************************************/
