
var items = [
	// Plein Soleil
	{ price: 5.65, maxQty: 2, fee: magazineFee, qtyId: "q1", amountId: "t1", errId: "err1" },

	// Clinique Mayo La dépression
	{ price: 26.20, memberPrice: 23.57, maxQty: 100, fee: bookFee, qtyId: "q2", amountId: "t2", errId: "err2" },

	// Clinique Mayo Le diabète
	{ price: 26.20, memberPrice: 23.57, maxQty: 100, fee: bookFee, qtyId: "q4", amountId: "t4", errId: "err4" },

	// Clinique Mayo Le poids santé
	{ price: 26.20, memberPrice: 23.57, maxQty: 100, fee: bookFee, qtyId: "q5", amountId: "t5", errId: "err5" },

	// Connaître son diabète...pour mieux vivre
	{ price: 28.31, memberPrice: 25.46, maxQty: 100, fee: bookFee, qtyId: "q6", amountId: "t6", errId: "err6" },

	// Le dessert se fait léger
	{ price: 23.05, memberPrice: 20.90, maxQty: 100, fee: bookFee, qtyId: "q8", amountId: "t8", errId: "err8" },

	// Le diabète agissez avant lui!
	{ price: 15.70, memberPrice: 14.13, maxQty: 100, fee: bookFee, qtyId: "q9", amountId: "t9", errId: "err9" },

	// Le diabète chez l'enfant et l'adolescent
	{ price: 19.90, memberPrice: 18.85, maxQty: 100, fee: bookFee, qtyId: "q10", amountId: "t10", errId: "err10" },
	
	// Diabétiques, les sucres et pourquoi ! 
	//{ price: 13.60, memberPrice: 11.50, maxQty: 100, fee: bookFee, qtyId: "q18", amountId: "t18", errId: "err18" },

	// 101 trucs et conseils de nutrition pour les diabétiques
	{ price: 15.70, memberPrice: 14.13, maxQty: 100, fee: bookFee, qtyId: "q11", amountId: "t11", errId: "err11" },

	// Le diabète de type 1 et ses défis alimentaires quotidiens
	{ price: 26.25, memberPrice: 23.63, maxQty: 100, fee: bookFee, qtyId: "q12", amountId: "t12", errId: "err12" },
	
	// La bible de la cuisine santé
	{ price: 31.45, memberPrice: 28.30, maxQty: 100, fee: bookFee, qtyId: "q19", amountId: "t19", errId: "err19" },
	
	// Vivre avec une personne diabétique 
	{ price: 26.20, memberPrice: 23.57, maxQty: 100, fee: bookFee, qtyId: "q20", amountId: "t20", errId: "err20" },
	
	// Vivre avec une personne diabétique 
	{ price: 12.00, memberPrice: 10.00, maxQty: 100, fee: bookFee, qtyId: "q21", amountId: "t21", errId: "err21" },

	// La personne diabétique : l'auto-soin des pieds (12 min.)
	{ price: 28.50, memberPrice: 25.85, maxQty: 4, fee: dvdFee, qtyId: "q13", amountId: "t13", errId: "err13" },
	
	// Accepter son diabète : suivez le guide 
	{ price: [[ 1, 19, 3.00 ]], maxQty: 19, fee: guideFee, qtyId: "q17", amountId: "t17", errId: "err17" },

	// La roulette de l'activité physique
	{ price: [[ 1, 10, 3.00 ], [11, 50, 2.80 ], [ 51, 100, 2.00 ]], maxQty: 100, fee: rouletteFee, qtyId: "q14", amountId: "t14", errId: "err14" },

	// Le Guide de voyage pour la personne diabétique
	{ price: [[ 1, 19, 3.00 ]], maxQty: 19, fee: guideFee, qtyId: "q15", amountId: "t15", errId: "err15" },

	// Mémo-Diabète Plus
	{ price: 20.26, memberPrice: 16.87, maxQty: 100, fee: bookFee, qtyId: "q16", amountId: "t16", errId: "err16" }
	
	
	];


