var url_base = "";

function getTransport() {
    if (window.XMLHttpRequest) {
        return new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        try {
            return new ActiveXObject('Msxml2.XMLHTTP');
        } catch(e) {
            return new ActiveXObject('Microsoft.XMLHTTP');
        }
    }
}
//======================
function valid_form(mod){

var nom = document.getElementById('nom').value;
var prenom = document.getElementById('prenom').value;
   var tel = document.getElementById('tel').value;
   var  mobile = document.getElementById('mobile').value;
  var  adresse = document.getElementById('adresse').value;
  var  cp = document.getElementById('cp').value;
  var ville = document.getElementById('ville').value;
   var email = document.getElementById('email').value;
   var objet = document.getElementById('objet').value;
   var commentaire = document.getElementById('commentaire').value;
   var newsletter = document.getElementById('newsletter').checked;

var xhr = getTransport();
      xhr.onreadystatechange = function() {
      update_form(xhr);
    }
    var adr = url_base + '/php/include/valide_contact.php';
    if(mod == 2)
     adr = url_base + '/php/include/valide_devis.php';
     
    xhr.open('get', adr + '?nom=' + nom + '&prenom=' + prenom 
    + '&tel=' + tel + '&mobile=' + mobile 
    + '&adresse=' + adresse + '&cp=' + cp + '&ville=' + ville 
    + '&email=' + email 
    + '&objet=' + objet + '&commentaire=' + commentaire
    + '&newsletter=' + newsletter  , true);
    xhr.send('');


}


function update_form(xhr){
    if (xhr.readyState == 4) {
        if (xhr.status == 200) {
   

      var content = document.getElementById('reponse_form');
             content.innerHTML =xhr.responseText;
             recalcul();

        }
        else {
            alert('Erreur: ' + xhr.status + '!');
        }
    }
}
function ferme_image(){
var Obj;
  //-- on cache le fond
  Obj = document.getElementById('D_FOND');
  if( Obj){
  
   var content_img = document.getElementById('container_image');
    var site = document.getElementById('principal');
       var image = document.getElementById('grande');
   if( content_img){

   with(  content_img.style){
        // visibility="hidden";
              disappear(content_img,Obj);
    }
    
  
    }
  
   // with( Obj.style){
   // }
   
  }
}
//======================================
var bool_img = false;
var time_out_img1;
var time_out_img2;
function affiche_image(chemin_img){
var Obj;
  Obj = document.getElementById('D_FOND');
  if( Obj){
   var content_img = document.getElementById('container_image');
   var site = document.getElementById('principal');
    var image = document.getElementById('grande');
     with( Obj.style){
        visibility="visible";
    }
   if( content_img){
    if(image)
    {
    image.width = 508;
      var marge_left = ( 960 - image.width )/2;
    var marge_top = 100;
     with(  content_img.style){
    width = image.width +'px';
    marginLeft = marge_left  +'px';
    if(marge_top > 0 );
    marginTop = marge_top +'px';
     visibility="visible";  
     } 
    }      
      //===========================  
   if(image)
   {
  // image.src = "";
  var test = new Image(); 
  bool_img = true;
  test.onload = function()
   { 
   if(bool_img){
    image.src = test.src;
    image.width = test.width;
    if(image.width >960)
    image.width = 960;
    marge_left = ( 960 - image.width )/2;
     marge_top = 100;
   
    with(  content_img.style){
    width = image.width +'px';
    marginLeft = marge_left  +'px';
    if(marge_top > 0 );
      marginTop = marge_top +'px';
      visibility="visible";   
      appear(content_img,0);
      }  
     }    
    }
    if(bool_img)
    test.src = chemin_img;
   }
   //===========================  
  } 
}

}
//==================
function appear(content, value) {
    if (typeof value == 'undefined') value = 0;
       if (value >1){ 
       clearTimeout(time_out_img1);
     return;
    }
    var property = content.filters ? 'filter' : 'opacity';
    content.style[property] = content.filters ? 'alpha(opacity=' + value*100 + ')' : value;
  time_out_img1 =  setTimeout(function () {appear(content, value + .1)}, 50);
}
//==============
function disappear(content,content2, value) {
var image = document.getElementById('grande');
    if (typeof value == 'undefined') value = 1;
    if (value < 0.2){ 
      
      image.src = "/image/loading.gif";
    with(  content.style){
        visibility="hidden";
    }
       with(  content2.style){
    visibility="hidden";
    }
     bool_img = false;
    clearTimeout(time_out_img2);
     return;
    }
    var property = content.filters ? 'filter' : 'opacity';
    content.style[property] = content.filters ? 'alpha(opacity=' + value*100 + ')' : value;
    var new_taille =  image.width - (image.width)/10;
    if(image.width > 0){
   image.width = new_taille;
    with(  content.style){
    width = image.width +'px';
     var marge_left = ( 960 - image.width )/2;
    marginLeft = marge_left  +'px';
     }
    }
   time_out_img2 =   setTimeout(function () {
           disappear(content, content2,value - .1)}, 50);
}

