function setStep(checkboxName, hiddenName, checkedValue, uncheckedValue){
	//used to set the next step. If the checkbox is checked, the next step value is "checkedValue", otherwise, it is "uncheckedValue"
	if(document.getElementById(checkboxName).checked)
		document.getElementById(hiddenName).value=checkedValue;
	else
		document.getElementById(hiddenName).value=uncheckedValue;
}

function verifyCcInformations(idCcPaymentMode, hasCardRefId, msgMissingCcNumber, msgMissingCcType, msgMissingCcOwner, msgNoWayToProceed, msgMissingCVCCode){
	var isValid = true;
	if(document.getElementById('paymentMode'+idCcPaymentMode).checked==true){
		if (document.getElementById('deliveryMode1').checked==true){
			alert(msgNoWayToProceed);
			isValid = false;
		}
		else{
			if (hasCardRefId == false){
				if (document.getElementById('spfCardNumber').value == ''){
					isValid = false;
					alert(msgMissingCcNumber);
				}
				else if (document.getElementById('ccOwner').value == ''){
					isValid = false;
					alert(msgMissingCcOwner);
				}
				else if (document.getElementById('cvcCode').value == ''){
					isValid = false;
					alert(msgMissingCVCCode);
				}
				else{
					var radiobuttons = document.getElementsByName('radioCreditCard');
					var radioOk = false
					var number = radiobuttons.length;
					for (var i = 0; i < number; i++){
						if (radiobuttons[i].checked == true){
							radioOk = true;
						}	
					}
					if (radioOk == false){
						isValid = false;
						alert(msgMissingCcType);
					}
				}
			}
		}
	}
	if (isValid){
			document.getElementById('continue').submit();
	}
}

