function trim(string)
{
	return string.replace(/(^\s*)|(\s*$)/g,'');
}

function miseajourwebcam()
{
	var valeur=Math.floor(Math.random()*10000);
	document.getElementById('cadrewebcam').innerHTML='<img src="~webcam/ccam.jpg?'+valeur+'" alt="Webcam en direct des studios" width="275" height="206" title="Webcam en direct des studios" />';
	setTimeout("miseajourwebcam()", 30000);
}

function changerlangue(langue, noreload)
{
   $.ajax({
   async: !noreload,
   type: "POST",
   url: "../../index.php",
   data: "action=changerlangue&langue="+trim(langue)+"&R="+new Date(),
         success:
         function(resultat)
         {
          	if (!noreload) window.location.reload();
         }
   });
}

function miseajourtitrage()
{
   $.ajax({
   type: "POST",
   url: "index.php",
   data: "action=infostitrage&R="+new Date(),
         success:
         function(resultat)
         {
		      document.getElementById("titreencours").innerHTML = resultat;
					setTimeout("miseajourtitrage()", 50000);
         }
   });
}

function patience()
{
	if (document.getElementById('bloc_patienter'))
	{
		document.getElementById('bloc_rechercher').style.display='none';
		document.getElementById('bloc_patienter').style.display='';
		texte=document.getElementById('bloc_patienter').innerHTML;
		if (texte=='') texte="Recherche en cours";
		else
		if (texte=='Recherche en cours') texte="Recherche en cours.";
		else
		if (texte=='Recherche en cours.') texte="Recherche en cours..";
		else
		if (texte=='Recherche en cours..') texte="Recherche en cours...";
		else
		if (texte=='Recherche en cours...') texte="Recherche en cours";
		document.getElementById('bloc_patienter').innerHTML=texte;
		recherche=setTimeout("patience()", 300);
	} else clearTimeout(recherche);
}

var recherche;

function rechercher(recherche_page)
{
	if (!rechercheencours)
	{
 	 	rechercheencours=true;
		clearTimeout(recherche);
		document.getElementById('bloc_patienter').innerHTML='';
		recherche=setTimeout("patience()", 1000);

 	 	if (document.getElementById('recherche_fiche').checked) recherche_lieu=1; else recherche_lieu=0;
 	 	if (document.getElementById('recherche_chronique').checked) recherche_chronique=1; else recherche_chronique=0;
 	 	if (document.getElementById('recherche_interview').checked) recherche_interview=1; else recherche_interview=0;
 	 	if (document.getElementById('recherche_photo').checked) recherche_photo=1; else recherche_photo=0;
 	 	if (document.getElementById('recherche_video').checked) recherche_video=1; else recherche_video=0;
 	 	if (document.getElementById('recherche_album').checked) recherche_album=1; else recherche_album=0;

 	 	if (document.getElementById('recherche_chronologique_fiche').checked) recherche_classement=1; else
 	 	if (document.getElementById('recherche_chronologique_audio').checked) recherche_classement=2; else recherche_classement=0;
 	 	document.getElementById('recherche_bouton').disabled=true;
  	$.ajax({
   	type: "POST",
 	 	url: "../index.php",
 	 	data: "action=recherche"+
 	       	"&recherche_donnee="+encodeURIComponent(trim(document.getElementById('recherche_donnee').value))+
 	       	"&recherche_style="+encodeURIComponent(trim(document.getElementById('recherche_style').value))+
 	       	"&recherche_lieu="+recherche_lieu+
 	       	"&recherche_chronique="+recherche_chronique+
 	       	"&recherche_interview="+recherche_interview+
 	       	"&recherche_photo="+recherche_photo+
 	       	"&recherche_video="+recherche_video+
 	       	"&recherche_album="+recherche_album+
 	       	"&recherche_classement="+recherche_classement+
 	       	"&recherche_page="+recherche_page+
 	       	"&R="+new Date(),
       		success:
       		function(resultat)
       		{
       			clearTimeout(recherche);
						document.getElementById('bloc_patienter').innerHTML='';
						document.getElementById('bloc_patienter').style.display='none';
						document.getElementById('bloc_rechercher').style.display='';
	      	 	document.getElementById('recherche_bouton').disabled=false;
			      document.getElementById("liste_groupes").innerHTML = resultat;
		      	rechercheencours=false;
       		}
   	});
  };
}

function strip_tags($text)
{
	return $text.replace(/<\/?[^>]+>/gi, '');
}

