function getpage(name,val,urlpage)
{
    $.ajax({
        type: "POST",
        data: 'val='+val,
        url: urlpage,
        success: function(r){	
            $("#"+name).html(r);
        }
    });
}

function zoomImage(img_url,width,height){
    window.open(img_url,"zoom","menubar=no, status=no, scrollbars=no, menubar=no, width="+width+", height="+height+"");
}

function render(id){
    div=document.getElementById(id);
    if(div.style.display=="block" || div.style.display==""){
        div.style.display="none";
    }
    else{
        div.style.display="block";
    }
}

function viewLogin(){
    if(document.getElementById('divCompte')){
        //divCompte=document.getElementById('divCompte');
        //divLink=document.getElementById('divLink');
        if(document.getElementById('divCompte').style.display=="block" || document.getElementById('divCompte').style.display==""){
            document.getElementById('divCompte').style.display="none";
            newText=document.getElementById('divLink').innerHTML.replace('flechClose','flechOpen');
        }
        else{
            document.getElementById('divCompte').style.display="block";
            newText=document.getElementById('divLink').innerHTML.replace('flechOpen','flechClose');
        }
        
        document.getElementById('divLink').innerHTML=newText;
    }
}

// Création d'un élément html de type div à la volée
function createElt(id, title){
        var popup="";
	if(!document.getElementById(id))
	{
		popup = document.createElement("div");
	
		popup.setAttribute('id', id);
		popup.setAttribute('title', title);
	
		document.body.appendChild(popup);
	}
        else{
           popup = document.getElementById(id);
           popup.setAttribute('title', title);
        }
}

function sendFriend(prd_id){
    $.ajax({
		type: "POST",
		data: {
			prd_id: prd_id,
			action: 'sendFriend'
		},
		url: "/includes/request.inc.php",
		contentType : "application/x-www-form-urlencoded; charset=utf-8", 
		success: function(r){
                    showAlertBox('Envoyer à un ami',r);
		}
	 });
}

function sendMailFriend(prd_id){
    mail=document.getElementById('email-friend').value;
    name=document.getElementById('name-friend').value;
    $.ajax({
		type: "POST",
		data: {
			mail: mail,
			name: name,
			prd_id: prd_id,
			action: 'sendMailFriend'
		},
		url: "/includes/request.inc.php",
		contentType : "application/x-www-form-urlencoded; charset=utf-8", 
		success: function(r){
           $('#popupAlert').dialog('close');
		}
    });
}

function showAlertBox(title, cnt, type, width){
    //alert(cnt);
	var idDiv = "popupAlert";
        if(width==""){width=350;}
    $("#"+idDiv).dialog("destroy");
	createElt(idDiv, title);
	
    if(type=="panier"){
        $("#"+idDiv).dialog({
            bgiframe: true,
            autoOpen: false,
            minHeight: 150,
            width: width,
            modal: true,
            resizable: false,
            show: 'blind',
            buttons: {
                'Valider mes achats': function() {
                    window.location.href='/panier.html';
                    $(this).dialog('close');
				},
				'Continuer mes achats': function() {
                    refreshCart();
					$(this).dialog('close');
				}
				
			}
        });
    }
    else{
        $("#"+idDiv).dialog({
            bgiframe: true,
            autoOpen: false,
            minHeight: 150,
            width: width,
            modal: true,
            resizable: false,
            show: 'blind'
        });
    }

	$("#"+idDiv).dialog('open');	
	
	$("#"+idDiv).html( cnt );
}


function setCartQte(id, action, nb, popup)
{
	$.ajax({
		type: "POST",
		data: {
			id: id,
			action: action,
			nb: nb
		},
		url: "/includes/request.inc.php",
		contentType : "application/x-www-form-urlencoded; charset=utf-8", 
		success: function(r){

            refreshCart();
            if(popup=="on"){
                if(r==""){
                    showAlertBox('Limite stock','Vous avez atteint la limite du stock.');
                }
                else{
                    showAlertBox('Confirmation',r,'panier');
                }
            }
            else{
                if(action=="delete"){
                    document.getElementById('tr_'+id).style.display="none";
                    refreshPrice();
                }
                else  if(r==""){
                    showAlertBox('Limite stock','Vous avez atteint la limite du stock.');
                    document.getElementById('input_quantity_'+id).value = document.getElementById('input_quantity_'+id).value -1;
                }
                else{
                    refreshPrice(id);
                }
                
            }
		}
	 });
}

function addPromo(title)
{
	$.ajax({
		type: "POST",
		data: {
			title: title,
			action: 'addPromo'
		},
		url: "/includes/request.inc.php",
		contentType : "application/x-www-form-urlencoded; charset=utf-8", 
		success: function(r){
            if(r==1){
                refreshPrice();
            }
            else{
                alert("Le code promo que vous venez de rentrer n'existe pas ou n'est plus valide.");
            }
		}
	 });
}

