// JavaScript Document
var CookieEnabled = (document.cookie.length>0);
var sAgent = navigator.userAgent.toLowerCase() ;
if (sAgent.indexOf("msie") == -1) {
  var IE = 0;
} else { var IE = 1; }
var mouseX = 0;
var mouseY = 0;
function getMouseXY() {
  if (IE) { // grab the x-y pos.s if browser is IE
    tempX = event.clientX + document.body.scrollLeft
    tempY = event.clientY + document.body.scrollTop
  } else {  // grab the x-y pos.s if browser is NS
    tempX = e.pageX
    tempY = e.pageY
  }  
  // catch possible negative values in NS4
  if (tempX < 0){tempX = 0}
  if (tempY < 0){tempY = 0}  
  // show the position values in the form named Show
  // in the text fields named MouseX and MouseY
  /*mouseX = tempX;
  mouseY = tempY;*/
  return {x:tempX, y:tempY};
  //alert(mouseX+'x'+mouseY);
}
var mousePos;
if (!IE) { document.onmousemove = mouseMove; }
function mouseMove(ev){
	ev           = ev || window.event;
	mousePos = mouseCoords(ev);
}

function mouseCoords(ev){
	if(ev.pageX || ev.pageY){
		return {x:ev.pageX, y:ev.pageY};
	}
	return {
		x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,
		y:ev.clientY + document.body.scrollTop  - document.body.clientTop
	};
}
function selectAddOption(sId, oText, oVal, where) {
	sTmp = _Elem(sId);
	nbOptions = sTmp.options.length;
	
	if (where == 'top') {
		for(i=nbOptions; i >= 1  ; i--) {
			sTmp.options[i] = new Option (sTmp.options[i-1].innerHTML, sTmp.options[i-1].value);
		}
		sTmp.options[0] = new Option(oText, oVal);
	} else {
		sTmp.options[nbOptions] = new Option(oText, oVal);
	}
}
// surcharge de la fonction getElementById pour simplifer le codage
function _Elem(id) {
  return document.getElementById(id);
}

function Show() {
	for (var i = 0; i < arguments.length; ++i) {
		_Elem(arguments[i]).style.display = '';
		_Elem(arguments[i]).style.visibility = 'visible';
	}
}

function Hide() {
	for (var i = 0; i < arguments.length; ++i) {
		_Elem(arguments[i]).style.display = 'none';
		_Elem(arguments[i]).style.visibility = 'hidden';
	}
}

function showHide(id) {
	if (_Elem(id).style.display != 'none')
		Hide(id);
	else
		Show(id);
		
}

function ucFirst(str) {
   return str.substr(0,1).toUpperCase() + str.substr(1,str.length);
}

function checkMail(adresse){

var regEx = /([a-z0-9]+([-_]?[a-z0-9]+)*.)*[a-z0-9]+([-_]?[a-z0-9]+)*@([a-z0-9]+([-_]?[a-z0-9]+)*.)+[a-z]{1,4}/gi;  

if (adresse.match(regEx) != adresse ) {

 return false;

} else {

 return true;

}

}

var cp = '';
var old_cp = '';
var villePrx = '';
function doVilles(prx) {
	var remoteVilles = new villes(villesCallBack);
  cp = _Elem(prx+'code_postal').value;
  if (cp != old_cp){
    if (cp) { remoteVilles.getvilles(cp); }
    old_cp = cp ;
	villePrx = prx;
  }
}

var villesCallBack = {
  getvilles: function (aryVilles) {
    var sel = _Elem(villePrx+'ville_id');
    sel.length=0;

	if (!aryVilles) {
		var res = document.createElement('option');
        res.value=0;
        res.innerHTML='Pas de villes pour ce Code postal';
        sel.appendChild(res);
	}
    else {
      for (var i = 0; i < aryVilles.length; ++i) {
        var res = document.createElement('option');
        res.value=aryVilles[i]['ville_id'];
        res.innerHTML=aryVilles[i]['commune'];
        sel.appendChild(res);
      }
      }
    }
  }
  
