// ####### CHEMINS ######

var path = '/';
//var path = '/unelvent.com/www/';
var path_post = path+'templeet.php/';

function add_fic(i) {
  var i2 = i + 1;
  var parent = document.getElementById('gestion_fichiers');
  var node = document.getElementById('add_fic_'+i); 
  
  // Création éléments
  // Fieldset
  fieldset = document.createElement('fieldset');
  fieldset.setAttribute('class','no-border');
  fieldset.setAttribute('className','no-border');
  fieldset.setAttribute('id','field_fic_'+i);
    // Legend
    //legend = document.createElement('legend');
    //legend_txt = document.createTextNode('Fichier '+i);
    // P intitulé
    p_lib = document.createElement('p');
      // Label
      label = document.createElement('label');
      label.setAttribute('for','lib_fichiers_'+i);
      label_txt = document.createTextNode('Libellé fichier: ');
      // Input
      input_lib = document.createElement('input');
      input_lib.setAttribute('type','text');
      input_lib.setAttribute('id','lib_fichiers_'+i);
      input_lib.setAttribute('name','lib_fichiers_'+i);
    // P fichier
    p_fichier = document.createElement('p');
    p_fichier.setAttribute('class','pad10');
    p_fichier.setAttribute('className','pad10');
      // Label chemin
      label_chemin = document.createElement('label');
      label_chemin.setAttribute('for','chemin_fichiers_'+i);
        label_chemin_txt = document.createTextNode('Joindre un pdf : ');
      // Input
      input_fichier = document.createElement('input');
      input_fichier.setAttribute('type','file');
      input_fichier.setAttribute('name','chemin_fichiers[]');
      //input_fichier.setAttribute('class','gauche-150');
      // Label
      label_public = document.createElement('label');
      label_public.setAttribute('for','public_'+i);
      label_public.setAttribute('class','lab-chkp');
      label_public.setAttribute('className','lab-chkp');
      label_public_txt = document.createTextNode('Public : ');
      // Input
      input_public = document.createElement('input');
      input_public.setAttribute('type','checkbox');
      input_public.setAttribute('name','public_'+i);
      input_public.setAttribute('id','public_'+i);
      input_public.setAttribute('class','chkp');
      input_public.setAttribute('className','chkp');
    // Div clr
    div_clr = document.createElement('div');
    div_clr.setAttribute('class','clr');
    div_clr.setAttribute('className','clr');
    /* A suppression
    suppr = document.createElement('a');
    suppr.setAttribute('id','del_fic'+i);
    suppr.setAttribute('href', 'javascript:del_fic('+i+');')
    suppr.setAttribute('class','clr');
    suppr_txt = document.createTextNode('Supprimer');
    */
    
  
  // Création des liens
  //fieldset.appendChild(legend);
  fieldset.appendChild(p_lib);
  fieldset.appendChild(p_fichier);
    //legend.appendChild(legend_txt);
    p_lib.appendChild(label);
    p_lib.appendChild(input_lib);
      label.appendChild(label_txt);
    p_fichier.appendChild(label_chemin);
      label_chemin.appendChild(label_chemin_txt);
    p_fichier.appendChild(input_fichier);
    p_fichier.appendChild(label_public);
      label_public.appendChild(label_public_txt);
    p_fichier.appendChild(input_public);
  //suppr.appendChild(suppr_txt);
  fieldset.appendChild(div_clr);
  //fieldset.appendChild(suppr);

  // Insertion dans la page
  fieldset = parent.insertBefore(fieldset,node);

  // Modification attributs
  node.setAttribute('id','add_fic_'+i2);
  node.setAttribute('href','javascript:add_fic('+i2+');');
}
function del_fic(i) {
  var parent = document.getElementById('form-edit');
  var ancien = document.getElementById('field_fic_'+i); 
  ancien = parent.removeChild(ancien);
}

