var cbFam;
var newWin = null;

// Declaring valid date character, minimum year and maximum year
var dtCh= "/";
var minYear=2000;
var maxYear=2050;

function isInteger(s)
{
	var i;
    for (i = 0; i < s.length; i++)
    {
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag)
{
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year)
{
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) 
{
	for (var i = 1; i <= n; i++) 
	{
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function getDate(strDate){	  
	day = strDate.substring(0,2);
	month = strDate.substring(3,5);
	year = strDate.substring(6,10);
	d = new Date();
	d.setDate(day);
	d.setMonth(month);
	d.setFullYear(year); 
	return d;  
}
 //Retourne:
 //   0 si date_1=date_2
 //   1 si date_1>date_2
 //  -1 si date_1<date_2	  
 function compare(date_1, date_2){
   diff = date_1.getTime()-date_2.getTime();
   return (diff==0?diff:diff/Math.abs(diff));
 }

function isDate(dtStr)
{
	if( dtStr == '' ) return true;
	
	var daysInMonth = DaysArray(12);
	var pos1=dtStr.indexOf(dtCh);
	var pos2=dtStr.indexOf(dtCh,pos1+1);
	var strDay=dtStr.substring(0,pos1);
	var strMonth=dtStr.substring(pos1+1,pos2);
	var strYear=dtStr.substring(pos2+1);
	strYr=strYear;
	
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1);
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1);
	
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1);
	}
	
	month=parseInt(strMonth);
	day=parseInt(strDay);
	year=parseInt(strYr);
	if (pos1==-1 || pos2==-1) return false;
	if (strMonth.length<1 || month<1 || month>12) return false;
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]) return false;
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear) return false;
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false) return false;
	
	return true;
}

function closeWin(){
	if (newWin != null){
	if(!newWin.closed)
	newWin.close();
	}
}
function popUp(strURL,strType,strHeight,strWidth) {
	closeWin();
	var strOptions="";
	var top=(screen.height-strHeight)/2;
	var left=(screen.width-strWidth)/2;
	if (strType=="console") strOptions="resizable,height="+strHeight+",width="+strWidth+",top="+top+",left="+left;
	if (strType=="fixed") strOptions="status,height="+strHeight+",width="+strWidth+",top="+top+",left="+left;
	if (strType=="elastic") strOptions="toolbar,menubar,scrollbars,resizable,location, height="+strHeight+",width="+strWidth+",top="+top+",left="+left;
	newWin = window.open(strURL, 'newWin', strOptions);
	newWin.focus();
}

function FBT_classTR(ele) {
	// get ele name & find all elements w/ that name
	cbFam = ele.name;
	//alert('cbFam => '+cbFam);
	var cbl = xGetElementsByAttribute('INPUT', 'type', 'radio', FBT_noClassFam);
	if (ele.checked) ele.parentNode.parentNode.className = 'alt';
	/*else ele.parentNode.parentNode.className = '';*/
}

function FBT_noClassFam (cb) {
	if (cb.name == cbFam) cb.parentNode.parentNode.className = '';
}

// xGetElementsByAttribute, Copyright 2002-2007 Michael Foster (Cross-Browser.com)
// Part of X, a Cross-Browser Javascript Library, Distributed under the terms of the GNU LGPL

function xGetElementsByAttribute(sTag, sAtt, sRE, fn)
{
  var a, list, found = new Array(), re = new RegExp(sRE, 'i');
  list = xGetElementsByTagName(sTag);
  for (var i = 0; i < list.length; ++i) {
    a = list[i].getAttribute(sAtt);
    if (!a) {a = list[i][sAtt];}
    if (typeof(a)=='string' && a.search(re) != -1) {
      found[found.length] = list[i];
      if (fn) fn(list[i]);
    }
  }
  return found;
}

// xGetElementsByTagName, Copyright 2002-2007 Michael Foster (Cross-Browser.com)
// Part of X, a Cross-Browser Javascript Library, Distributed under the terms of the GNU LGPL

