function produit_get_puce_active(support, id_produit) {

	var myRequest = new Request({url: './cap_pub/scripts/interface.php?get=valid_by_client&id_produit='+id_produit+'&support='+support
			,onSuccess: function(valid_by_client_formation, reponseXML){ 
		    //alert(valid_by_client_formation);    
				if(valid_by_client_formation=='OUI') {
					
					$('lien_activate_'+id_produit).innerHTML='<img src="./images/picto_activate.png" />';
					
				}
				else if (valid_by_client_formation=='NON') {
                        
 					$('lien_activate_'+id_produit).innerHTML='<img src="./images/picto_activate_no.png" />';
				}
				
			}
	} 
  );
  
	myRequest.send();
  
  
  	
}

function div_set_writable_prod(id_saisie){

   if(!in_edit){
   
     obj = document.getElementById(id_saisie);
     texte = obj.innerHTML;
     if(texte==default_texte) texte='';            

     new_content = "<textarea name=\"modif_en_cours\" id=\"modif_en_cours"+id_saisie+"\" row=\"4\" class=\"text_modif\"";
     new_content+=" onKeyUp=\"this.style['height']=this.scrollHeight+'px';\" ";
     new_content+=" onblur=\"div_set_in_texte_prod(this.value,'"+id_saisie+"')\">";
     new_content+=texte;
     new_content+= "</textarea>";
     
     obj.innerHTML = new_content;
     in_edit=true;
     document.getElementById('modif_en_cours'+id_saisie).focus();
     document.getElementById('modif_en_cours'+id_saisie).style['height']=document.getElementById('modif_en_cours'+id_saisie).scrollHeight+'px';
   }
}

function div_set_in_texte_prod(valeur,id_saisie){

   if(in_edit){
   
        if(valeur=='') valeur=default_texte;                  
        obj = document.getElementById(id_saisie);
        obj.innerHTML =valeur;
        in_edit=false;
        
        var XHR = new XHRConnection();
        if(valeur==default_texte) valeur='';  
   
        XHR.appendData('id_publi', id_saisie);
        XHR.appendData('valeur', valeur);
        XHR.appendData('mode', 'save_publi_titre');
        XHR.sendAndLoad("./scripts/interface.php", "POST", id_saisie);
    }
} 

function affecter_publi(id_client,support,id_publi){
  //window.location.replace("./produitheque.php?id_client="+id_client+"&support="+support+"&mode=list_publi&id_publi="+id_publi);
  
  document.location.href = modifyUrl(document.location.href, 'mode', 'list_publi');
  
}

function creer_publi(id_client,support,id_publi){
  //window.location.replace("./produitheque.php?id_client="+id_client+"&support="+support+"&mode=fiche_publi&id_publi="+id_publi);
  document.location.href = modifyUrl(document.location.href, 'mode', 'fiche_publi');
}

function clear_all_zs(){

      $('titre').value="";
      $('chapeau').value="";
      $('texte').value="";
      $('mails').value="";
      
}