function add_photo(i) {
  var i2 = i + 1;
  var parent = document.getElementById('gestion_fichiers');
  var node = document.getElementById('add_photo_'+i); 
  // Création éléments
  // Fieldset
  fieldset = document.createElement('fieldset');
  fieldset.setAttribute('class', 'no-border');
  fieldset.setAttribute('className', 'no-border');
  fieldset.setAttribute('id', 'field_photos_hd_'+i);
    // Legend
    /*legend = document.createElement('legend');
    legend_txt = document.createTextNode('Photo HD '+i);*/
    // P intitulé
    p_lib = document.createElement('p');
      p_lib.setAttribute('class', 'pad10');
      p_lib.setAttribute('className', 'padt10');
      // Label
      label = document.createElement('label');
      label.setAttribute('for','lib_photos_hd_'+i);
      label_txt = document.createTextNode('Libellé photo HD: ');
      // Input
      input_lib = document.createElement('input');
      input_lib.setAttribute('type','text');
      input_lib.setAttribute('id','lib_photos_hd_'+i);
      input_lib.setAttribute('name','lib_photos_hd_'+i);
    // P fichier
    p_fichier = document.createElement('p');
    p_fichier.setAttribute('class','pad10');
    p_fichier.setAttribute('className','pad10');
      // Label chemin
      label_chemin = document.createElement('label');
      label_chemin.setAttribute('for','chemin_photos_hd_'+i);
        label_chemin_txt = document.createTextNode('Joindre une photo HD : ');
      // Input
      input_fichier = document.createElement('input');
      input_fichier.setAttribute('type','file');
      input_fichier.setAttribute('name','chemin_photos_hd[]');
      input_fichier.setAttribute('class','gauche-150');
      input_fichier.setAttribute('className','gauche-150');
      // Div clr
      div_clr = document.createElement('div');
      div_clr.setAttribute('class','clr');
      div_clr.setAttribute('className','clr');
    /* A suppression
    suppr = document.createElement('a');
    suppr.setAttribute('id','del_photo_hd'+i);
    suppr.setAttribute('href', 'javascript:del_photo('+i+');')
    suppr.setAttribute('class','clr');
    suppr_txt = document.createTextNode('Supprimer');
    */
  
  // Création des liens
  //fieldset.appendChild(legend);
  fieldset.appendChild(p_lib);
  fieldset.appendChild(p_fichier);
    //legend.appendChild(legend_txt);
    p_lib.appendChild(label);
    p_lib.appendChild(input_lib);
      label.appendChild(label_txt);
    p_fichier.appendChild(label_chemin);
      label_chemin.appendChild(label_chemin_txt);
    p_fichier.appendChild(input_fichier);
  //suppr.appendChild(suppr_txt);
  fieldset.appendChild(div_clr);
  //fieldset.appendChild(suppr);

  // Insertion dans la page
  fieldset = parent.insertBefore(fieldset,node);
  
  // Modification attributs
  node.setAttribute('id','add_photo_'+i2);
  node.setAttribute('href','javascript:add_photo('+i2+');');
}

function del_photo(i) {
  var parent = document.getElementById('form-edit');
  var ancien = document.getElementById('field_photos_hd_'+i); 
  ancien = parent.removeChild(ancien);
}


function add_picto(i) {
  var i2 = i + 1;
  var parent = document.getElementById('gestion_pictos');
  var node = document.getElementById('add_picto_'+i); 
  // Création éléments
  // Fieldset
  fieldset = document.createElement('fieldset');
  fieldset.setAttribute('class', 'no-border pad10');
  fieldset.setAttribute('className', 'no-border pad10');
  fieldset.setAttribute('id', 'field_pictos_'+i);
    // Legend
    //legend = document.createElement('legend');
    //legend_txt = document.createTextNode('Pictogramme '+i);
    // P intitulé
    p_lib = document.createElement('p');
      // Label
      label = document.createElement('label');
      label.setAttribute('for','lib_pictos_'+i);
      label_txt = document.createTextNode('Libellé : ');
      // Input
      input_lib = document.createElement('input');
      input_lib.setAttribute('type','text');
      input_lib.setAttribute('id','lib_pictos_'+i);
      input_lib.setAttribute('name','lib_pictos_'+i);
    // P fichier
    p_fichier = document.createElement('p');
    p_fichier.setAttribute('class','pad10');
    p_fichier.setAttribute('className','pad10');
      // Label chemin
      label_chemin = document.createElement('label');
      label_chemin.setAttribute('for','chemin_pictos_'+i);
        label_chemin_txt = document.createTextNode('Joindre un pictogramme : ');
      // Input
      input_fichier = document.createElement('input');
      input_fichier.setAttribute('type','file');
      input_fichier.setAttribute('name','chemin_pictos[]');
      // Div clr
      div_clr = document.createElement('div');
      div_clr.setAttribute('class','clr');
      div_clr.setAttribute('className','clr');
    /* A suppression
    suppr = document.createElement('a');
    suppr.setAttribute('id','del_picto'+i);
    suppr.setAttribute('href', 'javascript:del_picto('+i+');')
    suppr.setAttribute('class','clr');
    suppr_txt = document.createTextNode('Supprimer');
    */
  
  // Création des liens
  //fieldset.appendChild(legend);
  fieldset.appendChild(p_lib);
  fieldset.appendChild(p_fichier);
    //legend.appendChild(legend_txt);
    p_lib.appendChild(label);
    p_lib.appendChild(input_lib);
      label.appendChild(label_txt);
    p_fichier.appendChild(label_chemin);
      label_chemin.appendChild(label_chemin_txt);
    p_fichier.appendChild(input_fichier);
  //suppr.appendChild(suppr_txt);
  fieldset.appendChild(div_clr);
  //fieldset.appendChild(suppr);

  // Insertion dans la page
  fieldset = parent.insertBefore(fieldset,node);
  
  // Modification attributs
  node.setAttribute('id','add_picto_'+i2);
  node.setAttribute('href','javascript:add_picto('+i2+');');
}

