﻿function cartArticleAdd(Article) {
    value = document.getElementById('Quantity_' + Article).value;
    if (value == '') {
        value = '0';
    }
    value = parseInt(value) + 1;

    if (value <= 0) {
        value = 0;
    }
    if (value >= 999) {
        value = 999;
    }
    document.getElementById('Quantity_' + Article).value = value;
    frmsubmit('recalc');
}

function cartArticleSubstract(Article) {
    value = document.getElementById('Quantity_' + Article).value;
    if (value == '') {
        value = '0';
    }
    value = parseInt(value) - 1;
    
    if (value <= 0) {
        value = 0;
    }
    if (value >= 999) {
        value = 999;
    }
    document.getElementById('Quantity_' + Article).value = value;
    frmsubmit('recalc');
}

function updateArticlePrice(Article) {
    quantity = parseInt(setToPriceFormat(document.getElementById('Quantity_' + Article).value));
    document.getElementById('Quantity_' + Article).value = quantity;
    
    price = parseFloat(quantity) * parseFloat(document.getElementById('UnitPrice_' + Article).value.replace(",", "."));
    document.getElementById('Price_' + Article).innerHTML = setToPriceFormat(price).replace(".", ",") + " €";
    changeToUpdateButton();
}

function setToPriceFormat(amount) {
    var i = parseFloat(amount);
    if (isNaN(i)) { i = 0.00; }
    var minus = '';
    if (i < 0) { minus = '-'; }
    i = Math.abs(i);
    i = parseInt((i + .005) * 100);
    i = i / 100;
    s = new String(i);
    if (s.indexOf('.') < 0) { s += '.00'; }
    if (s.indexOf('.') == (s.length - 2)) { s += '0'; }
    s = minus + s;
    return s;
}

function cancelCartEdit() {
    document.EditCartForm.reset();
    window.location.reload();
}

function cartArticleRemove(Article) {
    document.getElementById('Quantity_' + Article).value = 0;
    document.getElementById('LineArticle_' + Article).className = "off";
    updateArticlePrice(Article);
}

function submitSaveCart() {
    if (document.getElementById('CartName').value != '') {
        messageEnPlus = "\n(Changez le nom du panier pour en l'enregistrer comme nouveau)";
    }
    CartName = prompt('Veuillez donner un nom à votre panier', document.getElementById('CartName').value);
    if (CartName) {
        document.getElementById('CartName').value = CartName;
        document.EditCartForm.action = "recordcart.aspx?None";
        document.EditCartForm.submit();
    }
    else
        alert('Vous devez donner un nom à votre panier pour le sauvegarder !');
}

function updateCart() {
    document.EditCartForm.action = "updatecart.aspx?None";
    document.EditCartForm.submit();
}

function incrementQuantity(Article) {
    value = document.getElementById('QuantityVariant' + Article).value;
    if (value == '') {
        value = '0';
    }
    value = parseInt(value) + 1;

    if (value <= 0) {
        value = 0;
    }
    if (value >= 999) {
        value = 999;
    }
    document.getElementById('QuantityVariant' + Article).value = value;
}

function decrementQuantity(Article) {
    value = document.getElementById('QuantityVariant' + Article).value;
    if (value == '') {
        value = '0';
    }
    value = parseInt(value) - 1;

    if (value <= 0) {
        value = 0;
    }
    if (value >= 999) {
        value = 999;
    }
    document.getElementById('QuantityVariant' + Article).value = value;
}

