/** ajoute le blur sur le focus **/
function blurAnchors(){ 	
	if(document.getElementsByTagName) 
	{ 
		var a = document.getElementById("lcmCenter").getElementsByTagName("a"); 
		
		//collect all anchors A 
		for(var i = 0; i < a.length; i++)
		{ 
			// mouse onfocus, blur anchors 
			a[i].onfocus = function(){this.blur();}; 
		} 
	} 
} 

/** fin du blur **/

function GetMainFormName()
{
	var i;
	for (i=0;i<document.forms.length;i++)
	{
		// Framework v1.0.3705
		if (document.forms[i].name.indexOf("_ServerForm") != -1)
			return document.forms[i].name;
		// Framework v1.1.4322
		if (document.forms[i].name.indexOf("__aspnetForm") != -1)
			return document.forms[i].name;
		// Framework v2.1
		if (document.forms[i].name.indexOf("aspnetForm") != -1)
			return document.forms[i].name;
	}
	return null;
}

function SaveElement(ElementObject,ElementValue)
{
	var mf = GetMainFormName();					
	if (mf==null) return;
	var element  = GetElement(ElementObject,mf);
	if (element!=null) 
		element.value = ElementValue;
}

function GetElementName(n,mf)
{
	if (n==null || n=="") return null;
	n = n.toLowerCase();
	var i, s, j;
	for (i=0;i<document.forms[mf].elements.length;i++)
	{
		if (document.forms[mf].elements[i].name.toLowerCase().lastIndexOf(n) != -1)
		{
			var strform = document.forms[mf].elements[i].name.toLowerCase();
			var strformname = strform.substr(strform.lastIndexOf(n));
			if (strformname == n)
				return document.forms[mf].elements[i].name;
		}	
	}
	return null;
}

function GetElementId(n,mf)
{
	if (n==null || n=="") return null;
	n = n.toLowerCase();
	var i, s, j;
	for (i=0;i<document.forms[mf].elements.length;i++)
	{
		if (document.forms[mf].elements[i].id.toLowerCase().lastIndexOf(n) != -1)
		{
			var strform = document.forms[mf].elements[i].id.toLowerCase();
			var strformname = strform.substr(strform.lastIndexOf(n));
			if (strformname == n)
				return document.forms[mf].elements[i].id;
		}	
	}
	return null;
}



function GetElement(ElementName, formName)
{
	return document.forms[formName].elements[GetElementName(ElementName,formName)];
}

function swapfakeinput() {
	var mf = GetMainFormName();					
	if (mf==null) return;
 
   	if(document.getElementById('passkake'))document.getElementById('passkake').style.display='none';
   	
   	if(document.getElementById('zoneMyL') && document.getElementById('zoneMyL').getElementsByTagName('INPUT'))
   	{
		document.getElementById('zoneMyL').getElementsByTagName('INPUT')[2].style.display='block'; 
		document.getElementById('zoneMyL').getElementsByTagName('INPUT')[2].focus();
   	}   	

}

/**** au cas où la fonction serait appelée trop tôt ****/
function startAnim(){};
/*******************************************************/

function getTitle()
{
	if(document.getElementsByTagName('H1'))
	{
		var val2 = document.getElementsByTagName('H1')[0].innerHTML;		
	}else
	{
		var val2 ='';
	}
	return val2;
}

function getSubtitle()
{
	if(document.getElementsByTagName('H2'))
	{
		var val2 = document.getElementsByTagName('H2')[0].innerHTML;		
	}else
	{
		var val2 ='';
	}
	return val2;
}



/** transformation des caractères spéciaux dans les url **/

