function customMajPrix(elem, n, colorisIndex){

	if(colorisIndex ==undefined)
		var index = elem.selectedIndex;
	else 	
		var index = colorisIndex-1;

	var newItem = document.getElementById("items_"+n+"_"+index).value;
	var newPrice = document.getElementById("prices_"+n+"_"+index).value;
	var newOOS = document.getElementById("outofstock_"+n+"_"+index).value;
	
	if(newOOS == 1) {
		if(document.getElementById("outofstock"))document.getElementById("outofstock").innerHTML=document.getElementById("outOfStockMessage").value;
		if(document.getElementById("instocknow"))document.getElementById("instocknow").innerHTML='';
	}	
		else {
		if(document.getElementById("outofstock"))document.getElementById("outofstock").innerHTML='';
		if(document.getElementById("instocknow"))document.getElementById("instocknow").innerHTML=document.getElementById("inStockNowMessage").value;
	}	
	document.getElementById("outofstock_"+n).value=newOOS;
	document.getElementById("item_"+n).value=newItem;
	document.getElementById('prix'+n+'_unit').value=newPrice;
	
	customCalculPrix(n,'ja_JP');
}

function customCalculPrix(n, locale){
	var nbProd= document.getElementById('p'+n+'_quant').value;
	var prixUnit = document.getElementById('prix'+n+'_unit').value;
	if (locale == 'ja_JP' )
		 prixUnit = prixUnit.replace(",","");	
	else
	     prixUnit = prixUnit.replace(",",".");
	var total = (Math.round(nbProd * prixUnit*100))/100;
	total = total.toString().replace(".",",");
	if (locale == 'ja_JP' ) {
			var totalLength = total.toString().length
			if(totalLength >3)  total = total.substring(0,totalLength-3)+","+total.substring(totalLength-3,totalLength);
	}else{
		//conversion en monnÚtaire
			var pos = (total.length) - 2;
			//rÚcupÚration avant derniÞre lettre de la chaine, si c'est une virgule, on rajoute un 0 Ó la fin de la cha¯ne
			letter = total.substr(pos,1);
			if(letter == ",") total = total + "0";
			else{
				index = total.indexOf(",");
				if(index==-1) total=total+",00";
			}
		//fin de la conversion
	}
	if (locale == 'en_US' ) total = total.toString().replace(",",".");
	if (locale == 'en_UK' ) total = total.toString().replace(",",".");
	
	document.getElementById("price_total_"+n).innerHTML = total;
}


function rtrim(aString) {
    var regExpEnd       = /ml$/;
    return aString.replace(regExpEnd,"");
} 

function formatNb(nombre,sep)
{
	nombre = String(nombre);
	var conserv;
	var elements = nombre.split(sep);
	
	if (elements.length>1){
		if (elements[1].length>2)
		{
			conserv = elements[1].substring(0,2);
			nombre = elements[0] + sep + conserv;
			return parseFloat(nombre,10);
		} else {
			return parseFloat(nombre,10);
		} 
	} else{
		return nombre;
	}
}




function PriceCentMl(n,sep){

var prix=document.getElementById('prix'+n+'_unit').value;
var size=document.getElementById("p"+n+"_select").options[document.getElementById("p"+n+"_select").value].text;
size=rtrim(size);

var retour=(prix/size)*100;

retour= formatNb(retour,sep);
//alert (retour);

document.getElementById("pricepercent_"+n).innerHTML = retour;


}


var busy = false;

//
// ***
// * This javascript function is used by the 'Add to Shopcart' button.  Since the HTML form is shared by both 'Add to Shopcart' and 'Add to Wish List' button,
// * appropriate values are set using this javascript before the form is submitted.
// * The variable 'busy' is used to avoid submitting the same forms multiple times when users click the button more than once.
// ***
//

