/**
* Plus / Moins pour input
*
*/
function moins(form_name, field_name, min){
    var qte = (document.forms[form_name].elements[field_name].value * 1) - 1;
    if(qte >= 0) document.forms[form_name].elements[field_name].value = qte;
    else document.forms[form_name].elements[field_name].value = '0';
    if(qte < min) document.forms[form_name].elements[field_name].value = qte + 1;
    if(document.forms[form_name].elements[field_name].value == 0) document.forms[form_name].elements[field_name].value = '1';
}
function plus(form_name, field_name, max){
    var qte = (document.forms[form_name].elements[field_name].value * 1) + 1;
    if(qte > max) document.forms[form_name].elements[field_name].value = qte - 1;
    else document.forms[form_name].elements[field_name].value = qte;
    if(document.forms[form_name].elements[field_name].value == 0) document.forms[form_name].elements[field_name].value = '1';
}

/**
* Vérification de la valeur d'une variable javascript
*
*/
function IsNumeric(sText)
{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;
   for (i = 0; i < sText.length && IsNumber == true; i++)
      {
      Char = sText.charAt(i);
      if (ValidChars.indexOf(Char) == -1)
         {
         IsNumber = false;
         }
      }
   return IsNumber;
}

/**********************************************************/

/* Navigation au travers des menus verticaux-horizontaux */
function montre(id) {
    var d = document.getElementById(id);
        for (var i = 1; i<=20; i++) {
            if (document.getElementById('niveau'+i)) {
                document.getElementById('niveau'+i).style.display='none';
            }
        }
    if (d) {d.style.display='block';}
}
/**********************************************************/

/* Montre ou cache un element */
function montre_statut(id,statut) {
    var d = document.getElementById(id);
    if(statut != ""){
        if (d) {d.style.display=statut;}
    }
    else{
        if (d) {
            if(d.style.display == "block"){statut = "none";}
            else statut = "block";
            d.style.display=statut;
        }
    }
}
/**********************************************************/

/* Confirmation suppression */
function confirm_delete(){
    var valret;
    valret = confirm("Confirmez-vous la suppression de cet élément ?");
    return valret;
}
/******************************************************/
/* Confirmation modification */
function confirm_mod(){
    var valret;
    valret = confirm("Confirmez-vous les modifications effectuées sur cet élément ?");
    return valret;
}
/******************************************************/

/* Fonction d'ouverture d'une popup */
function OuvrirPop(theURL,features,h,w){
    splits = theURL.split(".");
    window.open(theURL,splits[0],features+',width='+w+',height='+h+',top='+((screen.height-h)/2)+',left='+((screen.width-w)/2));
}

/******************************************************/
/******************************************************/
// the timeout for the menu
var timeout = 10;

// not very clean but simple
// the function can be run in the HTML for faster display
// window.onload=initMenu;

// creat timeout variables for list item
// it's for avoid some warning with IE
for( var i = 0; i < 100; i++ )
{
    eval("var timeoutli" + i + " = false;");
}

// this fonction apply the CSS style and the event
function initMenu()
{
    // a test to avoid some browser like IE4, Opera 6, and IE Mac
    if ( browser.isDOM1 
    && !( browser.isMac && browser.isIE ) 
    && !( browser.isOpera && browser.versionMajor < 7 )
    && !( browser.isIE && browser.versionMajor < 5 ) )
    {
        if (!document.getElementById('pmenu'))
			return;
		 // get some element
       var menu = document.getElementById('pmenu'); // the root element
        var lis = menu.getElementsByTagName('li'); // all the li
        
        // change the class name of the menu, 
        // it's usefull for compatibility with old browser
        menu.className='pmenu';
        
        // i am searching for ul element in li element
        for ( var i=0; i<lis.length; i++ )
        {
            // is there a ul element ?
            if ( lis.item(i).getElementsByTagName('ul').length > 0 )
            {        
                // improve IE key navigation
                if ( browser.isIE )
                {
                    addAnEvent(lis.item(i),'keyup',show);
                }
                // link events to list item
                addAnEvent(lis.item(i),'mouseover',show);
                addAnEvent(lis.item(i),'mouseout',timeoutHide);
                addAnEvent(lis.item(i),'blur',timeoutHide);
                addAnEvent(lis.item(i),'focus',show);
                
                // add an id to list item
                lis.item(i).setAttribute( 'id', "li"+i );
            }
        }
    }
}

