var regex = new RegExp("[A-Za-z0-9._-]{1,}@[A-Za-z0-9._-]{1,}[.][A-Za-z]{2,}", "gi");
var regex2 = new RegExp("[0-9]{4,}", "gi");

function fermer (_div) {
	$("#"+_div).hide("fast");
}

function afficher_image(_id)
{ 
		$("image").show().html("<img src=images/attente.gif> chargement ..."); 
		
		$.get(
		"./ajax_afficher_image.php" , 
		{ id: _id, RECETTE: 1 } , 
		function(data){ $("#image").html(data); }
		);           
}

function afficher_image_prod(_id)
{ 
		$("image").show().html("<img src=images/attente.gif> chargement ..."); 
		
		$.get(
		"./ajax_afficher_image.php" , 
		{ id: _id, PRODUIT: 1 } , 
		function(data){ $("#image").html(data); }
		);           
}

function ajouter_img(_id, _id_ann) {	
	
		$("chargement").show().html("<img src=images/attente.gif> chargement ..."); 
		
		$.get(
		"./ajax_post_img.php" , 
		{ id: _id, id_ann: _id_ann } , 
		function(data){ $("#pop_img").html(data); }
		);      
}

function ajouter_img_prod(_id, _id_ann) {    
	
		$("chargement").show().html("<img src=images/attente.gif> chargement ..."); 
		
		$.get(
		"./ajax_post_img_prod.php" , 
		{ id: _id, id_ann: _id_ann } , 
		function(data){ $("#pop_img").html(data); }
		);      
}

function form_recette(_form)
{
   var erreur = 0;
   if(document.getElementById("titre").value == "") {
		document.getElementById("titre").style.background = "#ffcccc";
		//$("#erreur").show().append("<br>- Titre manquant"); 
		erreur++;
   }
   else document.getElementById("titre").style.background = ""; 
   
  if(document.getElementById("ingrediants").value == "") {
	   document.getElementById("ingrediants").style.background = "#ffcccc";
	   //$("#erreur").show().append("<br>- Ingrediants manquants"); 
	   erreur++;
   } else document.getElementById("ingrediants").style.background = ""; 
   
   
   if(document.getElementById("explications").value == "") {
	   document.getElementById("explications").style.background = "#ffcccc";
	   //$("#erreur").show().append("<br>- Explications manquant"); 
	   erreur++;
   } else document.getElementById("explications").style.background = "";       
   
   if(document.getElementById("tps_preparation").value == "") {
	   document.getElementById("tps_preparation").style.background = "#ffcccc";
	   //$("#erreur").show().append("<br>- Temps de préparation manquant");  
	   erreur++;
   } else document.getElementById("tps_preparation").style.background = "";  
   
   if(document.getElementById("tps_cuisson").value == "") {
	   document.getElementById("tps_cuisson").style.background = "#ffcccc";
	   //$("#erreur").show().append("<br>- Temps de cuisson manquant");  
	   erreur++;
   } else  document.getElementById("tps_cuisson").style.background = "";
	   
   if(document.getElementById("temperature").value == "") {
	   document.getElementById("temperature").style.background = "#ffcccc";
	  // $("#erreur").show().append("<br>- Température de cuisson manquante");  
	   erreur++;
   } else  document.getElementById("temperature").style.background = ""; 
	
   if((erreur) == 0) {
	$("#erreur").hide(); 
	_form.submit();    
   }
   else
	$("#erreur").show().html("Erreur: Les champs rouges sont requis!");
}



function form_produit(_form)
{
   var erreur = 0;
   if(document.getElementById("titre").value == "") {
		document.getElementById("titre").style.background = "#ffcccc";
		//$("#erreur").show().append("<br>- Titre manquant"); 
		erreur++;
   }
   else document.getElementById("titre").style.background = ""; 
   
  if(document.getElementById("texte").value == "") {
	   document.getElementById("texte").style.background = "#ffcccc";
	   //$("#erreur").show().append("<br>- Ingrediants manquants"); 
	   erreur++;
   } else document.getElementById("texte").style.background = ""; 
   
   
   if(document.getElementById("avis").value == "") {
	   document.getElementById("avis").style.background = "#ffcccc";
	   //$("#erreur").show().append("<br>- Explications manquant"); 
	   erreur++;
   } else document.getElementById("avis").style.background = "";       
   
   if(document.getElementById("marque").value == "") {
	   document.getElementById("marque").style.background = "#ffcccc";
	   //$("#erreur").show().append("<br>- Temps de préparation manquant");  
	   erreur++;
   } else document.getElementById("marque").style.background = "";  
	   
   if((erreur) == 0) {
	$("#erreur").hide(); 
	_form.submit();    
   }
   else
	$("#erreur").show().html("Erreur: Les champs rouges sont requis!");
}


function envoie_ami(_idRecette) {
	var t = new Date();
	$.get(
		"./envoi_ami.php" , 
		{ idRecette: _idRecette, t: t.getTime()} , 
		function(data){ $("#envoi_ami").show("fast").html(data); }
	);    
}

