// JavaScript Document

function SupprimeRecherche(id)
{
Ajax_FormSubmitWithData("includes/lib_client.php","POST","divList","recherche_id="+id+"&action=7");
Ajax_FormSubmitWithData("includes/lib_client.php","POST","divList","action=3");
}

//ajax validation formulaire, avec data, met la reponse direct dans l'element html specifie

function Ajax_FormSubmitWithData(actionpage,typeaction,htmlelementtofill,data)
{
  xhr = getXMLHTTP();

  xhr.open(typeaction, actionpage, true);
  
  xhr.onreadystatechange = function() {
      if(xhr.readyState == 4 && xhr.status == 200 && xhr.responseText)
      {
        var tmp = xhr.responseText;
        if(htmlelementtofill)
        {
          document.getElementById(htmlelementtofill).innerHTML = tmp;        
        }

     }
  }
  xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");  
  xhr.send(data);
}

function Ajax_FormSubmitWithDataTest(actionpage,formid,typeaction,data,htmlelementtofill)
{
  xhr = getXMLHTTP();

  xhr.open(typeaction, actionpage, true);

  var frmClient = document.forms[formid];
  
  if(!data)
  {
    data = "";
  }
  else
  {
    data += "&";
  }
  
  var items = frmClient.elements;
  
  for(var i = 0; i < items.length ; i++)
  {
    if(items[i].tagName== "INPUT")
    {
      if(items[i].type=="checkbox")
      {
        data += items[i].id + "=" + items[i].checked + "&";      
      }
      else if(items[i].type!= "button" && items[i].type!= "submit")
      {
        data += items[i].id + "=" + escape(items[i].value) + "&";      
      }
    }    
    if(items[i].tagName== "SELECT")
    {
    data += items[i].id + "=" + escape(items[i].value) + "&";
    }    
    if(items[i].tagName== "TEXTAREA")
    {
    data += items[i].id + "=" + escape(items[i].value) + "&";
    }  
  }
  
  data = data.substr(0,data.length-1);
  
  xhr.onreadystatechange = function() {
      if(xhr.readyState == 4 && xhr.status == 200 && xhr.responseText)
      {
        var tmp = xhr.responseText;
        document.getElementById(htmlelementtofill).innerHTML = tmp;
     }
  }
  xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");  
  xhr.send(data);
}

function ValiderCommande(form,confirmtext)
{
  var confirmation = true;
  
  if(document.getElementById('typereglement').value==1)
  {
    confirmation = confirm(confirmtext);
  }

  if(confirmation == true)
  {
    return true;
  }
  else
  {
    return false;
  }

}

//affiche le div contenant les options supplémentaire de la recherche avancee

function Show_Options(htmlelementtohide)
{
    var element = document.getElementById(htmlelementtohide);
    
    if(element.style.display == "block")
    {
      element.style.display = "none";
    }
    else
    {
      element.style.display = "block";
    }
}

//soumet le formulaire en js

function FormSubmit(formname)
{
  document.forms[formname].submit(); 
}

//vide un element de formulaire et le revalide dans la recherche avancee

function VideFormInput(id)
{
  var htmlinputelement = document.getElementById(id);
  htmlinputelement.value = "";
  document.forms["frmRechercheAvancee"].submit();
}

//on vide un element HTML
function ClearHTMLElement(id)
{
  var htmlelement = document.getElementById(id);
  htmlelement.innerHTML = "";
}

//fonction de max des images

function MaximizeImage(filename)
{
   var image = filename;
   document.getElementById('ImgZoomDiv').style.display = "block";
   document.getElementById('ImgZoomDiv').innerHTML = '<div><img style="height:300px;cursor:Pointer;" src="' + filename + '" /></div><div>cliquez sur la photo pour la fermer</div></div>';

largdispo = window.innerWidth;
hautdispo = window.innerHeight;

largeurdiv = document.getElementById('ImgZoomDiv').clientWidth;
hauteurdiv = document.getElementById('ImgZoomDiv').clientHeight;

l=(largdispo - largeurdiv)/2;
h=(hautdispo - hauteurdiv)/2;

document.getElementById('ImgZoomDiv').style.left = l + "px";
document.getElementById('ImgZoomDiv').style.top = h + "px";
}

