
// TEST email
function test_email(my_email) {
	var new_string = new String(my_email);
	if (!new_string.match('^[-_\.0-9a-zA-Z]{1,}@[-_\.0-9a-zA-Z]{1,}[\.][0-9a-zA-Z]{2,}$')) {
			return false;
	}
	else {
			return true;
	}
}




// POST un lien
function lien(id,div_contenu,urlpost,datapost,class_element){
	
	jQuery(document).ready(function() {
		

			$(id).click(function(){
									 
				
				$.ajax({
					
					type: "POST",
					url: "./templates/pages/"+urlpost,
					data: datapost,
					success: function(msg){
						//alert(msg);
						$("#"+div_contenu).html(msg);
						
					}
				 });
				 return false;
			});
			
			return false;

		
	});
	
}


// post lien en ajax
function postAjax(urlpost,datapost,div_contenu,fluide){
	
	if (fluide == 1) { 
		$("#"+div_contenu).hide();
	}
	$("#"+div_contenu).html("<div style='float:left;width:100%;height:450px;background:url(./content/img/loader.gif) 50% 50% no-repeat;'></div>")
	
		
	.ajaxStart(function(){
		return false;		
	});
			
	$.ajax({
		
		type: "POST",
		url: urlpost,
		data: datapost,
		success: function(msg){
			//alert(msg);
			
    		
    		
			$("#"+div_contenu).html(msg);
			
			$(".tinymce").tinymce({
					script_url : "./content/js/tinymce/tiny_mce.js",

					// General options
					language : "fr",
					theme : "advanced",
					plugins : "safari,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template",
		
					// Theme options
					//theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,|,insertdate,inserttime,preview,|,forecolor,backcolor",
					//theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|,print,|,ltr,rtl,|,fullscreen",
					//theme_advanced_buttons4 : "insertlayer,moveforward,movebackward,absolute,|,styleprops,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,template,pagebreak",
	
					theme_advanced_buttons1 : "bold,italic,underline,|,link,unlink,|,styleselect",
					theme_advanced_buttons2 : "",
					theme_advanced_buttons3 : "",
					paste_auto_cleanup_on_paste : true,
					theme_advanced_toolbar_location : "top",
					theme_advanced_toolbar_align : "left",
					theme_advanced_statusbar_location : "bottom",
					theme_advanced_resizing : true,// Example content CSS (should be your site CSS)
            		content_css : "./content/js/tinymce/content.css"
					
		
		
			});
			
			$("a[rel='shadowbox']").colorbox({transition:"fade"});
			
			if (fluide == 1) { 
				$("#"+div_contenu).show("slow");
			}
			
			$('.scroll-pane').jScrollPane({showArrows:true});
			
			
			
		}
	 });
	 
	//return false;
}

// ajoute une colonne
function ajout_colonne(id_gamme_marque){
	
	num = Math.floor(1000 * Math.random());
	
	if (typeof(id_gamme_marque) != 'undefined'){
		onchange = 'addColonne('+id_gamme_marque+','+num+');return false;';
	}else{
		onchange = '';
	}
	
	$("#hey").append('<input type="text" name="nouveau_champ[]" id="ajout_colonne'+num+'" value="" onchange="'+onchange+'" /><a id="supp_colonne'+num+'" name="supp_colonne'+num+'"  title="Supprimer cette colonne" onClick="suppColonne('+num+');return false;" href="#"><img src="./content/img/admin/icone_supp.png" border="0"/></a>');
																																			
}