function AjaxCartJSON(ProductID) {
    if (ProductID != 0 && !isNaN(ProductID)) {
        var liste = new Array();
        y = 0;
        total = 0;
        for (i = 0; i < document.forms["AddToCartForm_" + ProductID].elements.length; i++) {
            if (document.forms["AddToCartForm_" + ProductID].elements[i].name.indexOf("QuantityVariant", 0) == 0) {
                liste[y] = new Array();
                liste[y][0] = document.forms["AddToCartForm_" + ProductID].elements[i].name;
                liste[y][1] = document.forms["AddToCartForm_" + ProductID].elements[i].value;
                total += parseInt(document.forms["AddToCartForm_" + ProductID].elements[i].value);
                y++;
            }
        }
        ParamsListe = "?ProductID=" + ProductID;

        if (total != 0 && !isNaN(total)) {
            for (i = 0; i < liste.length; i++) {
                ParamsListe += "&" + liste[i][0] + "=" + liste[i][1];
            }


            document.getElementById('AddToCartButton_' + ProductID).value = "Veuillez patienter...";
            document.getElementById('AddToCartButton_' + ProductID).disabled = true;
            document.getElementById('AddToCartButton_' + ProductID).style.cursor = "default";

            jQuery.getJSON("AjaxCart.aspx" + ParamsListe, function(data) {

                textINNER = "<table width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">";

                jQuery.each(data.panier.ShoppingCartRecords, function(i, ShoppingCartRecord) {

                    Total = setToPriceFormat(parseFloat(ShoppingCartRecord.ProductPrice) * parseFloat(ShoppingCartRecord.Quantity));
                    textINNER +=

									"<tr id=\"LineArticle_" + ShoppingCartRecord.ID + "\"> " +
									"	<td> " +
									"		<a href=\"" + ShoppingCartRecord.LinkURL + "\" style=\"color:" + ShoppingCartRecord.Color + "\">" + ShoppingCartRecord.Name + "</a> " +
									"		<br> " +
									"	</td> " +
									"	<td width=\"12\"> " +
									"		<a onClick=\"cartArticleSubstract('" + ShoppingCartRecord.ID + "')\" style=\"cursor:pointer;\"> " +
									"			<img src=\"skins/Skin_2/img/btn_moins.jpg\" alt=\"moins\" width=\"11\" height=\"11\"> " +
									"		</a> " +
									"	</td> " +
									"	<td width=\"36\" class=\"center\"> " +
									"		<input type=\"text\" maxlength=\"3\" size=\"1\" id=\"Quantity_" + ShoppingCartRecord.ID + "\" name=\"Quantity_" + ShoppingCartRecord.ID + "\" value=\"" + ShoppingCartRecord.Quantity + "\" onChange=\"updateArticlePrice('" + ShoppingCartRecord.ID + "')\"> " +
									"		<input type=\"hidden\" id=\"UnitPrice_" + ShoppingCartRecord.ID + "\" name=\"UnitPrice_" + ShoppingCartRecord.ID + "\" value=\"" + ShoppingCartRecord.ProductPrice + "\"> " +
									"	</td> " +
									"	<td width=\"12\"> " +
									"		<a onClick=\"cartArticleAdd('" + ShoppingCartRecord.ID + "')\" style=\"cursor:pointer;\"> " +
									"			<img src=\"skins/Skin_2/img/btn_plus.jpg\" alt=\"plus\" width=\"11\" height=\"11\"> " +
									"		</a> " +
									"	</td> " +
									"	<td class=\"red\"> " +
									"		<span id=\"Price_" + ShoppingCartRecord.ID + "\">" + Total + "€</span> " +
									"	</td> " +
									"	<td> " +
									"		<a onClick=\"cartArticleRemove('" + ShoppingCartRecord.ID + "');\" style=\"cursor:pointer;\"> " +
									"			<img src=\"skins/Skin_2/img/btn_croix.jpg\" alt=\"supr\" width=\"11\" height=\"10\"> " +
									"		</a> " +
									"	</td> " +
									"</tr> ";

                });

                textINNER += "</table>";

                document.getElementById('ShoppingCartItems').innerHTML = textINNER;

                document.getElementById('QuantitePanier').innerHTML = data.panier.numitems;

                document.getElementById('TotalMiniCart').innerHTML = data.panier.subtotal;
                
                ongletChange("ongletpanier");

                document.forms["AddToCartForm_" + ProductID].reset();

                var scrollbar = new Control.ScrollBar('minicart_scrollbar_content', 'minicart_scrollbar_track');
                scrollbar.recalculateLayout();
                scrollbar.scrollTo('bottom');

                document.getElementById('AddToCartButton_' + ProductID).value = "AJOUTER AU PANIER +";
                document.getElementById('AddToCartButton_' + ProductID).disabled = false;
                document.getElementById('AddToCartButton_' + ProductID).style.cursor = "pointer";

            });
        }
    }
}

function ongletChange(Id) {
    if (Id == "ongletcompte") {
        document.getElementById("divongletcompte").className = 'on';
        document.getElementById("divongletpanier").className = 'off';
    }
    if (Id == "ongletpanier") {
        document.getElementById("divongletcompte").className = 'off';
        document.getElementById("divongletpanier").className = 'on';
    }
}