function get_cache_stat_produit(id_produit, support,type, periode ,dt_deb,dt_fin) {
			
			div_imp_news = 'imp_news_'+id_produit;
			div_clic_news = 'clic_news_'+id_produit;
			div_imp_site = 'imp_site_'+id_produit;
			div_clic_site = 'clic_site_'+id_produit;
			
			$(div_imp_news).innerHTML = '<img src="./images/wait.gif" />';
			
	    var XHR = new XHRConnection();
	        
	  	mon_url = "./cap_pub/scripts/interface.php?get=stat-produit&id_produit="+id_produit+'&support='+support+'&type='+type+'&periode='+periode+'&dt_deb='+dt_deb+'&dt_fin='+dt_fin;
	  /*  
	    if(date!=null) {
				mon_url+='&date='+date;
			}
	    */
			var myRequest = new Request({url: mon_url
			,id_produit: id_produit
			,onSuccess: function(contenu, reponseXML){ 
					
					div_imp_news = 'imp_news_'+id_produit;
					div_clic_news = 'clic_news_'+id_produit;
					div_imp_site = 'imp_site_'+id_produit;
					div_clic_site = 'clic_site_'+id_produit;
					
					eval(contenu);
			
					$(div_imp_news).innerHTML=imp_news;
					$(div_clic_news).innerHTML=clic_news;
					$(div_imp_site).innerHTML=imp_site;
					$(div_clic_site).innerHTML=clic_site;
			}
			}
			);
		
			myRequest.send();

}
function get_stat_produit(div, id_produit, support,type,action,origine, periode ,dt_deb,dt_fin) {

			$(div).innerHTML = '<img src="./images/wait.gif" />';
			
	    var XHR = new XHRConnection();
	        
	    XHR.setRefreshArea(div);
	    
	    url="./scripts/interface.php";
	    
	    XHR.appendData('mode','stat-produit');
	    
			XHR.appendData('id_produit',id_produit);
	    XHR.appendData('support',support);
	    XHR.appendData('type',type);
	    XHR.appendData('action',action);
	    XHR.appendData('origine',origine);
	    
	    XHR.appendData('periode',periode);
	    XHR.appendData('dt_deb',dt_deb);
	    XHR.appendData('dt_fin',dt_fin);
	    /*if(date!=null) {
				XHR.appendData('date',date);
			}*/
	    
	    XHR.sendAndLoad(url, "GET");
	

}
function apercu_produit(url_apercu,id_produit){
	    
	    var XHR = new XHRConnection();
	        
	    nomdiv='apercu_produit';
	       
	    XHR.setRefreshArea(nomdiv);
	    
	    url="./scripts/interface.php";
	    XHR.appendData('url',url_apercu);
	    XHR.appendData('mode','apercu_produit');
	    XHR.appendData('id_produit','id_produit');
	    XHR.sendAndLoad(url, "GET");
} 


function load_famille(id_famille,niveau_courant,support){

    var XHR = new XHRConnection();
    lvl_suivant=niveau_courant+1;
    
    nomdiv='famille';
    
    XHR.setRefreshArea(nomdiv);

		url="../scripts/interface.php";

    XHR.appendData('id_famille',id_famille);
    XHR.appendData('lvl_suivant',lvl_suivant);
    XHR.appendData('support',support);
    XHR.appendData('mode','charge_famille_suivante'); 
    
    XHR.sendAndLoad(url, "GET");

}

function credit_insuffisant(){
    alert("Vous ne possédez pas assez de crédit pour mettre cette fiche Produit en ligne, pour obtenir des crédits supplémentaires, contactez notre service commercial au 01 53 68 40 20 ou dévalidez d'autres fiches Produits afin de libérer un emplacement Produit.")
}

function tri_produit(parametre){
    document.location.href=modifyUrl(document.location.href,'parametre_tri',parametre);
}
      
function tri_dossier(parametre){
    document.location.href=modifyUrl(document.location.href,'parametre_tri',parametre);
}

function extract_stat(id_indus, support, periode ,month) {
		if(month != undefined){
      month = document.forms['form1'].elements[month].options[document.forms['form1'].elements[month].selectedIndex].value;
    }else{
      month="";
    }

		url='./dlg/pop_in_extract_stat.php?id_indus='+id_indus+'&support='+support+'&periode='+periode+'&month='+month;

		document.location.href=url;
		
}

function pop_in_extract_stat(file_name){
  url= './tmp/'+file_name;
  document.location.href=url;  
}
      
function pop_in_rapport_pdf(support,type,periode,month){
    if(month != undefined){
      month = document.forms['form1'].elements[month].options[document.forms['form1'].elements[month].selectedIndex].value;
    }else{
      month="";
    }
    
    url='./dlg/pop_in_rapport_pdf_prod.php?support='+support+'&type='+type+'&periode='+periode+'&<?=time() ?>'+'&month='+month; 
	  document.location.href=url;
}
     
