			/* Page des publications */
// Fonction d'ajout d'un article au panier
function addToCart (id) {
	var xhr = getXHR ();
	
	if (xhr.readyState != 0) {
		xhr.abort ();
	}
	xhr.onreadystatechange = function () {		
		if (xhr.readyState != 4) {
			XHRStatusDivDisplay ('XHRStatusDiv', xhr.responseText, 0);
		}
		else if (xhr.readyState == 4 && (xhr.status == 0 || xhr.status == 200)) {
			XHRStatusDivDisplay ('XHRStatusDiv', xhr.responseText, 0);
		}
		else {
			XHRStatusDivDisplay ('XHRStatusDiv', xhr.responseText, 0);
		}
	}
	
	xhr.open ('POST', 'scripts/xhr_addToCart.php', true);
	xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xhr.send ('id='+id);
}

// Fonction d'affichage du bloc de status XHR
function XHRStatusDivDisplay (divID, message, bool) {
	if (divID != '' && message != '' && bool == 0) {
		setTimeout ("XHRStatusDivDisplay (\'"+divID+"\', '', 1)", 1000);
		
		document.getElementById(divID).style.display = 'block';
		document.getElementById(divID).innerHTML = message;
	}
	else if (bool == 1) {
		document.getElementById(divID).style.display = 'none';
	}
}


			/* Page du panier */
// Fonction de vérification du champ de quantité
function checkQuantity (entree)
{
	reg = /[^0-9.,]/g;
	entree.value = entree.value.replace(reg,"");
}
// Fonction d'incrémentation/décrémentation des quantité
function incrementOrDecrement (id, increment) {
	var quantInput = document.getElementById('quantInput_'+id);
	// Incrémentation
	if (increment) {
		quantInput.value++;
	}
	// Décrémentation
	else {
		if (quantInput.value >= 1) {
			quantInput.value--;
		}
	}
}
// Fonction de calcul des totaux
function totalCosts (id, mainPrice) {
	var quantity = document.getElementById('quantInput_'+id).value;
	var total = quantity*mainPrice;
	document.getElementById('quantity_'+id).innerHTML = quantity;
	document.getElementById('total_'+id).innerHTML = total;
	
	var nombreEntrees = document.getElementById('nombreEntrees').value;
	var finalTotal = 0;
	for (var i = 0; i < nombreEntrees; i++) {
		var iPrix = document.getElementById('total_'+i).innerHTML*1;
		finalTotal += iPrix;
	}
	finalTotal = number_format (finalTotal, 2, ',', '.');
	document.getElementById('totalSpan').innerHTML = finalTotal;
}
// Fonction de sauvegarde du panier
function saveCart (id, q) {
	var xhr = getXHR ();
	
	if (xhr.readyState != 0) {
		xhr.abort ();
	}
	xhr.onreadystatechange = function () {		
		if (xhr.readyState != 4) {
			document.getElementById('savingStatus').style.color = 'orange';
			document.getElementById('savingStatus').innerHTML = 'sauvegarde en cours';
		}
		else if (xhr.readyState == 4 && (xhr.status == 0 || xhr.status == 200)) {
			document.getElementById('savingStatus').style.color = 'green';
			document.getElementById('savingStatus').innerHTML = 'sauvegardé';
		}
		else {
			document.getElementById('savingStatus').innerHTML = xhr.responseText;
			document.getElementById('savingStatus').style.color = 'red';
			document.getElementById('savingStatus').innerHTML = 'erreur - veuillez sauvegarder manuellement';
		}
	}
	
	xhr.open ('POST', 'scripts/xhr_saveCart.php', true);
	xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xhr.send ('id='+id+'&quant='+q);
}

// Fonctions des traitements du formulaire


			/* Pages d'administration */
			
// Fonction d'affichage du menu principal de l'administration
function displayAdminMenu (className, afficher) {
	if (className != '') {
		var td = document.getElementsByTagName('td');
		
		for (var i = 0; i < td.length; i++) {
			if (td[i].className == className) {
				if (afficher) { td[i].style.visibility = 'visible'; }
				else { td[i].style.visibility = 'hidden'; }
			}
		}
	}
}

// Fonction de confirmation de la suppression d'une publication
function delBook (titre, id) {
	if (confirm ('Voulez-vous vraiment supprimer la publication suivante ?\n'+titre)) {
		window.location = './log.php?action=delBook&id='+id;
	}
}

// Fonction de formatage des nombres
function number_format(number, decimals, dec_point, thousands_sep )
{   
 
    var n = number, c = isNaN(decimals = Math.abs(decimals)) ? 2 : decimals;
    var d = dec_point == undefined ? "." : dec_point;
    var t = thousands_sep == undefined ? "," : thousands_sep, s = n < 0 ? "-" : "";
    var i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0;
    
    return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
}

// Get XHR
function getXHR ()
{
	var xhr = null;
	
	if (window.XMLHttpRequest || window.ActiveXObject)
	{
		if (window.XMLHttpRequest)
		{
			xhr = new XMLHttpRequest ();
		}
		else
		{
			try
			{
				xhr = new ActiveXObject ("Msxml2.XMLHTTP");
			}
			catch (e)
			{
				xhr = new ActiveXObject ("Microsoft.XMLHTTP");
			}
		}
	}
	else
	{
		alert ('Votre navigateur ne supporte pas le XMLHttpRequest');
		return null;
	}
	
	return xhr;
}