// ajoute une colonne dans la base
function addColonne(id_gamme_marque,id){
	titre = $("#ajout_colonne"+id).val();
	
	urlpost = './admin/traitement.php';
	datapost = 'action=colonne_add&id_gamme_marque='+id_gamme_marque+'&titre_colonne='+titre;
				
	$.ajax({
		
		type: "POST",
		url: urlpost,
		data: datapost,
		success: function(idnew){
			$("#ajout_colonne"+id).attr("name","ajout_colonne"+idnew);
			$("#ajout_colonne"+id).removeAttr("onChange");
			$("#ajout_colonne"+id).attr("onChange","modifColonne('"+idnew+"');return false;");
			$("#ajout_colonne"+id).attr("id","ajout_colonne"+idnew);
			
			$("#supp_colonne"+id).attr("name","supp_colonne"+idnew);
			$("#supp_colonne"+id).removeAttr("onChange");
			$("#supp_colonne"+id).attr("onChange","suppColonne('"+idnew+"');return false;");
			$("#supp_colonne"+id).attr("id","supp_colonne"+idnew);
		}
	 });
																																			
}

// modifie une colonne
function modifColonne(id){
	titre = $("#ajout_colonne"+id).val();
	
	urlpost = './admin/traitement.php';
	datapost = 'action=colonne_modif&id_colonne='+id+'&titre_colonne='+titre;
				
	$.ajax({
		
		type: "POST",
		url: urlpost,
		data: datapost,
		success: function(msg){
			
		}
	 });
																																			
}

// supprime une colonne
function suppColonne(id){
	urlpost = './admin/traitement.php';
	datapost = 'action=colonne_supp&id_colonne='+id;
				
	$.ajax({
		
		type: "POST",
		url: urlpost,
		data: datapost,
		success: function(msg){
			
		}
	 });
	
	$("#supp_colonne"+id).remove();
	$("#ajout_colonne"+id).remove();

}

// maj du titre
function majTitre(){
	gamme = $("#gamme option:selected").text();
	marque = $("#marque option:selected").text();
	
	if (gamme!='' && marque != ''){
		 $("#titre").val(gamme+" "+marque);
	}
}

// affiche les colonnes en ajax
function ajax_affichage_colonne(){
	
	idgammeproduit = $("#idgammeproduit option:selected").val();
	
	id_produit = $("#id_produit").val();
	
	urlpost = './admin/traitement.php';
	datapost = 'action=recup_colonne&colonne='+idgammeproduit+'&id_produit='+id_produit;
				
	$.ajax({
		
		type: "POST",
		url: urlpost,
		data: datapost,
		success: function(msg){
			//alert(msg);
			$("#les_colonnes").html(msg);
		}
	 });

}


// upload un fichier dans un div
/*
	div_post -> div dans lequel est poster le retour de fichier
	champ_chargement -> champ "file" de chargment du fichier
	nb_element_max -> nb d'element possible à charger (1 par defaut)
	max_size -> taille maxi ces fichiers
	height_max -> hauteur max du fichier si image
	width_max -> largeur max du fichier si image
	extension -> exention possible 'doc,img,all'
*/
function uploadFile(div_post,champ_chargement,nb_element_max, max_size, width_max, height_max, extension){

	if (typeof(champ_chargement) == 'undefined'){
		champ_chargement = 'fileToUpload';
	}
	if (typeof(nb_element_max) == 'undefined'){
		nb_element_max = 1;
	}
	if (typeof(max_size) == 'undefined'){
		max_size = '';
	}
	if (typeof(height_max) == 'undefined'){
		height_max = '';
	}
	if (typeof(width_max) == 'undefined'){
		width_max = '';
	}
	if (typeof(extension) == 'undefined'){
		extension = "all";
	}
	
	if ($("#"+champ_chargement).val() != ""){
		
		//starting setting some animation when the ajax starts and completes
        $("#fileloading")
        .ajaxStart(function(){
            $(this).show();
        })
        .ajaxComplete(function(){
            $(this).hide();
        });
		
		$.ajaxFileUpload
		(
			{
				url:'./admin/ajaxfileupload.php?extension='+extension+'&max_size='+max_size+'&height_max='+height_max+'&width_max='+width_max,
				secureuri:false,
				fileElementId:champ_chargement,
				dataType: 'json',
				success: function (data, status)
				{
					if(typeof(data.error) != 'undefined')
					{
						if(data.error != '')
						{
							//alert(data.error);
							$("#div_message").html(data.error);
							$("#div_message").dialog("open");
				   			$("#div_message").dialog({
				   				modal: true,
				   				resizable: false,
				   				draggable: false 
				   				});
						}else
						{
							if(typeof(div_post) != 'undefined'){
								nb_element_actu = $("#"+div_post+" > *").length;
								
								id_div = Math.floor(1000 * Math.random());
								contenu = "<div id='"+id_div+"' class='fichier_uploader'><a href='#' onclick='$(\"#"+id_div+"\").remove();return false;' title='Supprimer cet élément'><img src='./content/img/admin/icone_supp.png' border='0' alt='' /></a>&nbsp;<a href='./files/img/"+data.msg+"' target='_blank' title=''>"+data.msg+"</a><input type='hidden' name='fichiers_"+champ_chargement+"[]' id='fichiers_"+champ_chargement+"[]' value='"+data.msg+"' /></div>";
								
								if (nb_element_max != 1){
									contenu_anc =  $("#"+div_post).html();
									if (nb_element_max >= nb_element_actu+1){
										contenu = contenu_anc + contenu;
										$("#"+div_post).html(contenu);
									}else{
										alert("Vous avez atteint le nombre maximum de téléchargement possible");
									}
								}else{
									$("#"+div_post).html(contenu);
								}
								
								
								
							}else{
								$("#image").val(data.msg);
							}
						}
					}
				},
				error: function (data, status, e)
				{
					//alert(data+'\n'+status+'\n'+e);
				}
			}
		)
 	}
}


