function init_field(elem_id)
{
	if($("#"+elem_id).val()=="0")
		$("#"+elem_id).val("");
}


function index_alpha()
{
	var letter_id = $("#letter_select").val();
	$(location).attr("href","index_alpha.php?letter_id="+letter_id);
}

function change_pic(src, code)
{
	$("#main_pic_"+code).html("<a id='pic_"+code+"' href='./photos/originals/"+src+".jpg'><img src='./photos/200x200/"+src+".jpg' class='main_pic' alt='"+src+"' /></a>");
	
	$('a#pic_'+code).fancybox({
		'titleShow'		: false,
		'transitionIn'	: 'elastic',
		'transitionOut'	: 'elastic'
	});
}

function add_to_basket(ref)
{
	
	var qte = $("#qte_"+ref).val();
	$.ajax({
			type: "POST",
			url: "ajout_panier.php",
			data: "ref="+ref+"&qte="+qte,
			success: function(msg){
				$("#panier span").html(msg);
				$("#panier span").effect("bounce", "{}",300);
				},
			error: function(msg){
			 	alert("Impossible d'ajouter cette reference.")
				},
			beforeSend: function(msg){
				}
			});
}

function delete_from_basket(ref, elem)
{
	if(confirm("Etes-vous sur de vouloir supprimer cette reference du panier?"))
	{
		$.ajax({
				type: "POST",
				url: "supprime_panier.php",
				data: "ref="+ref,
				success: function(msg){
					$("#panier span").html(msg);
					$("#panier span").effect("bounce", "{}",300);
					$(elem).remove();
					calcul_total()
					},
				error: function(msg){
				 	alert("Impossible de supprimer cette ligne.")
					},
				beforeSend: function(msg){
					}
				});
	}
	
}

function affiche_panier(txt)
{
	$("#panier span").html(txt);
	$("#panier span").effect("bounce", "{}",300);
}

function change_qte(bouton, id)
{
	if(bouton=="m")
	{
		if(parseInt($("#"+id).val())>0)
			$("#"+id).val(parseInt($("#"+id).val())-1)
	}
	if(bouton=="p")
		$("#"+id).val(parseInt($("#"+id).val())+1)
		
}

function update_ligne_panier(ref)
{
	var qte = parseFloat($("#qte_"+ref).val());
	var prix = $("#prix_"+ref).html();
	prix = prix.replace(/,/g,".");
	prix = parseFloat(prix);
	var total = prix*qte;
	total = Math.round(total*100)/100;
	total = total.toString();
	var tab = total.split(".")
	if(tab.length==1)
	{
		total = tab[0]+",00";
	}
	else if(tab.length==2)
	{
		var decimal = tab[1].substr(0,2);
		if(decimal.length<2)
			decimal = decimal+"0";
			
		total = tab[0]+","+decimal;
	}
	
	$("#total_"+ref).html(total)
	
	calcul_total()
}

function calcul_total()
{
	var list_prix = $(".total");
	var total = 0;
	var prix = 0;
	for(var i=0;i<list_prix.length;i++)
	{
		prix = $(list_prix[i]).children().html();
		prix = prix.replace(/,/g,".");
		total += parseFloat(prix);
	}
	total_ttc = total*1.196;
	total = total.toString();
	total_ttc = total_ttc.toString();
	var tab = total.split(".")
	if(tab.length==1)
	{
		total = tab[0]+",00";
	}
	else if(tab.length==2)
	{
		var decimal = tab[1].substr(0,2);
		if(decimal.length<2)
			decimal = decimal+"0";
			
		total = tab[0]+","+decimal;
	}
	var tab = total_ttc.split(".")
	if(tab.length==1)
	{
		total_ttc = tab[0]+",00";
	}
	else if(tab.length==2)
	{
		var decimal = tab[1].substr(0,2);
		if(decimal.length<2)
			decimal = decimal+"0";
			
		total_ttc = tab[0]+","+decimal;
	}
	
	
	
	$("#total_order").html(total)
	$("#total_order_ttc").html(total_ttc)
}

