function updateURL (url, filterOut, replacement) {
	url = ((url||document.location) +"").replace(/^.*?(\?|$)/gi, ""); 
	var rgx = new RegExp("&?("+ filterOut +")=[^&]*", "gi");
	var prefixRgx = new RegExp("^&", "gi");
	var oldQuery = url.replace(rgx, ""); 
	if (prefixRgx.test(oldQuery)) 
			oldQuery = oldQuery.replace(prefixRgx, "")
	if (replacement != '') {
		oldQuery += (oldQuery.length ? "&" : "") + replacement;
	} 
	return "?"+ oldQuery;
}

function updateListFilter (field) {
	var url = field.selectedIndex ? field.name +"="+ escape(field.value) : "";
	url = updateURL($('idml_filterUrl_input').value, field.name, url);
	$('idml_filterUrl_input').value = url;
}

function applyListFilter () {
	document.location = $('idml_filterUrl_input').value;
}

function doSearch() {
	var url = document.location
	if ($('idml_searchStore_input').value=="1") {
		url = updateURL(url, "cat", "cat=");
	}
	url = updateURL(url, "q|idp|char", "q="+ escape($('idml_search_input').value)) ;
	document.location = url;
}

function updateProduct(car, forcePreselect){
	var url = '/_include/store/liveupdate.asp';
	var preselected = /(&char=|_char-)([^&]*)(?:&|$)/gi.exec(document.location.search);
	var c = [];
	if (preselected) {
		c = preselected[1].split("|"); 
	}
	var preselect = $('mainId').value || (preselected +'') || 'false';
	if (!forcePreselect) {
		for(var i=0; i<3; i++) {
			var select = window["SelectCharacteristic"+ (i+1)];
			c[i] = ((select&&select.options.length) ? encodeURIComponent(select.value) : "") || c[i] || "";
		}
	} 
	var pars = ["c1=", c[0], "&c2=", c[1], "&c3=", c[2], "&ref="+ js_mainReference, "&preselect="+ preselect].join("");
	var myAjax = new Ajax.Request( url, {method: 'get',parameters: pars, onLoading: showLoad, onComplete: updateStoreDetail, onFailure: updateStoreDetailFail} );
}


function updateStoreDetailFail(xhr){
   alert('failure');
}

function showLoad(){
	if ($('divLoad')){
		$('divLoad').style.display='block'
	}
}

function hideLoad(){
	if ($('divLoad')){
		$('divLoad').style.display='none'
	}
}