function addAnEvent( target, eventName, functionName )
{
    // apply the method to IE
    if ( browser.isIE )
    {
        //attachEvent dont work properly with this
        eval('target.on'+eventName+'=functionName');
    }
    // apply the method to DOM compliant browsers
    else
    {
        target.addEventListener( eventName , functionName , true ); // true is important for Opera7
    }
}
    
// hide the first ul element of the current element
function timeoutHide()
{
    // start the timeout
    eval( "timeout" + this.id + " = window.setTimeout('hideUlUnder( \"" + this.id + "\" )', " + timeout + " );");
}

// hide the ul elements under the element identified by id
function hideUlUnder( id )
{   
    document.getElementById(id).getElementsByTagName('ul')[0].style['visibility'] = 'hidden';
    document.getElementById(id).getElementsByTagName('a')[0].className = 'linkOut';
}

// show the first ul element found under this element
function show()
{
    // show the sub menu
    this.getElementsByTagName('ul')[0].style['visibility'] = 'visible';
    var currentNode=this;
    while(currentNode)
    {
            if( currentNode.nodeName=='LI')
            {
                currentNode.getElementsByTagName('a')[0].className = 'linkOver';
            }
            currentNode=currentNode.parentNode;
    }
    // clear the timeout
    eval ( "clearTimeout( timeout"+ this.id +");" );
    hideAllOthersUls( this );
}

// hide all ul on the same level of  this list item
function hideAllOthersUls( currentLi )
{
    var lis = currentLi.parentNode;
    for ( var i=0; i<lis.childNodes.length; i++ )
    {
        if ( lis.childNodes[i].nodeName=='LI' && lis.childNodes[i].id != currentLi.id )
        {
            hideUlUnderLi( lis.childNodes[i] );
        }
    }
}

// hide all the ul wich are in the li element
function hideUlUnderLi( li )
{
    var as = li.getElementsByTagName('a');
    for ( var i=0; i<as.length; i++ )
    {
        as.item(i).className="";
    }
    var uls = li.getElementsByTagName('ul');
    for ( var i=0; i<uls.length; i++ )
    {
        uls.item(i).style['visibility'] = 'hidden';
    }
} 

// Permet d'afficher une image en grand
function Affichegrande(cheminImage,texte)
{
    newImage = new Image;
    newImage.src = cheminImage;
    html = '<HTML><HEAD><TITLE>'+texte+'</TITLE><meta http-equiv="Pragma" content="no-cache"></HEAD><BODY leftmargin=0 marginwidth=0 topmargin=0 marginheigth=0 oncontextmenu="return false"><CENTER>'+
    '<a href="#" onClick="window.close()"><IMG SRC="'+cheminImage+'" BORDER=0 NAME=monImage alt="'+texte+'"border="0" onLoad="window.resizeTo(document.monImage.width+20,document.monImage.height+80); window.moveTo((screen.width-document.monImage.width)/2,5)"> </a></CENTER></BODY></HTML>';
    ouvrirImage = window.open('','_blank','toolbar=0,location=0,menuBar=0,scrollbars=0,resizable=0');
    ouvrirImage.document.write(html);
}

var strUrl = "ajax.php";
var xmlhttp;