function clean_basket()
{
	if(confirm("Etes-vous sur de vouloir vider votre panier?\nTout les articles presents seront perdu."))
	{
		$(".bg_1").remove();
		$(".bg_2").remove();
		
		$.ajax({
				type: "POST",
				url: "clean_basket.php",
				data: "",
				success: function(msg){
					calcul_total()
					},
				error: function(msg){
				 	alert("Impossible de vider le panier")
					},
				beforeSend: function(msg){
					}
				});
	}
}

function check_ref(indice)
{
	var ref = $("#art_code_"+indice).val();
	
	$.ajax({
			type: "POST",
			url: "check_ref.php",
			data: "ref="+ref,
			success: function(msg){
				$("#art_des_"+indice).html(msg);
				},
			error: function(msg){
			 	alert("erreur")
				},
			beforeSend: function(msg){
				}
			});
	
}

function send_data()
{
	var saisie = $("#recherche_input").val();
	
	$.ajax({
			type: "POST",
			url: "save_session.php",
			data: "search_value="+saisie,
			success: function(msg){
				$(location).attr("href","recherche.php?page=1&search=1");
				},
			error: function(msg){
			 	alert("erreur")
				},
			beforeSend: function(msg){
				}
			});
	return false;
}

function sub_menu(elem)
{
	/*
	var top = elem.position().top;
	if(top>20)
		top = "85px";
	else
		top = "40px";
	*/
	var top = "85px";
	$(".h_submenu").hide();
	var id = elem.attr("id");
	$("#overlay").show();
	var color = $("#"+id).attr("rel");
	color = color.replace(/-/g,",");
	$("#"+id+"_sub").css("border-color", "rgb("+color+")");
	$("#"+id+"_sub").css("left", "16px");
	$("#"+id+"_sub").css("top", top);
	$("#"+id+"_sub").show();
	
}

function sub_menu_hide()
{
   $('.h_submenu').hide(); 
   $('#overlay').hide(); 
    
    
	
}


function print_preview(code)
{
	window.open('type_print.php?code='+code+'','Impression','menubar=yes, status=no, scrollbars=no, width=780, height=800');
}

function valid_form()
{
	
	var tab_input = $('.require'); 
   	flag_error = false;
   	for(var i = 0; i < tab_input.length; i++) 
   	{ 
   		if($(tab_input[i]).val() == '') 
   		{ 
   			flag_error = true; 
   			$(tab_input[i]).css('border-color', 'red'); 
   			$(tab_input[i]).effect('bounce', { times:3 }, 300);
   		}
   		else
   		{
   			if($(tab_input[i]).attr("id")=="mail" && !IsValidEmail($(tab_input[i]).val()))
   			{
   				flag_error = true; 
   				$(tab_input[i]).css('border-color', 'red'); 
   				$(tab_input[i]).effect('bounce', { times:3 }, 300);
   			}
   			else
   				$(tab_input[i]).css('border-color', 'black'); 
   		}
   			
   		
   	}
   	
   	if(flag_error==false)
   		$("#send_contact_form").submit();
}

function IsValidEmail(email)
{
	var filter = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
	return filter.test(email);
}

function start_blink()
{
	if($(".blink_elem").css("display")=="block")
		$(".blink_elem").css("display", "none");
	else
		$(".blink_elem").css("display", "block");
	
	setTimeout("start_blink()",500);
}

function display_msg()
{
	alert("Rubrique prochainement disponible.")
}

function display_cat()
{
	window.open('catalogue.php', 'Catalogue', ',type=fullWindow,fullscreen,location=no,scrollbars=yes');
}