//cache le div de zoom des images

function HideImage()
{
  document.getElementById('ImgZoomDiv').innerHTML = "";
  document.getElementById('ImgZoomDiv').style.display = "none";
}

//recupere un champ 

function Ajax_AdminTab(action,showcalendrier)
{
  
  var divDate = document.getElementById('divDate');
  
  if(showcalendrier==1)
  {
    divDate.style.display = 'block';
  }
  else
  {
    divDate.style.display = 'none';
  }  

  Clear();

  xhr = getXMLHTTP();
  var data = "action="+action;
  var typeaction = "POST";
  var actionpage = "../includes/lib_admin.php";

  xhr.open(typeaction, actionpage, true);
  
  xhr.onreadystatechange = function() {
      if(xhr.readyState == 4 && xhr.status == 200 && xhr.responseText)
      {
        var contenu = xhr.responseText;
        var divTabFields = document.getElementById('divTabFields');        
        divTabFields.innerHTML = contenu;
        
        var divDate = document.getElementById('divDate');
   
     }
  }
  xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");  
  xhr.send(data);
}

function Ajax_GetField(idfield)
{
  xhr = getXMLHTTP();
  var data = "id=" + idfield + "&action=0";
  var typeaction = "POST";
  var actionpage = "../includes/lib_admin.php";

  xhr.open(typeaction, actionpage, true);
  
  xhr.onreadystatechange = function() {
      if(xhr.readyState == 4 && xhr.status == 200 && xhr.responseXML)
      {
        var xmlDoc = xhr.responseXML;
        //alert(xhr.responseText);
        //on parse l'xml pour récuperer les champs du flux
        var libelle = xmlDoc.getElementsByTagName('libelle_champ')[0].firstChild.data;
        var contenu = xmlDoc.getElementsByTagName('contenu_champ')[0].firstChild.data;
        var id_champ = xmlDoc.getElementsByTagName('champ_id')[0].firstChild.data;
        if(xmlDoc.getElementsByTagName('champ_date')[0].firstChild)
        {
          var date = xmlDoc.getElementsByTagName('champ_date')[0].firstChild.data;
        }        
        var inputdate = document.getElementById('inputDate');
        var idchamp = document.getElementById('hdIdfield');
        var libellechamp = document.getElementById('libChamp');
        var contenuchamp = document.getElementById('contChamp');
        var hdnrte1 = document.getElementById('hdnrte1');
        currentRTE = "rte1";

        //var toto = rte1
        //rte1.value = contenu;
        ClearHTML();
        insertHTML(contenu);
        hdnrte1.value = contenu;
        updateRTEs();	
        libellechamp.value = libelle;
        idchamp.value = id_champ;
        inputdate.value = date;
        
        //et enfin on change la classe de la ligne selectionnée
        idrow = "rowAdmin_"+idfield;
        SelectRow(idrow);
     }
  }
  xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");  
  xhr.send(data);
}

function GetField(idfield)
{
  //on met l'id du champ dans la variable cachée
  var hdIdfield = document.getElementById("hdIdfield");
  hdIdfield.value = idfield;
  document.getElementById("hdAction").value=0;
  document.forms["fAdmin"].submit();
}
/*
function SyncWhizzy()
{
  rte1 = document.getElementById("whizzyedited");
  body = rte1.contentDocument.body;  
  contenu = body.innerHTML;
  document.getElementById("hdWiz").value=contenu;
}
*/
function SelectRow(idrow)
{
  if(idrow)
  {
  var selectedrow = document.getElementById(idrow);
  selectedrow.bgColor="#FF6600";
  }

  var tabparent = document.getElementById("tabChampsAdmin");
  var tabTR = tabparent.getElementsByTagName('tr');
  for(var i = 0; i < tabTR.length ; i++)
  {
    if(tabTR[i].id && tabTR[i].id!=idrow)
    {
    tabTR[i].bgColor="";    
    }

  }
}