function calculatePrice(priceEconomy, pricePriority, priceGiftcardEconomy, priceGiftcardPriority, freeLimitEconomy, freeLimitPriority, freeLimitGiftcard, indexApplySupplementaryPrice, supplementaryPriceGrouped, supplementaryPriceUngrouped, basketItemsBooksNo, nrGroups, totalBookPrice, totalGCPrice, totalPriceWithoutDelivery, idDeliveryModeEconomy, idDeliveryModePriority, idDeliveryModeShop, TaxRecommande, arrayUngrouped)
{
	//priceEconomy - price for economy delivery
	//pricePriority - price for priority delivery
	//freeLimitEconomy - price over which the shipping is free (80)
	//freeLimitPriority - price over which the shipping is free (120)
	//nrBooks - the number of book items
	//indexApplySupplementaryPrice - the number of books after which the supplementary price/book will be applied
	//supplementaryPriceGrouped - the supplementary price to be applied for grouped commands
	//supplementaryPriceUngrouped - the supplementary price to be applied for ungrouped commands
	//nrGroups - the number of groups in which the ungrouped command can be delivered
	//totalPriceWithoutDelivery - the total price, not including the delivery price
	var deliveryPrice = 0;
	var calculatedDeliveryPrice = 0;
	var calculatedTotalPrice =0;
	
	//alert('On effectue bien le javascript calculatePrice');

	//alert(TaxRecommande);
	//alert(idDeliveryModePriority);
	//alert(idDeliveryModeEconomy);
	//alert(idDeliveryModeShop);
	//alert(document.getElementById('deliveryOption1').checked);

	var nrBooks= parseInt(top.document.getElementById('nrBooks').value);
	
		if((document.getElementById('deliveryMode'+idDeliveryModeEconomy)!=null)&&(document.getElementById('deliveryMode'+idDeliveryModeEconomy).checked == true))//expedition - economy
		{	
			document.getElementById('deliveryOption1').enabled=true;
			document.getElementById('deliveryOption2').enabled=true;			
		}
		if((document.getElementById('deliveryMode'+idDeliveryModePriority)!=null)&&(document.getElementById('deliveryMode'+idDeliveryModePriority).checked == true))//expedition - priority
		{
			document.getElementById('deliveryOption1').enabled=true;
			document.getElementById('deliveryOption2').enabled=true;
		}
		if((document.getElementById('deliveryMode'+idDeliveryModeShop)!=null)&&(document.getElementById('deliveryMode'+idDeliveryModeShop).checked == true))//retrait en magasin
		{
			document.getElementById('deliveryOption1').checked=true;
			document.getElementById('deliveryOption2').checked=false;
			document.getElementById('deliveryOption1').enabled=false;
			document.getElementById('deliveryOption2').enabled=false;
			document.getElementById('paymentMode1').checked=true;
			if (document.getElementById('paymentMode2') != null){
				document.getElementById('paymentMode2').checked=false;
				document.getElementById('paymentMode2').enabled=false;
			}
			if (document.getElementById('paymentCCTotal') != null){
				document.getElementById('paymentCCTotal').style.display='none';
			}
			deliveryPrice = 0;
		}
		if((document.getElementById('deliveryOption1').checked == true) || (document.getElementById('deliveryOption1').checked == 'checked'))
		{//grouped command
			//alert('commande groupee');
			if((document.getElementById('deliveryMode'+idDeliveryModeShop)==null) || ((document.getElementById('deliveryMode'+idDeliveryModeShop)!=null)&&(!document.getElementById('deliveryMode'+idDeliveryModeShop).checked == true)))//pas retrait en magasin
			{			
				calculatedDeliveryPrice+=deliveryPrice;
				if (totalBookPrice>0)
				{
					if(nrBooks>indexApplySupplementaryPrice)
					{
						var i=0;
						for(i=0;i<(nrBooks-indexApplySupplementaryPrice);i++)
						{
							calculatedDeliveryPrice += supplementaryPriceGrouped; 
						}
					}
				}
				if ((document.getElementById('deliveryMode'+idDeliveryModeEconomy)!=null)&&((document.getElementById('deliveryMode'+idDeliveryModeEconomy).checked == true)||(document.getElementById('deliveryMode'+idDeliveryModeEconomy).checked == 'checked')))
				{
					//alert('Mode economy');
					//alert(priceEconomy);
					if (totalBookPrice + totalGCPrice < freeLimitEconomy && (totalBookPrice > 0))
						calculatedDeliveryPrice += priceEconomy;
				}
				else if ((document.getElementById('deliveryMode'+idDeliveryModePriority)!=null)&&((document.getElementById('deliveryMode'+idDeliveryModePriority).checked == true)||(document.getElementById('deliveryMode'+idDeliveryModePriority).checked == 'checked')))
				{
					//alert('Mode priority');
					//alert(pricePriority);
					if (totalBookPrice + totalGCPrice < freeLimitPriority && (totalBookPrice > 0))
						calculatedDeliveryPrice += pricePriority
				}	

				//alert('totalGCPrice' + totalGCPrice);
				//alert('freeLimitGiftcard' + freeLimitGiftcard);
				if (totalGCPrice > 0 && totalGCPrice < freeLimitGiftcard){
					if ((document.getElementById('deliveryMode'+idDeliveryModePriority)!=null)&&((document.getElementById('deliveryMode'+idDeliveryModePriority).checked == true)||(document.getElementById('deliveryMode'+idDeliveryModePriority).checked == 'checked')))
					{
						if (totalBookPrice == 0)
						{
							calculatedDeliveryPrice += priceGiftcardPriority;
						}
					}
					else {
						if (totalBookPrice == 0)
						{
						calculatedDeliveryPrice += priceGiftcardEconomy;
						}
					}
					
					//alert('On est en dessous du seuil');
				}
				else if (totalGCPrice >= freeLimitGiftcard && totalBookPrice == 0){
					calculatedDeliveryPrice += TaxRecommande;
					//alert('On est en dessus du seuil');
					
				}
			}				
		}else
		{//ungrouped command
			calculatedDeliveryPrice+=deliveryPrice;
			//alert('commande non groupee');
			//alert('Nombre d items: ' + arrayUngrouped);
			for (var i = 0; i < arrayUngrouped.length; i++){
				if (arrayUngrouped[i] != 0){
					//alert('Voici un livre');
					if ((document.getElementById('deliveryMode'+idDeliveryModeEconomy)!=null)&&(document.getElementById('deliveryMode'+idDeliveryModeEconomy).checked == true))
					{
						if (arrayUngrouped[i] <= freeLimitEconomy){
							calculatedDeliveryPrice+= priceEconomy;
						}
					}
					else if ((document.getElementById('deliveryMode'+idDeliveryModePriority)!=null)&&(document.getElementById('deliveryMode'+idDeliveryModePriority).checked == true))
					{
						if (arrayUngrouped[i] <= freeLimitPriority){
							calculatedDeliveryPrice+= pricePriority;
						}
					}
				}
			}
			if (totalGCPrice > 0 && totalGCPrice < freeLimitGiftcard)
			{
				//alert('totalGCPrice: ' + totalGCPrice);
				if ((document.getElementById('deliveryMode'+idDeliveryModePriority)!=null)&&((document.getElementById('deliveryMode'+idDeliveryModePriority).checked == true)||(document.getElementById('deliveryMode'+idDeliveryModePriority).checked == 'checked')))
				{
					calculatedDeliveryPrice += priceGiftcardPriority;
				}
				else {
					calculatedDeliveryPrice += priceGiftcardEconomy;
				}
			}
			else if (totalGCPrice >= freeLimitGiftcard)
				calculatedDeliveryPrice += TaxRecommande;
		}
		//alert('fin');
		//alert('calculatedDeliveryPrice vaut: ' + calculatedDeliveryPrice);
		calculatedTotalPrice = calculatedDeliveryPrice + totalPriceWithoutDelivery;
		calculatedTotalPrice = Math.round(calculatedTotalPrice*100)/100;
		top.document.getElementById('tax').innerHTML= 'CHF : ' +calculatedDeliveryPrice.toFixed(2);
		top.document.getElementById('totalTax').value = calculatedDeliveryPrice.toFixed(2);
		top.document.getElementById('total2').innerHTML= 'CHF : ' +calculatedTotalPrice.toFixed(2);
		top.document.getElementById('totalAmount').value = calculatedTotalPrice.toFixed(2);
}