function xGetElementsByTagName(t,p)
{
  var list = null;
  t = t || '*';
  p = p || document;
  if (typeof p.getElementsByTagName != 'undefined') { // DOM1
    list = p.getElementsByTagName(t);
    if (t=='*' && (!list || !list.length)) list = p.all; // IE5 '*' bug
  }
  else { // IE4 object model
    if (t=='*') list = p.all;
    else if (p.all && p.all.tags) list = p.all.tags(t);
  }
  return list || new Array();
}
/*MS
permet de se deconnecter , fermeture de session
*/
function deconnexion(langueencours)
{
	document.location.href = '../index.php?lang='+langueencours+'&_act=exit';
}

/*MS
Permet de valider formulaire d'identification
*/
function valide_form_identification(formulaire,txt_msg)
{
	if (suppr_espace_inutile(formulaire.email.value) == '' || suppr_espace_inutile(formulaire.password.value) == '') 
	{
		alert(txt_msg.toString());
		formulaire.email.focus();
		return false;
	}
	
	return true;
}

/*MS
Permet de tester si un email a été saisi dans le formulaire d'identification pour envoi de mot de passe oublié
*/
function verif_email_form_identification(formulaire,txt_msg)
{
	if (suppr_espace_inutile(formulaire.email.value) == '' ) 
	{
		alert(txt_msg.toString());
		formulaire.email.focus();
		return false;
	}
	formulaire.motpasseoublie.value = 'ok'; 
	formulaire.submit();
	return true;
}
/** MS
*Suppression des espaces de début et de fin et globalement en trop (espaces multiples)
*@param  chaine 
* @return chaine traité
*/
function suppr_espace_inutile(chaine)
{
	return chaine.replace(/(^\s*)|(\s*$)/g,'');
}

/** MS
*Permet de recharger la pageune page en fonction de la langue selectionnée dans une liste box 
*@param  objet : objet html "select"
*@param  url_base : url de base fixe exemple 'index.php?lang='
* @return aucun
*/
function ChangeUrl(objet,url_base)
{
   location.href = url_base + objet.options[objet.selectedIndex].value;
} 

/** Ms 
* Permet de forcer un POST sur un formulaire
*/
function PostFormulaire(myform,nomselect)
{
   myform._fromselect.value = nomselect;
   myform.submit();
}

//permet le dialogue entre 2 fenetres mere/fille (exemple page/popup)
function TransfertFilleMere(champ_mere,champ_fille,champ_mere_option1,champ_mere_option2)
{
 var elmtWinParent=window.opener.document.getElementById(champ_mere);
 var elmtWinFille=window.document.getElementById(champ_fille);
//  alert(window.opener.document.forms[0]);
 var elmtWinParentChampOption;
 
 if (champ_mere_option1 != null)
 {
 	 elmtWinParentChampOption=window.opener.document.getElementById(champ_mere_option1);
 	
 	 if (elmtWinFille.value != 0)
	 	 elmtWinParentChampOption.value=elmtWinFille.value;
 }
 
 if (champ_mere_option2 != null)
 {
 	 elmtWinParentChampOption=window.opener.document.getElementById(champ_mere_option2);
 	
 	 if (elmtWinFille.value != 0)
 	 {
		 var brokenstring=elmtWinFille.options[elmtWinFille.selectedIndex].text.split(' ['); //permettra de ne garder que le nom de la commune sans le code postal
	 	 elmtWinParentChampOption.value= brokenstring[0] ;
	 }
 }

 if (elmtWinFille.value != 0)
 {
	 var brokenstring=elmtWinFille.options[elmtWinFille.selectedIndex].text.split(' ['); //permettra de ne garder que le nom de la commune sans le code postal
	 elmtWinParent.value= brokenstring[0] ;
 }

 window.opener.document.forms[0].submit();
 
 window.close();
}

