document.write('<div id="topdecklink"></div>');
document.write('<div id="imgtocaddie"></div>');
document.write('<div id="tablefdp"></div>');

/******************************************/
/***        Gestion des cookies         ***/
/******************************************/
function IsIE()
{
	if (navigator.appName.substring(0,3)=="Mic")
		return true;
	else
		return false;
//	Mozilla ou netscape ...
//	else if (navigator.appName.substring(0,3)=="Net")
}

function SetCookie(name, value)
{
	if(navigator.cookieEnabled)
	{
		var expires = new Date();
		// Durée de vie de 1 heure
		expires.setTime(expires.getTime()+(3600*4000));
		document.cookie=name+"="+escape(value)
			+"; expires="+expires.toGMTString()
			+"; path=/";
//			+"; domain=www.docmicro.com";
	}
//	else
//	{
//		 alert("Pour pouvoir commander les cookies doivent être activer !");
//	}
}

function VerifCookie()
{
	SetCookie("TestActivation", "TEST")
	if(GetCookie("TestActivation") != "TEST")
	{
		 alert("Pour pouvoir commander les cookies doivent être activés, merci de modifier les paramètres de votre navigateur !");
	}
	DelCookie("TestActivation");
}

function GetCookieVal(offset)
{
	var endstr=document.cookie.indexOf (";", offset);
	if (endstr==-1)
      		endstr=document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
}