function refreshCart()
{
    idDiv="cartCount";
	$.ajax({
		type: "POST",
		data: {
			action: 'refreshCart'
		},
		url: "/includes/request.inc.php",
		contentType : "application/x-www-form-urlencoded; charset=utf-8", 
		success: function(r){
                    if(r=='erreur'){
                        window.location.replace('/panier.html');
                    }
                    else{
                        $("#"+idDiv).html( r );
                    }
		}
	 });
}

function displayPayment( id )
{
    $('div[id*="paiement_"]').each( function( )
    {
        if ( this.id != 'paiement_'+id ){
            $( '#'+this.id ).slideUp();
        }else{
            $( '#'+this.id ).slideDown();
        }
    });     
}

function refreshPrice(id,type)
{
    if(id){
        $.ajax({
            type: "POST",
            data: {
                id : id,
                type : type,
                action: 'refreshPrice'
            },
            url: "/includes/request.inc.php",
            contentType : "application/x-www-form-urlencoded; charset=utf-8", 
            success: function(r){
                if(type=="panierGourmand"){
                    $("#price_pnr_"+id).html( r );
                }
                else{
                    $("#price_"+id).html( r );
                }
            }
         });
    }
    
    $.ajax({
		type: "POST",
		data: {
			action: 'refreshFP'
		},
		url: "/includes/request.inc.php",
		contentType : "application/x-www-form-urlencoded; charset=utf-8", 
		success: function(r){
                    if(r!='erreur'){
                        $("#FP").html( r );
                    }
		}
	 });
     
     $.ajax({
		type: "POST",
		data: {
			action: 'refreshPromo'
		},
		url: "/includes/request.inc.php",
		contentType : "application/x-www-form-urlencoded; charset=utf-8", 
		success: function(r){
                    if(r!='erreur'){
                    $("#promo").html( r );
                    }
		}
	 });
    
    $.ajax({
		type: "POST",
		data: {
			action: 'refreshTVA55'
		},
		url: "/includes/request.inc.php",
		contentType : "application/x-www-form-urlencoded; charset=utf-8", 
		success: function(r){
                    if(r!='erreur'){
                    $("#tva55").html( r );
                    }
		}
	 });
    
    $.ajax({
		type: "POST",
		data: {
			action: 'refreshTVA196'
		},
		url: "/includes/request.inc.php",
		contentType : "application/x-www-form-urlencoded; charset=utf-8", 
		success: function(r){
                    if(r!='erreur'){
                     $("#tva196").html( r );
                    }
		}
	 });
    
    $.ajax({
		type: "POST",
		data: {
			action: 'refreshTotalHTPrice'
		},
		url: "/includes/request.inc.php",
		contentType : "application/x-www-form-urlencoded; charset=utf-8", 
		success: function(r){
                    if(r!='erreur'){
                     $("#totalHTPrice").html( r );
                    }
		}
	 });
     
     $.ajax({
		type: "POST",
		data: {
			action: 'refreshTotalPrice'
		},
		url: "/includes/request.inc.php",
		contentType : "application/x-www-form-urlencoded; charset=utf-8", 
		success: function(r){
            if(r=="erreur"){
                window.location.replace('/panier.html');
            }
            else{
                $("#totalPrice").html( r );
            }
		}
	 });
}

