
//alert('optimisation des performances : '+optimisation_performance);
var obj_color="";

function set_couleur(couleur){
	obj_color.value=couleur;
	if(couleur!=0){
		document.all['image_'+obj_color.name].style.backgroundColor='#'+couleur;
		document.all['image_'+obj_color.name].style.backgroundImage='url(image/transparent.gif)';
	}
}
function editer_couleur(obj){
	obj_color=obj;
	change_page('xml_pop_couleur.php?couleur='+obj.value,'','1');
}

function del(){
	return confirm("Etes vous sur de vouloir supprimer ?");
}
function del_art(){
	return confirm("Etes vous sur de vouloir supprimer un article");
}
function del_contrat(){
	return confirm("Etes vous sur de vouloir supprimer un contrat");
}
function arch(){
	return confirm("Etes vous sur de vouloir archiver");
}
function enreg(){
	return confirm("Etes vous sur de vouloir enregistrer les modifications");
}
function del_dossier(){
	return confirm("Ce dossier contient peut-être des e-mails. Confirmez la suppression");
}
function del_compte(){
	return confirm("Vous allez perdre les messages ainsi que les dossiers associés à ce compte. Confirmez la suppression");
}
function del_user(){
	return confirm("Vous allez perdre les taches, rendez-vous et contacts associés à cet utilisateur. Confirmez la suppression");
}
function del_facture(type){
	if(type!=undefined)
	type="de "+type;
	else
	type="";
	return confirm("Etes vous sur de vouloir valider la facture "+type);
}
function del_fact_ligne(){
	return confirm("Etes vous sur de vouloir supprimer la ligne de facture");
}
function del_facture_art_offert(){
	return confirm("Etes vous sur de vouloir supprimer l'article offert de la facture");
}
function del_liste_noire(){
	return confirm("Confirmez la libération de cette adresse email");
}

function setPointer(theRow, theAction, theDefaultColor, thePointerColor, theMarkColor)
{
	var theCells = null;

	// 1. Pointer and mark feature are disabled or the browser can't get the
	//    row -> exits
	if ((thePointerColor == '' && theMarkColor == '')
	|| typeof(theRow.style) == 'undefined') {
		return false;
	}

	// 2. Gets the current row and exits if the browser can't get it
	if (typeof(document.getElementsByTagName) != 'undefined') {
		theCells = theRow.getElementsByTagName('td');
	}
	else if (typeof(theRow.cells) != 'undefined') {
		theCells = theRow.cells;
	}
	else {
		return false;
	}

	// 3. Gets the current color...
	var rowCellsCnt  = theCells.length;
	var domDetect    = null;
	var currentColor = new String('');
	var newColor     = null;
	// 3.1 ... with DOM compatible browsers except Opera that does not return
	//         valid values with "getAttribute"
	if (typeof(window.opera) == 'undefined'
	&& typeof(theCells[0].getAttribute) != 'undefined') {
		currentColor = theCells[0].getAttribute('bgcolor');
		domDetect    = true;
	}
	// 3.2 ... with other browsers
	else {
		currentColor = theCells[0].style.backgroundColor;
		domDetect    = false;
	} // end 3
	if(currentColor == null) currentColor = new String('');
	// 4. Defines the new color
	// 4.1 Current color is the default one
	if (currentColor == ''
	|| currentColor.toLowerCase() == theDefaultColor.toLowerCase()) {
		if (theAction == 'over' && thePointerColor != '') {
			newColor = thePointerColor;
		}
		else if (theAction == 'click' && theMarkColor != '') {
			newColor = theMarkColor;
		}
	}
	// 4.1.2 Current color is the pointer one
	else if (currentColor.toLowerCase() == thePointerColor.toLowerCase()) {
		if (theAction == 'out') {
			newColor = theDefaultColor;
		}
		else if (theAction == 'click' && theMarkColor != '') {
			newColor = theMarkColor;
		}
	}
	// 4.1.3 Current color is the marker one
	else if (currentColor.toLowerCase() == theMarkColor.toLowerCase()) {
		if (theAction == 'click') {
			newColor = (thePointerColor != '')
			? thePointerColor
			: theDefaultColor;
		}
	} // end 4

	// 5. Sets the new color...
	if (newColor) {
		var c = null;
		// 5.1 ... with DOM compatible browsers except Opera
		if (domDetect) {
			for (c = 0; c < rowCellsCnt; c++) {
				theCells[c].setAttribute('bgcolor', newColor, 0);
			} // end for
		}
		// 5.2 ... with other browsers
		else {
			for (c = 0; c < rowCellsCnt; c++) {
				theCells[c].style.backgroundColor = newColor;
			}
		}
	} // end 5

	return true;
}