function apercu_fiche_fabricant(url_fic){
      
    var XHR = new XHRConnection();
    
    nomdiv='fiche_fabricant';
    XHR.setRefreshArea(nomdiv);

		url="./scripts/interface.php";

    XHR.appendData('url',url_fic);
    XHR.appendData('mode','fiche_fabricant'); 
    XHR.sendAndLoad(url, "GET");
}

function apercu_publi_dossier(url_apercu){

    var XHR = new XHRConnection();
    
    nomdiv='publi-dossier';
    
    XHR.setRefreshArea(nomdiv);

		url="./scripts/interface.php";
    XHR.appendData('url',url_apercu);
    XHR.appendData('mode','apercu_publi');
    XHR.sendAndLoad(url, "GET",nomdiv);
}  
 
function _set_produit_activation(support,id_produit, set_to_valid) {
	path=dir_http+'images/';
	if(set_to_valid) {

		var myRequest = new Request({url: './cap_pub/scripts/interface.php?put=valid_by_client&validate_by_client=OUI&support='+support+'&id_produit='+id_produit
			,onSuccess: function(valid_by_client, reponseXML){ 
				
				if(valid_by_client=='OK') {
					 aff_info_credit();
					$('lien_activate_'+id_produit).innerHTML='<img src="./images/picto_activate.png" />';
					
					alert('La mise en ligne de votre produit sera effective dans un délai maximum de 24 heures.');
					
					
				}
				else {
					valid_by_client = Remplacer_dans_chaine(	valid_by_client, '&eacute;'	,'é');
					alert(valid_by_client);
				}
				
			}
		});
		
	}
	else {
		
		var myRequest = new Request({url: './cap_pub/scripts/interface.php?put=valid_by_client&validate_by_client=NON&support='+support+'&id_produit='+id_produit
			,onSuccess: function(valid_by_client, reponseXML){ 
				if(valid_by_client=='OK') {
					 aff_info_credit();
					$('lien_activate_'+id_produit).innerHTML='<img src="./images/picto_activate_no.png" />';
					alert('Le retrait de votre produit sera effectif dans un délai maximum de 24 heures.');
					
				
				}
				else {
					alert(valid_by_client);
				}
				
			}
		});
	}
	
	myRequest.send();	
	
}
function activation_produit(support,id_produit){
	
	/* récupération du l'info statut d'activation du produit */
	var myRequest = new Request({url: './cap_pub/scripts/interface.php?get=valid_by_client&support='+support+'&id_produit='+id_produit
			,onSuccess: function(valid_by_client, reponseXML){ 
				
				if(valid_by_client=='OUI') {
					
					_set_produit_activation(support,id_produit, false);
					
				}
				else if (valid_by_client=='NON') {

 				 _set_produit_activation(support,id_produit, true);
 				
				}
				
			}
	});

	myRequest.send();	
	
}
     
function modifie_contenu(element){
    if(element=='nom' || element=='type'){
        
         // Remplacer_dans_chaine(document.getElementById(element).value,'\n','<br>');
		 
		 if($('type')!=null) {
			 $("contenu_nom").innerHTML=$('type').value+' ('+$('nom').value+')';  
		 }
		 else {
			 $("contenu_nom").innerHTML=$('nom').value;   
		 }
    }
	else if(element=='tel' || element=='fax' || element=='adresse') {
		document.getElementById("contenu_adresse").innerHTML=$('adresse').value+'<br><br>Tél. : '+$('tel').value+'<br>Fax : '+$('fax').value;
//		document.getElementById(element).value;      
        	
	}
    else{
        if(element=='descr'){
           // ctrl_longueur_text_area(element);
        }
        document.getElementById("contenu_"+element).innerHTML=Remplacer_dans_chaine(document.getElementById(element).value,'\n','<br>');      
        //        document.getElementById("contenu_"+element).innerHTML=Remplacer_dans_chaine(document.getElementById(element).value,'\n','<br>');  
    }    
}

function tri_produit_periode(){
    combo_change=$('paraperiode').value;
    document.location.href=modifyUrl(document.location.href,'paraperiode',combo_change);              
}  