function Add2ShopCart(form, n){
   
   if (!busy) {
   		  var outofstock = document.getElementById("outofstock_"+n).value;
   		  var outofstockMessage = document.getElementById("outOfStockMessage").value;	
   		   if(outofstock == 1)
   		   		alert(outofstockMessage);
   		   else{	
   		  var catentryId = document.getElementById("item_"+n).value;
   		  var quantity = document.getElementById("p"+n+"_quant").value;
          busy = true;
          form.action="ExtendedOrderItemAdd";
          form.catEntryId.value = catentryId;
          form.quantity.value = quantity;
          form.URL.value='OrderCalculate?item_quantity*=&URL=OrderItemDisplay';
          form.submit();
         } 
   }
}

function Add2ShopCartWithConfirmation(form, n){
   
   if (!busy) {
   		  var outofstock = document.getElementById("outofstock_"+n).value;
   		  var outofstockMessage = document.getElementById("outOfStockMessage").value;	
   		  var travelTrial="";
   		  for(var i=1;i<9;i++){
   		  if(document.getElementById("travelCatEntyId_"+i) && document.getElementById("travelCatEntyId_"+i).checked){
   		  	travelTrial+="&checkedTravelCatEnt"+i+"="+document.getElementById("travelCatEntyId_"+i).value;
   		  	}
   		  }
   		   for(var i=1;i<7;i++){
   		  if(document.getElementById("trialCatEntyId_"+i) && document.getElementById("trialCatEntyId_"+i).checked){
   		  	travelTrial+="&checkedTrialCatEnt"+i+"="+document.getElementById("trialCatEntyId_"+i).value;
   		  	}
   		  }
   		  
   		   if(document.getElementById("no_package") && document.getElementById("no_package").checked){
   		  	travelTrial+="&noPackage=true";
   		  	}
		  if(document.getElementById("orderitemsCounter")){
   		  	var orderItemCounter=document.getElementById("orderitemsCounter").value;
   		  	var table= new Array();
   		  	for(var i=1;i<orderItemCounter+1;i++){
	   		  if(document.getElementById("gift_"+i)){
	   		  	 if(document.getElementById("gift_"+i).checked)
	   		  		table[i]=true;
	   		  	 else 
	   		  		table[i]=false;

	   		  }
	   		  else table[i]=false;
   			}
   			travelTrial+="&checkedGiftWrap="+table;
   		  }
   		   if(outofstock == 1)
   		   		alert(outofstockMessage);
   		   else{	
		   		  var catentryId = document.getElementById("item_"+n).value;
		   		  var quantity = document.getElementById("p"+n+"_quant").value;
		   		  var storeId=form.storeId.value;
		   		  var langId=form.langId.value;
		   		  var locale;
		   		  if (form.locale)
		   		  	locale=form.locale.value;
		   		  var catalogId=form.catalogId.value;
		   		  //var URL='AddToBagView';
		   		  var URL='OrderCalculate?URL=AddToBagView';
		   		  busy = true;
		   		  if(document.getElementById("nb_item")!=null)
		   			var currentBag=parseInt(document.getElementById("nb_item").innerHTML);
		   		  var qtty=parseInt(quantity);
		   		  var nbItems=qtty+currentBag;
		   		  affichDetail('ExtendedOrderItemAdd?catEntryId='+catentryId+'&quantity='+quantity+'&storeId='+storeId+'&langId='+langId+'&locale='+locale+'&catalogId='+catalogId+'&URL='+URL+travelTrial+'&nbItems='+nbItems,'details_box');
		   		  
			      busy=false;
			      
         	} 
   	}
}


