/**
 * Utilitaires javascript
 * 	permet de centraliser l'ensemble des test js de base
 * 
 * @author arnaud
 * $Id: Exp $
 * MAJ le 16/12/2003 ajout de checkChampDate
 */

/**
 * Vérification que le champ passé en param est remplie
 * @param nomChamp
 * @param texte le msg affiché 
 * @param focus si vrai on fait le focus sur le champ
 */
	function checkChamp(nomChamp , texte, focus){
		ret= false;
		champ = document.getElementById(nomChamp);
		if (champ){
				if (!isEmpty(champ)){
					//test pour HTMLArea
					if(Trim(champ.value)!="<P>&nbsp;</P>") {
						ret = true;
					}else{
						alert(texte);
						if (focus)		
							champ.focus(); // on fait pas le focus car certains sont caché avec les div
					} 
				}
				else{
					alert(texte);
					if (focus)		
						champ.focus(); // on fait pas le focus car certains sont caché avec les div
				  }
			}	
		return ret;
	}

/**
 *  Vérification du format de la date
 *  Retourne si la date saisi répond au format jj/mm/aaaa
 *  @param nomChamp
 *  @param texte le msg affiché (le libelle du champ)
 *  @author : arnaud
 */
	function checkChampDate(nomChamp , texte){
		ret = true;
		if(isEmpty(document.getElementById(nomChamp))){
			alert("Vous devez renseigner le champ " + texte + ".");
			ret = false;
		}else
			if(!isDate(document.getElementById(nomChamp).value,"dd/MM/yyyy")){
				alert("Le champ " + texte + " doit être au format 'jj/mm/aaaa'.");
				ret = false;
			}
		return ret; 
	 }



/**
* Despécialisation de certains caractères dans les chaines  
*/
function addslashes(ch) {
   ch = ch.replace(/\\/g,"\\\\") 
   ch = ch.replace(/\'/g,"\\'") 
   ch = ch.replace(/\"/g,"\\\"")
   return ch
}

function isEmpty(champs)
{
	var ch = champs;
	txt = Trim(ch.value);
	if(txt.length == 0)
	{
		return true;
	}
	else
	{
		return false;
	}
}

function CaracMax(texte, max)
{
	if (texte.value.length >= max)
	{
		alert('Pas plus de ' + max + ' caractère(s) !!!') ;
		texte.value = texte.value.substr(0, max - 1) ;
	}
}

function openWindow(w_url, w_name, w_width, w_height)
{
	//fen_child=window.open(w_url, w_name,'resizable=no,toolbar=no,directories=no,status=no,menubar=no,width='+w_width+',height='+w_height);
	fen_child=window.open(w_url, w_name,'resizable=yes,toolbar=yes, scrollbars=yes,directories=yes,status=yes,menubar=yes,width='+w_width+',height='+w_height);
	fen_child.focus();
}

function openWindowWithScroll(w_url, w_name, w_width, w_height)
{
	fen_child=window.open(w_url, w_name,'resizable=no,toolbar=no,scrollbars=yes,directories=no,status=no,menubar=no,width='+w_width+',height='+w_height);
	fen_child.focus();
}


		
function LTrim(str)
{
  var whitespace = new String(" \t\n\r");

  var s = new String(str);

  if (whitespace.indexOf(s.charAt(0)) != -1) {
    // We have a string with leading blank(s)...

    var j=0, i = s.length;

    // Iterate from the far left of string until we
    // don't have any more whitespace...
    while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
    j++;


    // Get the substring from the first non-whitespace
    // character to the end of the string...
    s = s.substring(j, i);
  }

  return s;
}

// --------------------------------------------------------------------------
// Remove trailing blanks from our string.

// I               str - the string we want to RTrim
// Return          the input string without any trailing whitespace

// Date            03 avr. 2003 23:13:50
// Author          Aurélien Tisné	(CS)
// --------------------------------------------------------------------------
function RTrim(str)
{
  // We don't want to trip JUST spaces, but also tabs,
  // line feeds, etc.  Add anything else you want to
  // "trim" here in Whitespace
  var whitespace = new String(" \t\n\r");

  var s = new String(str);

  if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
    // We have a string with trailing blank(s)...

    var i = s.length - 1;       // Get length of string

    // Iterate from the far right of string until we
    // don't have any more whitespace...
    while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
      i--;


    // Get the substring from the front of the string to
    // where the last non-whitespace character is...
    s = s.substring(0, i+1);
  }

  return s;
}


// --------------------------------------------------------------------------
// Remove trailing and leading blanks from our string.

// I               str - the string we want to Trim
// Return          the trimmed input string

// Date            03 avr. 2003 23:15:09
// Author          Aurélien Tisné	(CS)
// --------------------------------------------------------------------------
function Trim(str)
{
  return RTrim(LTrim(str));
}
		