// ajoute au panier
function addPanier(id_produit){
	urlpost = './admin/traitement.php';
	datapost = 'action=panier_add&id_produit='+id_produit;
				
	$.ajax({
		type: "POST",
		url: urlpost,
		data: datapost,
		success: function(msg){
			if (msg == 1){
				nb_produit_panier = "1 produit";
			}else{
				nb_produit_panier = msg+" produits";
			}
			
			message = "<br/>"+nb_produit_panier+" dans votre panier<br><br>";
			
			$("#div_message").dialog("close");
			$("#div_message").html(message);
			$("#div_message").dialog("open");
			$("#div_message").dialog({
	   				modal: true,
	   				resizable: false,
	   				draggable: false,
	   				close: function(event, ui) {
	   						
	   						$("#panier_contenu").html(nb_produit_panier);
	   				},
	   				buttons: {
						"Continuer": function() {
							$(this).dialog('close');
						},
						"Votre panier": function() {
							postAjax("./templates/pages/panier.php","action=","bloc_centre_home");
							$(this).dialog('close');
						}
					}
	   				
				});
	   				
			
		}
	 });
}

// supprime du panier
function suppPanier(id_produit){
	if (confirm("Vous voulez vraiment supprimer ce produit de votre panier ?")){
		urlpost = './admin/traitement.php';
		datapost = 'action=panier_supp&id_produit='+id_produit;
					
		$.ajax({
			type: "POST",
			url: urlpost,
			data: datapost,
			success: function(msg){
				if (msg == 1){
					nb_produit_panier = "1 produit";
				}else{
					nb_produit_panier = msg+" produits";
				}
				$("#panier_contenu").html(nb_produit_panier);
				
				postAjax("./templates/pages/panier.php","action=","bloc_centre_home");
				
			}
		 });
	}
}

function changePrix(id_produit){
	nb = $("#nb_"+id_produit).val();
	
	urlpost = './admin/traitement.php';
	datapost = 'action=panier_modifprix&id_produit='+id_produit+'&nb='+nb;
				
	$.ajax({
		type: "POST",
		url: urlpost,
		data: datapost,
		success: function(msg){
			
			postAjax("./templates/pages/panier.php","action=","bloc_centre_home");
			
		}
	 });
}

// affiche un div
function afficheDiv(id){
	$("#"+id).show();
	return false;
}

//masque un div
function masqueDiv(id){
	$("#"+id).hide();
	return false;
}

// supprime les espaces
function trim(myString)
{
	return myString.replace(/^\s+/g,'').replace(/\s+$/g,'')
} 