panierGourmand = {

    get : function (fam_id,action){
        if(action=="getFamily"){
            $("#menu_compte").attr('className','etape2');
        }
        else if(action="getPanier"){
            $("#menu_compte").attr('className','etape1');
        }
        $.ajax({
                type: "POST",
                data: {
                        id: fam_id,
                        action: action
                },
                url: "/includes/request.inc.php",
                contentType : "application/x-www-form-urlencoded; charset=utf-8",
                success: function(r){
                    $("#blockCenter").html( r );
                }
        });

    },
    change : function (pnr_id){

        $.ajax({
                type: "POST",
                data: {
                    id: pnr_id,
                    action: 'changePanier'
                },
                url: "/includes/request.inc.php",
                contentType : "application/x-www-form-urlencoded; charset=utf-8",
                success: function(r){
                    if(r=="maxSlot"){
                       showAlertBox('Mauvais panier', 'Vous devez choisir un panier plus gros ou supprimer des produits.');
                    }
                    else{
                        $("#panierSelect").html( r );
                        panierGourmand.updateSlot();
                        panierGourmand.updatePrice();
                    } 
                }
        });

    },
    viewOptions : function (prd_id,title){
        $.ajax({
                type: "POST",
                data: {
                    id: prd_id,
                    action: 'viewOptions'
                },
                url: "/includes/request.inc.php",
                contentType : "application/x-www-form-urlencoded; charset=utf-8",
                success: function(r){
                    if(r==0){
                        showAlertBox('Pas de panier', 'Vous devez choisir un panier avant de sélectionner vos produits.');
                    }
                    else{
                        showAlertBox(title, r, '',450);
                    }
                    
                }
        });
    },

    addOption : function (opt_id,qty){
        $.ajax({
            type: "POST",
            data: {
                id: opt_id,
                qty: qty,
                action: 'addOption',
                index: $('#ulCarousel > li').length
            },
            url: "/includes/request.inc.php",
            contentType : "application/x-www-form-urlencoded; charset=utf-8",
            success: function(r){
                //  alert(r);
                if(r=='maxSlot'){
                    showAlertBox('Nombre d\'unités maximum atteint', 'Vous avez atteint le nombre maximum d\'unités pour votre panier ou votre produit est trop gros. Vous pouvez soit changer de panier soit supprimer des produits de votre liste.');
                }
                else{
                    $('li').remove('#liCarous_'+opt_id);
                    html=$('#ulCarousel').html();
                    $('#ulCarousel').html(html+r);
                    $(document).ready(function() {
                        $("#carousel").jcarousel({
                            vertical:true,
                            loop: true,
                            dispItems: 3,
                            scroll:1
                        });
                    });
                    panierGourmand.updateSlot();
                    panierGourmand.updatePrice();
                    $('#popupAlert').dialog('close');
                }
                

            }
        });
    },
    deleteOption : function (opt_id){
        $.ajax({
            type: "POST",
            data: {
                id: opt_id,
                action: 'deleteOption',
                index: $('#ulCarousel > li').length
            },
            url: "/includes/request.inc.php",
            contentType : "application/x-www-form-urlencoded; charset=utf-8",
            success: function(r){
                if(r==""){
                    $('li').remove('#liCarous_'+opt_id);
                    if($('#ulCarousel > li').length > 0){
                        $(document).ready(function() {
                            $("#carousel").jcarousel({
                                vertical:true,
                                loop: true,
                                dispItems: 3,
                                scroll:1
                            });
                        });
                    }
                    else{
                        document.getElementById('ulCarousel').style.height="0px";

                    }
                }
                else{
                    $('li').remove('#liCarous_'+opt_id);
                    html=$('#ulCarousel').html();
                    $('#ulCarousel').html(html+r);
                    $(document).ready(function() {
                        $("#carousel").jcarousel({
                            vertical:true,
                            loop: true,
                            dispItems: 3,
                            scroll:1
                        });
                    });
                }
                panierGourmand.updateSlot();
                panierGourmand.updatePrice();
                //$('#popupAlert').dialog('close');
            }
        });
    },
    empty : function(){
        
    },
    valid : function(){
        $.ajax({
            type: "POST",
            data: {
                action: 'valid'
            },
            url: "/includes/request.inc.php",
            contentType : "application/x-www-form-urlencoded; charset=utf-8",
            success: function(r){
                if(r!=""){
                    alert(r);
                }
                else{
                    window.location.href='/panier.html';
                }
                
            }
        });
    },
    updateSlot : function (){
        $.ajax({
            type: "POST",
            data: {
                action: 'updateSlot'
            },
            url: "/includes/request.inc.php",
            contentType : "application/x-www-form-urlencoded; charset=utf-8",
            success: function(r){
                //alert(r);
                $("#CounterSlot").html( r );
            }
        });
    },
    updatePrice : function (){
        $.ajax({
            type: "POST",
            data: {
                action: 'updatePrice'
            },
            url: "/includes/request.inc.php",
            contentType : "application/x-www-form-urlencoded; charset=utf-8",
            success: function(r){
                $("#totalPrice").html( r );
            }
        });
    },
    del : function(key){
        $.ajax({
            type: "POST",
            data: {
                action: 'deletePnr',
                id:key
            },
            url: "/includes/request.inc.php",
            contentType : "application/x-www-form-urlencoded; charset=utf-8",
            success: function(r){
                refreshPrice();
            }
        });
    },
    add : function(key){
        $.ajax({
            type: "POST",
            data: {
                action: 'addPnr',
                id:key
            },
            url: "/includes/request.inc.php",
            contentType : "application/x-www-form-urlencoded; charset=utf-8",
            success: function(r){
                refreshPrice(key,'panierGourmand');
            }
        });
    },
    remove : function(key){
        $.ajax({
            type: "POST",
            data: {
                action: 'removePnr',
                id:key
            },
            url: "/includes/request.inc.php",
            contentType : "application/x-www-form-urlencoded; charset=utf-8",
            success: function(r){
                if(r=="refresh"){
                    window.location.reload();
                }
                else{
                    refreshPrice(key,'panierGourmand');
                }
            }
        });
    }

}