function Add2Selection(form, n,indice){
   
   //alert("form="+form);
   //alert("indice="+indice);
   //alert("n="+n);
   
   if (!busy) {
   		  var outofstock = document.getElementById("outofstock_"+n).value;
   		  var outofstockMessage = document.getElementById("outOfStockMessage").value;	
   		   if(outofstock == 1)
   		   		alert(outofstockMessage);
   		   else{	
   		  var catentryId = document.getElementById("item_"+n).value;
   		  var quantity = document.getElementById("p"+n+"_quant").value;
          busy = true;
          //form.action="OrderItemAdd";
          
          //on utilise indice1 pour rendre le numero indice qui va être utiliser avec 
          //la catentryID acceptable dans le type "Int" (sinonil generara une erreur dans une commande)
          var indice1 = (indice.toString()).replace('0000','0');    
          var hiddenCatInput = document.createElement('input');
		  hiddenCatInput.type = 'hidden';
		  hiddenCatInput.name = 'catEntryId_'+indice1;
		  hiddenCatInput.id = 'catEntryId_'+indice1;
		  hiddenCatInput.value = catentryId;
		 
		  var hiddenQttInput = document.createElement('input');
		  hiddenQttInput.type = 'hidden';
		  hiddenQttInput.name = 'quantity_' +indice1;
		  hiddenQttInput.id = 'quantity_' +indice1;
		  hiddenQttInput.value = quantity;
		  
		  form.appendChild(hiddenCatInput);
		 
		  form.appendChild(hiddenQttInput);
		 
		  document.getElementById(indice).checked = true;
		 
		  document.getElementById("details_box").style.display = "none";
		  
		  busy = false;
         
         } 
   }
}


function SpecialAdd2ShopCart(form, n){

   if (!busy) {
   		   var outofstock = document.getElementById("outofstock_"+n).value;	
   		   var outofstockMessage = document.getElementById("outOfStockMessage").value;
   		   if(outofstock == 1)
   		   		alert(outofstockMessage);
   		   else{	
   		  		var catentryId = document.getElementById("item_"+n).value;
   		  		var quantity = document.getElementById("p"+n+"_quant").value;
          		busy = true;
          		form.action="ExtendedOrderItemAdd";
         		form.catEntryId.value = catentryId;
         		form.quantity.value = quantity;
          		form.submit();
   		  } 
   }
}




//
// ***
// * Special add for dynamicKit
// ***
//
function AddDK2ShopCart(form, configId,n){
   
   if (!busy) {
   		  var catentryId = document.getElementById("item_"+n).value;
   		  var quantity = document.getElementById("p"+n+"_quant").value;
   		  var configurationId = configId;
          busy = true;
          form.action="OrderItemAdd";
          form.catEntryId.value = catentryId;
          form.quantity.value = quantity;
          form.configurationId.value = configurationId;
          form.URL.value='OrderCalculate?item_quantity*=&URL=OrderItemDisplay';
          form.submit();
   }
}

function AddDK2ShopCartWithConfirmation(form, configId,n){
   
   if (!busy) {
   		  var catentryId = document.getElementById("item_"+n).value;
   		  var quantity = document.getElementById("p"+n+"_quant").value;
   		  busy = true;
   		  var storeId=form.storeId.value;
   		  var langId=form.langId.value;
   		  var catalogId=form.catalogId.value;
   		  var locale;
   		  if (form.locale)
   		  	locale=form.locale.value;
   		  var URL='AddToBagView';
          affichDetail('ExtendedOrderItemAdd?catEntryId='+catentryId+'&quantity='+quantity+'&locale='+locale+'&storeId='+storeId+'&langId='+langId+'&catalogId='+catalogId+'&configurationId='+configId+'&URL='+URL,'details_box');
		  busy=false;
   } 
   
}

//
// ***
// * This function has been created to cover adding a dynamic kit with configuration in the iitem table
// * we customize field3 to contain the configurationId 
// ***
//

function AddDK2WishList(form, catEntryId, configId) {
   if (!busy) {
          busy = true;
          var storeId=form.storeId.value;
   		  var langId=form.langId.value;
   		  var catalogId=form.catalogId.value;
   		  var URL='AddToFavouritesView';
          affichDetail('InterestItemAdd?catEntryId='+catEntryId+'&storeId='+storeId+'&langId='+langId+'&catalogId='+catalogId+'&field3='+configId+'&URL='+URL+'&listId=.','details_box');
          busy=false;
		 
   }
}


// This javascript function is used by the 'Add to Wish List' button to set appropriate values before the form is submitted