function isvalidurl(address)
{
	var regexp= /^(http|ftp)\:\/\/\w+([\.\-]\w+)*\.\w{2,4}(\:\d+)*([\/\.\-\?\&\%\#]\w+)*\/?$/i;
  return regexp.test(trim(address));
}

function isvalidmail(address)
{
 var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
 if(reg.test(trim(address)) == false) {alert("Merci d'indiquer une adresse mail valide."); return false;} else return true;
}

function verifiermail(address)
{
	return (isvalidmail(address) && confirm('Confirmez-vous vouloir vous inscrire à la newsletter de RockEnFolie ?'));
}

function envoyermail(service, nom, mail, message, code, sujet, md5)
{
   $.ajax({
   type: "POST",
   url: "../index.php",
   data: "action_mail=envoyermail&service="+encodeURIComponent(trim(service))+"&nom="+encodeURIComponent(trim(nom))+"&mail="+encodeURIComponent(trim(mail))+"&message="+encodeURIComponent(trim(message))+"&code="+encodeURIComponent(trim(code))+"&sujet="+encodeURIComponent(trim(sujet))+"&md5="+encodeURIComponent(trim(md5))+"&R="+new Date(),
         success:
         function(resultat)
         {
         	if (resultat=='ok')
         	{
         		document.getElementById('contact_nom').value='';
         		document.getElementById('contact_mail').value='';
         		document.getElementById('contact_message').value='';
         		document.getElementById('contact_code').value='';
         		document.getElementById('contact_sujet').value='';
         		document.getElementById('contact_service').focus();
         		afficherpiecejointe();
         		alert("Votre mail a bien été transmis à notre équipe.\n\nUne réponse vous sera adressée dans les prochaines heures.");
         	}
         	else alert(resultat);
         }
   });
}

function details_titre(id_titre)
{
   $.ajax({
   type: "POST",
   url: "../../index.php",
   data: "action=infos_titre&id="+id_titre+"&R="+new Date(),
         success:
         function(resultat)
         {
					 document.getElementById("details_titre").innerHTML=resultat;
         }
   });
}

function ouvrirplayer(lien)
{
	var width = 600,
		height = 250,
		left = Math.round( (screen.width - width) / 2 ),
		top = Math.round( (screen.height - height) / 2 );

	window.open(lien,'RockEnFolieAudio','toolbar=0,menubar=0,resize=0,location=0,scrollbars=0,width=' + width + ',height=' + height + ',top=' + top + ',left=' + left);
}

function Asc(String)
{
	return String.charCodeAt(0);
}

function Chr(AsciiNum)
{
	return String.fromCharCode(AsciiNum);
}

function ReplaceAll(Source,stringToFind,stringToReplace)
{
	var temp = Source;
	var index = temp.indexOf(stringToFind);
	while(index != -1)
	{
 		temp = temp.replace(stringToFind,stringToReplace);
 		index = temp.indexOf(stringToFind);
	}
	return temp;
}

var rechercheencours=false;
var pagecourante=1;

function publier_commentaire(estmoderateur)
{		
	var urlcommentaire;
	if (document.getElementById('commentaire_artiste')) artiste_id=document.getElementById('commentaire_artiste').value; else artiste_id=-1;
	if (document.getElementById('commentaire_concert')) concert_id=document.getElementById('commentaire_concert').value; else concert_id=0;
	if ((artiste_id>=0 && concert_id==0) || (artiste_id==0 && concert_id>0))
	{
		if (trim(document.getElementById('commentaire_pseudo').value).length>=2 && trim(document.getElementById('commentaire_pseudo').value).length<=30)
		{
			if (isvalidmail(trim(document.getElementById('commentaire_mail').value)))
			{
				if (trim(document.getElementById('commentaire_url').value)=='' || isvalidurl(trim(document.getElementById('commentaire_url').value)) || isvalidurl('http://'+trim(document.getElementById('commentaire_url').value)) )
				{
					if (trim(document.getElementById('commentaire_url').value)!='')
					{
						urlcommentaire=trim(document.getElementById('commentaire_url').value);
						if (!isvalidurl(trim(document.getElementById('commentaire_url').value))) urlcommentaire='http://'+urlcommentaire;														
					} else urlcommentaire='';
					if (trim(document.getElementById('commentaire_commentaire').value).length>=2 && trim(document.getElementById('commentaire_commentaire').value).length<=1024)
					{						
						if (trim(document.getElementById('commentaire_code').value)!='')
						{						
							if (document.getElementById('commentaire_mail_commentaires') && document.getElementById('commentaire_mail_commentaires').checked) mail_commentaire=1; else mail_commentaire=0;
							if (confirm("Confirmez-vous l'envoi de votre message à notre équipe ?"))
							{
								document.getElementById('commentaire_bouton').disabled=true;
     						$.ajax({
   							type: "POST",
   							url: "../../index.php",
   							data: "action=publier_commentaire&artiste_id="+artiste_id+"&concert_id="+concert_id+"&pseudo="+encodeURIComponent(trim(document.getElementById('commentaire_pseudo').value))+"&mail="+encodeURIComponent(trim(document.getElementById('commentaire_mail').value))+"&commentaire="+encodeURIComponent(trim(document.getElementById('commentaire_commentaire').value))+"&code="+document.getElementById('commentaire_code').value+"&code2="+document.getElementById('commentaire_code2').value+"&mail_commentaire="+mail_commentaire+"&ville="+encodeURIComponent(trim(document.getElementById('commentaire_ville').value))+"&url="+encodeURIComponent(trim(urlcommentaire))+"&R="+new Date(),
      	   						success:
         							function(resultat)
         							{
								 				if (resultat=='ok')
					 							{
								 					document.getElementById('cadrecommentaire').innerHTML='';
						 							document.getElementById('publicite').style.display='';
						 							if (estmoderateur!=0) affichercommentaires(pagecourante); else alert('Votre message a été envoyé à notre équipe, merci ! Il sera publié après validation.\n\nNotez que pour un meilleur confort de lecture, les messages mal orthographiés sont rejetés.\n\nPour toute demande veuillez nous contacter en vous rendant dans la rubrique "Contacts".');
					 							}
					 							else
					 							{
						 							alert(resultat);
           								document.getElementById('commentaire_bouton').disabled=false;
					 							}
         							}
   							});
   						}
   					}
   					else
   					{
	     				alert('Merci de renseigner le code de sécurité.');
   						document.getElementById('commentaire_code').focus();
   					}
  				}
    			else
  				{
	   				alert('Merci de renseigner un commentaire valide (entre 2 et 1024 caractères).');
 						document.getElementById('commentaire_commentaire').focus();
  				}
  			}
  			else
    		{
   				alert('Merci de renseigner une URL valide.');
 					document.getElementById('commentaire_url').focus();
  			}
  		}
  		else
 			{
 				document.getElementById('commentaire_mail').focus();
 			}
 		}
		else
		{
			alert('Merci de renseigner un pseudo valide.');
			document.getElementById('commentaire_pseudo').focus();
		}
	}
}

function affichercommentaires(page)
{
	if (document.getElementById('commentaire_artiste')) artiste_id=document.getElementById('commentaire_artiste').value; else artiste_id=-1;
	if (document.getElementById('commentaire_concert')) concert_id=document.getElementById('commentaire_concert').value; else concert_id=0;
	if ((artiste_id>=0 && concert_id==0) || (artiste_id==0 && concert_id>0))
	{
		$.ajax({
		type: "POST",
		url: "../../index.php",
		data: "action=afficher_commentaires&artiste_id="+artiste_id+"&concert_id="+concert_id+"&page="+page+"&R="+new Date(),
 					success:
					function(resultat)
					{
 						pagecourante=page;
						document.getElementById('cadrecommentaires').innerHTML=resultat;
 					}
		});
	}
}

function supprimercommentaire(id)
{
	if (confirm('Confirmez-vous la suppression du commentaire ?'))
	{
		$.ajax({
		type: "POST",
		url: "../../index.php",
		data: "action=supprimer_commentaire&id="+id+"&R="+new Date(),
 					success:
					function(resultat)
					{
 						if (resultat=='ok') affichercommentaires(pagecourante); else alert(resultat);
 					}
		});
	}
}

function all_callback_fnc(var1, var2, var3)
{
 	afficherpiecejointe();
}
		
function afficherpiecejointe()
{
	$.ajax({
	type: "POST",
	url: "../../index.php",
	data: "action=afficher_piecejointe&R="+new Date(),
 				success:
				function(resultat)
				{
					document.getElementById('contact_pj').innerHTML=resultat;
 				}
	});		
}

function supprimerpiecejointe()
{
	if (confirm('Supprimer le fichier joint ?'))
	{
		$.ajax({
		type: "POST",
		url: "../../index.php",
		data: "action=supprimer_piecejointe&R="+new Date(),
 					success:
					function(resultat)
					{
						if (resultat!='ok') alert(resultat);
 						afficherpiecejointe();
 					}
		});		
	}
}

function formaterword(chaine)
{
	return chaine;
	// Obsolète depuis le passage à la version UTF-8 du site
  /*chaine = ReplaceAll(chaine, Chr(8217), "'");  
  chaine = ReplaceAll(chaine, Chr(8211), '-');  
  chaine = ReplaceAll(chaine, Chr(8364), 'Euros');  
  chaine = ReplaceAll(chaine, Chr(187), '"');  
  chaine = ReplaceAll(chaine, Chr(171), '"');  
  chaine = ReplaceAll(chaine, Chr(8216), "'");  
  chaine = ReplaceAll(chaine, Chr(339), "oe");  
  chaine = ReplaceAll(chaine, Chr(8230), "...");	
	return chaine;*/
}

function miseajourvideo_live()
{
   $.ajax({
   type: "POST",
   url: "../../index.php",
   data: "action=etat_videolive&R="+new Date(),
         success:
         function(resultat)
         {
         	if (resultat=='1') document.getElementById('video_live').style.display='block'
         							  else document.getElementById('video_live').style.display='none';
					setTimeout("miseajourvideo_live()", 20000);
         }
   });
}

function ouvrirvideo_live(lien)
{
	var width = 600,
		height = 400,
		left = Math.round( (screen.width - width) / 2 ),
		top = Math.round( (screen.height - height) / 2 );

	window.open(lien,'RockEnFolieVideo','toolbar=0,menubar=0,resize=0,location=0,scrollbars=0,width=' + width + ',height=' + height + ',top=' + top + ',left=' + left);
}