//permet le dialogue entre 2 fenetres mere/fille (exemple page/popup)
function TransfertFilleMerePlus(msg_mercisaisirville)
{
 //recuperer la region
 var elmtWinParent1=window.opener.document.getElementById('region_id');
 var elmtWinFille1=window.document.getElementById('Regions');
 
 if (elmtWinFille1.value != 0)
 {
	 elmtWinParent1.value = elmtWinFille1.value ;
 }
 
 //recuperer le departement
 var elmtWinParent2=window.opener.document.getElementById('departement_id');
 var elmtWinFille2=window.document.getElementById('Departements');
 
 if (elmtWinFille2.value != 0)
 {
	 elmtWinParent2.value = elmtWinFille2.value ;
 }
 
 //recuperer l'arrondissement
 var elmtWinParent3=window.opener.document.getElementById('arrondissement_id');
 var elmtWinFille3=window.document.getElementById('ArrondissementsDistricts');
 
 if (elmtWinFille3.value != 0)
 {
	 elmtWinParent3.value = elmtWinFille3.value ;
 }
 
 //recuperer la commune = ville
 var elmtWinParent4=window.opener.document.getElementById('commune_id');
 var elmtWinFille4=window.document.getElementById('ListeCommunes');
 
 if (elmtWinFille4.value != 0)
 {
	 elmtWinParent4.value = elmtWinFille4.value ;
 }
 else
 {
 	alert(msg_mercisaisirville.toString());
 	return false;
 }

 window.opener.document.forms[0].submit();
 
 window.close();
}

/**lorsqu'on fait une saisie libre sur la ville lors de la création d'article
*
*/
/*function saisielibre(myform)
{
	if (myform.Communes_save.value != myform.Communes.value) //on a tenté de modifier la ville (=> saisie libre)
	{
		myform.Communes_id.value = '0'; //on ne prendra en compte dans ce cas que le pays et la ville car on suppose qu'on est en saisie libre
		myform.ArrondissementsVilles.value = '0'; 
	}
	
}
*/

/**lorsqu'on fait une saisie libre sur la ville lors de la création d'article
*
*/
function saisielibre(myform)
{
	myform.Communes_id.value = '0'; //on ne prendra en compte dans ce cas que le pays et la ville car on suppose qu'on est en saisie libre
	
	if (myform.Regions != null)
		myform.Regions.value = '0'; 
		
	if (myform.Departements != null)
		myform.Departements.value = '0'; 
		
	if (myform.ArrondissementsDistricts != null)
		myform.ArrondissementsDistricts.value = '0';
		 
	if (myform.ArrondissementsVilles != null)
		myform.ArrondissementsVilles.value = '0'; 
}

/**lorsqu'on fait une saisie libre sur la ville lors de la création d'article
*
*/
function saisielibre_adresse(myform)
{
	myform.commune_id.value = '0'; //on ne prendra en compte dans ce cas que le pays et la ville car on suppose qu'on est en saisie libre
	
	if (myform.region_id != null)
		myform.region_id.value = '0'; 
		
	if (myform.departement_id != null)
		myform.departement_id.value = '0'; 
		
	if (myform.arrondissement_id != null)
		myform.arrondissement_id.value = '0';
}

/** MS :  fonction pour le foliotage
*
*/
/*function changePage(debut)
{
	aform=window.document.FormRecherche;
	aform._pg.value=debut;	
	aform.submit();		
}
*/


/* Redirection pour la pagination */
function changePage(url,numpage) {
	window.top.location.href = url+'&_pg='+numpage;
}

function ouvcompte(msg)
{
	if (confirm(msg.toString()))
	{
		javascript:popUp('../pages/ouverturecompte.php','console',300,400);
	}
	return false;
}
/*permet de selectionner/deselectionner automatiquement un checkbox lié à un champ de saisie
* @param : id : id du champ de saisie
* @param id1 : id du checkbox
*/
function selection_deselection_checkbox(id,id1) {
 
 if( document.getElementById(id) ) 
{
 if (suppr_espace_inutile(document.getElementById(id).value) != "")
      document.getElementById(id1).checked=true;
}
}

/*
Permet de fermer la popup d'ouverture de compte et de raffraichir la liste des articles
@param : nb = nombre de compte ouvert
*/
function fermerrafraichirlistearticle(nb)
{
	if (nb > 0)
		window.opener.location.reload(); //on ne rafraichit que si au moins un compte a été ouvert 
		
	window.close();
}

/*
permet de récupérer le support sélectionné lorsqu'on provient d'une inscription anonyme
*/
function url_authentification(formulaire,langueencours)
{
	formulaire.action = '../index.php?from=registration&lang='+langueencours;
	formulaire.submit();
}

function EnregistrerEtFiltrer(page_filtre)
{
	var form = document.getElementById('form_export');
	form.action = page_filtre;
	form.submit();
	
	return true;
}