function Add2WishList(form, catEntryId) {
   if (!busy) {
   		  busy = true;
   		  var storeId=form.storeId.value;
   		  var langId=form.langId.value;
   		  var catalogId=form.catalogId.value;
   		  var URL='AddToFavouritesView';
          affichDetail('ExtendedInterestItemAdd?catEntryId='+catEntryId+'&storeId='+storeId+'&langId='+langId+'&catalogId='+catalogId+'&URL='+URL+'&listId=.&updateable=0','details_box');
		  busy=false;
          
   }
}

function redirectToForm(form,destination) {
   if (!busy) {
          busy = true;
          form.action="ProductDisplay";
          form.partNumber.value = destination;
          form.submit();
   }
}

function redirectToFormWithURLRW(form, destination) {
	if (!busy) {
          busy = true;
          var storeId=form.storeId.value;
   		  var langId=form.langId.value;
   		  var catalogId=form.catalogId.value;
   		  var tableau=destination.split("#"); 
   		  if (tableau[0].indexOf("cat")!=-1)
   		  {
   		  	form.action=tableau[1];
   		  }
   		  else
   		  {
          	form.action="beauty-products_"+tableau[2]+"_"+tableau[3]+"_"+tableau[0]+"_"+storeId+"_"+catalogId+"_"+langId+"_"+tableau[1];
          }
          form.submit();
	}
}


function checkMail(){
	
		//alert('11111');
		messageAlert = "";
		controlEmail("forgotpass_mail");
			
		if(messageAlert == "")
			{
			var email = document.getElementById("forgotpass_mail").value;
			affichDetail('ResetPassword?URL=DiagFaceCare3View&logonId='+email+'&challengeAnswer=-&storeId=10051','details_box');
			document.getElementById("lostemail_div").style.display = "none";
			}
			else
			{
			alert(messageAlert);
			}
			return false;

}
// Cormetrics
function popinCormetrics(partNumber, name, sequence, subcategoryId, storeId, currency){
if (sequence=="") 
	sequence="1";
try{
	var itemSelected= document.getElementById("p"+sequence+"_select").value;
	var price = document.getElementById("prices_"+sequence+"_"+itemSelected).value.replace(",",".");
	var qty= document.getElementById("p"+sequence+"_quant").value;
	//alert("'"+partNumber+"'"+"'"+name+"'"+"'"+qty+"'"+"'"+price+"'"+"'"+catalogId+"'"+"'"+catalogId+"'");
	cmCreateShopAction5Tag("'"+partNumber+"'","'"+name+"'","'"+qty+"'","'"+price+"'","'"+subcategoryId+"'","'"+storeId+"'","'"+currency+"'");
	cmDisplayShop5s();
}catch(err){
}
}
function popinPackageCormetrics(partNumber, name, sequence, subcategoryId, storeId, currency){
if (sequence=="") 
	sequence="1";
try{
	var price = document.getElementById("prix"+sequence+"_unit").value.replace(",",".");
	var qty= document.getElementById("p"+sequence+"_quant").value;
	//alert("'"+partNumber+"'"+"'"+name+"'"+"'"+qty+"'"+"'"+price+"'"+"'"+subcategoryId+"'"+"'"+storeId+"'");
	cmCreateShopAction5Tag("'"+partNumber+"'","'"+name+"'","'"+qty+"'","'"+price+"'","'"+subcategoryId+"'","'"+storeId+"'","'"+currency+"'");
	cmDisplayShop5s();
}catch(err){
}
}