//enresgitre un champ

function Ajax_SetField()
{
  xhr = getXMLHTTP();
  
  var libelle = document.getElementsByName('libChamp')[0].value;

  rte1 = window.frames["rte1"];
  body = rte1.document.body;
  idfield =  document.getElementById('hdIdfield').value;
  
  contenu = body.innerHTML;
  
  var data = "id=" + idfield + "&action=1&libelle=" + libelle + "&contenu=" + contenu;

  var divDate = document.getElementById('divDate');
  if(divDate.style.display != "none")
  {
    data  += "&date=" + document.getElementById('inputDate').value;
  }
  
  var typeaction = "POST";
  var actionpage = "../includes/lib_admin.php";
  
  xhr.open(typeaction, actionpage, true);
  
  xhr.onreadystatechange = function() {
      if(xhr.readyState == 4 && xhr.status == 200 && xhr.responseText)
      {
        var tmp = xhr.responseText;
        //alert(tmp);
        //document.getElementById('divResponse').innerHTML = tmp;
     }
  }
  xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");  
  xhr.send(data);
}

//nouveau champ
function Clear()
{
  document.getElementById("hdIdfield").value="";
  //document.getElementById("hdAction").value=3;
  document.getElementById("libChamp").value="";
}

//raffraichit la liste

function Ajax_RefreshField()
{
  xhr = getXMLHTTP();
  
  action = 2;
  var divDate = document.getElementById('divDate');
  if(divDate.style.display != "none")
  {
    action = 3;
  }
  var idfield =  document.getElementById('hdIdfield').value;
  var data = "action="+action;
  var typeaction = "POST";
  var actionpage = "../includes/lib_admin.php";
  
  xhr.open(typeaction, actionpage, true);
  
  xhr.onreadystatechange = function() {
      if(xhr.readyState == 4 && xhr.status == 200 && xhr.responseText)
      {
        var tmp = xhr.responseText;
        var tabfields =  document.getElementById('divTabFields');
        tabfields.innerHTML = tmp;
        idrow = "rowAdmin_"+idfield;
        SelectRow(idrow);
     }
  }
  xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");  
  xhr.send(data);
}

//ajoute au panier un element

function Ajax_AddToCart(idlivre)
{

  xhr = getXMLHTTP();
  var data = "action=1&idlivre=" + idlivre;
  var typeaction = "POST";
  var actionpage = "includes/lib_panier.php";

  xhr.open(typeaction, actionpage, true);
  
  xhr.onreadystatechange = function() {
      if(xhr.readyState == 4 && xhr.status == 200 && xhr.responseText)
      {
        var tmp = xhr.responseText;
        alert(tmp);        
     }
  }
  xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");  
  xhr.send(data);

}

//supprime du panier

function Ajax_RemoveFromCart(idlivre)
{
  //on retire l'element du panier
  xhr = getXMLHTTP();
  var data = "action=0&idlivre=" + idlivre;
  var typeaction = "POST";
  var actionpage = "includes/lib_panier.php";

  xhr.open(typeaction, actionpage, true);
  
  xhr.onreadystatechange = function() {
      if(xhr.readyState == 4 &&xhr.status == 200 && xhr.responseText)
      {
        var tmp = xhr.responseText;
        return tmp;
        
     }
  }
  xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");  
  xhr.send(data);
}