function ctrl_longueur_text_area(element, maximum){

	if(maximum==null || maximum==0) maximum = 300;

  if (document.getElementById(element).value.length > maximum){
      montexte = document.getElementById(element).value.substring(0,maximum-1);
      document.getElementById(element).value = montexte;
  }
}
function pop_avertissement() {
		SqueezeBox.open($('msg-avertissement'),
			{
        handler: 'clone'
				,size: {x: 500, y: 100}
        ,overlayOpacity:.2
      });  
	    

}
function pop_rapport() {
    if($('frequence1').checked){
      if($('validate').checked){
    		SqueezeBox.open($('msg-rapport-hebdo'),
    			{
            handler: 'clone'
    				,size: {x: 500, y: 100}
            ,overlayOpacity:.2
          });
      }else{
        SqueezeBox.open($('msg-rapport-hebdo-desactive'),
    			{
            handler: 'clone'
    				,size: {x: 500, y: 100}
            ,overlayOpacity:.2
          });
      } 
    }else{
      if($('validate').checked){
        SqueezeBox.open($('msg-rapport-mensuel'),
    			{
            handler: 'clone'
    				,size: {x: 500, y: 100}
            ,overlayOpacity:.2
          });
      }else{
        SqueezeBox.open($('msg-rapport-mensuel-desactive'),
    			{
            handler: 'clone'
    				,size: {x: 500, y: 100}
            ,overlayOpacity:.2
          });
      }  
    } 
	    
  return false;
}

function checkEmail(email) {
  var filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
  if (!filter.test(email)) {
    return false;
  }
  return true;
} 

function validateEmails(field, delimiter, max) {
	var delimiter = delimiter || '\n';
	var filter1  = /([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+>?$/;
	var filter2  = /<([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+>$/;
	var filter3  = /"[^"]+"\s?<([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+>$/;
	var error = 0;

	// Create an array by splitting the field along the delimiter
	var aEmails = field.value.split(delimiter);

	if(aEmails.length>max)return 2;
	
	// For each of the emails
	for(index = 0; index < aEmails.length; index++) {
		// Trim spaces from the ends
		aEmails[index] = (aEmails[index].replace(/^\s+/,'')).replace(/\s+$/,'');
		// Check whether an email is present
		if(aEmails[index] != '' && aEmails[index].search(filter1) == -1)
			error = 1;
		// Check whether brackets are closed
		else if(aEmails[index].search(filter1) > -1 && aEmails[index].search(/[<>]/) > -1 && aEmails[index].search(filter2) == -1)
			error = 1;
		// Check whether brackets are not present when needed
		else if(aEmails[index].search(filter1) > -1 && aEmails[index].search(/\s/) > -1 && aEmails[index].search(filter2) == -1)
			error = 1;
		// Check whether quotes are closed
		else if(aEmails[index].search(filter1) > -1 && aEmails[index].search(/"/) > -1 && aEmails[index].search(filter3) == -1)
			error = 1;
		// Check whether quotes are not present when needed
		else if(aEmails[index].search(filter1) > -1 && aEmails[index].search(',') > -1 && aEmails[index].search(filter3) == -1)
			error = 1;
			
		if(error == 1) break;
	}
	
	// Update the value of the field
	//field.style.backgroundColor = (error == 1) ? 'FFFFCC' : 'FFFFFF';
	return error;
}

function sub_rapport(){
  // vérification des champs
  errormail = validateEmails($('emails'),'\n',5);
  if($('emails').value==""){
    alert("Un e-mail est nécessaire.");
  }else if(errormail==1){
    alert("Un e-mail est invalide.");
  }else if(errormail==2){
    alert("5 e-mails maximum!");
  }else if($('titre').value.length==0){
    alert("Un titre de rapport est nécessaire!");
  }else if(errormail==0){
    pop_rapport();
  }
}

function selection_month(formname,id,value_month){

  var button = id + '_button';
  if(value_month==""){
    // on cache le bouton
    $(button).style.display='none';
  }else{
   // on affiche le bouton
    $(button).style.display='block';
  }
}