function actionGiftwrap(checkboxElement, rowindex){
	var elementId = 'giftwrapText'+rowindex.toString(10);
	if(checkboxElement.checked==false){
		document.getElementById(elementId).parentNode.style.display='none'; 
		document.getElementById(elementId).value=""; 
	}else 
		document.getElementById(elementId).parentNode.style.display='block';
}

function actionDedication(checkboxElement, rowindex){
	var elementId = 'dedicationText'+rowindex.toString(10);
	if(checkboxElement.checked==false){
		document.getElementById(elementId).parentNode.style.display='none'; 
		document.getElementById(elementId).value=""; 
	}else 
		document.getElementById(elementId).parentNode.style.display='block';
}

function paymentValidation(billURL, creditcardURL, giftcardURL){
	//pay with default payment mode (bill)
	// ajout avo 20060402:
	document.getElementById('pmtValidation').style.visibility = 'hidden';
	// fin ajout avo 
	if(document.getElementById('paymentMode4')!=null){
		document.getElementById('payWithBill').submit();
		return;
	}
	if((document.getElementById('paymentMode1')!=null)&&(document.getElementById('paymentMode1').checked==true)){
		document.getElementById('payWithBill').submit();
	}else if((document.getElementById('paymentMode2')!=null)&&(document.getElementById('paymentMode2').checked==true)){
		w = window.open(
			creditcardURL,
			'SaferpayTerminal',
			'scrollbars=1,resizable=0,toolbar=0,location=0,directories=0,status=1,menubar=0,width=580,height=400'
		);
		w.focus();	
	}else{
		document.getElementById('payWithGiftcard').submit();
	}
}

function verifyGiftcard(url){
	window.open(url+"?gccde="+document.getElementById('egccode').value,"_blank","toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=no, copyhistory=yes, width=400, height=400");
}