function setOpacity(obj, opacity) {
	opacity = (opacity == 100)?99.999:opacity;
	// IE/Win
	obj.style.filter = "alpha(opacity:"+opacity+")";
	// Safari<1.2, Konqueror
	obj.style.KHTMLOpacity = opacity/100;
	// Older Mozilla and Firefox
	obj.style.MozOpacity = opacity/100;
	// Safari 1.2, newer Firefox and Mozilla, CSS3
	obj.style.opacity = opacity/100;
}

function fadeIn(objId,start_opacity,end_opacity,full_screen,cache_select,no_fade) {
	if(optimisation_performance=="1")no_fade=1;
	if(cache_select==1)cacher_select();
	if (document.getElementById) {
		obj = document.getElementById(objId);
		if(full_screen==1){
			if (navigator.appName!="Microsoft Internet Explorer") {
				obj.style.height=document.body.clientHeight+"px";
				obj.style.width=document.body.clientWidth+"px";
				//			obj.style.position="absolute";
				//			obj.style.left = "0px";
				//			obj.style.top = "0px";

			} else {
				obj.style.height=document.documentElement.scrollHeight+"px";
				obj.style.width=document.documentElement.scrollWidth+"px";
				//			obj.style.position="absolute";
				//			obj.style.left = "0px";
				//			obj.style.top = "0px";
			}
		}
		obj.style.display="block";
		//obj.style.display="";
		if(no_fade!=1){
			if (start_opacity <= end_opacity) {
				setOpacity(obj, start_opacity);
				start_opacity += 20;
				window.setTimeout("fadeIn('"+objId+"',"+start_opacity+","+end_opacity+")", 5);
			}
		}
		else {
			setOpacity(obj, end_opacity);
		}
	}
}
function fadeOut(objId,start_opacity,end_opacity,aff_select,no_fade) {
	if(objId==undefined || objId=='')objId="div_pop";
	if(optimisation_performance=="1")no_fade=1;
	if (document.getElementById) {
		obj = document.getElementById(objId);
		if(no_fade!=1){
			if (start_opacity >= end_opacity) {
				setOpacity(obj, start_opacity);
				start_opacity -= 20;
				window.setTimeout("fadeOut('"+objId+"',"+start_opacity+","+end_opacity+","+aff_select+")", 5);
			}
		}
		else {
			start_opacity = 0;
			end_opacity = 0;
		}
		if (start_opacity <= end_opacity){obj.style.display="none"; if(aff_select==1)afficher_select();}
	}
}


function cacher_select()
{
	ss = window.document.getElementsByTagName("select");
	for (var i = 0; i < ss.length; i++) {

		ss[i].style.visibility = 'hidden';
	}
	p = window.iframe_contenu;
	if(p!=undefined){
		ss2 = window.iframe_contenu.document.getElementsByTagName("select");

		for (var i = 0; i < ss2.length; i++) {

			ss2[i].style.visibility = 'hidden';
		}
	}
}

function afficher_select()
{
	ss = window.document.getElementsByTagName("select");
	for (var i = 0; i < ss.length; i++) {
		ss[i].style.visibility = 'visible';
	}

	p = window.iframe_contenu;
	if(p!=undefined){
		ss2 = window.iframe_contenu.document.getElementsByTagName("select");

		for (var i = 0; i < ss2.length; i++) {

			ss2[i].style.visibility = 'visible';
		}
	}
}