//=======
function ShowForm(val){
  var Obj;
  //-- on affiche le fond
   if(val == "2")
     Obj = document.getElementById('D_FOND1');
  else
  Obj = document.getElementById('D_FOND');
  if( Obj){
    with( Obj.style){
        visibility="visible";
    }
  }

}

//-----------------
function HideForm(val){
  var Obj;
 
  //-- on masque le fond  
  if(val == "2")
  Obj = document.getElementById('D_FOND1');
  else
  Obj = document.getElementById('D_FOND');
  if( Obj){
    disappear(Obj, 1);
  }
}

//============
//========================
function ajoute_prod(){
var ref_art  = document.getElementById('ref_article').value;
var xhr = getTransport();
      xhr.onreadystatechange = function() {
      update_achat(xhr);
    }
    xhr.open('get', url_base + '/php/classe/achat_article.php?ref=' + ref_art  , true);
    xhr.send('');


}
function add_prod(ref_art){

var xhr = getTransport();
      xhr.onreadystatechange = function() {
      update_panier(xhr);
    }
    xhr.open('get',  url_base + '/php/classe/ajoute_article.php?ref=' + ref_art , true);
    xhr.send('');


}
function sup_prod(ref_art){


var xhr = getTransport();
      xhr.onreadystatechange = function() {
      update_panier(xhr);
    }
    xhr.open('get',  url_base + '/php/classe/efface_article.php?ref=' + ref_art  , true);
    xhr.send('');


}
function update_achat(xhr){
    if (xhr.readyState == 4) {
        if (xhr.status == 200) {
    var reponse = xhr.responseText;
            if(reponse == "ok"){
            anim_ajout();
           
             }

        }
        else {
            alert('Erreur: ' + xhr.status + '!');
        }
    }
}
function anim_ajout(){
montre("Produit ajouté.");
//if(navigator.appName.substring(0,3) == "Net")
document.onmousemove=move;
setTimeout(function () {cache()}, 1500);
}
//==================

//*****************
function GetId(id)
				{
				return document.getElementById(id);
				}
				
//*****************				
function move(e) {
		if (navigator.appName!="Microsoft Internet Explorer") { // Si on est pas sous IE
			 GetId("curseur").style.left=e.pageX + 5+"px";
			 GetId("curseur").style.top=e.pageY + 10+"px";
				 }
			 else { // Modif proposé par TeDeum, merci à lui
		
				    if(document.documentElement.clientWidth>0) {
				      GetId("curseur").style.left=20+event.x+document.body.scrollLeft+ document.getElementById('principale').offsetLeft + "px";
				        GetId("curseur").style.top=300+event.y+document.body.scrollTop+"px";
				      //  GetId("curseur").style.left=20+event.x+document.documentElement.scrollLeft+"px";
				      //  GetId("curseur").style.top=10+event.y+document.documentElement.scrollTop+"px";
				    }
				    else {
				     GetId("curseur").style.left=20+event.x+"px";
				        GetId("curseur").style.top=10+event.y+"px";
				       // GetId("curseur").style.left=20+event.x+document.body.scrollLeft+"px";
				       // GetId("curseur").style.top=10+event.y+document.body.scrollTop+"px";
				    }
				}
				  

}
//*****************						
function montre(text) {
GetId("curseur").style.visibility="visible"; // Si il est cacher (la verif n'est qu'une securité) on le rend visible.
GetId("curseur").innerHTML = text; // Cette fonction est a améliorer, il parait qu'elle n'est pas valide (mais elle marche)

}
				function cache() {
				GetId("curseur").style.visibility="hidden"; // Si la bulle etais visible on la cache
				//		if(navigator.appName.substring(0,3) == "Net")
        document.onmousemove=null;
				//GetId("curseur").style.left ="0px";
			//	GetId("curseur").style.top="0px";
		
				  recalcul();
				return;
				}