function del_picto(i) {
  var parent = document.getElementById('form-edit');
  var ancien = document.getElementById('field_pictos_'+i); 
  ancien = parent.removeChild(ancien);
}

// ###### ADD FIC #######

function AddFichierEp(id) {
  var parent = document.getElementById('cat_'+id);
  var node = document.getElementById('link_frame_'+id);
  
  var iframe = document.createElement('iframe');
  iframe.setAttribute('id','iframe_cat'+id);
  iframe.setAttribute('src','edit_espace_pro/ajout_fichier,'+id+'.html');
  iframe.setAttribute('frameborder','0');
  
  parent.appendChild(iframe);
  parent.removeChild(node);
}

// ###### CLOSE FORM #######

function closeForm(id) {
  var node = window.parent.document.getElementById('iframe_cat'+id);
  var parent = window.parent.document.getElementById('cat_'+id);
  
  lien = window.parent.document.createElement('a');
  lien.setAttribute('href','javascript:AddFichierEp('+id+');');
  lien.setAttribute('id','link_frame_'+id);
  lien.setAttribute('title','Ajouter un fichier');
  
  lien_txt = window.parent.document.createTextNode('Ajouter un fichier');
  lien.appendChild(lien_txt);
  
  lien = parent.insertBefore(lien,node);
  parent.removeChild(node);
}

function validForm(intitule,id_cat,lien,id,file_ext) {
  var parent = window.parent.document.getElementById('list_'+id_cat);

  var li = window.parent.document.createElement('li');
  li.setAttribute('id','fic_'+id);
  li.setAttribute('style','background:url('+path+'images/'+file_ext+'.gif) left no-repeat;');
  
    a = window.parent.document.createElement('a');
    a.setAttribute('href',lien);
    a.setAttribute('class','ext');
    a.setAttribute('className','ext');
    
    a_txt = window.parent.document.createTextNode(intitule);
    a.appendChild(a_txt);
    
    li.appendChild(a);
    
      a_delete = window.parent.document.createElement('a');
      a_delete.setAttribute('href',"javascript:ajaxDelete('Etes-vous certain de vouloir supprimer ce fichier ?',"+id+", "+id_cat+");");
      /*a_delete.setAttribute('class','moins');
      a_delete.setAttribute('className','moins');
        a_delete_txt = window.parent.document.createTextNode('Supprimer');
        a_delete.appendChild(a_delete_txt);*/
    
      img_del = window.parent.document.createElement('img');
      img_del.setAttribute('src','images/delete.gif');
      img_del.setAttribute('alt','Supprimer le fichier'+intitule);
      a_delete.appendChild(img_del);
    
    li.appendChild(a_delete);
      
  parent.appendChild(li);
}

// ###### AJAX DELETE ######

function ajaxDelete(mess,id,id_cat) {
  if(confirm(mess))
  {
    var parent = $('list_'+id_cat);
    var node = $('fic_'+id);
    parent.removeChild(node);
    
    new Ajax.Request(
      path_post+'suppression_fic_ep.html',
      {
        method:'post',
        postBody:'id='+id
      }
    );
  }
}

