var cart_xhr = createXhrObject();
var status_xhr = createXhrObject();
var last_cart = "";
var quotes = new Array();
var quotes_text = "";
var cart_status = "";

function addtoCart(image) {
	cart_xhr.open('GET', 'cart/cart.add.php?image=' + image, true);
	cart_xhr.onreadystatechange = addedtoCart;
	cart_xhr.send(null);
	
	last_cart = image;
	
}

function addedtoCart() {
	if(cart_xhr.readyState == 4) {
		document.getElementById('cart_button').src = "gfx/buttons.php?txt=remove_cart&lang=fr";
		document.getElementById('cart_link').href = "javascript:removefromCart('" + last_cart + "');";
		document.getElementById('cart_status').innerHTML = cart_xhr.responseText;
		resetStatus();
		updateList();
	}
}

function removefromCart(image) {
	cart_xhr.open('GET', 'cart/cart.remove.php?image=' + image, true);
	cart_xhr.onreadystatechange = removedCart;
	cart_xhr.send(null);
	
	last_cart = image;
}

function removedCart() {
	if(cart_xhr.readyState == 4) {
		document.getElementById('cart_button').src = "gfx/buttons.php?txt=add_cart&lang=fr";
		document.getElementById('cart_link').href = "javascript:addtoCart('" + last_cart + "');";
		document.getElementById('cart_status').innerHTML = cart_xhr.responseText;
		resetStatus();
		updateList();
	}	
}

function updateList() {
	cart_xhr.open('GET', 'cart/cart.listing.php', true);
	cart_xhr.onreadystatechange = updateListDone;
	cart_xhr.send(null);
}

function updateListDone() {
	if(cart_xhr.readyState == 4) {
		document.getElementById('cart_items').innerHTML = cart_xhr.responseText;
	}
}

function resetStatus() {
	setTimeout(statusReset, 1500);
}

function statusReset() {
	getcartStatus();
}

function getcartStatus() {
	status_xhr.open('GET', 'cart/cart.status.php', true);
	status_xhr.onreadystatechange = cartStatus;
	status_xhr.send(null);
}

function cartStatus() {
	if(status_xhr.readyState == 4) {
		setcartStatus(status_xhr.responseText);
	}
}

function setcartStatus(s) {
	
	cart_status = s;
	
	var sta = document.getElementById('cart_status');	
	if(s == 0) {
		sta.innerHTML = "Votre panier est vide";
	}
	else if(s == 1) {
		sta.innerHTML = "<a href=\"index.php?a=cart\">Cliquez ici pour compléter vos exigences et soumettre votre panier.</a>";
	}
	else if(s == 2) {
		sta.innerHTML = "Vous avez soumis votre panier, notre équipe fixera vos prix au plus vite.";
	}
	else if(s == 3) {
		sta.innerHTML = "Notre équipe a fixé les prix, vous pouvez maintenant faire votre paiement !";
		highlightCart();
		
	}
	
	if(cart_status > 1) {
		if(page_type == "showimage") {
			document.getElementById('cart_button').style.display = "none";
		}
	}
}

function highlightCart() {
	
	blue = 0;
	red = 255;
	
	anim_highlight = setInterval('highlightCartAnim()', 40);	
	
}

function highlightCartAnim() {
	
	if(blue == 255) {
		blue = 0;
		red = 255;							
	}
	
	blue = blue + 5;
	document.getElementById('cart_status').style.backgroundColor = 'rgb(255, 255, ' + blue + ')';
	red = red - 5;
	document.getElementById('cart_status').style.color = 'rgb(' + red + ', 0, 0)';
	
}

