/*
 *      general.js
 *      
 *      Auteur : Marc FREREBEAU <marc.frerebeau@agama.fr>
 *      Société : AGAMA
 * 
 *      Licence : GPL
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *      
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 *      
 *      
 *      Application : Monplannnig
 *      Version : 2.0 
 *      
 *      Description :
 *      	Librairie de fonctions générales 
 *      Nécessite : 
 *      	prototype.js
 *      Date : 04/03/2010
 *      Version : 0.1
 */

function afficher_masquer(thingId)
{
//alert( 'afficher_masquer' );
	Element.toggle(thingId);
}

function trim (myString)
{
return myString.replace(/^\s+/g,'').replace(/\s+$/g,'')
} 

//////////////////////////////////////////////////////////////////////
// Fonction afficher_div_du_groupe
//
// Description :
//		Ferme toutes les DIV portant une chaine discriminante en début 
//		d'identifiant et affiche seulement celle demandée
// Entrées :
//		@nom_detail (string) : nom du layer de détail à afficher
//		@discriminant (string) : chaine discriminant les div concernées
// Sorties :
//		@Néant
// Valeur de retour :
//		*Néant
// Auteurs : Marc FREREBEAU
// Date dernière modification : 30/03/2011
// Commentaires :
//		
//////////////////////////////////////////////////////////////////////
function afficher_div_du_groupe( nom_detail, discriminant )
{
	var liste_details = document.getElementsByTagName( 'DIV' );

	for( var i = 0 ; i < liste_details.length ; i++ )
	{
		if( liste_details[i].id.substring( 0, discriminant.length ) == discriminant )
			Element.hide( liste_details[i].id );
	}
	if( $( nom_detail ) )
		Element.show( nom_detail );
}

//////////////////////////////////////////////////////////////////////
// Fonction trim
//
// Description :
//		retourne la chaîne chaine, après avoir supprimé les caractères 
//		invisibles en début et fin de chaîne.
//  * " " (ASCII 32  (0x20)), un espace ordinaire.
//  * "\t" (ASCII 9 (0x09)), une tabulation.
//  * "\n" (ASCII 10 (0x0A)), une nouvelle ligne (line feed).
//  * "\r" (ASCII 13 (0x0D)), un retour chariot (carriage return).
//  * "\0" (ASCII 0 (0x00)), le caractère NUL.
//  * "\x0B" (ASCII 11 (0x0B)), une tabulation verticale.
// Entrées :
//		@chaine (string) : chaine de cractère
// Sorties :
//		@Néant
// Valeur de retour :
//		* (string) : La chaîne de caractères coupée. 
// Auteurs : Marc FREREBEAU
// Date dernière modification : 11/05/2010
// Commentaires :
//		
//////////////////////////////////////////////////////////////////////
function trim( chaine )
{
	chaine = chaine.replace( /[ \t\n\r\0\x0B]+$/g, '' ); 
	return chaine.replace( /^[ \t\n\r\0\x0B]+/g, '' ); 
}




// Division function, to be precise division results
// Note: javascript error will be the result of the division, two floating-point division at a time when it would be more obvious. This function return the results of a more precise division.
// Source : http://www.codeweblog.com/javascript-to-avoid-the-floating-point-bug-ways-reprint-provenance-unknown/
// Call: accDiv (arg1, arg2)
// Return value: arg1 divided by arg2 precise results
function accDiv (arg1, arg2) {
var t1 = 0, t2 = 0, r1, r2;
try {t1 = arg1.toString (). split (".")[ 1]. length} catch (e) {}
try {t2 = arg2.toString (). split (".")[ 1]. length} catch (e) {}
with (Math) {
r1 = Number (arg1.toString (). replace (".",""))
r2 = Number (arg2.toString (). replace (".",""))
return (r1/r2) * pow (10, t2-t1);
}
}

// Number type give way to add a div, call up more convenient.
Number.prototype.div = function (arg) {
return accDiv (this, arg);
}

// Multiplication function, to be precise multiplication results
// Note: javascript error multiplication results will be, at a time when two floating point multiply more obvious. This function return the results of more precise multiplication.
// Call: accMul (arg1, arg2)
// Return value: arg1 multiplied by arg2 precise results
function accMul (arg1, arg2)
{
var m = 0, s1 = arg1.toString (), s2 = arg2.toString ();
try {m += s1.split (".")[ 1]. length} catch (e) {}
try {m += s2.split (".")[ 1]. length} catch (e) {}
return Number (s1.replace (".",""))* Number (s2.replace (".",""))/ Math.pow (10, m)
}

// Give Number type mul add a method call is more convenient.
Number.prototype.mul = function (arg) {
return accMul (arg, this);
}

// Adder functions, to be precise adder results
// Note: javascript error will be the result of the addition, at a time when the sum of two floating-point numbers will be more obvious. This function return more precise results adder.
// Call: accAdd (arg1, arg2)
// Return value: arg1 coupled with the accurate results arg2
function accAdd (arg1, arg2) {
var r1, r2, m;
try {r1 = arg1.toString (). split (".")[ 1]. length} catch (e) {r1 = 0}
try {r2 = arg2.toString (). split (".")[ 1]. length} catch (e) {r2 = 0}
m = Math.pow (10, Math.max (r1, r2))
return (arg1 * m + arg2 * m) / m
}

// Give Number type add to add a way to call up more convenient.
Number.prototype.add = function (arg) {
return accAdd (arg, this);
}