function loadNewDiv(url,id,no_style,no_fade,no_cacher_select) {

	if(no_cacher_select=="undefined"){
		cacher_select();
	}
	document.body.style.overflow='hidden';
	//document.body.style.cursor='wait';
	document.getElementById(id).innerHTML='<div align="center" style="padding:20px;"><object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="300" height="30"><param name="movie" value="images/loading.swf" /><param name="quality" value="high" /><param name="wmode" value="transparent" /><embed src="image/loading.swf" wmode="transparent" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="300" height="30"></embed></object></div>';
	var xhr_object = null;
	//var position = id;
	if(window.XMLHttpRequest) xhr_object = new XMLHttpRequest();
	else
	if (window.ActiveXObject) xhr_object = new ActiveXObject("Microsoft.XMLHTTP");

	xhr_object.open("POST", url, true);

	xhr_object.onreadystatechange = function(){
		if ( xhr_object.readyState == 4)
		{
			// j'affiche dans la DIV spécifiées le contenu retourné par le fichier
			if(no_style==1){
				document.getElementById(id).className="";
			}
			else {
				document.getElementById(id).className="div_popup";
			}

			//document.body.style.cursor='auto';
			document.getElementById(id).innerHTML = xhr_object.responseText;


		}
	}
	// dans le cas du get
	xhr_object.send(null);
	fadeIn("masque",0,60,1,1,no_fade);
	fadeIn(id,0,100,0,0,no_fade);
}

function reduce_div(id,no_fade){
	fadeOut("masque",60,0,0,no_fade);
	fadeOut(id,100,0,1,no_fade);
	document.body.style.overflow='';

}
function fermer_session(){
	loadNewDiv("fermer_session.php","div_pop_old",1);
}

function affiche_popup(url,url_retour,no_fade,nom_div_popup,nom_iframe_popup,nom_iframe_contenu){
	if(url_retour==undefined)url_retour="";
	if(nom_div_popup==undefined || nom_div_popup=='')nom_div_popup="div_pop";
	if(nom_iframe_popup==undefined || nom_iframe_popup=='')nom_iframe_popup="iframe_popup";
	if(nom_iframe_contenu==undefined || nom_iframe_contenu=='')nom_iframe_contenu="iframe_contenu";
	if(no_fade==undefined)no_fade="";
	eval("document.getElementById(\"bouton_fermer_popup\").onclick=function (){ fermer_popup('"+url_retour+"','"+no_fade+"','"+nom_div_popup+"','"+nom_iframe_popup+"','"+nom_iframe_contenu+"'); };");
	if(no_fade==1){
		fadeIn("masque",60,60,1,1);
		fadeIn("div_pop",100,100,0,0);
	}
	else{
		fadeIn("masque",0,60,1,1);
		fadeIn("div_pop",0,100,0,0);
	}
	if(document.all)window.eval(nom_iframe_popup).set_lien_ajax(url);
	else document.getElementById(nom_iframe_popup).contentWindow.set_lien_ajax(url);
}

function fermer_popup(url_retour,no_fade,nom_div_pop){
	if(nom_div_pop=='undefined' || nom_div_pop=='')nom_div_pop="div_pop";
	if(no_fade==1){
		fadeOut("masque",0,0,0);
		fadeOut(nom_div_pop,0,0,1);
	}
	else{
		fadeOut("masque",60,0,0);
		fadeOut(nom_div_pop,100,0,1);
	}
	if(url_retour!=""){
		if(document.getElementById("iframe_contenu")){
			document.getElementById("iframe_contenu").src=url_retour;
		}
		else{
			document.location.href=url_retour;
		}
	}
	window.iframe_popup.vider_page();
}

/**********************************
affichage des categories avec ajax
**********************************/

var categorie_initial=new Array();

function charge_categorie(parent,niveau_categorie,type_categorie,nom_champ){
	var XHR_categorie = new XHRConnection();
	// Envoie des informations
	XHR_categorie.appendData('retour[niveau_categorie]',niveau_categorie );
	XHR_categorie.appendData('retour[type_categorie]',type_categorie );
	XHR_categorie.appendData('retour[nom_champ]',nom_champ );
	XHR_categorie.appendData('type',type_categorie );
	XHR_categorie.appendData('id', parent);
	XHR_categorie.sendAndLoad("../ajax/recherche.php", "GET",affiche_categorie);
}