// function suppression Precommande (commande, partie mon compte)
function suppPrecommande(id){
	if (confirm("Etes vous sur de vouloir supprimer cette prÃ©commande ?")){
		$.ajax({
		   type: "POST",
		   url: "./admin/traitement.php",
		   data: "action=precommande_supp&id="+id,
		   success: function(msg){
		   			
		   			if (msg == 1){
		   				message = "<br/>La pr&eacute;commande a &eacute;t&eacute; supprim&eacute;e";
		   				$("#precommande_"+id).hide();
		   			}else{
		   				message = "<br/>La pr&eacute;commande n'a pas &eacute;t&eacute; supprim&eacute;e";
		   			}
		   			
		   			$("#div_message_moncompte").html(message);
		   			$("#div_message_moncompte").dialog('open');
		   			$("#div_message_moncompte").dialog({
		   				modal: true,
		   				resizable: false,
		   				draggable: false 
		   				});
	 				}
	 			});
	 }
 			
 	return false;
}


// renvoie la prÃ©commande
function precommandeEnvoyer(guid){
	$.ajax({
	   type: "POST",
	   url: "./admin/traitement.php",
	   data: "action=precommande_renvoyer&guid="+guid,
	   success: function(msg){
   			$("#div_message_moncompte").html(msg);
   			$("#div_message_moncompte").dialog('open');
   			$("#div_message_moncompte").dialog({
   				modal: true,
   				resizable: false,
   				draggable: false 
   				});
		}
	});
 			
 	return false;	
}
 
 // fonction qui retourne l'iframe google
 function geocalisation(adresse,div){
 	$.ajax({
	   type: "POST",
	   url: "./admin/traitement.php",
	   data: "action=geocalisation&adresse="+adresse,
	   success: function(msg){
   			$("#"+div).html(msg);
   			$("#"+div).show();
   			$("#"+div).mouseleave(function(){
				 $("#"+div).hide();
			});
		}
	});
 			
 	return false;	
 }



/// pour le SLIDE des images
function SelectSlide(formulaire) {
	slide = formulaire.slide.options[formulaire.slide.selectedIndex].value;
	alert(slide);
	thisFlashMovie("Coverflow").SelectSlide(slide);
}

function thisFlashMovie(movieName) {
	if (navigator.appName.indexOf("Microsoft") != -1) {
		return window[movieName]
	} else {
		return document[movieName]
	}
}

function setActiveStyleSheet(title) {
	var i, a, main;
	for (i = 0; (a = document.getElementsByTagName("link")[i]); i++) {
		if (a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
			a.disabled = true;
			if (a.getAttribute("title") == title)
				a.disabled = false;
		}
	}
}
 

// PARTENAIRES HOME PAGE
function affDivPartenaire(id){
	$(".descriptif").hide();
	$(".descriptif_min").show();
	$("#descriptif_min_"+id).hide();
	$("#descriptif_"+id).show("slow");
}

function masqueDivPartenaire(id){
	$("#descriptif_"+id).hide();
	$("#descriptif_min_"+id).show();
}

// VIDEO
function affDivVideo(id){
	$(".descriptifvideo").hide();
	$(".descriptifvideo_min").hide();
	//$("#descriptif_min_"+id).hide();
	$("#descriptif_video_"+id).show();
}

function affDivVideomin(id){
	$(".descriptifvideo").hide();
	$(".descriptifvideo_min").hide();
	//$("#descriptif_min_"+id).hide();
	$("#descriptif_min_video_"+id).show();
}

function masqueDivVideo(id){
	$(".descriptifvideo").hide();
	$(".descriptifvideo_min").hide();
	$("#descriptif_min_video_"+id).show();
}

function affVideo(id,taille){
	
	$.ajax({
	   type: "POST",
	   url: "./templates/pages/video.php",
	   data: "id_video="+id+"&taille="+taille,
	   success: function(msg){
   			$("#contenu_video").html(msg);
   			$("#contenu_video").show();
   			
   			affDivVideomin(id);
		}
	});
	
}