// cookies
function EcrireCookie(nom, valeur) {
   var argv=EcrireCookie.arguments;
   var argc=EcrireCookie.arguments.length;
   var expires=(argc > 2) ? argv[2] : null;
   var path=(argc > 3) ? argv[3] : null;
   var domain=(argc > 4) ? argv[4] : null;
   var secure=(argc > 5) ? argv[5] : false;
   document.cookie=nom+"="+escape(valeur)+
      ((expires==null) ? "" : ("; expires="+expires.toGMTString()))+
      ((path==null) ? "" : ("; path="+path))+
      ((domain==null) ? "" : ("; domain="+domain))+
      ((secure==true) ? "; secure" : "");
}
function getCookieVal(offset) {
   var endstr=document.cookie.indexOf (";", offset);
   if (endstr==-1) endstr=document.cookie.length;
   return unescape(document.cookie.substring(offset, endstr));
}
function LireCookie(nom) {
   var arg=nom+"=";
   var alen=arg.length;
   var clen=document.cookie.length;
   var i=0;
   while (i<clen)
   {
      var j=i+alen;
      if (document.cookie.substring(i, j)==arg) return getCookieVal(j);
      i=document.cookie.indexOf(" ",i)+1;
      if (i==0) break;
   }
   return null;
}

function EffacerCookie( nom, path, domain ) {
if ( LireCookie( nom ) ) document.cookie = nom + "=" +
( ( path ) ? ";path=" + path : "") +
( ( domain ) ? ";domain=" + domain : "" ) +
";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}
function Alerte(titre,msg) {
  _Elem('AlerteTitre').innerHTML = titre;
  _Elem('AlerteMsg').innerHTML = msg;
  Show('Alerte');
  dy = (document.documentElement.clientHeight - 100) / 2;
  dx = (document.documentElement.clientWidth - 325) / 2;

   _Elem('Alerte').style.left = dx +'px';
   _Elem('Alerte').style.top = dy +'px';
}

//panier
var Panier = {
	nbElem:0,
	aryEntry: Array(),
	
	editer:function(produit_id,coul,marq,quant,alerter) {
		idx = -1;
		for(i=0; i<this.aryEntry.length; i++) {
			if (this.aryEntry[i].id == produit_id) idx = i;
		}
		if (idx >=0) {
			if (coul) this.aryEntry[idx].couleur = coul;
			if (marq) this.aryEntry[idx].marquage = marq;
			if (quant) this.aryEntry[idx].quantite = quant;			
		} else {
			this.aryEntry[this.aryEntry.length] = {id:produit_id, couleur:coul, marquage:marq, quantite:quant};
			this.nbElem = this.aryEntry.length;
		}
		this.sauver();
		this.afficher();
        if (alerter == true) {
          if (CookieEnabled)
            Alerte('Message', '<br><br>Le produit a &eacute;t&eacute; ajout&eacute; &agrave; votre demande de devis<br><br>');
          else
            Alerte('Message', '<br><br>Vous devez activer les &laquo;cookies&raquo; dans les pr&eacute;f&eacute;rences de votre navigateur pour pouvoir demander un devis.<br><br><a href="aide-cookies.html">Cliquez ici pour savoir comment activer les cookies.</a><br><br>');
        }
	},
	
	supprimer:function(produit_id) {
		idx = -1;
		for(i=0; i<this.aryEntry.length; i++) {
			if (this.aryEntry[i].id == produit_id) idx = i;
		}
		if (idx >=0) {
			for (i=idx; i<(this.aryEntry.length-1); i++) {
              this.aryEntry[i] = this.aryEntry[i+1];
              with(this.aryEntry[i]) {
              EcrireCookie('PanierEntry'+i,id+'|'+couleur+'|'+marquage+'|'+quantite);
              }
            }
            this.aryEntry.splice(this.aryEntry.length-1,1);
			this.nbElem = this.aryEntry.length;
            
			EffacerCookie('PanierEntry'+this.aryEntry.length-1);
			EcrireCookie('PanierNbElem',this.nbElem);
		}
		self.location = 'devis.html';
	},
	
	sauver:function() {
		EcrireCookie('PanierNbElem', this.nbElem);
		for(i=0; i<this.aryEntry.length; i++) {
			with(this.aryEntry[i]) {
			EcrireCookie('PanierEntry'+i,id+'|'+couleur+'|'+marquage+'|'+quantite);
			}
		}
	},
	
	charger:function() {
		nbElem = LireCookie('PanierNbElem');
		for (i=0; i<nbElem;i++) {
			tmp = LireCookie('PanierEntry'+i);
			entry = tmp.split('|');
			this.aryEntry[this.aryEntry.length] = {id:entry[0], couleur:entry[1], marquage:entry[2], quantite:entry[3]};
		}
		this.nbElem = this.aryEntry.length;
		this.afficher();
	},
	
	afficher:function() {
		if (this.nbElem > 1) s = 's';
		else s = '';
		if (this.nbElem > 0)
		_Elem('Panier').innerHTML = '<a href="devis.html">'+this.nbElem+' produit'+s+' dans votre devis.</a>';
		else _Elem('Panier').innerHTML = 'Votre devis est vide.';
	},
	
	vider:function() {
		EffacerCookie('PanierNbElem');
		for (i=0; i<this.nbElem;i++) {
			EffacerCookie('PanierEntry'+i);				
		}
		this.aryEntry = Array();
		this.nbElem = 0;
		this.afficher();
	}
	
};
function Move(id, X, Y) {
         _Elem(id).style.left = X;
         _Elem(id).style.top = Y;
}
function createAkisiBulle() {
         /*ttmp = document.createElement('div');
         ttmp.id = 'mousePosDv';
         ttmp.style.position = 'fixed';
         ttmp.style.top = '0px';
         ttmp.style.zIndex = '1000';
         document.body.appendChild(ttmp);*/
         
         tmp = document.createElement('div');
         tmp.id = 'AkisiBulleDiv';
         tmp.style.position = 'absolute';
         tmp.style.zIndex = '1000';
         document.body.appendChild(tmp);
         if (IE) {
         tmp2 = document.createElement('iframe');
         tmp2.id = 'AkisiBulleIframe';
         tmp2.style.position = 'absolute';
         tmp2.style.height = '25px';
         tmp2.style.zIndex = '999';
         document.body.appendChild(tmp2);
         }         
}
function HideAkisiBulle() {_Elem('AkisiBulleDiv').innerHTML = ''; Hide('AkisiBulleDiv'); if (IE) Hide('AkisiBulleIframe'); }
function AkisiBulleFollowMouse() {
 if (IE) mousePos = getMouseXY();
 Move('AkisiBulleDiv',mousePos.x+AkisiBulleOffset.x, mousePos.y+AkisiBulleOffset.y);
 if (IE) {
 _Elem('AkisiBulleIframe').style.width = _Elem('AkisiBulleDiv').clientWidth; 
 _Elem('AkisiBulleIframe').style.height = _Elem('AkisiBulleDiv').clientHeight;
 Move('AkisiBulleIframe',mousePos.x+AkisiBulleOffset.x, mousePos.y+AkisiBulleOffset.y);
 }
 //_Elem('mousePosDv').innerHTML = mousePos.x+' x '+mousePos.y;
}
var AkisiBulleOffset = {x:5,y:5};
function AkisiBulle(obj, texte) {
 
 if (!_Elem('AkisiBulleDiv')) { createAkisiBulle() ;}
 Show('AkisiBulleDiv');
 AkisiBulleOffset = {x:5,y:5};
 if (IE) Show('AkisiBulleIframe');
 if (IE) { 
    document.onmousemove = AkisiBulleFollowMouse;
    obj.onmouseout = HideAkisiBulle;
 } else {
    document.addEventListener('mousemove', AkisiBulleFollowMouse, true);
    obj.addEventListener('mouseout', HideAkisiBulle, true);
  }
  
 _Elem('AkisiBulleDiv').innerHTML = texte;
}

function AkisiBullePhoto(obj, src) {
 //maxheight = window.innerHeight;
 if (src.indexOf('1x1.gif')== -1) {
 if (!_Elem('AkisiBulleDiv')) { createAkisiBulle() ;}
 if (IE) mousePos = getMouseXY();
 //if ( (mousePos.y-220) > 180)
 //AkisiBulleOffset = {x:5,y:-180};
 //else AkisiBulleOffset = {x:5,y:35};
 AkisiBulleOffset = {x:5,y:-180};
 Show('AkisiBulleDiv');
 if (IE) Show('AkisiBulleIframe');
 if (IE) { 
    document.onmousemove = AkisiBulleFollowMouse;
    obj.onmouseout = HideAkisiBulle;
 } else {
    document.addEventListener('mousemove', AkisiBulleFollowMouse, true);
    obj.addEventListener('mouseout', HideAkisiBulle, true);
  }
 _Elem('AkisiBulleDiv').innerHTML = '<div class="overPhoto"><img src="'+src.replace(/50x50/,'155x155')+'" alt=""></div>';
 }
}

function Zoomer(src) {
  if (src.indexOf('1x1.gif')== -1) {
  _Elem('ZoomImg').src = src.replace(/250x250/,'500x500');
  Show('dvZoom');
  }
}

function CatShow(id) {
  PageHeight = IE?document.body.scrollHeight:document.body.offsetHeight;
  PageScroll = document.body.scrollTop;
  ClientHeight = document.body.clientHeight;
  Show(id);
  MyTop = _Elem(id).offsetParent.offsetTop;
  MyHeight = _Elem(id).offsetHeight;  
  //alert(PageScroll+' '+ClientHeight+' '+MyTop+' '+MyHeight);
  if (MyTop+MyHeight-PageScroll >= ClientHeight) {
    //alert(MyTop - ((MyTop+MyHeight)-(PageScroll+ClientHeight)));
    _Elem(id).style.top = - ((MyTop+MyHeight)-(PageScroll+ClientHeight));
  } 
}

function favoris() {
if ( navigator.appName != 'Microsoft Internet Explorer' ){
window.sidebar.addPanel("Objet publicitaire Md-kdo","http://www.md-kdo.com/","");
alert ('Un favori va être créé, il risque de s\'ouvrir dans le panneau latéral de votre navigateur. Pour y remédier faites un clique droit sur le marque-page Md-kdo puis choisissez <Propriétés> et décochez <Charger ce marque-page comme panneau latéral>. Merci')
_Elem('imgFavoris').src = imagesPath+'ajouter-aux-favoris-on.gif';
}
else {
window.external.AddFavorite("http://www.md-kdo.com/","Objet publicitaire Md-kdo");
_Elem('imgFavoris').src = imagesPath+'ajouter-aux-favoris-on.gif';
}
}
var anims = {
  bp:{X:0, dX:0, sens:'gauche',marge:5, etat:'run'}
};
var tt;
var slideWidth = 0;
function initDecal() {
  for (ii in anims) { 
	cleanNodes(ii);
  }
  dvtest = document.getElementById("bp");
  tt = setInterval('decal2()',30);
}
var ttime;

function decal() {
  for (ii in anims) {
	 if (document.getElementById(ii)) {
	  if (anims[ii].etat == 'run') {
		  if (anims[ii].sens == 'gauche') document.getElementById(ii).style.left = anims[ii].dX+'px';
		  else document.getElementById(ii).style.top = anims[ii].dX+'px';
		  anims[ii].dX -= 2;
		  if (anims[ii].dX + anims[ii].X == 0) {
			 //ttime = setTimeout('swapNodes(\''+ii+'\')',1);
			 //swapNodes(ii);
			 anims[ii].dX = 0;
		  }
	  }
	 }
  }
}
var dvtest;
function decal2() {
  ii = 'bp';
dvtest.style.left = anims[ii].dX+'px';		  
anims[ii].dX -= 2;
if (anims[ii].dX + anims[ii].X == 0) {
   //ttime = setTimeout('swapNodes(\''+ii+'\')',1);
   //swapNodes(ii);
   anims[ii].dX = 0;
}	 
}

function animStop(nid) { anims[nid].etat = 'stop' ;}
function animStart(nid) { anims[nid].etat = 'run' ;}
function cleanNodes(nid) {
    if (_Elem(nid)) {
	  for (i=_Elem(nid).childNodes.length-1; i>=0;i--) {
		  if (_Elem(nid).childNodes[i].nodeType == 3) {
			  _Elem(nid).removeChild(_Elem(nid).childNodes[i]);
		  } else
			  anims[nid].X+=_Elem(nid).childNodes[i].offsetWidth;
  
		  
	  }
	  _Elem(nid).innerHTML += _Elem(nid).innerHTML;
	}
	else setTimeout('cleanNodes(\''+nid+'\')',10);
}

function swapNodes(nid) { 
    t = _Elem(nid).childNodes[0].cloneNode(true);
    _Elem(nid).appendChild(t);
    _Elem(nid).removeChild(_Elem(nid).childNodes[0]);
	anims[nid].dX = -1 * anims[nid].marge;
}

function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}