//rafraichit le panier
function Ajax_RefreshPanier()
{
  //juste pour recharger le tableau du panier dans panier.php
  xhr = getXMLHTTP();
  data = "refresh=1";
  typeaction = "POST";
  actionpage = "panier.php";

  xhr.open(typeaction, actionpage, true);
  
  xhr.onreadystatechange = function() {
      if(xhr.readyState == 4 && xhr.status == 200 && xhr.responseText)
      {
        var tmp = xhr.responseText;
        var divpanier = document.getElementById("divPanier");
        if(divpanier)
        {
          //alert(tmp);
          divpanier.innerHTML = tmp;
        }
     }
  }
  xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");  
  xhr.send(data);
}

//affiche le formulaire d'enregistrement de recherche client

function ShowDivEnrRecherche()
{
  var divenrrecherche = document.getElementById('divenrrecherche');
  //divenrrecherche.innerHTML = "";
  var inputlib = document.getElementById('inputlib');
  inputlib.value = "";

  document.getElementById("divenrrecherche").style.left = mouseX +"px";
  document.getElementById("divenrrecherche").style.top = mouseY +"px";
  divenrrecherche.style.display ='block';
}

//cache le form et vide les entrées

function HideRechercheDiv()
{
  var enrrechrsp = document.getElementById('enrrechrsp');
  enrrechrsp.innerHTML = ""; 
  document.getElementById('inputlib').value = "";
  var divenrrecherche = document.getElementById('divenrrecherche');
  divenrrecherche.style.display ='none';
}

//enregistre une recherche client

function Ajax_EnregistrerRecherche(uri)
{

  xhr = getXMLHTTP();
  var libelle = document.getElementById('inputlib').value;
  var data = "uri=" + uri + "&libelle=" + libelle;
  var typeaction = "POST";
  var actionpage = "includes/enrrecherche.php";
  
  xhr.open(typeaction, actionpage, true);
  
  xhr.onreadystatechange = function() {
      if(xhr.readyState == 4 && xhr.status == 200 && xhr.responseText)
      {
        var tmp = xhr.responseText;
        var enrrechrsp = document.getElementById('enrrechrsp');
        enrrechrsp.innerHTML = tmp;
        document.getElementById('inputlib').value = "";
     }
  }
  xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");  
  xhr.send(data);
}

//envoi d'une question a abraxas

function Ajax_Send()
{
  xhr = getXMLHTTP();
  var divmail = document.getElementById('divmail');
  var nom = document.getElementById('inputnom').value;
  var mail = document.getElementById('inputmail').value;
  var message = document.getElementById('inputmessage').value;
  var idlivre =  document.getElementById('inputidlivre').value;
  var ref =  document.getElementById('inputhiddenref').value;
  
  var data = "ref=" + ref + "&name=" + nom + "&mail=" + mail + "&message=" + escape(message);
  
  var typeaction = "POST";
  var actionpage = "includes/lib_ajaxmail.php";

  xhr.open(typeaction, actionpage, true);
  
  xhr.onreadystatechange = function()
  {
      if(xhr.readyState == 4 && xhr.status == 200 && xhr.responseText)
      {
        var tmp = xhr.responseText;
        var mailrsp = document.getElementById('mailrsp');
        mailrsp.innerHTML = tmp; 
        
        document.getElementById('inputnom').value = "";
        document.getElementById('inputmail').value = "";
        document.getElementById('inputmessage').value = "";
        document.getElementById('inputidlivre').value = "";
     }
  }
  xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");  
  xhr.send(data);
}

//affiche le div pour envoyer un mail

function AfficheDivMail(idlivre,lienmail,ref)
{
  var divmail = document.getElementById('divmail');
  
  var lienmail = document.getElementById(lienmail);
  
  var hiddenidlivre =  document.getElementById('inputidlivre');
  hiddenidlivre.value = idlivre;
  var hiddenref =  document.getElementById('inputhiddenref');
  hiddenref.value = ref;
  
  document.getElementById("divmail").style.left = mouseX +"px";
  document.getElementById("divmail").style.top = mouseY +"px";
  divmail.style.display ='block';
}

//cache ce meme div et supprime les entrees de formulaire