function URLEncode(url)
{
	// The Javascript escape and unescape functions do not correspond
	// with what browsers actually do...
	var SAFECHARS = "0123456789" +					// Numeric
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'()";					// RFC2396 Mark characters
	var HEX = "0123456789ABCDEF";

	var plaintext = url;
	var encoded = "";
	for (var i = 0; i < plaintext.length; i++ ) {
		var ch = plaintext.charAt(i);
	    if (ch == " ") {
		    encoded += "%20";				// x-www-urlencoded, rather than %20
		} else if (SAFECHARS.indexOf(ch) != -1) {
		    encoded += ch;
		} else {
		    var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
			    alert( "Unicode Character '" 
                        + ch 
                        + "' cannot be encoded using standard URL encoding.\n" +
				          "(URL encoding only supports 8-bit characters.)\n" +
						  "A space (+) will be substituted." );
				encoded += "%20";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	} // for

	return encoded;
};

function URLDecode(url)
{
   // Replace + with ' '
   // Replace %xx with equivalent character
   // Put [ERROR] in output if %xx is invalid.
   var HEXCHARS = "0123456789ABCDEFabcdef"; 
   var encoded = url;
   var plaintext = "";
   var i = 0;
   while (i < encoded.length) {
       var ch = encoded.charAt(i);
	   if (ch == "+") {
	       plaintext += " ";
		   i++;
	   } else if (ch == "%") {
			if (i < (encoded.length-2) 
					&& HEXCHARS.indexOf(encoded.charAt(i+1)) != -1 
					&& HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 ) {
				plaintext += unescape( encoded.substr(i,3) );
				i += 3;
			} else {
				alert( 'Bad escape combination near ...' + encoded.substr(i) );
				plaintext += "%[ERROR]";
				i++;
			}
		} else {
		   plaintext += ch;
		   i++;
		}
	} // while
   return plaintext;
};


/** changements de classe **/

function biChangeClass(id,classname)
{
	if(document.getElementById(id))document.getElementById(id).className = classname;
}



/** trouver les coordonnées d'un objet **/

function findPosX(obj)
{
	
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}

/** coordonnées du début du calque principal **/
function biCoordX()
{
	if(document.getElementById('lcmCenter'))
	{
	extX = document.getElementById('lcmCenter').style.left;
	extX = extX.substring(0,extX.length-2);
	extX = parseInt(extX);
	return extX;
	}
}


function biCoordY()
{
	if(document.getElementById('lcmCenter'))
	{
	extY = document.getElementById('lcmCenter').style.top;
	extY = extY.substring(0,extY.length-2);
	extY = parseInt(extY);
	return extY;
	}
}

var iSize = false;
var iSizeFrag = false;
var bulleShow = false;

/** creation et gestion de la bulle **/
function biShowBulle(titre,prd){
	if(!bulleShow)
	{
		//message = message.replace("'","\'");
		var content='';
		if(!iSizeFrag && iSize)content = "<div class='biTitreBulle' style='padding:2px; padding-right:3px;z-index:1'>"+titre+"</div>";	
		if(iSizeFrag || !iSize)content = "<div class='biTitreBulle' align='right' style='padding:2px; padding-right:3px;z-index:1'>"+titre+"</div>";

		var posy = 0;
		var posx = 0;
		var anchor = 'img'+prd;
		var divname = 'legend';		

// || (!document.getElementById('lcmLaMarque') && document.all)

		if(!document.getElementById('lcmRight'))
		{

			posx=findPosX(document.getElementById(anchor))-findPosX(document.getElementById('lcmCenter'));
			posy=findPosY(document.getElementById(anchor))-findPosY(document.getElementById('lcmCenter'));
			posy+=52;


		}else
		{

			posy = lib_getImageYfromTop(anchor);
			posx = lib_getImageXfromLeft(anchor);
			if(iSize && !iSizeFrag)posy = parseInt(posy) +  iSize;
			if(!iSize && !iSizeFrag)posy = parseInt(posy) + 52;
			if(!iSize && iSizeFrag)posy = parseInt(posy) + 52;
			if(!iSize && iSizeFrag)posx = parseInt(posx)-68;
			if(!iSize && !iSizeFrag)posx = parseInt(posx)-68;

		}

		posx = parseInt(posx);

		if(document.getElementById('legend'))
		{

			document.getElementById('legend').style.left = posx+"px";
			document.getElementById('legend').style.top = posy+"px";		

			document.getElementById('legend').innerHTML = content;

			document.getElementById('legend').style.display ='block';
			document.getElementById('legend').style.visibility ='visible';
		}
		bulleShow = true;
	}
	
}

function biShowBulleRetail(titre,prd){
if(!bulleShow)
	{
		//message = message.replace("'","\'");
		var content='';
		if(!iSizeFrag && iSize)content = "<div class='biTitreBulle' style='padding:2px; padding-right:3px;z-index:1'>"+titre+"</div>";	
		if(iSizeFrag || !iSize)content = "<div class='biTitreBulle' align='right' style='padding:2px; padding-right:3px;z-index:1'>"+titre+"</div>";

		var posy = 0;
		var posx = 0;
		var anchor = 'img'+prd;
		var divname = 'legend';		


		if(document.all)
		{
			posx=findPosX(document.getElementById(anchor))-findPosX(document.getElementById('lcmCenter'));
			posy=findPosY(document.getElementById(anchor))-findPosY(document.getElementById('lcmCenter'));
			posy+=52;
		}else{

			posy = lib_getImageYfromTop(anchor);
			posx = lib_getImageXfromLeft(anchor);
			if(iSize && !iSizeFrag)posy = parseInt(posy) +  iSize;
			if(!iSize && !iSizeFrag)posy = parseInt(posy) + 52;
			if(!iSize && iSizeFrag)posy = parseInt(posy) + 52;
			if(!iSize && iSizeFrag)posx = parseInt(posx)-68;
			if(!iSize && !iSizeFrag)posx = parseInt(posx)-68;
		}


		posx = parseInt(posx);

		if(document.getElementById('legend'))
		{

			document.getElementById('legend').style.left = posx+"px";
			document.getElementById('legend').style.top = posy+"px";		

			document.getElementById('legend').innerHTML = content;

			document.getElementById('legend').style.display ='block';
			document.getElementById('legend').style.visibility ='visible';
		}
		bulleShow = true;
	}
}

function biHideBulle()
{
	if(document.getElementById('legend'))
	{
	document.getElementById('legend').style.display ='none';
	document.getElementById('legend').style.visibility ='hidden';
	bulleShow = false;
	}
}


var zeX = 0;
var zeY = 0;
function biStoreCoordinate(e)
{
	
	if (!e) var e = window.event;
	if (e.pageX || e.pageY)
	{
		zeX = e.pageX;
		zeY = e.pageY;		
	}
	else if (e.clientX || e.clientY)
	{
		zeX = e.clientX + document.body.scrollLeft;
		zeY = e.clientY + document.body.scrollTop;
	}
}

/**** fonctions génériques ****/
function lib_getRealTop(imgElem) {
	yPos = eval('self.document.images["'+imgElem+'"]').offsetTop;
	tempEl = eval("self.document.images['"+imgElem+"']").offsetParent;
	while (tempEl != null) {
  		yPos += tempEl.offsetTop;
  		tempEl = tempEl.offsetParent;
  	}
	return yPos;
}

function lib_getImageYfromTop(imgID) {
  if (dom) return lib_getRealTop(imgID);

}

function lib_getImageXfromLeft(imgID) {
  if (dom) return lib_getRealLeft(imgID);
}

function lib_getRealLeft(imgElem) {
xPos = eval('self.document.images["'+imgElem+'"]').offsetLeft
tempEl = eval('self.document.images["'+imgElem+'"]').offsetParent
while (tempEl != null) {
xPos += tempEl.offsetLeft
tempEl = tempEl.offsetParent
}
return xPos
}

function lib_setVisibility(id, flag) {
 
  if(dom)
  {
    var str = (flag) ? 'block' : 'none';    
    eval("document.getElementById('" + id +"')").style.display = str;

  }  
  
}


function lib_setPosFromTop(id, yCoord) {
  if (dom) {eval("document.getElementById('" + id+"')").style.top = yCoord  }
}

function lib_setPosFromLeft(id, xCoord) {
	if (dom) {eval("document.getElementById('" + id+"')").style.left = xCoord  }
}
/** fin des fonctions génériques de positionnement **/



/** retourne 0 si impair et 1 si pair **/
function pair(nombre)
{
return ((nombre-1)%2);
}

/** gestion du bug de centrage si la fenêtre est trop petite pour le site **/
var larg=0;
var haut = 0;
function lcmIfInf1024()
{
	if (self.innerHeight) // all except Explorer
	{
		larg = self.innerWidth;
		haut = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight)
		// Explorer 6 Strict Mode
	{
		larg = document.documentElement.clientWidth;
		haut = document.documentElement.clientHeight;
	}
	else if (document.body) // other Explorers
	{
		larg = document.body.clientWidth;
		haut = document.body.clientHeight;
	}
}

function lcmWriteFix(x)
{

	if(larg<1000 || haut <595){
	document.writeln('<style type="text/css">');
	document.writeln('#lcmCenter{');
	document.writeln('width:100%;');
	document.writeln('height:100%;');
	document.writeln('position:absolute;left:0px;top:0px;margin:0;padding:0');
	document.writeln('display:block;}');
	document.writeln('body{');
	document.writeln('background-image: url(/img/bg_defaultsite.png);');
	document.writeln('background-position: -4px 0;');
	document.writeln('background-color:#fff;');
	document.writeln('background-repeat:no-repeat;');
	document.writeln('}');
	document.writeln('</style>')
	}else{	
		if(!x) var valSite = 'defaultsite';
		else var valSite = x;
		
		if(haut>720){
			document.body.style.backgroundImage='url(/img/bg_defaultsite_highres.png)';
		}else{
			document.body.style.backgroundImage='url(/img/bg_defaultsite.png)';
		}
		document.body.style.backgroundPosition='50% 50%';
	}
}
/** fin de gestion du pb de centrage **/

/** Fonction gérant l'ouverture de l'open window d'infos légales **/
function openprivacy(){

	window.open("/_fr/_ca/services/privacynotice.aspx?"+rtlid, "privacynotice", "toolbar=no, directories=no, menubar=no, status=no, resizable=no, scrollbars=no, width=600, height=584, left=100, top=100");
}

function ouvriroffre() {
	openanimation('offres.aspx',510,370,150,160,'');
}

/************ fonctions d'ouvertures **************/

function openvideo(prdcode,debit,axe)
{
	openvid = window.open('/_fr/_ca/catalog/video.aspx?prdcode='+ prdcode + '&debit='+ debit + '&axe=' + axe,'opwdwvid',"width=100,height=100,resizable=no,scrollbars=no,menubar=no,toolbar=no,directories=no,location=no,status=no,left=100,top=100");
	if (openvid) openvid.focus();
}

function openTell(curPage)
{
	var lcmtellow = new virtualPopup("/_fr/_ca/services/tellafriend.aspx?pageurl=" + escape(curPage),397,260,"redirectlayer",340,270);
	lcmtellow.show();
}

function openTellMajeur(curPage)
{
	var lcmtellow = new virtualPopup("/_fr/_ca/skincare/animations/majeur/tellafriend.aspx?pageurl=" + escape(curPage),372,338,"redirectlayer",307,160);
	lcmtellow.show();
}

function redirectFav(sUrl)
{
	if(isMember)
	{
		window.location=sUrl;		
	}else
	{
		var url = "/_fr/_ca/mylancome/redirectow.aspx?redir="+ URLEncode(sUrl);
		var lcmredirectow = new virtualPopup(url,278,224,"redirectlayer",350,250);
		lcmredirectow.show();
	}
}


function openanimation(codeurl,largeur,hauteur,gauche,haut,design){
	var gauche_defaut = 75;
	var haut_defaut = 120;
	var design_defaut = '';	
	if(codeurl.indexOf('http')==-1)
	{		
		if(codeurl.indexOf('?')==-1)
		{
			codeurl='/_fr/_ca/'+codeurl+'?'+rtlid;	
		}
		else
		{
			codeurl='/_fr/_ca/'+codeurl+'&'+rtlid;	
		}
	}else
	{
		window.open(codeurl);
		return;	
	}
	
	
	if(!largeur && !hauteur && !gauche && !haut && !design)
	{
		if(codeurl.indexOf('?')==-1)
		{
			window.location = codeurl+'?'+rtlid;
		}
		else
		{
			window.location = codeurl+'&'+rtlid;
		}
	}else
	{	
		if(!gauche || gauche=='')gauche = gauche_defaut;
		if(!haut || haut=='')haut = haut_defaut;
		if(!design || design=='')design = design_defaut;
		
		if(codeurl.indexOf('?')==-1)
		{
			codeurl=codeurl+'?'+rtlid;	
		}
		else
		{
			codeurl=codeurl+'&'+rtlid;	
		}
		
		
		var animationow = new virtualPopup(codeurl,largeur,hauteur,"redirectlayer", gauche, haut, design);
		animationow.show();
	}
	
}


/** add to bag descente de catalogue, prend 2 paramètres : le lien fiche produit (type product/produc1...) et le prd concerné **/
function openBuy(lienprod,prd,button)
{	
	if((lienprod.indexOf('product1')!=-1) || (lienprod.indexOf('productfragrance')!=-1) || (lienprod.indexOf('product_hypmen')!=-1) || (lienprod.indexOf('product_magnifique')!=-1))
	{
		url = 'ow_buy.aspx';
		w=380;
		h=170;
	}else
	{
		url = 'ow_buy_color.aspx';	
		w=380;
		h=270;			
	}
	if(button)
	{
		var addbutton = '&addbutton='+button;
	}else
	{
		var addbutton = '';
	}

	var lcmachatow = new virtualPopup("/_fr/_ca/catalog/"+url+"?prdcode=" + prd+addbutton,w,h,"redirectlayer",300,170,'gamme');
	lcmachatow.show();
}


/** add to basket retailer **/
function GoPreAddToBasket(rtlid, RetailerFile)
{
	var frm = GetMainFormName();

	if (frm!=null)
	{
		var sku = GetElementName("varlist", frm);
		if (sku!=null)
		{
			var selindex = document.forms[frm].elements[sku].selectedIndex;
			var ProductId= document.forms[frm].elements[sku].options[selindex].value;
			ProductId=ProductId.slice(0,ProductId.indexOf("#"));			
			// ProductId = ProductId.slice(ProductId.lastIndexOf("#")+1);
			var qtyEl = GetElementName("qtylist", frm);
			
			if(qtyEl!=null)
			{
				var qty = document.forms[frm].elements[qtyEl].value;
			
				if(GoAddToBasket!=null && GoAddToBasket!='undefined' && qty!=null && qty!='undefined' && ProductId!=null && ProductId!='undefined')
				{
					if (qty > 0)
					{
						// window.open (RetailerFile + '?pid=' + ProductId + '&qty=' + qty + '&rtlid=' + rtlid, "_blank", "top=0,left=5000,height=100,width=100");
						// Call the retailer addtobasket action
						GoAddToBasket(document.forms[frm],ProductId, qty);
					}
				}
			}
		}
	}
}

function GoPreAddToBasketForCollection(nameprefix, ContainerNumber, rtlid, RetailerFile)
{
      var frm= GetMainFormName();
      if (frm!=null)
      {
					var sku = GetElName("varlist",ContainerNumber);
					var selindex = document.forms[frm].elements[sku].selectedIndex;
					var ProductId= document.forms[frm].elements[sku].options[selindex].value;
					var qtyEl = GetElName("qtylist",ContainerNumber);
					ProductId=ProductId.slice(0,ProductId.indexOf("#"));			
					// ProductId = ProductId.slice(ProductId.lastIndexOf("#")+1);
                  //alert('sku: '+sku+'--index: '+ selindex+ '--Id: '+ProductId+'--qtyEl: '+qtyEl);
                  if(qtyEl!=null)
                  {
                        var qty = document.forms[frm].elements[qtyEl].value;
                        // alert('qty: '+qty+'--ProductId: '+ProductId);
                        if(GoAddToBasket!=null && GoAddToBasket!='undefined' && qty!=null && qty!='undefined' && ProductId!=null && ProductId!='undefined')
                        {
                             if (qty > 0)
                             {
                                   window.open (RetailerFile + '?pid=' + ProductId + '&qty=' + qty + '&rtlid=' + rtlid, "_blank", "top=0,left=5000,height=100,width=100");
                                   // Call the retailer addtobasket action
                                   GoAddToBasket(document.forms[frm],ProductId, qty);
                             }
                        }
                  }
      }
}

/************************/
/*** conversion prix ***/
var sTaux = 0.6805;
function convertDevise(devise)
{
 var sEurosFromDevise =Math.round((devise/sTaux)*100)/100;
 return sEurosFromDevise;
}
/*** modifs de texte apres appel moneris ***/
function changePage(elem1,elem2,elem3,elem4)
{
	var title = document.getElementById('mainTitle');
	ancientitre = document.getElementById('mainTitle').removeChild(document.getElementById('mainTitle').firstChild);
	var text = document.createtextNode(elem1);
	title.appendChild(text);
}
/**********************/

/** permet de rediriger automatiquement vers une Fiche produit en passant le prd de celui ci en paramètre **/
function gotoprod(prd)
{
if(!rtlid)rtlid = '';
	if(document.getElementById('ifredirectlayer'))
	{
	document.getElementById('ifredirectlayer').style.display = 'block';		
	document.getElementById('ifredirectlayer').src='/_fr/_ca/catalog/generate_link.aspx?prdcode='+prd+'&'+rtlid;
	}
}
var gotoProd = gotoprod;

/* Désacive la validation d'un formulaire via la touche 'enter' dans un champs de formulaire */
function initInput(tab,f) {
	for(var i=0; i<tab.length ; i++) {
		if(tab[i].type == "text") tab[i].onkeypress = f;
	}				
}

function handleEnter(event) {
	if((window.event && window.event.keyCode == 13) || (event && event.keyCode == 13)) return false;
	else return true;
}