//***********
function update_panier(xhr){
    if (xhr.readyState == 4) {
        if (xhr.status == 200) {
   

      var content = document.getElementById('list_panier');
             content.innerHTML =xhr.responseText;
             recalcul();

        }
        else {
            alert('Erreur: ' + xhr.status + '!');
        }
    }
}
//=============================================================
function recalcul(){


var xhr = getTransport();
      xhr.onreadystatechange = function() {
      update_nb(xhr);
    }
    xhr.open('get',  url_base + '/php/classe/nb_panier.php' , true);
    xhr.send('');


}
function update_nb(xhr){
    if (xhr.readyState == 4) {
        if (xhr.status == 200) {
      var content = document.getElementById('nb_art');
             content.innerHTML =xhr.responseText;
        }
        else {
            alert('Erreur: ' + xhr.status + '!');
        }
    }
}
//==============================================================
function proced_this() {
  var mot = document.getElementById('recherche').value;
  var adr = document.getElementById('cherche_adr').href;
  window.location.href = adr  + mot +"/";
}

function set_requete(sql){
requete = sql;
affiche_result();
}
//===================================
var obj_save=null;
var	Position =0;
var longueur_total = 400;
var time_out;
var time_out2;
var diff_x_ie=0;
function init_longueur(longueur_bandeau) {	
obj_save=null;	
Position =0;
longueur_total = longueur_bandeau + 10;
if(longueur_total <400){
document.getElementById('scoll_image').style.left =((400 - longueur_total)/2)  + "px";
}
}
function position(obj) {
if(bool_img == false)
if(obj_save == null){

obj_save =obj;
if(longueur_total >400){
if (navigator.appName!="Microsoft Internet Explorer") 
document.onmousemove=deplace;
else{
document.onmousemove=deplace_ie;
}
}

}
}
function stop_position() {
if(bool_img == false){
document.onmousemove=null;
obj_save = null;
}
}

function deplace(e) {
clearTimeout(time_out);
clearTimeout(time_out2);

time_out = setTimeout(function () {deplace2(e)}, 50);
}
function deplace2(e) {
if(obj_save != null){
var diff_x;
 diff_x = e.pageX - (obj_save.offsetLeft + document.getElementById('principale').offsetLeft);

var Vitesse = ( (diff_x - (400/2)) ) /10;
Position=Position-Vitesse;
	var ma_gauche = -(longueur_total - 400);
			
var ma_droite =0;
	if (Position<ma_gauche) {
					Position=ma_gauche;
				}
				else if (Position>ma_droite) {
					Position=ma_droite;
				}
			  
				  document.getElementById('scoll_image').style.left =Position + "px";
  
time_out2 = setTimeout(function () {deplace2(e)}, 50);
}
}

		//=====================
function deplace_ie(e) {
diff_x_ie =   event.clientX - (obj_save.offsetLeft + document.getElementById('principale').offsetLeft)  ;
clearTimeout(time_out2);
deplace_ie_suite();
}
function deplace_ie_suite() {
//document.getElementById('log').innerHTML =event.x  +"<br />";
if(obj_save != null){
try{
var Vitesse = ( (diff_x_ie - (400/2)) ) /10;
Position=Position-Vitesse;
var ma_gauche =-(longueur_total  - 400);
var ma_droite =0;
	if (Position<ma_gauche) {
					Position=ma_gauche;
				}
				else if (Position>ma_droite) {
					Position=ma_droite;
				}
document.getElementById('scoll_image').style.left =Position + "px";
//alert(test4);
clearTimeout(time_out2);
time_out2 = setTimeout(function () {deplace_ie_suite()}, 30);
}catch(err){
clearTimeout(time_out2);
time_out2 = setTimeout(function () {deplace_ie_suite()}, 30);
}
}
}

		//=====================
		function affiche_Menu(obj){
var el = document.getElementById(obj);

		if(el.style.display != "block"){ //DynamicDrive.com change
			el.style.display = "block";

		}else{
			el.style.display = "none";

		}

}