function HideMailDiv()
{
  var mailrsp = document.getElementById('mailrsp');
  mailrsp.innerHTML = "";
  document.getElementById('inputnom').value = "";
  document.getElementById('inputmail').value = "";
  document.getElementById('inputmessage').value = "";
  document.getElementById('inputidlivre').value = "";
  document.getElementById('inputhiddenref').value = "";
  var divmail = document.getElementById('divmail');
  divmail.style.display ='none';
}

//pour la recup de mot de passe
function AfficheDivRecupMDP()
{
  var divrecupmdp = document.getElementById('divrecupmdp');
  divrecupmdp.style.left = mouseX +"px";
  divrecupmdp.style.top = mouseY +"px";
  divrecupmdp.style.display ='block';
}

function Ajax_RecupMDP()
{
  var xhr = getXMLHTTP();
  var divmail = document.getElementById('divmail');
  var mail = document.getElementById('inputmail').value;
  
  var data = "action=4&m=" + mail;
  
  var typeaction = "POST";
  var actionpage = "includes/lib_client.php";

  xhr.open(typeaction, actionpage, true);
  
  xhr.onreadystatechange = function() {
      if(xhr.readyState == 4 && xhr.status == 200 && xhr.responseText)
      {
        var tmp = xhr.responseText;
        var mailrsp = document.getElementById('mailrsp');
        mailrsp.innerHTML = tmp;
        document.getElementById('inputmail').value = "";
     }
  }
  xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");  
  xhr.send(data);
}

function HideDivRecupMDP()
{
  var mailrsp = document.getElementById('mailrsp');
  mailrsp.innerHTML = ""; 
  document.getElementById('inputmail').value = "";
  divmail = document.getElementById('divrecupmdp');
  divmail.style.display ='none';
}

function CalculeTotFdpIn()
{
  //on recupère le poids total
  var poids = document.getElementById('libTotalPoids').innerHTML;
  //on recupère le type d'envoi
  var typeenvoi = document.getElementById('port').value;
  //on récupère le total en cours
  var curtotal = document.getElementById('hdTotal').value;
  //on récupère le pays en cours dans la lise
  var pays_id = document.getElementById('fPays').value;  
  
  var data = "p=" + poids + "&t=" + typeenvoi + "&pays=" + pays_id;

  xhr = getXMLHTTP();

  xhr.open("POST", "includes/Ajax_getfdp.php", true);
  
  xhr.onreadystatechange = function() {
      if(xhr.readyState == 4 && xhr.status == 200 && xhr.responseText)
      {
        fdp = xhr.responseText;
        document.getElementById('libFdp').innerHTML = fdp+"\u20AC";
        document.getElementById('total').innerHTML = (parseFloat(fdp) + parseFloat(curtotal))+"\u20AC";
     }
  }
  xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");  
  xhr.send(data);
}

function PopUpCGV()
{
window.open ('includes/popup_cgv.php',"popup", "height=600,width=600,menubar=no,scrollbars=yes,resizable=no,left=0,top=10"); ; 
}

function PopUpFDP()
{
window.open ('includes/popup_fdp.php',"popup", "height=600,width=600,menubar=no,scrollbars=yes,resizable=no,left=0,top=10"); ; 
}

var isIE = document.all;
var mouseX = 0;
var mouseY = 0;

function getMouseXY(e)
	{
		if (!e) e = window.event;
		if (e)
			{
				mouseX = isIE ? (e.clientX + document.body.scrollLeft) : e.pageX;
				mouseY = isIE ? (e.clientY + document.body.scrollTop) : e.pageY;
			}
	}

document.onmousemove = getMouseXY;

function CalculeFraisDePort()
{	
 
	var windowOptions = 'history=no,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=no,resizable=no,width=500,height=600';
	windowOptions = 'history=no,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=no,resizable=yes,width=500,height=600';
  window.open('includes/fdp.php?pays=&poids=&type=&prix=', 'Frais de port', windowOptions);

}