function updateStoreDetail(xhr){
	var idf = xhr.responseXML.getElementsByTagName('idf');
	
	var price = xhr.responseXML.getElementsByTagName('price');
	var priceht = xhr.responseXML.getElementsByTagName('price-ht');
	var pricettc = xhr.responseXML.getElementsByTagName('price-ttc');
	var promoprice = xhr.responseXML.getElementsByTagName('promoprice');
	var promopriceht = xhr.responseXML.getElementsByTagName('promoprice-ht');
	var promopricettc = xhr.responseXML.getElementsByTagName('promoprice-ttc');
	var reduction = xhr.responseXML.getElementsByTagName('reduction');
	var image = xhr.responseXML.getElementsByTagName('image');
	var stock = xhr.responseXML.getElementsByTagName('stock');
	var reference = xhr.responseXML.getElementsByTagName('reference');
	var deliveryTime = xhr.responseXML.getElementsByTagName('delivery-time');
	var deliveryDate = xhr.responseXML.getElementsByTagName('delivery-date');
	var ean13 = xhr.responseXML.getElementsByTagName('ean13');
	var weight = xhr.responseXML.getElementsByTagName('weight');
	var ecotax = xhr.responseXML.getElementsByTagName('ecotax');
	var partnumber = xhr.responseXML.getElementsByTagName('partnumber');
	
	var characteristic1 = xhr.responseXML.getElementsByTagName('characteristic1');
	var characteristic2 = xhr.responseXML.getElementsByTagName('characteristic2');
	var characteristic3 = xhr.responseXML.getElementsByTagName('characteristic3');
	
//try {
	
	if (idf.length){
		$('mainId').value = idf[0].firstChild.nodeValue;
	}
	for(var cIdx=1; cIdx<=3; cIdx ++) {
		var characteristic = eval("characteristic"+ cIdx);
		if (characteristic.length){
			characteristic = characteristic[0].getElementsByTagName('option');
			var objOption= eval("SelectCharacteristic"+ cIdx);
			var divOption= eval("ObjCharacteristic"+ cIdx);
			
			if (!objOption) {
				if (divOption) {
					divOption.style.display = "none";
				}
				continue;
			}
			if (!idf.length) {//objOption.options[0] && objOption.options[0].value=="") {
				objOption.options.length=1;
			} else {
				objOption.options.length=0;
			}
			var selectedIndex = 0;
			for (var i=0 ; i<characteristic.length ; i++){
				var charOption = characteristic[i];
				var texte=charOption.firstChild.nodeValue;
				//var valeur=charOption.getAttribute("id");
				var option = new Option(texte, texte); 
				selectedIndex = objOption.length;
				objOption.options[objOption.length] = option;
				option.selected = !!charOption.getAttribute("selected");
				//if (!!charOption.getAttribute("selected"))
				//	objOption.selectedIndex = selectedIndex;
			}
			
			if (divOption) {
				if (objOption.options.length==0){
					divOption.style.display="none"
				} else {
					divOption.style.display=""
				}
			}
		}
	} 
	
	//remplace les elements du panier par les nouveaux du fichier xml
	var promo=0;
	if ($('idml_promo_price_detail')){
		if (promoprice.length){
			promo = promoprice[0].firstChild.nodeValue;
			if (parseFloat(promo.replace(",", "."))) {
				if ($('idml_price_detail')) {
					$('idml_price_detail').innerHTML = promo +'&nbsp;'+ js_SigneMonnaie +'';
				}
				$('idml_promo_price_detail').innerHTML = price[0].firstChild.nodeValue+'&nbsp;'+js_SigneMonnaie+'';
				ProductOptions.setBasePrice(promo.replace(",", "."));
			} else {
				$('idml_promo_price_detail').innerHTML='';
				if (objPromo){
					objPromo.style.display="none"
				}
			}
		} else {
			$('idml_promo_price_detail').innerHTML='';
			if (objPromo) {
				objPromo.style.display="none"
			}
		}
	} 
	if (price.length && promo==0 && $('idml_price_detail')){
		$('idml_price_detail').innerHTML=price[0].firstChild.nodeValue+'&nbsp;'+js_SigneMonnaie+''
		
	}
	
	var promoht=0;
	if ($('idml_promo_price_detail_ht')){
		if (promopriceht.length){
			promoht = promopriceht[0].firstChild.nodeValue;
			if (parseFloat(promoht.replace(",", "."))) {
				if ($('idml_price_detail_ht')) {
					$('idml_price_detail_ht').innerHTML = promoht +'&nbsp;'+ js_SigneMonnaie +'';
				}
				$('idml_promo_price_detail_ht').innerHTML = priceht[0].firstChild.nodeValue+'&nbsp;'+js_SigneMonnaie+'';
			} else {
				$('idml_promo_price_detail_ht').innerHTML='';
				if (objPromo){
					objPromo.style.display="none"
				}
			}
		} else {
			$('idml_promo_price_detail_ht').innerHTML='';
			if (objPromo) {
				objPromo.style.display="none"
			}
		}
	} 
	if (priceht.length && promoht==0 && $('idml_price_detail_ht')){
		$('idml_price_detail_ht').innerHTML=priceht[0].firstChild.nodeValue+'&nbsp;'+js_SigneMonnaie+''
	}	

	
	var promottc=0;
	if ($('idml_promo_price_detail_ttc')){
		if (promopricettc.length){
			promottc = promopricettc[0].firstChild.nodeValue;
			if (parseFloat(promottc.replace(",", "."))) {
				if ($('idml_price_detail_ttc')) {
					$('idml_price_detail_ttc').innerHTML = promottc +'&nbsp;'+ js_SigneMonnaie +'';
				}
				$('idml_promo_price_detail_ttc').innerHTML = pricettc[0].firstChild.nodeValue+'&nbsp;'+js_SigneMonnaie+'';
			} else {
				$('idml_promo_price_detail_ttc').innerHTML='';
				if (objPromo){
					objPromo.style.display="none"
				}
			}
		} else {
			$('idml_promo_price_detail_ttc').innerHTML='';
			if (objPromo) {
				objPromo.style.display="none"
			}
		}
	} 
	if (pricettc.length && promottc==0 && $('idml_price_detail_ttc')){
		$('idml_price_detail_ttc').innerHTML=pricettc[0].firstChild.nodeValue+'&nbsp;'+js_SigneMonnaie+''
	}		

	if (!promoprice.length){
		ProductOptions.setBasePrice(price[0].firstChild.nodeValue.replace(",", "."))
	}else{
		ProductOptions.setBasePrice(promoprice[0].firstChild.nodeValue.replace(",", "."))
	}
	ProductOptions.onUpdateStoreDetail();
	

	if ($('idml_reduction_obj')){
		if (reduction.length){
			$('idml_reduction_obj').innerHTML='-'+reduction[0].firstChild.nodeValue+'%';
			$('idml_reduction_obj').style.display = "";
		} else {
			$('idml_reduction_obj').style.display = "none";
		}
	}
	
	if (reference.length){
		if ($('idml_reference_detail')){
			$('idml_reference_detail').innerHTML=reference[0].firstChild.nodeValue
		}
	}

	if (deliveryTime.length){
		if ($('idml_delivery_time_detail')){
			$('idml_delivery_time_detail').innerHTML=deliveryTime[0].firstChild.nodeValue
		}
		if ($('idml_delivery_date_detail')){
			$('idml_delivery_date_detail').innerHTML=deliveryDate[0].firstChild.nodeValue
		}
	}

	if (partnumber.length){
		if ($('idml_partnumber_detail')){
			$('idml_partnumber_detail').innerHTML=partnumber[0].firstChild.nodeValue
		}
	}

	if (ean13.length){
		if ($('idml_ean13_detail')){
			$('idml_ean13_detail').innerHTML=ean13[0].firstChild.nodeValue
		}
	}

	if (weight.length){
		if ($('idml_weight_detail')){
			$('idml_weight_detail').innerHTML=weight[0].firstChild.nodeValue
		}
	}

	if (ecotax.length){
		if ($('idml_ecotax_detail')){
			$('idml_ecotax_detail').innerHTML=ecotax[0].firstChild.nodeValue +'&nbsp;'+js_SigneMonnaie+''
		}
	}

	if (stock.length){
		
		if ($('idml_stock_detail')){
			$('idml_stock_detail').innerHTML=stock[0].firstChild.nodeValue
		}
		//detection pour changer le texte du stock
		if (stock[0].firstChild.nodeValue>0){
			if (objStockOk){objStockOk.style.display=''}
			if (objStockkO){objStockkO.style.display="none"}
		}else{
			if (objStockOk){objStockOk.style.display="none"}
			if (objStockkO){objStockkO.style.display=""}
		}
	}	
	

	
	if (image.length){
		if (image[0].firstChild.nodeValue!=''){
			swpMainPics(image[0].firstChild.nodeValue,js_mainPicSize,js_mainPicSizeZoom)
		}else{
			swpMainPics(js_mainPicName,js_mainPicSize,js_mainPicSizeZoom)
		}
	}
	
	//modifi l'id de la fiche pour prendre la sous référence

//} catch(any) {
//	alert(any);
//}	
	//verifie que le logo de chargement soit bien supprimé
	setTimeout("hideLoad()",100);

	if (typeof(onUpdateStoreDetail)=='function'){
		onUpdateStoreDetail();
	}
}