/*********************************************************************************************************/
/********************************************ADMIN********************************************************/
/*********************************************************************************************************/
function load_type()
{
	var coden2 = $("#type_selector").val();
	if(coden2!="-1")
	{
		$.ajax({
			type: "POST",
			url: "load_type.php",
			data: "coden2="+coden2,
			success: function(msg){
				temp = msg.split("{..}");
				$("#nouveaute_titre").val(temp[0]);
				$('.nouveaute_title h1').html(temp[0])
				$("#nouveaute_texte").val(temp[1]);
				$("#nou_photo").css("background-image", "url(../photos/200x200/"+temp[2]+".jpg)");
				$("#nou_photo_input").val("photos/200x200/"+temp[2]+".jpg");
				},
			error: function(msg){
			 	alert("erreur")
				},
			beforeSend: function(msg){
				}
			});
	}
}
function Deplacer(l1,l2)
{
	var selected_type = $("#"+l1).val();
	if(selected_type!="-1")
	{
		selected_type = selected_type.toString();
		var temp = selected_type.split(",");
		for(i=0;i<temp.length;i++)
		{
			$("#"+l2).append("<option value='"+temp[i]+"'>"+temp[i]+"</option>")
		}
		$("#"+l1+" :selected").remove()
	}
}
function getList(elem)
{
	var list_elem = "";
	var i=0;
	$("#"+elem+" option").each(function() { 
		if($(this).val()!="-1")
		{
			if(list_elem=="")
				list_elem = $(this).val();
			else
				list_elem += ","+$(this).val();
		}
	})
	$("#"+elem+"_input").val(list_elem);
}
function getTypes(coden1)
{
	if(coden1!="-1")
	{
		$.ajax({
			type: "POST",
			url: "ajax_get_type.php",
			data: "coden1="+coden1,
			success: function(msg){
				$("#list_types_c").html(msg);
				$("#list_types_c select").change(function(){
					if($(this).val()!="-1")
					{
						$(location).attr('href','types.php?coden2='+$(this).val())
					}
				})

				
				
				},
			error: function(msg){
			 	alert("erreur")
				},
			beforeSend: function(msg){
				}
			});
	}
}
function getTypes_2(coden1)
{
	if(coden1!="-1")
	{
		$.ajax({
			type: "POST",
			url: "ajax_get_type.php",
			data: "coden1="+coden1,
			success: function(msg){
				$("#list_types_c").html(msg);
				$("#list_types_c select").change(function(){
					getSousTypes($(this).val())
				})

				
				
				},
			error: function(msg){
			 	alert("erreur")
				},
			beforeSend: function(msg){
				}
			});
	}
}
function getTypes_3(coden1)
{
	if(coden1!="-1")
	{
		$.ajax({
			type: "POST",
			url: "ajax_get_type.php",
			data: "coden1="+coden1,
			success: function(msg){
				$("#list_types_c").html(msg);
				$("#list_types_c select").css("width", "300px");
				},
			error: function(msg){
			 	alert("erreur")
				},
			beforeSend: function(msg){
				}
			});
	}
}
function getSousTypes(coden2)
{
	if(coden2!="-1")
	{
		$.ajax({
			type: "POST",
			url: "ajax_get_sous_type.php",
			data: "coden2="+coden2,
			success: function(msg){
				$("#list_sstypes_c").html(msg);
				$("#list_sstypes_c select").change(function(){
					if($(this).val()!="-1")
					{
						$(location).attr('href','sous_types.php?coden3='+$(this).val())
					}
				})

				
				
				},
			error: function(msg){
			 	alert("erreur")
				},
			beforeSend: function(msg){
				}
			});
	}
}
function getScat(cat_id, typcat_id)
{
	if(cat_id!="-1")
	{
		$.ajax({
			type: "POST",
			url: "ajax_get_scat.php",
			data: "cat_id="+cat_id+"&typcat_id="+typcat_id,
			success: function(msg){
				$("#list_scat_"+typcat_id).html(msg);
				},
			error: function(msg){
			 	alert("erreur")
				},
			beforeSend: function(msg){
				}
			});
	
	}
}
function getFam(let_id, index_type_id)
{
	if(let_id!="-1")
	{
		$.ajax({
			type: "POST",
			url: "ajax_get_fam.php",
			data: "let_id="+let_id+"&index_type_id="+index_type_id,
			success: function(msg){
				$("#list_fam_"+index_type_id).html(msg);
				},
			error: function(msg){
			 	alert("erreur")
				},
			beforeSend: function(msg){
				}
			});
	
	}
}
function getSfam(fam_id, index_type_id)
{
	if(fam_id!="-1")
	{
		$.ajax({
			type: "POST",
			url: "ajax_get_sfam.php",
			data: "fam_id="+fam_id+"&index_type_id="+index_type_id,
			success: function(msg){
				$("#list_sfam_"+index_type_id).html(msg);
				},
			error: function(msg){
			 	alert("erreur")
				},
			beforeSend: function(msg){
				}
			});
	
	}
}