function getXmlRequest() {
  try
    {
    if (is_ie5up) xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
    // Firefox, Opera 8.0+, Safari
    else xmlhttp=new XMLHttpRequest();
    }
  catch (e)
    {
    // Internet Explorer
    try
      {
      xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
      }
    catch (e)
      {
      try
        {
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
      catch (e)
        {
        alert("Your browser does not support AJAX!");
        return false;
        }
      }
    }
    return xmlhttp;
}

function checkPrix(id,qte,prix)
{
  url = strUrl + "?cle=" + id + "&qte=" + qte + "&prix=" + prix;
  //alert(url);
  xmlhttp=getXmlRequest();
  if (xmlhttp)
  {
    xmlhttp.open("GET", url, false);
    xmlhttp.send(null);
    return xmlhttp.responseText; 
  }               
  
}

function checkLivr(pays,poids)
{
  url = strUrl + "?pays=" + pays + "&poids=" + poids;
  //alert(url);
  xmlhttp=getXmlRequest();
  if (xmlhttp)
  {
    xmlhttp.open("GET", url, false);
    xmlhttp.send(null);
    return xmlhttp.responseText; 
  }               
  
}

function coupon_valide(code)
{
    url = strUrl + "?code=" + code;
    xmlhttp=getXmlRequest();
    if (xmlhttp)
    {
        xmlhttp.onreadystatechange = function(){
            if(xmlhttp.readyState==4 )
            {                                                        
                document.getElementById("code_valide").innerHTML=xmlhttp.responseText;
            }
        }
        xmlhttp.open("GET", url, true);
        xmlhttp.send(null);
    }
}

function inputCheck(type,fieldname,fieldvalue,comparatif,annee)
{
  url = strUrl + "?type=" + type + "&value=" + fieldvalue + "&comparatif=" + comparatif + "&annee=" + annee;
  //alert(url);
  xmlhttp=getXmlRequest();
  if (xmlhttp)
  {
    xmlhttp.onreadystatechange = function(){
      var fieldid = fieldname + "_error";
      if(xmlhttp.readyState==4)
      {
        document.getElementById(fieldid).innerHTML=xmlhttp.responseText;
        if(xmlhttp.responseText.indexOf('ok.gif') > 0) document.forms["identification"].elements[fieldname].style.backgroundColor = "";
        }
    }
    xmlhttp.open("GET", url, true);
    xmlhttp.send(null);
  }
}               
function cloture_paiement(mode_paie,type_paie)
{
    url = strUrl + "?mode_paie=" + mode_paie + "&type_paie=" + type_paie;
    //alert(url);
    xmlhttp=getXmlRequest();
    if (xmlhttp)
    {
        xmlhttp.open("GET", url, false);
        xmlhttp.send(null);
        return xmlhttp.responseText; 
    }               

}
function addFavoris(url,titre)
{
  var reg=new RegExp("(&)", "g");
  url = url.replace(reg,"µ")
  if(url.indexOf("#") != -1) url = url.substring(0,url.length-1);
  url = "url=" + url + "&titre=" + titre;
  //alert(url);
  xmlhttp=getXmlRequest();
  if (xmlhttp)
  {
        xmlhttp.onreadystatechange = function(){
            if(xmlhttp.readyState==4 )
            {   
                if(xmlhttp.responseText == "true"){
                    alert('Lien ajouté');
                    window.document.location.reload();
                    return true;
                }
                else{
                    alert('Lien déjà présent !');
                    return false;
                }
            }
        }
        xmlhttp.open("POST", "../" + strUrl, true);
        xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
        xmlhttp.send(url);
  }               
  
}

function delFavoris(fav)
{
  url = "../" + strUrl + "?delFav=" + fav;
  //alert(url);
  xmlhttp=getXmlRequest();
  if (xmlhttp)
  {
        xmlhttp.onreadystatechange = function(){
            if(xmlhttp.readyState==4 )
            {   
                if(xmlhttp.responseText == "true"){
                    alert('Lien supprimé');
                    window.document.location.reload();
                    return true;
                }
                else{
                    return false;
                }
            }
        }
        xmlhttp.open("GET", url, true);
        xmlhttp.send(null);
  }               
  
}
function openMenu(id)
{
  url = "../" + strUrl + "?openMenu=" + id;
  xmlhttp=getXmlRequest();
  if (xmlhttp)
  {
        xmlhttp.onreadystatechange = function(){
            if(xmlhttp.readyState==4 )
            {   
                return true;
            }
        }
        xmlhttp.open("GET", url, false);
        xmlhttp.send(null);
  }               
}