//verification si les options obligatoire sont bien rempli et ajouter au panier
function addToCart(idf,mainId){
	var qt=($('qt_'+mainId))?$('qt_'+mainId).value:'1';
	document.location.href='/_include/cart/cart.asp?action=add&idf='+idf+'&qt='+qt
}


function submitCart(){
	var bad=0
	
	if (SelectCharacteristic1){
		if (SelectCharacteristic1.value==''&&SelectCharacteristic1.options.length>1){bad=1}
	}
	if (SelectCharacteristic2){
		if (SelectCharacteristic2.value==''&&SelectCharacteristic2.options.length>1){bad=1}
	}
	if (SelectCharacteristic3){
		if (SelectCharacteristic3.value==''&&SelectCharacteristic3.options.length>1){bad=1}
	}
	
	if (bad==0){
		document.getElementById('detailProduct').submit();
	}else{
		alert("Vous devez sélectionner les options avant d'ajouter au panier.")
	}
}

function swpMainPics(name,size,zoom){

	mainPic=$('idml_image_main_img')
	if (mainPic){
		mainPic.src='/images/store/'+size+'/'+name
		if (zoom>0){
			mainPic.onclick=function(){eval('modalDiv.show("'+name+'","'+zoom+'")')}
		}else{
			mainPic.onclick=null
		}
	}
}



