function searchForm(){
    $("fieldset#search form").submit();
}

function addToCart(action, productname, quantity){
    /*
        fonction pour ajouter un item dans le cart
        
        ** params **
        action        : le path du formulaire
        
        productname   : le nom du produit à ajouter
        quantity      : la quantite du produit courant
        
        ** si les 2 derniers parametres sont absents,
        ** on s'imagine qu'on se trouve dans la page du produit et on tente
        ** d'obtenir ses infos dans les champs appropriés
    */

    
    if (!productname) {
        productname = $("form input#productname").val();
    }
    
    if (!quantity) {
        quantity = $("form input#quantity").val();
        if (parseInt(quantity) == 0) {
            quantity = 1;
        }
    }
    
    var d = {
        "productname" : productname
        , "quantity" : quantity
    }
    
    $.ajax({
        url: action
        , type : "POST"
        , data : $.param(d)
        , success: function(data, textStatus){
            window.location.reload();
        }
        , error: function(XMLHttpRequest, textStatus, errorThrown){
            if (textStatus == "error") {
                alert("An error occured while adding the item to the cart.");
            }
        }
    });
}

function modifierTotal() {
    var itemPrice = $("dl#info dd span#price").html();
    var total = 0;
    if (itemPrice) {
        itemPrice = itemPrice.replace("$", "");
        itemPrice = itemPrice.replace(",", "");

        itemPrice = parseFloat(itemPrice);
    }
    
    var quantity = $("input#quantity").val();
    
    if (quantity) {
        var anum=/(^\d+$)|(^\d+\.\d+$)/;
        if ( anum.test(quantity) ){
            quantity = parseInt(quantity);
            total = quantity * itemPrice;
            $("dl#info dd span#total").html( total.toFixed(2) );
        }else{
            $("dl#info dd span#total").html( itemPrice.toFixed(2) );
        }
    }
}

function modifierImage(val){
    
    // parcourir chacunes des images à la recherche de celle qui correspond
    // à la sélection
    if (val) {
        val = val.toLowerCase();
    }
    $("form #photo .inner a").each(
        function(){
            if ($(this).attr("title")) {
                var title = $(this).attr("title")
                title = title.toLowerCase();
                if ( title.indexOf(val) == 0 ) {
                    $(this).removeClass("cache");
                }else{
                    $(this).addClass("cache");
                }
            }
        }
    );
    // si aucune image ne correspond, afficher la 1e
    if ($("form #photo .inner a:visible").length == 0 ) {
        $("form #photo .inner a:first").removeClass("cache");
    }
}

function printit(){
    window.print();
}

function sendToFriend(productname){
    alert("Imprimer " + productname);
}
