var ACTIONS_CART_PATH = "modules/cart/actions.php";
	function doFinalize() {
		if(document.getElementById('checkCGV').checked) {
			document.getElementById('mycart').action = ACTIONS_CART_PATH+'?action=finalize';
			document.getElementById('mycart').target = 'frmRemote';
			document.getElementById('mycart').submit();
		} else {
			jAlert("Vous devez accepter les conditions générales de vente!", "Message");
		}
	}

function validateDoFinalize(){
  	var txtErr = "";
    if (jQuery("#livLastname").val() == ""){
  		txtErr += "Vous devez saisir le nom de livraison !" + "<br />";
    }
      if (jQuery("#livFirstname").val() == ""){
  		txtErr += "Vous devez saisir le prenom de livraison !" + "<br />";
    }
      if (jQuery("#livAddress").val() == ""){
  		txtErr += "Vous devez saisir le adresse de livraison !" + "<br />";
    }
      if (jQuery("#livPostalcode").val() == ""){
  		txtErr += "Vous devez saisir le code postal de livraison !" + "<br />";
    }
      if (jQuery("#livCity").val() == ""){
  		txtErr += "Vous devez saisir le ville de livraison !" + "<br />";
    }
      if (jQuery("#livPhone").val() == ""){
  		txtErr += "Vous devez saisir le téléphone de livraison !" + "<br />";
    }
  	if (txtErr != ""){
     	 jError(txtErr, "Erreurs");
  	}else{
  	doFinalize();
    }
}


	function doModify() {
		document.getElementById('mycart').target='frmRemote';
		document.getElementById('mycart').action=ACTIONS_CART_PATH+'?action=modify';
		document.getElementById('mycart').submit();
	}
    function doModifyRight() {
		document.getElementById('mycartRight').target='frmRemote';
		document.getElementById('mycartRight').action=ACTIONS_CART_PATH+'?action=modify';
		document.getElementById('mycartRight').submit();
	}
	function doCheck(total) {

	    		document.getElementById('mycart').action='panier.php?action=check';
    		document.getElementById('mycart').submit();

	}
	function doRemove(id) {
		arrQty = document.getElementsByName('qty[]');
		arrQty[id].value='0';
		doModify();
	}
    function doRemoveRight(id) {
		arrQtyRight = document.getElementsByName('qtyRight[]');
		arrQtyRight[id].value='0';
		doModifyRight();
	}
	function getCartTotal() {
		try{
			nDepartament = document.getElementById('txtCommandAddressDep').value;
			//nCartQty = document.getElementById('nCartQty').value;
			//nCodePromo  = document.getElementById('codpromo').value;
			ordreTotal = document.getElementById('nCartTotal').value;

			var nTotal = document.getElementById('nCartTotal').value;
			document.getElementById('td_carttotal').innerHTML  = parseFloat(nTotal) +  parseFloat(nDepartament);
			document.getElementById('td_fdp').innerHTML  = nDepartament+"&euro;";
			//AjaxExecute("getCartTotal",escape("'"+nDepartament+"', '"+nCartQty+"','"+nCodePromo+"','"+ordreTotal+"'"), "setCatrTotal");
		} catch(e){}
	}

	function saveBasket() {
		AjaxExecutePost("saveBasket", escape(""), "afterSaveBasket");
	}
	function afterSaveBasket(strRes) {
		document.location = "mon-compte.php";
	}
	function loadAdresse(n){
		AjaxExecutePost("loadClientAddress", escape(n), "afterload");
	}
	function setCatrTotal(result) {
		try{

    		arrResult = result.split("|");
    		document.getElementById('td_cartfdp').innerHTML = arrResult[0];

//    		document.getElementById('td_carttotal').innerHTML = number_format(arrResult[1],2);
        	document.getElementById('td_carttotal').innerHTML = arrResult[1];
        	document.getElementById('cartTotal').innerHTML = arrResult[1];
		} catch(e){}
	}
	function gedFDP() {
		n = jQuery("#livCountry_id").val();
        if(n != undefined)
        AjaxExecutePost("getCartFdpTotal",escape(n),"setCatrTotal");
	}
    jQuery(document).ready(function() {
        gedFDP();
    });
    function valideCodePromo(){
        n = jQuery("#txtCodePromo").val();
        AjaxExecutePost("getCodePromo",escape("'"+n+"'"),"setCodePromo");
    }
    bValidate = false;
    function setCodePromo(result) {
        try{
	        arrResult = result.split("|");
            n = $("#txtCodePromo").val();
            if (result>0){
                $("#tr_remise").show();
                $("#td_cartremise").html(result);
                $("#divOrderTotal").html(number_format(parseFloat($("#divOrderTotal").html()) - result , 2));
            } else if (bValidate) {
                promptMsg("Codes promotionnels '"+n+"' n'est pas valide<br /><br /><br /><center> <input class=\"button\" type=\"button\" onmouseout=\"this.className=\'button\'\" onmouseover=\"this.className=\'buttonover\'\" onclick=\"$(\'#dialogX\').dialog(\'close\');\" value=\"Fermer\" title=\"Fermer\" style=\"width:140px\" /></center>");
                bValidate = false;
            }
		} catch(e){
	    }
    }
	function afterload(strRes) {

	    if (strRes !="[]"){
		    eval ("objRES="+strRes+";");
			jQuery("#livCompany").val(objRES.clientaddress_company);
			jQuery("#livCivility").val(objRES.clientaddress_civility);
			jQuery("#livLastname").val(objRES.clientaddress_lastname);
			jQuery("#livFirstname").val(objRES.clientaddress_firstname);
			jQuery("#livVoie").val(objRES.clientaddress_voie);
			jQuery("#livAddress").val(objRES.clientaddress_address);
			jQuery("#livPostalcode").val(objRES.clientaddress_postalcode);
			jQuery("#livCity").val(objRES.clientaddress_city);
			jQuery("#livDepartment_id").val(objRES.clientaddress_department);
			jQuery("#livPhone").val(objRES.clientaddress_phone);
            try {$("select").msDropDown();
           	} catch(e) {
			//alert("Error: "+e.message);
		}
		}
		gedFDP();
	}
	function refreshCart() {
		var arrQuant = jQuery("input[name='qty[]']");
		var bOk = true;
		for(var i = 0; i< arrQuant.length;i++){
			if(isNumber(arrQuant[i].value) == false){
				bOk = false;
				break;
			}
		}
		if (bOk == false){
			jError("Le quantit&eacute; doit &ecirc;tre un numero positif !", "Erreur");
		} else {
			doModify();
		}
	}
	function validateCoupon(code){
		if(code == ""){
			jError("Vous devez saisir le code pour le bon de r&eacute;duction !","Erreur");
			return false;
		}
		jQuery.getJSON("request.php",{method:2,code:code},				
				function(json){
				//alert(json);
				//return;
				if(typeof(json) != "undefined" && typeof(json.error) != "undefined" && typeof(json.message) != "undefined"){
					if(json.error){
						jError(json.message,"Erreur");
					}else{
						jAlert(json.message,"Message",function(){
								if(typeof(json.cart) != "undefined" && typeof(json.cart.products) != "undefined" && typeof(json.cart.subtotalnew) != "undefined" && typeof(json.cart.subtotalold) != "undefined" && json.cart.products.length > 0){
									$.each(json.cart.products,function(key,value){
										var tdPrice = $("#produit_row_"+value.index).children("td:eq(2)");
										var tdTotalPrice = $("#produit_row_"+value.index).children("td:eq(3)");
										tdPrice.html("<span class='cart-newprice'>"+value.pricenew+"</span>/<span class='cart-oldprice'>"+value.priceold+"</span>&euro;");
										tdTotalPrice.html("<span class='cart-newprice'>"+value.totalpricenew+"</span>/<span class='cart-oldprice'>"+value.totalpriceold+"</span>&euro;");
									});									
								$("#td_carttotal").html("<span class='cart-newprice'>"+json.cart.subtotalnew+"</span>/<span class='cart-oldprice'>"+json.cart.subtotalold+"</span>&euro;");
								}
						});						
					}
					
				}
								
		});
	}
	function useCoupon(code){
		if(code == ""){
			jError("Vous devez saisir le code pour le bon de r&eacute;duction!","Erreur");
			return false;
		}
		jQuery.getJSON("request.php",{method:3,code:code},				
			function(json){
				if(typeof(json) != "undefined" && typeof(json.error) != "undefined"){
					if(json.error){
						jError(json.message,"Erreur");
					}else{
						document.location = document.location;						
					}
				}								
		});
	}
	function removeCoupon(){
		jQuery.post("request.php",{method:4},function(json){document.location = document.location;});								
	}