var modalDiv = {
	construct : function(){
		var divs_transparentDiv
		divs_transparentDiv = document.createElement('DIV');
		divs_transparentDiv.id='modaldiv';
		divs_transparentDiv.style.left='0px';
		divs_transparentDiv.style.top='0px';
		divs_transparentDiv.style.backgroundColor='#000000' 
		divs_transparentDiv.style.filter='alpha(opacity=75)' 
		divs_transparentDiv.style.opacity='0.75' 
		divs_transparentDiv.style.position='absolute'
    	divs_transparentDiv.style.width = document.body.scrollWidth + 'px';
    	divs_transparentDiv.style.height = document.body.scrollHeight + 'px';
		divs_transparentDiv.style.zIndex="200";
		divs_transparentDiv.style.cursor="pointer";
		divs_transparentDiv.onclick=function(){modalDiv.hide();};
		document.body.appendChild(divs_transparentDiv);
		
		div_picture = document.createElement('DIV');
		div_picture.id='idml_imageZoom_div';
		div_picture.className='cssml_imageZoom_div';
		div_picture.style.position='absolute'
		div_picture.style.border='1px solid #000000'	
		div_picture.style.backgroundColor='#FFFFFF' 
		div_picture.style.padding='8px'		
		div_picture.style.zIndex="201";
		//div_picture.style.display='none';
		div_picture.style.top='-400px'	
		document.body.appendChild(div_picture);	
		//$('idml_zoom_div').style.display='none'
	},
	
	hide : function(){
		$('modaldiv').style.display='none'
		$('idml_imageZoom_div').style.visibility='hidden'
		$('idml_imageZoom_div').style.top='-400px'	
		window.onscroll=null
	},
	
	show : function(imageName,Size){
		
		ModalObj=$('modaldiv')
		if (!ModalObj){
			modalDiv.construct();
			ModalObj=$('modaldiv')
		}
		


		pictureZoom=$('idml_pictureZoom_img')
		if (!pictureZoom){
			pictureZoom=document.createElement('IMG');
			pictureZoom.id='idml_pictureZoom_img';
			pictureZoom.onerror=function(){setDefaultPics(this,Size);}
			pictureZoom.onload=function(){centerPopup('idml_imageZoom_div')}
			pictureZoom.src='/images/store/'+Size+'/'+imageName
			pictureZoom.style.cursor="pointer";
			pictureZoom.onclick=function(){modalDiv.hide();};
			$('idml_imageZoom_div').appendChild(pictureZoom);

			
		}else{
			pictureZoom.src='/images/store/'+Size+'/'+imageName
		
		}
		
		
		
		window.onscroll=function(){
			centerPopup('idml_imageZoom_div')
		}
		
		$('modaldiv').style.display=''
		$('idml_imageZoom_div').style.display=''
		
		//centerPopup('idml_imageZoom_div')

				
	},
	
	resize : function(){
    	$('modaldiv').style.width = document.body.offsetWidth + 'px';
    	$('modaldiv').style.height = document.body.offsetHeight + 'px';
	}

}


function setDefaultPics(obj,Size){
	obj.src='/images/store/'+Size+'/error.png'
}

