var isNetscape4 = document.layers;
var isNetscape6 = document.getElementById && !document.all
var isNetscape = isNetscape4 || isNetscape6;
var isIE = document.all && !isNetscape;
var DHTML = document.getElementById || document.all || document.layers;



function getStyle(nom) {
  if (isNetscape6)
    style = document.getElementById(nom).style;
  else if (isNetscape4)
    eval("style = document."+nom);
  else
    style = document.all[nom].style;
  return(style);
}


function clipping(nom, x1, y1, x2, y2) {
  if (isNetscape4) {
    getStyle(nom).clip.left = x1;
    getStyle(nom).clip.top = y1;
    getStyle(nom).clip.right = x2; 
    getStyle(nom).clip.bottom =  y2;
  }
  else if (isNetscape6)
    getStyle(nom).clip = "rect("+y1+" "+x1+" "+y2+" "+x2+")";
  else
    getStyle(nom).clip = "rect("+y1+" "+x2+" "+y2+" "+x1+")";
}

function deplacement_absolu(nom, abscisse, ordonnee) {
  if (!DHTML)
    return;
  getStyle(nom).left = abscisse;
  getStyle(nom).top = ordonnee;
}

function deplacement_relatif(nom, deltaX, deltaY) {
  if (!DHTML)
    return;
  getStyle(nom).left = parseInt(getStyle(nom).left) + deltaX;
  getStyle(nom).top = parseInt(getStyle(nom).top) + deltaY;
}


function check_email(email) {
        b = true;
        if (email.lastIndexOf('@') < 1)
            b = false;
        if (email.lastIndexOf('@') > email.lastIndexOf('.'))
            b = false;
        if (email.lastIndexOf('@') == email.lastIndexOf('.')+1)
            b = false;
        if (email.length-email.lastIndexOf('.') < 3 || email.length-email.lastIndexOf('.') > 5)
            b = false;
        if (email.length<6)
            b = false;
        return(b);
    }


// retourne un objet xmlHttpRequest.
// méthode compatible entre tous les navigateurs (IE/Firefox/Opera)
function getXMLHTTP(){
  var xhr=null;
  if(window.XMLHttpRequest) // Firefox et autres
  xhr = new XMLHttpRequest();
  else if(window.ActiveXObject){ // Internet Explorer
    try {
      xhr = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        xhr = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e1) {
        xhr = null;
      }
    }
  }
  else { // XMLHttpRequest non supporté par le navigateur
    return null;
  }
  return xhr;
}

function ajoutPanier(PDT_ID, QUANTITE){
  var xhr=getXMLHTTP();

  xhr.open("GET", "panier.php?so=a&PDT_ID="+PDT_ID+"&QUANTITE="+QUANTITE, false);
  xhr.send(null);

  alert(xhr.responseText);
}


function arrondiPrix(nombre){
  nombre = Math.round(nombre*100)/100 + 0.001;
  nombre += '';
  return nombre.substr(0, nombre.indexOf('.')+3);
}


//fonctions pour l'éditeur html
var navigateur = navigator.userAgent.toLowerCase();
var isGecko = ((navigateur.indexOf('gecko')!=-1) && (navigateur.indexOf('spoofer')==-1)
                && (navigateur.indexOf('khtml') == -1) && (navigateur.indexOf('netscape/7.0')==-1));

var insertion=false;
function insereBalise(ouverture, fermeture, exemple) {
  if(!insertion){
    insertion=true;
    var idTexte = document.getElementById('TEXTE');
    if(document.selection  && !isGecko){
      var laSelection = document.selection.createRange().text;
      if(!laSelection)
        laSelection=exemple;
      idTexte.focus();
      if(laSelection.charAt(laSelection.length - 1) == " "){
        laSelection = laSelection.substring(0, laSelection.length - 1);
        document.selection.createRange().text = ouverture + laSelection + fermeture + " ";
      }
      else
        document.selection.createRange().text = ouverture + laSelection + fermeture;
    }
    else if(idTexte.selectionStart || idTexte.selectionStart == '0'){
      var remplace = false;
      var posDebut = idTexte.selectionStart;
      var posFin = idTexte.selectionEnd;
      if(posFin-posDebut)
        remplace=true;
      var scrollTop=idTexte.scrollTop;
      var contenu = (idTexte.value).substring(posDebut, posFin);
      if(!contenu)
        contenu=exemple;
      if(contenu.charAt(contenu.length - 1) == " ")
        sousChaine = ouverture + contenu.substring(0, (contenu.length - 1)) + fermeture + " ";
      else
        sousChaine = ouverture + contenu + fermeture;
      idTexte.value = idTexte.value.substring(0, posDebut) + sousChaine +
        idTexte.value.substring(posFin, idTexte.value.length);
      idTexte.focus();
      if(remplace){
        var position=posDebut+(ouverture.length+contenu.length+fermeture.length);
        idTexte.selectionStart=position;
        idTexte.selectionEnd=position;
      }else{
        idTexte.selectionStart=posDebut+ouverture.length;
        idTexte.selectionEnd=posDebut+ouverture.length+contenu.length;
      }  
      idTexte.scrollTop=scrollTop;
    }
    if (idTexte.createTextRange)
      idTexte.caretPos = document.selection.createRange().duplicate();
    insertion=false;
    texte_change=true;
  }
}