function affiche_categorie(obj){
	s="";
	var tabResult = obj.responseXML.getElementsByTagName('resultat');
	var niveau_categorie = obj.responseXML.getElementsByTagName('niveau_categorie').item(0).getAttribute('value')*1;
	var type_categorie = obj.responseXML.getElementsByTagName('type_categorie').item(0).getAttribute('value');
	var nom_champ = obj.responseXML.getElementsByTagName('nom_champ').item(0).getAttribute('value');

	for(i=0;i<tabResult.length;i++){
		resultat = tabResult.item(i);
		if(categorie_initial[nom_champ][niveau_categorie]==resultat.getAttribute('id'))selected=" selected";
		else selected="";
		s+="<option value='"+resultat.getAttribute('id')+"'"+selected+">"+resultat.getAttribute('titre')+"</option>";
	}
	if(niveau_categorie>0)slash="&nbsp;/ ";
	else slash="";
	if(s!="")s="<div style='float:left;'>"+slash+"<select name='"+nom_champ+"_"+niveau_categorie+"' id='"+nom_champ+"_"+niveau_categorie+"' class='liste-deroulante' onChange='change_categorie("+niveau_categorie+",\""+type_categorie+"\",\""+nom_champ+"\")'><option value=''></option>"+s+"</select> </div><div id='refresh_"+nom_champ+"_"+(niveau_categorie+1)+"'></div>";
	document.getElementById('refresh_'+nom_champ+'_'+niveau_categorie).innerHTML=s;

	if(categorie_initial[nom_champ][niveau_categorie]!=undefined){
		categorie_initial[nom_champ][niveau_categorie]=undefined;
		change_categorie(niveau_categorie,type_categorie,nom_champ);
	}
}

function change_categorie(niveau,type_categorie,nom_champ){
	id=document.getElementById(nom_champ+'_'+niveau).value;
	if(id==""){
		if(niveau>0)id=document.getElementById(nom_champ+'_'+(niveau-1)).value;
		else id="";
		document.getElementById(nom_champ).value=id;
		//niveau_categorie=niveau-1;
		document.getElementById('refresh_'+nom_champ+'_'+(niveau+1)).innerHTML="";
	}
	else{
		document.getElementById(nom_champ).value=id;
		charge_categorie(id,niveau+1,type_categorie,nom_champ);
	}
}

function affichageElement(string_idElement, bool_OuiNon){
	if(bool_OuiNon){
		document.getElementById(string_idElement).style.display = '';

	}else{
		document.getElementById(string_idElement).style.display = 'none';
	}
}

function affichagecacheElement(string_idElement){
	if(document.getElementById(string_idElement).style.display == 'none'){
		document.getElementById(string_idElement).style.display = '';

	}else{
		document.getElementById(string_idElement).style.display = 'none';
	}
}




var t_memorise_formulaire=new Array();

function memorise_formulaire(nom_form,cible)
{
	if(nom_form!=""){
		if(cible==undefined)w=window;
		else eval("w=window."+cible+";");

		t_memorise_formulaire[nom_form]=new Array();

		// Recuperation des inputs
		nb=w.document[nom_form].length;
		for(i=0;i<nb;i++){
			name=w.document[nom_form].elements[i].name;
			if(w.document[nom_form].elements[i].type=='radio' && w.document[nom_form].elements[i].checked==true){
				t_memorise_formulaire[nom_form][name]=w.document[nom_form].elements[i].value;
			}
			if(w.document[nom_form].elements[i].type=='checkbox' && w.document[nom_form].elements[i].checked==true){
				t_memorise_formulaire[nom_form][name]=w.document[nom_form].elements[i].value;
			}
			if(w.document[nom_form].elements[i].type!='radio' && w.document[nom_form].elements[i].type!='checkbox'){
				t_memorise_formulaire[nom_form][name]=w.document[nom_form].elements[i].value;
			}
		}
	}
}