function centerPopup(element) {
	document.getElementById(element).style.visibility=''
	
	var height=document.getElementById(element).offsetHeight;//hauteur de l'élément à positionner
	var width=document.getElementById(element).offsetWidth;//largeur de l'élément à positionner
	//alert(document.getElementById(element).offsetHeight+'--'+pHeight+'--'+-sTop)



	var myParent=document.getElementById(element).parentNode;
	var pHeight=myParent.clientHeight;//Hauteur de l'élément parent
	var pWidth=myParent.offsetWidth;//Largeur de l'élément parent
	var sTop=myParent.scrollTop;//Hauteur de défilement de l'élément parent
	var sLeft=myParent.scrollLeft;//Longueur de défilement de l'élément parent
//	var posY=(pHeight/2)-(height/2)+sTop;//Calcul de la position en Y
//	var posX=(pWidth/2)-(width/2)+sLeft;//Calcul de la position en X


	//alert(document.getElementById(element).offsetHeight+'--'+pHeight+'--'+-sTop)
	
	var posY=(pHeight-height)/2+sTop;//Calcul de la position en Y
	var posX=(pWidth/2)-(width/2)+sLeft;//Calcul de la position en X

	document.getElementById(element).style.left=posX+"px";
	document.getElementById(element).style.top=posY+"px";

}

function zoom(nomImage,sizeImage){
	alert('test')
	//modalDiv.show(nomImage,sizeImage);
}

















function loadMlStore(){
	//definition des varibles
	SelectCharacteristic1=$('id_characteristic_01')
	SelectCharacteristic2=$('id_characteristic_02')
	SelectCharacteristic3=$('id_characteristic_03')
 
	//detection pour masquer le tritre des Caractéristiques (option) 
	ObjCharacteristic1=$('idml_characteristic1_obj')
	ObjCharacteristic2=$('idml_characteristic2_obj')
	ObjCharacteristic3=$('idml_characteristic3_obj')
	
	objStockOk=$('idml_stockok_obj')
	objStockkO=$('idml_stockko_obj')
	
	objReference=$('idml_reference_detail')
	
	//detection pour masquer le tritre des detail supplémentaires 
	objSupplement=$('idml_supplement_obj')
	tableSupplement=$('idml_supplement_table')
	if (objSupplement&&!tableSupplement){objSupplement.style.display="none"}
	
	objFavoriteProducts=$('idml_favorite_products_obj')
	tableFavoriteProducts=$('idml_favorite_products_table')
	if (objFavoriteProducts&&!tableFavoriteProducts){objFavoriteProducts.style.display="none"}
	
	objPromoProducts=$('idml_promo_products_obj')
	tablePromoProducts=$('idml_promo_products_table')
	if (objPromoProducts&&!tablePromoProducts){objPromoProducts.style.display="none"}
	
	objRelatedProducts=$('idml_related_products_obj')
	tableRelatedProducts=$('idml_related_products_table')
	if (objRelatedProducts&&!tableRelatedProducts){objRelatedProducts.style.display="none"}	
	
	objReviews=$('idml_reviews_obj')
	divReviews=$('idml_reviews_div')
	if (objReviews&&!divReviews){objReviews.style.display="none"}
	
	objReviews=$('idml_images_auto_obj')
	divReviews=$('idml_images_auto_table')
	if (objReviews&&!divReviews){objReviews.style.display="none"}	
	
	objPromo=$('idml_promo_obj')
	spanPromo=$('idml_promo_price_detail')
	if (objPromo&&spanPromo){
		if (spanPromo.innerHTML==''){
			objPromo.style.display="none"
		}
	}
	
	objButtonAddToCart=$('id_button_addtocart')
	
	//gestion du bouton ajouter au panier
	if (objButtonAddToCart){
		objButtonAddToCart.onclick=addToCart;
		objButtonAddToCart.style.cursor='pointer';
	}
	
	updateProduct('', true);

	for (var cIdx=1; cIdx<=3; cIdx++) {
		var objOption= eval("SelectCharacteristic"+ cIdx);
		var divOption= eval("ObjCharacteristic"+ cIdx);
		
		if (!objOption) {
			if (divOption) {
				divOption.style.display = "none";
			}
		}
	}
}