function GetCookie (name)
{
	var arg=name+"=";
	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 DelCookie(name)
{
	var expires = new Date();
	expires.setTime(expires.getTime()-999);
	// la date d'expiration est inférieure à l'heure actuelle
	// Donc le cookie est détruit
	document.cookie=name+"=0"
			+"; expires="+expires.toGMTString()
			+"; path=/";
//			+"; domain=www.docmicro.com";
}

/******************************************/
/***      Fin gestion des cookies       ***/
/******************************************/

/******************************************/
/***         Gestion du caddie          ***/
/******************************************/
function ResetCaddie()
{
	DelCookie("CaddieDocMicro")
	DelCookie("CaddieDocMicroDescription")
	DelCookie("CaddieDocMicroTotal")
	DelCookie("LivraisonDocMicro")
	DelCookie("BonDeReduction")
	DelCookie("PrixReduction");
	DelCookie("RelaisColisTNT");
	DelCookie("PieceCadeau");
/*
	SetCookie("CaddieDocMicro","");
	SetCookie("CaddieDocMicroDescription","");
	SetCookie("CaddieDocMicroTotal","0|0");
*/	
	if( typeof(document.caddie) != "undefined" && typeof(document.caddie.image_caddie) != "undefined" )
	{
		document.caddie.image_caddie.src='caddie/imagecaddie.php?TotalProduit=0&TotalPanier=0';
		document.caddie.image_caddie.alt='';
	}
}

function ResetCaddieReload()
{
	ResetCaddie();
	if( IsIE() )
		self.location="index.php";
	else
		self.location="caddie/index.php";
}

function ReloadLivraison(strChoix)
{
	SetCookie("LivraisonDocMicro",strChoix);
	
	if( IsIE() )
		self.location="index.php";
	else
		self.location="caddie/index.php";
}

function ReloadBonDeReduction(strReduction, bClear)
{
    //if (strReduction != "")
	    SetCookie("BonDeReduction",strReduction);
	
	if( IsIE() )
		self.location="index.php";
	else
		self.location="caddie/index.php";
}

function ReloadCadeau(strCadeau)
{
	    SetCookie("PieceCadeau",strCadeau);
	
	if( IsIE() )
		self.location="index.php";
	else
		self.location="caddie/index.php";
}

function AddCaddie(addID,addQte,addPrix,addDescr,imgName)
{
  	var bOk = 0;
  	if( addQte <= 0 && addDescr != "")
  	{
		alert('La quantité spécifiée est incorrecte !');
		return;
	}
	if(addDescr == "")
	{
		bOk = 1;
	}
	else
	{
		bOk = confirm('Ajouter '+addQte+' fois le produit '+addDescr+' dans votre panier ?');
	}
	if(bOk)
	{
		AddCaddie2(addID,addQte,addPrix,addDescr,1);
		FlyToCaddie(imgName+addID);
	}
}

function AddCaddie2(addID,addQte,addPrix,addDescr,bReload)
{
	if(addQte > 99) addQte = 99;
	VerifCookie();
	var strTmpDesrc,strDescr,strOldDescr,strOldCaddie, strNewCaddie, strTmpNum, strTmpQte, strNewDescr,strNewTotal,strOldTotal;
	var nPos=-1;
	var nPosd=-1;
	var nLCookie = 0;
	var PieceFind = false;
	var nNewTotal = Math.abs(addPrix)*Math.round(addQte);
	var nNewQte = addQte;

	strDescr = "";
	strNewCaddie = "";
	strNewDescr = "";
	strNewTotal = "0|0";
	strOldCaddie = "";
	strTmpNum = "";
	strTmpQte = "";
	strOldCaddie = GetCookie("CaddieDocMicro");
	strOldDescr = GetCookie("CaddieDocMicroDescription");
	strOldTotal = GetCookie("CaddieDocMicroTotal");
	if( strOldTotal && strOldTotal != "")
	{
		nPos = strOldTotal.indexOf("|",0);
		nNewQte = strOldTotal.substr(0,nPos);
		nNewQte = Math.round(nNewQte)+Math.round(addQte);
		nNewTotal = strOldTotal.substr(nPos+1,strOldTotal.length-nPos-1);
		nNewTotal = Math.abs(nNewTotal)+(Math.round(addQte) * Math.abs(addPrix));
		if(nNewQte <= 0)
		{
			nNewQte = 0;
			nNewTotal = 0;
		}
	}
	strNewTotal = nNewQte + "|" + nNewTotal;

	strDescr = "";
	if(strOldCaddie && strOldCaddie != "")
	{
		nLCookie = strOldCaddie.length;
		while(nLCookie > 0)
		{
			nPos = strOldCaddie.indexOf("|",0);
			strTmpNum = strOldCaddie.substr(0,nPos);
			strOldCaddie = strOldCaddie.substr(nPos+1,strOldCaddie.length-nPos-1);

			nPosd = strOldDescr.indexOf("|",0);
			strTmpDesrc = strOldDescr.substr(0,nPosd);
			strOldDescr = strOldDescr.substr(nPosd+1,strOldDescr.length-nPosd-1);

			nPos = strOldCaddie.indexOf("|",0);
			strTmpQte = strOldCaddie.substr(0,nPos);
			strOldCaddie = strOldCaddie.substr(nPos+1,strOldCaddie.length-nPos-1);
			if(strTmpNum==addID)
			{
				PieceFind=true;
				strTmpQte = Math.round(strTmpQte) + Math.round(addQte);
			}
			if( Math.round(strTmpQte) > 0 )
			{
				strNewCaddie += strTmpNum + "|" + strTmpQte + "|";
				strNewDescr += strTmpDesrc+"|";
				strDescr += strTmpQte+" x "+strTmpDesrc+"\n";
			}
			nLCookie = strOldCaddie.length;
		}
	}
	if(!PieceFind && addQte > 0)
	{
		strNewCaddie += addID + "|" + addQte + "|";
		strNewDescr += addDescr + "|" ;
		strDescr += addQte+" x "+addDescr+"\n";
	}
	if(strDescr.length > 0)
		strDescr = strDescr.substr(0,strDescr.length-1);
	SetCookie("CaddieDocMicro",strNewCaddie);
	SetCookie("CaddieDocMicroDescription",strNewDescr);
	SetCookie("CaddieDocMicroTotal",strNewTotal);
	if( bReload	 == 1)
	{
		document.caddie.image_caddie.src='caddie/imagecaddie.php?TotalProduit='+nNewQte+'&TotalPanier='+nNewTotal;
		//document.caddie.image_caddie.alt=''+strDescr;
		if(addDescr == "") // Si on est dans le panier on relit la page
		{
			if( IsIE() )
				self.location="index.php";
			else
				self.location="caddie/index.php";
		}
	}
}

function ShowCaddie()
{
	var strCaddie = GetCookie("CaddieDocMicro");
	var strDescr = GetCookie("CaddieDocMicroDescription");
	var strTotal = GetCookie("CaddieDocMicroTotal");
	var nNewQte = 0;
	var nNewTotal = 0;
	if(strTotal!=null && strTotal != "")
	{
		var nPos = -1;
		nPos = strTotal.indexOf("|",0);
		if(nPos >= 0)
		{
			nNewQte = strTotal.substr(0,nPos);
			nNewQte = Math.round(nNewQte);
			nNewTotal = strTotal.substr(nPos+1,strTotal.length-nPos-1);
			nNewTotal = Math.abs(nNewTotal);
			if(nNewQte <= 0)
			{
				nNewQte = 0;
				nNewTotal = 0;
			}
		}
	}
	document.write('<IMG name="image_caddie" src="" alt="Panier" border="0">');
	var caddie = DOC_findObj("image_caddie");
	if(caddie!=null)
	{
		caddie.src = '';
		caddie.src = 'caddie/imagecaddie.php?TotalProduit='+nNewQte+'&TotalPanier='+nNewTotal;
		//caddie.alt = ''+strDescr;
	}
	// document.write('<IMG name="image_caddie" src="caddie/imagecaddie.php?TotalProduit='+nNewQte+'&TotalPanier='+nNewTotal+'" alt="'+strDescr+'" border="0">');
	// document.caddie.image_caddie.src='caddie/imagecaddie.php?TotalProduit='+nNewQte+'&TotalPanier='+nNewTotal;
	// document.caddie.image_caddie.alt=''+strDescr;
}

/******************************************/
/***       Fin gestion du caddie        ***/
/******************************************/

function ajoute(pm,name)
{
	nb=document[pm][name].value;
	nb++;
	if (nb>99) {nb=1;}
	document[pm][name].value=nb;
}

function enleve(pm,name)
{
	nb=document[pm][name].value;
	nb--;
	if (nb<1) {nb=1;}
	document[pm][name].value=nb;
}


/******************************************/
/***    Gestion des images survolées    ***/
/******************************************/

// Cherche un objet nom_objet|name_frame, name_doc
function DOC_findObj(nameObj, inDoc)
{
	var pos,i,ret=null, tabNames=null;
	if(!inDoc)
		var inDoc=document;
	
	pos = nameObj.indexOf("|");
	if( pos>0 && parent.frames.length)
	{
		inDoc = parent.frames[nameObj.substring(pos+1)].document;
		nameObj=nameObj.substring(0,pos);
	}
	ret = inDoc[nameObj];
	if(!(ret) && inDoc.all)
		ret=inDoc.all[nameObj];
	if(!(ret))
		ret=inDoc.getElementById(nameObj);
		
	// on cherche dans les forms ...
	for(i=0;!ret&&i<inDoc.forms.length;i++)
	{
		ret=inDoc.forms[i][nameObj];
	}
	// on cherche dans les layers
	for(i=0;!ret && inDoc.layers && i<inDoc.layers.length;i++)
		ret=DOC_findObj(nameObj,inDoc.layers[i].document);

	if(!(ret))
	{
		tabNames=inDoc.getElementsByName(nameObj);
		if (tabNames.length > 0) ret = tabNames[0];
	}

	return ret;
}

// Restore les image d origine stocker
function DOC_swapImgRestore()
{
	var i;
	var docSrc=document.DOC_sr;
	if(docSrc)
	{
		for(i=0; i<docSrc.length && docSrc[i].oldSrc; i++)
		{
	  		docSrc[i].src=docSrc[i].oldSrc;
	  	}
	}
}

// DOC_swapImage('nom_objet','image_origine','image_new')
function DOC_swapImage(nameObj, imgOrigine, imgNew)
{
	var i,j=0;
	var obj = DOC_findObj(nameObj);
	document.DOC_sr=new Array;
	if(obj!=null)
	{
		document.DOC_sr[0]=obj;
		if(!obj.oldSrc)
			obj.oldSrc=obj.src;
		obj.src=imgNew;
	}
}

// Permet de lire des images et de les mettre dans le cache
function DOC_preloadImages()
{
	if(!document.DOC_preload)
		document.DOC_preload=new Array();
	var i;
	var j=document.DOC_preload.length;
	var args=DOC_preloadImages.arguments;
	for(i=0; i<args.length; i++)
	{
		document.DOC_preload[j]=new Image;
		document.DOC_preload[j].src=args[i];
		j++;
	}
}

/******************************************/
/***        Fin images survolées        ***/
/******************************************/

var sizecurtab = 200;
var hightcurtab = 140;

function poplink(msgTitre, msg) {
	var content ="<TABLE BORDER=1 CELLPADDING=0 CELLSPACING=0 width=200><TR><TD><TABLE WIDTH=100% BORDER=0 CELLPADDING=3 CELLSPACING=1><TR><TD BGCOLOR=#FFFFCC><span class=dossier2><div align='center' style='color: navy; text-decoration: underline'>"+msgTitre+"</div><br />"+msg+"</span></TD></TR></TABLE></TD></TR></TABLE>";

	sizecurtab = 200;
	var skn = DOC_findObj('topdecklink');
	skn.innerHTML = content;
	skn.style.visibility = "visible";
}

function HideHover(idfind)
{
	var element_style=null;
	element_style = DOC_findObj(idfind);
	if(element_style)
	{
		element_style.style.backgroundPosition = "0% 0px";
		element_style.style.color = "#000";
		var arHref = element_style.getElementsByTagName("a");
		arHref[0].style.backgroundPosition = "100% 0px";
		arHref[0].style.color = "#000";
	}
}

function killTableFPD()
{
	var skn = DOC_findObj('tablefdp');
	skn.innerHTML = "";
	skn.style.visibility = "hidden";
}

function ShowTableFDP(idobj, zone1, zone2)
{
	var objShow = DOC_findObj(idobj);
	var y = findPosY(objShow) + 20;
	var x = findPosX(objShow);
	sizecurtab = 300;
	hightcurtab = 70;

	x = x-(sizecurtab/2);			
	var skn = DOC_findObj('tablefdp');
	skn.style.top = y+"px";
	skn.style.left = x+"px";
	skn.innerHTML = "<TABLE BORDER=1 CELLPADDING=0 CELLSPACING=0><TR><TD><table BORDER=0 CELLSPACING=2 CELLPADDING=2 BGCOLOR=#F2F4FF ><tr><td><img src='design/pays/be.gif' border='0'> <img src='design/pays/de.gif' border='0'> <img src='design/pays/nl.gif' border='0'> <img src='design/pays/lu.gif' border='0'></td><td>Colissimo expert I (Zone 1) : "+zone1+"</td></tr><td><img src='design/pays/gb.gif' border='0'> <img src='design/pays/es.gif' border='0'> <img src='design/pays/it.gif' border='0'></td><td>Colissimo expert I (Zone 2) : "+zone2+"</td></tr></table></td></tr></table>"
	skn.style.visibility = "visible";
}

function killlink()
{
	var skn = DOC_findObj('topdecklink');
	skn.innerHTML = "";
	skn.style.visibility = "hidden";
}

function ShowLegende(idobj)
{
	var objShow = DOC_findObj(idobj);
	var y = findPosY(objShow) + 20;
	var x = findPosX(objShow);
	sizecurtab = 180;
	hightcurtab = 140;

/*	if ((y+hightcurtab)>document.body.clientHeight)
	{
		y = y -20-hightcurtab;
		if (y<0) y=0;
	}
	if(x < (sizecurtab/2))
		x = 3;
	else if(x+sizecurtab > document.body.clientWidth)
		x = document.body.clientWidth - sizecurtab;
	else
		x = x-(sizecurtab/2);			
*/
	x = x-(sizecurtab/2);			
	var skn = DOC_findObj('topdecklink');
	skn.style.top = y+"px";
	skn.style.left = x+"px";
	skn.innerHTML = "<TABLE BORDER=1 CELLPADDING=0 CELLSPACING=0><TR><TD><table BORDER=0 CELLSPACING=2 CELLPADDING=2 BGCOLOR=#F2F4FF ><tr><td><img border='0' src='caddie/feux_vert.gif'></td><td>En stock</td></tr><td><img border='0' src='caddie/feux_vert_orange.gif'></td><td>2 à 5 jours de délai</td></tr><tr><td><img border='0' src='caddie/feux_orange.gif'></td><td>1 à 2 semaines de délai</td></tr><tr><td><img border='0' src='caddie/feux_rouge.gif'></td><td>Délai inconnu</td></tr><tr><td><img border='0' src='caddie/feux_bleu.gif'></td><td>Précommande</td></tr></table></td></tr></table>";
	skn.style.visibility = "visible";
}

function FlyToCaddie(imgname)
{
	var imgfly = DOC_findObj('imgtocaddie');
	var imgAdd = DOC_findObj(imgname);
	if (imgfly.style.visibility != "visible" && imgAdd)
	{
	  	imgfly.innerHTML = "<TABLE BORDER=1 CELLPADDING=0 CELLSPACING=0><TR><TD><img border='0' src='"+imgAdd.src+"'></TD></TR></TABLE>";
		imgfly.style.left = findPosX(imgAdd)+"px";
		imgfly.style.top = findPosY(imgAdd)+"px";
		imgfly.style.visibility = "visible";
		window.setTimeout('MoveImg()',10);
	}
}
function findPosX(obj) {
    var curleft = 0;
    if (obj.offsetParent) {
        while (1) {
            curleft+=obj.offsetLeft;
            if (!obj.offsetParent) {
                break;
            }
            obj=obj.offsetParent;
        }
    } else if (obj.x) {
        curleft+=obj.x;
    }
    return curleft;
}
function findPosY(obj) {
    var curtop = 0;
    if (obj.offsetParent) {
        while (1) {
            curtop+=obj.offsetTop;
            if (!obj.offsetParent) {
                break;
            }
            obj=obj.offsetParent;
        }
    } else if (obj.y) {
        curtop+=obj.y;
    }
    return curtop;
}
function MoveImg()
{
	var flyingSpeed = 20;
	var caddie = DOC_findObj("image_caddie");
	var imgfly = DOC_findObj('imgtocaddie');
	var posXCaddie = findPosX(caddie);
	var posYCaddie = findPosY(caddie);
	var newleft = imgfly.offsetLeft;
	var newtop = imgfly.offsetTop;
	var diffX = newleft - posXCaddie;
	var diffY = newtop - posYCaddie;
	var maxDiff = Math.max(Math.abs(diffX),Math.abs(diffY));
	var moveX = Math.abs((diffX / maxDiff) * flyingSpeed);
	var moveY = Math.abs((diffY / maxDiff) * flyingSpeed);	
	
	if(newleft > posXCaddie) newleft = newleft - moveX;
	else newleft = newleft + moveX;
	if(newtop > posYCaddie) newtop = newtop - moveY;
	else newtop = newtop + moveY;
	imgfly.style.left = newleft+"px";
	imgfly.style.top = newtop+"px";

	if(newleft >  posXCaddie || newtop < posYCaddie || (newleft == posXCaddie && newtop == posYCaddie))
	{
	  	imgfly.style.visibility = "hidden";
	}
	else
	{
	    window.setTimeout('MoveImg()',10);
	}
}