function addTrial(form, quantityCounter, thisOrder){
	var existingTravel=false;	 
	var errorSamplesMessage;
	var countTrial=0;
	var countTravel=0;
	
	if(document.getElementById("existingTravel")) existingTravel=true;	
	if (form.errorSamplesMessage) errorSamplesMessage=form.errorSamplesMessage.value;
	form.returnPage.value='quickcheckout';
	form.createSamples.value='true';
	
	switch (quantityCounter){
		case 1:
			countTrial=1;
			break;
		case 2:
			countTrial=2;
			break;
		case 3:
			countTrial=3;
			break;
		case 4:
			countTrial=4;
			break;
		case 5:
			countTrial=5;
		default : countTrial=5;	
	}
	if(quantityCounter > 1 && quantityCounter <= 3 && existingTravel) countTravel=1
	if(quantityCounter > 3 && existingTravel) countTravel=2;
		
	if (form.trialCatEntyId_1) if(form.trialCatEntyId_1.checked) countTrial=countTrial-1;
	if (form.trialCatEntyId_2) if(form.trialCatEntyId_2.checked) countTrial=countTrial-1;
	if (form.trialCatEntyId_3) if(form.trialCatEntyId_3.checked) countTrial=countTrial-1;
	if (form.trialCatEntyId_4) if(form.trialCatEntyId_4.checked) countTrial=countTrial-1;
	if (form.trialCatEntyId_5) if(form.trialCatEntyId_5.checked) countTrial=countTrial-1;
	if (form.trialCatEntyId_6) if(form.trialCatEntyId_6.checked) countTrial=countTrial-1;
	if (form.trialCatEntyId_7) if(form.trialCatEntyId_7.checked) countTrial=countTrial-1;
	if (form.trialCatEntyId_8) if(form.trialCatEntyId_8.checked) countTrial=countTrial-1;	
	
	if (form.travelCatEntyId_1) if(form.travelCatEntyId_1.checked) countTravel=countTravel-1;
	if (form.travelCatEntyId_2) if(form.travelCatEntyId_2.checked) countTravel=countTravel-1;
	if (form.travelCatEntyId_3) if(form.travelCatEntyId_3.checked) countTravel=countTravel-1;
	if (form.travelCatEntyId_4) if(form.travelCatEntyId_4.checked) countTravel=countTravel-1;
	if (form.travelCatEntyId_5) if(form.travelCatEntyId_5.checked) countTravel=countTravel-1;
	if (form.travelCatEntyId_6) if(form.travelCatEntyId_6.checked) countTravel=countTravel-1;
	if (form.travelCatEntyId_7) if(form.travelCatEntyId_7.checked) countTravel=countTravel-1;
	if (form.travelCatEntyId_8) if(form.travelCatEntyId_8.checked) countTravel=countTravel-1;
	if(countTrial < 0 || countTravel < 0){
		alert(errorSamplesMessage);
		return false;
	}
	else {
		deleteDefaultComment();
		
		$("input:checkbox").each( function() {
			$(this).attr('disabled','disabled');
		});
		
		if (form.trialCatEntyId_1) if(form.trialCatEntyId_1.checked) form.trialCatEntyId_1.disabled = false;
		if (form.trialCatEntyId_2) if(form.trialCatEntyId_2.checked) form.trialCatEntyId_2.disabled = false;
		if (form.trialCatEntyId_3) if(form.trialCatEntyId_3.checked) form.trialCatEntyId_3.disabled = false;
		if (form.trialCatEntyId_4) if(form.trialCatEntyId_4.checked) form.trialCatEntyId_4.disabled = false;
		if (form.trialCatEntyId_5) if(form.trialCatEntyId_5.checked) form.trialCatEntyId_5.disabled = false;
		if (form.trialCatEntyId_6) if(form.trialCatEntyId_6.checked) form.trialCatEntyId_6.disabled = false;
		if (form.trialCatEntyId_7) if(form.trialCatEntyId_7.checked) form.trialCatEntyId_7.disabled = false;
		if (form.trialCatEntyId_8) if(form.trialCatEntyId_8.checked) form.trialCatEntyId_8.disabled = false;
		
		form.URL.value = 'OrderPrepare?URL=OrderCalculate?URL=OrderItemDisplay&orderId=' +thisOrder+ '&updatePrices=1&calculationUsageId=-1&calculationUsageId=-2&calculationUsageId=-3&calculationUsageId=-4&calculationUsageId=-5&calculationUsageId=-6&calculationUsageId=-7';
		form.submit();
	}
}