function appliqueLien(){
  with (window.document.forms['editeur']) {
    lien=LIEN.value;
    if(lien.length==0 || lien=='http://'){
      alert('Veuillez indiquer un lien. ex: http://www.maman.fr');
      LIEN.value='http://';
      LIEN.focus();
    }
    else{
      if(lien.substr(0,7) == 'http://')
        libelle = lien.substr(7,lien.length - 7);
      else if(lien.substr(0,6) == 'ftp://')
        libelle = lien.substr(6,lien.length - 6);
      else if(lien.substr(0,8) == 'https://')
        libelle = lien.substr(8,lien.length - 8);
      else{
        libelle = lien;
        lien = 'http://'+lien;
        LIEN.value = lien;
      }
      insereBalise('[lien='+lien+']', '[/lien]', libelle);
    }
  }
}


var texte_change = false;
function auto_apercu(){
  if(texte_change){
    apercu();
    texte_change = false;
  }
}

var attente_apercu=false;
function apercu(){
  if(!attente_apercu){
    attente_apercu=true;
    document.getElementById('id_apercu').innerHTML = '<img src="images/widget_sablier.gif" alt="">';
    var xhr=getXMLHTTP();
    xhr.open('POST','ajax_commun.php?so=ap',true);
    xhr.onreadystatechange = function(){
      if (xhr.readyState == 4){
        document.getElementById('id_apercu').innerHTML = xhr.responseText;
        attente_apercu=false;
      }
    }
    xhr.setRequestHeader('Content-type','application/x-www-form-urlencoded');
    var data = 'TEXTE='+escape(document.forms.editeur.TEXTE.value);
    xhr.send(data);
  }
}

function pageAccueil(anchor){
  if(isIE){
    anchor.style.behavior='url(#default#homepage)';
    anchor.setHomePage('http://www.maman.fr/');
  }
  else if(window.external){
    if(confirm("Vous pouvez gérer votre page de démarrage depuis le menu outil->option->général->démarrage.\n\nSouhaitez-vous ajouter maman.fr à vos favoris ?"))
      window.external.AddFavorite(anchor.getAttribute('href'), anchor.getAttribute('title'));
  }
  else
    alert("Vous pouvez gérer votre page de démarrage depuis le menu outil->option->général->démarrage.");
}


function montre(id, display){
  document.getElementById(id).style.display=display;
}
function cache(id){
  document.getElementById(id).style.display='none';
}


function affSousTag(form, libelle, tag_id_1, tag_id_2, tag_id_3){
  span_id = document.getElementById(form+'_'+tag_id_1);
  span_id.innerHTML = '';

  if(tag_id_2){
    var xhr=getXMLHTTP();
    xhr.open('GET','ajax_commun.php?so=st&TAG_ID_2='+tag_id_2+"&TAG_ID_3="+tag_id_3,true);
    xhr.onreadystatechange = function(){
      if (xhr.readyState == 4){
        var aff='';
        span_id = document.getElementById(form+'_'+tag_id_1);
        if(xhr.responseText){
          aff += "<div class=row><label for=\""+form+"_TAB_TAG_ID_"+tag_id_2+"\">"+libelle+" : </label>";
          aff += "<select name=\"TAB_TAG_ID["+tag_id_2+"]\" id=\""+form+"_TAB_TAG_ID_"+tag_id_2+"\">";
          aff += "<option value=''>"+(tag_id_1 == '3' ? "(Ne pas préciser la ville)" : "...")+"</option>";
          aff += xhr.responseText;
          aff += "</select></div>";
        }
        span_id.innerHTML = aff;
      }
    }
    xhr.send(null);
  }
}


function getSelected(opt) {
    var selected = new Array();
    var index = 0;
    for (var intLoop = 0; intLoop < opt.length; intLoop++) {
       if ((opt[intLoop].selected) ||
           (opt[intLoop].checked)) {
          selected.push(opt[intLoop].value);
       }
    }
    return selected;
}


function affSousTagMultiple(form, libelle, tag_id_1, tag_id_2, tag_id_3){

  span_id = document.getElementById(form+'_'+tag_id_1)
  span_id.innerHTML = '';

  if(tag_id_2.length > 0){
    var xhr=getXMLHTTP();
    var tags = '';
    for(var i=0; i<tag_id_2.length; i++){
      if(tag_id_2[i].length > 0)
        tags += '&TAG_ID_2['+i+']='+tag_id_2[i];
    }
    for(i=0; i<tag_id_3.length; i++){
      if(tag_id_3[i].length > 0)
        tags += '&TAG_ID_3['+i+']='+tag_id_3[i];
    }
    if(tags.length > 0){
      xhr.open('GET','ajax_commun.php?so=st'+tags,true);
      xhr.onreadystatechange = function(){
        if (xhr.readyState == 4){
          var aff='';
          span_id = document.getElementById(form+'_'+tag_id_1);
          if(xhr.responseText){
            aff += "<div class=row><label for=\""+form+"_TAB_TAG_ID_"+tag_id_2+"\">"+libelle+" : </label>";
            aff += "<select name=\"TAB_TAG_ID["+tag_id_2+"][]\" id=\""+form+"_TAB_TAG_ID_"+tag_id_2+"\" multiple size=10>";
            aff += "<option value=''";
            if(tag_id_3.length == 0)
              aff += " selected"
            aff += ">...</option>";
            aff += xhr.responseText;
            aff += "</select></div>";
          }
          span_id.innerHTML = aff;
        }
      }
      xhr.send(null);
    }
  }
}

function checkQue() {
    with (window.document.forms['form_que']) {
        mes = "";


        if (LIBELLE.value.length == 0)
            mes += "Vous devez indiquer une question\n";

    }
    if (mes)
        alert(mes);
    return(mes.length == 0 ? true : false);
}