function ajaxDeleteProd(mess,table,id,id_produits,clean_lib_produits) {
  if(confirm(mess))
  {
    var parent = document.getElementById('fichiers_prod');
    var node = document.getElementById('fic_'+id);
    parent.removeChild(node);
    
    new Ajax.Request(
      path_post+'ajax_suppr_fic,'+table+','+id+','+id_produits+','+clean_lib_produits+'.html',
      {
        method:'post',
        postBody:'id='+id
      }
    );
  }
}
function ajaxDeleteImg(mess,table,id,id_produits,clean_lib_produits) {
  if(confirm(mess))
  {
    var parent = document.getElementById('hd_prod');
    var node = document.getElementById('hd_'+id);
    parent.removeChild(node);
    
    new Ajax.Request(
      path_post+'ajax_suppr_fic,'+table+','+id+','+id_produits+','+clean_lib_produits+'.html',
      {
        method:'post',
        postBody:'id='+id
      }
    );
  }
}
function ajaxDeletePictos(mess,table,id,id_produits,clean_lib_produits) {
  if(confirm(mess))
  {
    var node = document.getElementById('pictos_'+id);
    var parent = document.getElementById(node.parentNode.id);
    parent.removeChild(node);
    new Ajax.Request(
      path_post+'ajax_suppr_fic,'+table+','+id+','+id_produits+','+clean_lib_produits+'.html',
      {
        method:'post',
        postBody:'id='+id
      }
    );
  }
}
function ajaxDeleteService(mess,id) {
  if(confirm(mess))
  {
    var node = document.getElementById('service_'+id);
    var parent = document.getElementById(node.parentNode.id);
    parent.removeChild(node);
    new Ajax.Request(
      path_post+'ajax_suppr.html',
      {
        method:'post',
        postBody:'type=contact_mail&id='+id
      }
    );
  }
}
function ajaxDeletePartenaire(mess,id) {
  if(confirm(mess))
  {
    var node = document.getElementById('partenaire_'+id);
    var parent = document.getElementById(node.parentNode.id);
    parent.removeChild(node);
    new Ajax.Request(
      path_post+'ajax_suppr.html',
      {
        method:'post',
        postBody:'type=partenaires&id='+id
      }
    );
  }
}
// ###### AJAX SEARCH ######

function ajaxSearch(r) {
  new Ajax.Request(
    path_post+'traitements_ajax-search.html',
    {
      method:'post',
      postBody:'r='+r,
      onSuccess:function(ajax) {
        var response = eval('('+ajax.responseText+')');
        
        if(response.lib_produits != '') {
          var p = document.createElement('p')
            p.setAttribute('class','clr');
            p.setAttribute('className','clr');
          
          var label = document.createElement('label');
            label.setAttribute('for',response.lib_produits);
          var intitule = document.createTextNode(response.lib_produits);
          label.appendChild(intitule);
          p.appendChild(label);
          
          var checkbox = document.createElement('input');
          checkbox.setAttribute('name','links[]');
          checkbox.setAttribute('type','checkbox');
          checkbox.setAttribute('class','chkb');
          checkbox.setAttribute('className','chkb');
          checkbox.setAttribute('defaultChecked', 'defaultChecked');
          checkbox.setAttribute('value', response.id);
          p.appendChild(checkbox);
          var parent = $('fieldSearch');
          var node = $('search');
          
          parent.appendChild(p);
          node.value = '';
        }
      }
    }
  );
}


function addListenerSearch() {
  if (window.addEventListener) {
    $('search').addEventListener('keypress', eventSearch, true);
    return false;
  }
  else if (window.attachEvent) {    
    $('search').attachEvent('onkeypress', eventSearch);
  }
}

function eventSearch(e) {
  var keynum;
  if(window.event) 
  // IE
  {
    keynum = e.keyCode;
  }
  else if(e.which)
  // Netscape/Firefox/Opera
  {
    keynum = e.which;
  }
  if(keynum == 13) {
    if($('search').value != '')
      ajaxSearch($('search').value);
    addListenerSearch();
  }
}

function clearFirstFocus(e) {
  e.value = '';
  e.onfocus = function() {}
}

function conf(msg) {
  if(!msg){var msg="Confirmez-vous cette action?";}
  var agree=confirm(msg);
  if (agree){
	  return true;
  }
  else {
	  return false;
  }
}

function open_ext_link()
{
	var liens = document.getElementsByTagName('a');
	// On récupère tous les liens du document dans une variable (array)
	// Une boucle qui parcourt le tableau (array) liens du début à la fin.
	for (var i = 0 ; i < liens.length ; ++i)  {
		// Si les liens ont un nom de class égal à lien_ext
		if (liens[i].className == 'ext')  {
			liens[i].title = 'Ouvrir dans une nouvelle fenêtre';
			// Au clique de la souris.
			liens[i].onclick = function()  {
				window.open(this.href);
				return false; // On ouvre une nouvelle page ayant pour URL le href du lien cliqué et on inhibe le lien réel.
			};
		}
	}
}
window.onload = open_ext_link;