function compare_formulaire(nom_form,cible)
{
	//alert("nom form "+nom_form);
	if(nom_form!=""){
		if(cible==undefined)w=window;
		else eval("w=window."+cible+";");

		t=new Array();
		k=0;
		// Recuperation des inputs
		nb=w.document[nom_form].length;
		for(i=0;i<nb;i++){
			name=w.document[nom_form].elements[i].name;
			if(w.document[nom_form].elements[i].type=='radio' && w.document[nom_form].elements[i].checked==true){
				t[k]=new Array();
				t[k]['name']=name;
				t[k]['value']=w.document[nom_form].elements[i].value;
				k++;
			}
			if(w.document[nom_form].elements[i].type=='checkbox' && w.document[nom_form].elements[i].checked==true){
				t[k]=new Array();
				t[k]['name']=name;
				t[k]['value']=w.document[nom_form].elements[i].value;
				k++;
			}
			if(w.document[nom_form].elements[i].type!='radio' && w.document[nom_form].elements[i].type!='checkbox'){
				t[k]=new Array();
				t[k]['name']=name;
				t[k]['value']=w.document[nom_form].elements[i].value;
				k++;
			}

		}

		for(i=0;i<t.length;i++){
			if(t[i]['value']!=t_memorise_formulaire[nom_form][t[i]['name']])return false;
		}
		return true;
	}
	return true;
}

function check_form(cible,message_erreur){
	if(cible==undefined || cible=="")w=window;
	else eval("w=window."+cible+";");

	bool=true;
	if(typeof w.get_champ_obligatoire == "function"){
		t=w.get_champ_obligatoire();
		for(i=0;i<t.length;i++){
			if(w.document.getElementById(t[i]["champ"])!=undefined && w.document.getElementById(t[i]["label"])!=undefined){
				if(w.document.getElementById(t[i]["champ"]).value==""){
					w.document.getElementById(t[i]["label"]).style.color="#FF0000";
					bool=false;
				}
				else{
					w.document.getElementById(t[i]["label"]).style.color="";
				}
				w.document.getElementById(t[i]["champ"]).onchange=function(){w.check_form()};
			}
		}
	}
	if(message_erreur==1 && !bool)alert("Tous les champs obligatoires ne sont pas remplis.");
	return bool;
}
/*Fonction de geolocalisation avec Map24*/

function goMap24() {
	Map24.loadApi( ["core_api", "wrapper_api"],init_map);
}

function goMap24_no_carte() {
	Map24.loadApi( ["core_api", "wrapper_api"],DoNothing);
}

function init_map()
{
	Map24.MapApplication.init({ NodeName: "maparea" }  );
}

function DoNothing()
{
	Map24.MapApplication.Map = new Map24.Map();
}


function get_lg_lt_map24(adresse,cp,ville,callback)
{

	//	alert(adresse+" "+cp+" "+ville);
	if(Map24.trim(adresse+" "+cp+" "+ville) == "") { return; }

	var geocoder = new Map24.GeocoderServiceStub();
	//Geocodes the address. The address is passed in the Search field. The Alternatives field defines the number
	//of geocoded addresses that are returned in the response. You must pass the name of the callback function
	//that is called as soon as the client has received the response.

	if(callback==undefined)
	callback = ResultGeocode;
	geocoder.geocode( { SearchText: Map24.trim( adresse+" "+cp+" "+ville ), MaxNoOfAlternatives: 10, CallbackFunction: callback } );

}

function get_lg_lt_map24_world(adresse,cp,ville,dep,pays,callback)
{
	if(Map24.trim(adresse+" "+cp+" "+ville+" "+dep+" "+pays) == "") { return; }

	var geocoder = new Map24.GeocoderServiceStub();
	//Geocodes the address. The address is passed in the Search field. The Alternatives field defines the number
	//of geocoded addresses that are returned in the response. You must pass the name of the callback function
	//that is called as soon as the client has received the response.

	if(callback==undefined)
	callback = ResultGeocode;
	geocoder.geocode( { SearchText: Map24.trim( adresse+" "+cp+" "+ville ), MaxNoOfAlternatives: 10, CallbackFunction: callback } );

}

function ResultGeocodeAdresse(locs)
{
	alert(locs[0].getLongitude());
	alert(locs[0].getLatitude());

}

function format_mel(champ)
  {
    var _Mel = document.getElementById(champ);
    if (_Mel.value != "")
      {
        var usr    = "([a-zA-Z][a-zA-Z0-9_.-]*|\"([^\\\\\x80-\xff\015\012\"]|\\\\[^\x80-\xff])+\")";
        var domain = "([a-zA-Z0-9][a-zA-Z0-9._-]*\\.)*[a-zA-Z0-9][a-zA-Z0-9._-]*\\.[a-zA-Z]{2,5}";
        var regex  = "^" + usr + "\@" + domain + "$";
        var rgx    = new RegExp(regex);
        var etat = rgx.exec(_Mel.value);
        if (!etat)
          {
            alert('Veuillez saisir une adresse E-Mail de la forme : X@X.XX');
            document.getElementById(champ).focus();
          }
      }
  }
  