function post_ami(_idaf) {
	
	_nom = escape(document.inviter.nom.value);
	_email = escape(document.inviter.email.value);
	_email1 = escape(document.inviter.email1.value);
	_email2 = escape(document.inviter.email2.value);
	_email3 = escape(document.inviter.email3.value);
	
	$("#envoi_ami").show("fast").html("Envoi en cours ...");
	
	var t = new Date();
	$.post(
		"./ajax_envoi_ami.php" , 
		{ idaf: _idaf, nom: _nom, email: _email, email1: _email1, email2: _email2, email3: _email3, t: t.getTime()} , 
		function(data){ $("#envoi_ami").show("fast").html(data); }
	);    
}


//-----------------------------------------------------------------------
// Système de vote par points  (RANK)
//            Joni Jokhaji
//            17-09-2007
//-----------------------------------------------------------------------

var NOMBRE_ETOILES = 5;


//------------------------------------------------------------------------
// Initialisation des points
//------------------------------------------------------------------------
function init_votes()
{
	var ratings = document.getElementsByTagName('div');
	for (var i = 0; i < ratings.length; i++)
	{
		if (ratings[i].className != 'rating')
			continue;

		var rating = ratings[i].firstChild.nodeValue;
		ratings[i].removeChild(ratings[i].firstChild);
		if (rating > NOMBRE_ETOILES || rating < 0)
			continue;

		// On cherche les étoiles :-)
		for (var j = 0; j < NOMBRE_ETOILES; j++)
		{
			var etoile = document.createElement('img');
			if (rating >= 1)
			{
				etoile.setAttribute('src', './images/stars/rating_on.gif');
				etoile.className = 'on';
				rating--;
			}
			else if(rating == 0.5)
			{
				etoile.setAttribute('src', './images/stars/rating_half.gif');
				etoile.className = 'half';
				rating = 0;
			}
			else
			{
				etoile.setAttribute('src', './images/stars/rating_off.gif');
				etoile.className = 'off';
			}
			var widgetId = ratings[i].getAttribute('id').substr(7);
			etoile.setAttribute('id', 'star_'+widgetId+'_'+j);
			etoile.onmouseover = new Function("evt", "displayHover("+widgetId+", "+j+");");
			etoile.onmouseout = new Function("evt", "displayNormal("+widgetId+", "+j+");");
			ratings[i].appendChild(etoile);
		}
	}
}

// Mettre à jour l'affichage du vote
function resultatVote(_div) {

	var ratings = document.getElementById(_div);

		var rating = ratings.firstChild.nodeValue;
		ratings.removeChild(ratings.firstChild);

		for (var j = 0; j < NOMBRE_ETOILES; j++)
		{
			var etoile = document.createElement('img');
			if (rating >= 1)
			{
				etoile.setAttribute('src', './images/stars/rating_on.gif');
				etoile.className = 'on';
				rating--;
			}
			else if(rating == 0.5)
			{
				etoile.setAttribute('src', './images/stars/rating_half.gif');
				etoile.className = 'half';
				rating = 0;
			}
			else
			{
				etoile.setAttribute('src', './images/stars/rating_off.gif');
				etoile.className = 'off';
			}
			var widgetId = ratings.getAttribute('id').substr(7);
			etoile.setAttribute('id', 'star_'+widgetId+'_'+j);
			etoile.onmouseover = new Function("evt", "displayHover("+widgetId+", "+j+");");
			etoile.onmouseout = new Function("evt", "displayNormal("+widgetId+", "+j+");");
			ratings.appendChild(etoile);
		}

}

// Afficher le hover des étoiles
function displayHover(ratingId, etoile)
{                                                     
	for (var i = 0; i <= etoile; i++)
	{
		document.getElementById('star_'+ratingId+'_'+i).setAttribute('src', './images/stars/rating_over.gif');
	}
}

// Afficher normalement les étoiles
function displayNormal(ratingId, etoile)
{
	for (var i = 0; i <= etoile; i++)
	{
		var status = document.getElementById('star_'+ratingId+'_'+i).className;
		document.getElementById('star_'+ratingId+'_'+i).setAttribute('src', './images/stars/rating_'+status+'.gif');
	}
}

// Voter
function vote(evt) {
	var tmp = evt.target.getAttribute('id').substr(5);
	var _id = tmp.substr(0, tmp.indexOf('_'));
	var etoileNbr = tmp.substr(tmp.indexOf('_')+1);

	$("#resultat_"+_id).html("Chargement ...");

	// AJAX --> mise à jour des données
	$.get(
		  "./ajax.php" ,
		  { action: 'vote' , ID: _id , points: parseInt(etoileNbr)+1 } ,
		  function(data){ $("#resultat_"+_id).html(data); }
		 );

	// Mise à jour du vote
	$("#rating_"+_id).html(parseInt(etoileNbr)+1);
	resultatVote("rating_"+_id);
}

// Initialiser l'affichage des étoiles
function init() {
	init_votes();
	$('div.rating img').click(vote);
}