function createRequestObject() {
    var ro;
    var browser = navigator.appName;
    if(browser == "Microsoft Internet Explorer"){
        ro = new ActiveXObject("Microsoft.XMLHTTP");
    }else{
        ro = new XMLHttpRequest();
    }
    return ro;
}

var http = createRequestObject();

function updatePort(validate)
{
	var pays = document.cart.pays[document.cart.pays.selectedIndex].value;
	
	$.ajax ({
		type: "GET",
		url: "ajax.php",
		data: "action=changePays&pays="+pays,
		async: false
	});
	
	var url = "panier.php";
	
	if (validate == 1)
	{
		url = url + "?validate=1";
	}
	
	window.location = url;
}

function checkAssurance()
{
	var ancien_total = parseFloat(document.getElementById('total').innerHTML);
	
	if ($('#assurance').attr("checked")) {
		var nouveau_total = ancien_total + 3;
		document.getElementById('total').innerHTML = nouveau_total.toFixed(2);
		document.getElementById('visuel_assurance').innerHTML = '3.00';
		eval("document.cart.montant_assurance.value = 3.00");
		
		$.ajax ({
			type: "GET",
			url: "ajax.php",
			data: "action=assurance&value="+1,
			async: false
		});
	} else
	{
		var nouveau_total = ancien_total - 3;
		document.getElementById('total').innerHTML = nouveau_total.toFixed(2);
		document.getElementById('visuel_assurance').innerHTML = '0.00';
		eval("document.cart.montant_assurance.value = 0.00");
		
		$.ajax ({
			type: "GET",
			url: "ajax.php",
			data: "action=assurance&value="+0,
			async: false
		});
	}
}

function updatePrice(qte, somme, prix, id, zone)
{
	var ancienne_somme = parseFloat(document.getElementById( somme ).innerHTML);
	var ancien_total = parseFloat(document.getElementById('total').innerHTML);
	var ancien_port = parseFloat(document.getElementById('fraisdeport').innerHTML);
	var quantite = eval("document.cart."+qte+".value");
	var prix = parseFloat(eval("document.cart."+prix+".value"));
	var montantpromo = parseFloat(eval("document.cart.montantpromo.value"));
	var tauxpromo = parseFloat(eval("document.cart.tauxpromo.value"));
	var assurance = parseFloat(eval("document.cart.montant_assurance.value"));
	
	if (quantite == '')
	{
		quantite = 1;
	}

	quantite = parseInt(quantite);
	var nouvelle_somme = (prix * quantite.toFixed(2));
	var nouveau_total = (ancien_total - ancienne_somme - ancien_port - assurance) + nouvelle_somme + montantpromo;
	
	if (tauxpromo != 0)
	{
		var newmontantpromo = nouveau_total*(tauxpromo/100);
		nouveau_total = nouveau_total - newmontantpromo.toFixed(2);
		document.getElementById('promo').innerHTML = '- '+newmontantpromo.toFixed(2);
		eval("document.cart.montantpromo.value = "+newmontantpromo.toFixed(2));
	}
	
	if (nouveau_total > 200 && zone == 'por_prix_zone_d')
	{
		alert('pour toute commande d\'un montant supérieur à 200 euros, nous contacter');
		quantite = 1;
		nouvelle_somme = (prix * quantite.toFixed(2));
		eval("document.cart."+qte+".value = 1");
		var nouveau_total = (ancien_total - ancienne_somme - ancien_port - assurance) + nouvelle_somme + montantpromo;
		
		if (tauxpromo != 0)
		{
			var newmontantpromo = nouveau_total*(tauxpromo/100);
			nouveau_total = nouveau_total - newmontantpromo.toFixed(2);
			document.getElementById('promo').innerHTML = '- '+newmontantpromo.toFixed(2);
			eval("document.cart.montantpromo.value = "+newmontantpromo.toFixed(2));
		}
	} else if (nouveau_total > 300)
	{
		alert('pour toute commande d\'un montant supérieur à 300 euros, nous contacter');
		quantite = 1;
		nouvelle_somme = (prix * quantite.toFixed(2));
		eval("document.cart."+qte+".value = 1");
		var nouveau_total = (ancien_total - ancienne_somme - ancien_port - assurance) + nouvelle_somme + montantpromo;
		
		if (tauxpromo != 0)
		{
			var newmontantpromo = nouveau_total*(tauxpromo/100);
			nouveau_total = nouveau_total - newmontantpromo.toFixed(2);
			document.getElementById('promo').innerHTML = '- '+newmontantpromo.toFixed(2);
			eval("document.cart.montantpromo.value = "+newmontantpromo.toFixed(2));
		}
	}
	
	var nouveau_port = $.ajax ({
		type: "GET",
		url: "ajax.php",
		data: "action=getFraisPort&prix="+nouveau_total,
		async: false
	}).responseText;
	
	nouveau_port = parseFloat(nouveau_port);
	
	nouveau_total = nouveau_total + nouveau_port + assurance;

	document.getElementById('fraisdeport').innerHTML = nouveau_port.toFixed(2);
	document.getElementById( somme ).innerHTML = nouvelle_somme.toFixed(2);
	document.getElementById('total').innerHTML = nouveau_total.toFixed(2);

	http.open('get', 'ajax.php?action=updateQte&id='+id+'&qte='+quantite);
	http.send(null);
}

function checkForm()
{
	if (document.coord.nom.value.length == 0)
	{
		alert("Précisez votre nom");
		return false;
	} else if (document.coord.prenom.value.length == 0)
	{
		alert("Précisez votre prénom");
		return false;
	} else if (document.coord.adresse.value.length == 0)
	{
		alert("Précisez votre adresse");
		return false;
	} else if (document.coord.cp.value.length == 0)
	{
		alert("Précisez votre code postal");
		return false;
	} else if (document.coord.ville.value.length == 0)
	{
		alert("Précisez votre ville");
		return false;
	} else if (document.coord.mail.value.length == 0)
	{
		alert("Précisez votre Mail");
		return false;
	} else
	{	
		return true;
	}
}