function updateTotal()
{
	var	member,item,text,quantity,amount,total,fee,i;

	total=0;
	fee=0;

	processMagazine();

	member=isMember();

	for (i in items) {
		item=items[i];

		text=getQuantity(item);
		if (text && isNumber(text)) {	// text is a number
			quantity=parseNumber(text);
			if (quantity>0 && quantity<=item.maxQty) {	// check against boundaries
				amount=getAmount(item,quantity,member);

				setQuantity(item,quantity);
				setAmount(item,amount);

				total+=amount;
				fee+=item.fee(quantity);
				}
			else if (quantity==0) {	// if zero, no need to display an error
				setQuantity(item,null);
				setAmount(item,null);
				}
			else {
				displayError(item);
				setAmount(item,null);
				}
			}
		else if (text) {	// text is not a number
			displayError(item);
			setAmount(item,null);
			}
		else {	// text is empty or composed of spaces
			setQuantity(item,null);
			setAmount(item,null);
			}
		}

	setAmount("stotal",total);
	setAmount("postaux",fee);
	setAmount("total",total+fee);

	// special case for "Plein Soleil magazine":
	//   check that a title is selected if a valid quantity is given...
	//   if not, add an error message!
	processMagazine();
}

function processMagazine()
{
	var	elem,text,quantity;

	text=getQuantity(items[0]);
	if (text && isNumber(text)) {
		quantity=parseNumber(text);
		if (quantity>0 && quantity<=items[0].maxQty) {
			if (indexOfMagazine()<1) {
				elem=document.getElementById("err1");
				elem.style.display="block";
				elem.innerHTML="Vous devez sélectionner un numéro de revue.";
				}
			}
		}
}

function indexOfMagazine()
{
	var	elem,i;

	elem=document.getElementById("pleinSoleil");
	if (!elem || !elem.options) {
		return -1;
		}
	for (i=0; i<elem.options.length; i++) {
		if (elem.options[i].selected) {
			return i;
			}
		}
	return -1;
}

function isMember()
{
	return document.getElementById("membre").checked;
}

function getQuantity( item )
{
	var	text;

	text=document.getElementById(item.qtyId).value;
	return (text ? text.replace(/^\s*(.*\S|)\s*$/,"$1") : null);	// trim the text
}

function setQuantity( item, value )
{
	document.getElementById(item.qtyId).value=(value ? value.toString() : "");
	document.getElementById(item.errId).style.display="none";
}

function getAmount( item, quantity, member )
{
	var	amount;

	amount=((member && item.memberPrice) ? item.memberPrice : item.price);
	if ((typeof amount)=="number") {
		return quantity*amount;
		}
	for (i in amount) {
		range=amount[i];
		if (quantity>=range[0] && quantity<=range[1]) {
			return quantity*range[2];
			}
		}
	return 0;
}

function setAmount( item, value )
{
	var	id;

	id=((typeof item)!="string" ? item.amountId : item);
	document.getElementById(id).value=(value ? formatAmount(value)+" $" : "");
}

function formatAmount( value )
{
	var	text;

	// rounding...
	value=parseInt(value*100+0.5);

	text=value.toString();
	return text.substring(0,text.length-2)+"."+text.substring(text.length-2);
}

function isNumber( text )
{
	return /^[+-]?[0-9]+$/.test(text);
}

function parseNumber( text )
{
	// do not forget the radix, otherwise "09" is parsed as 0...
	return parseInt(text,10);
}

function displayError( item )
{
	var	elem;

	elem=document.getElementById(item.errId);
	elem.style.display="block";
	elem.innerHTML="La quantité doit être comprise entre 1 et "+item.maxQty+".";
}

function bookFee( quantity )
{
	//return 7.55+(quantity-1)*1.15;
	return (quantity<1 ? 7.55 : 7.55+(quantity-1)*1.15);
}

function dvdFee( quantity )
{
	return (quantity<3 ? 2.15 : 3);
}

function guideFee( quantity )
{
	return (quantity<3 ? 1.80 : (quantity<6 ? 2.80 : (quantity<12 ? 6.80 : 7.10)));
}

function magazineFee( quantity )
{
	return 2.45;
}

function rouletteFee( quantity )
{
	return (quantity<11 ? 2.00 : (quantity<51 ? 3.00 : 6.00));
}