function format_tel(champ)
{
	var nb=2;
	var resultat = '';
	var separateur = ' ';
	var tableau = new Array();

	var _Champ = document.getElementById(champ);
	_Champ.value = _Champ.value.replace(/[- .]/g,'');

	// Remplir le tableau par paquet de 2
	for ( i = 0; i <= _Champ.value.length; i++ ){
		var tempo = _Champ.value.substr(i,nb);
		var tempo2 = 1*(_Champ.value.substr(i,nb));
		if (tempo == tempo2)
		{
			tableau[i] = _Champ.value.substr(i,nb);
			for ( j = 1; j < nb; j++ )
			{
				i++
			}
		}
	}

	// Insertion du séparateur
	for ( i = 0; i < tableau.length; i++ )
	{
		if ( tableau[i] != undefined )
		{
			resultat += tableau[i] + separateur;
		}
	}

	// Si on trouve le séparateur à la fin, on le vire
	if ( resultat.lastIndexOf(separateur) == resultat.length-1 )
	{
		resultat = resultat.substring(0,(resultat.length-1));
	}
	// On réaffecte le champ
	_Champ.value = resultat;
}

// Série de Formatage de date
// -----------------------------------------------------------------------------
// Formatage du Jour
function format_jour(jour,mois)
{
	var _Jour = document.getElementById(jour);
	var _Mois = document.getElementById(mois);
	if (_Jour.value > 31)
	{
		_Jour.value=31;
	}
	if (_Jour.value.length == 2)
	{
		_Mois.focus();
		_Mois.select();
		_Mois.value='';
	}
}

// Formatage du Mois
function format_mois(jour,mois,annee)
{
	var _Jour = document.getElementById(jour);
  var _Mois = document.getElementById(mois);
	var _Annee = document.getElementById(annee);
	if (_Mois.value > 12){
		_Mois.value=12;
	}
	if (_Jour.value == '')
    {
      _Jour.value = '01';
  	}
	if (_Mois.value.length == 2){
		_Annee.focus();
		_Annee.select();
		_Annee.value='';
	}
}

//Formatage de l'Année
function format_annee(jour,mois,annee)
{
  // Attention encore quelques petits soucis avec Opéra
	var _Jour = document.getElementById(jour);
  var _Mois = document.getElementById(mois);
  var _Annee = document.getElementById(annee);
	encours = new Date();
	var annee_en_cours = encours.getYear();
	if( document.all ) 
		{
			// IE
			// encours.getYear(); retourne ex : 2007
      annee_en_cours_2 = parseInt(annee_en_cours)-2000; // Sur deux chiffres Ex : 07
		}
	else
		{
			// Firefox
			// encours.getYear(); retourne ex : 107
      annee_en_cours_2 = parseInt(annee_en_cours)-100; // Sur deux chiffres Ex : 07
	    annee_en_cours = parseInt(annee_en_cours)+1900;  // Sur Quatre chiffres Ex : 2007
		}
	if(_Annee.value.length < 2)
    {
  		_Annee.value=0+_Annee.value;
  	}
	if (_Annee.value <= parseInt(annee_en_cours_2) && _Annee.value >= 0 && _Annee.value != '')
  	{
  		_Annee.value='20'+_Annee.value;
  	}
	if (_Annee.value > parseInt(annee_en_cours_2) && _Annee.value < 100 && _Annee.value != '')
  	{
  		_Annee.value='19'+_Annee.value;
  	}
	if (_Annee.value > parseInt(annee_en_cours) && _Annee.value != '')
  	{
  		_Annee.value=parseInt(annee_en_cours);
  	}
	if (_Annee.value < 1900 && _Annee.value != '')
  	{
  		_Annee.value=1900;
  	}
  if (_Jour.value == '')
    {
      _Jour.value = '01';
  	}
  if (_Mois.value == '')
    {
      _Mois.value = '01';
  	}
}
// -----------------------------------------------------------------------------