function switch_pic(src, elem, pho_id)
{
	if(src=="add_pic")
		$("#main_pic_c").html("<img class='main_pic' src='../design/images/add_pic.jpg' alt='' />");
	else
		$("#main_pic_c").html("<img class='main_pic' src='../photos/200x200/"+src+".jpg' alt='' />");
	
	$("#selected_pic").val(pho_id);
	$("#list_all_pic div").removeClass("selected");
	if(elem!="")
		elem.addClass("selected");
}

function delete_img()
{
	if(confirm("Etes-vous sur de vouloir supprimer cette photo"))
	{
		var current_elem = $("#current_elem").val()
		if(current_elem=="types")
		{
			var selected_pic = $("#selected_pic").val();
			var coden2 = $('#coden2').val();
			if(selected_pic!="0")
			{
				$.ajax({
					type: "POST",
					url: "ajax_delete_img.php",
					data: "selected_pic="+selected_pic+"&elem=typ",
					success: function(msg){
						$(location).attr("href", "types.php?coden2="+coden2)
						},
					error: function(msg){
					 	alert("erreur")
						},
					beforeSend: function(msg){
						}
					});
			}
		}
		else if(current_elem=="sous-types")
		{
			var selected_pic = $("#selected_pic").val();
			var coden3 = $('#coden3').val();
			if(selected_pic!="0")
			{
				$.ajax({
					type: "POST",
					url: "ajax_delete_img.php",
					data: "selected_pic="+selected_pic+"&elem=sty",
					success: function(msg){
						$(location).attr("href", "sous_types.php?coden3="+coden3)
						},
					error: function(msg){
					 	alert("erreur")
						},
					beforeSend: function(msg){
						}
					});
			}
		}
	}
}

function switch_elem_type(elem_val, elem_id)
{
	if(elem_val=="CODE")
	{
		$("#link_pdf_"+elem_id).hide();
		$("#link_code_"+elem_id).show();
	}
	else if(elem_val=="PDF")
	{
		$("#link_pdf_"+elem_id).show();
		$("#link_code_"+elem_id).hide();
	}
}

function save_list_info(elem_id, table)
{
	var libelle = $("#libelle_"+elem_id).val();
	var type = $("#type_"+elem_id).val();
	if(type=="CODE")
		var link = $("#link_code_"+elem_id).val();
	else if(type=="PDF")
		var link = $("#link_pdf_"+elem_id).val();
		
	$.ajax({
		type: "POST",
		url: "ajax_save_list_info.php",
		data: "libelle="+libelle+"&type="+type+"&link="+link+"&table="+table+"&elem_id="+elem_id,
		success: function(msg){
			alert("Enregistrement reussi")
			},
		error: function(msg){
		 	alert("erreur")
			},
		beforeSend: function(msg){
			}
		});
}

function delete_list_info(elem_id, table)
{
	if(confirm("Etes-vous sur de vouloir supprimer cette information?"))
	{
		$.ajax({
		type: "POST",
		url: "ajax_delete_list_info.php",
		data: "table="+table+"&elem_id="+elem_id,
		success: function(msg){
			if(table=="savoir_faire")
				$(location).attr('href','savoir_faire.php');
			if(table=="infos_pratiques")
				$(location).attr('href','infos_pratiques.php');
			},
		error: function(msg){
		 	alert("erreur")
			},
		beforeSend: function(msg){
			}
		});
	}
}

function deletePrixNet(prf_id)
{
		$.ajax({
		type: "POST",
		url: "ajax_delete_prix_net.php",
		data: "prf_id="+prf_id+"",
		success: function(msg){
			$(location).attr('href','profils.php?prf_id='+prf_id+'');
			},
		error: function(msg){
		 	alert("erreur")
			},
		beforeSend: function(msg){
			}
		});
}





