// JavaScript Document

function menu(menu, parent){
	if($(menu) != undefined){
		if($(parent) != undefined){
			fermerOuvert(parent);
		}
		Effect.toggle(menu,'BLIND',{duration:0.3, queue:{scope:'myscope', position:'end'}});
		return false;
	}else{
		return true;	
	}
}
function multiplierPrix(quantite,cat,prix){
	 quantite = parseInt($F(quantite));
	 prixTotal = quantite*parseFloat(prix);
	 $(cat).value = prixTotal;
	  prix1 = parseFloat($('prix1').value);
	   prix2 = parseFloat($('prix2').value);
	    prix3 = parseFloat($('prix3').value);
		prix4 = parseFloat($('prix4').value);
		 prix5 = parseFloat($('prix5').value);
	 	total = parseFloat(prix1+prix2+prix3+prix4+prix5);
	 $("total").value = total;
}

	
	function verifForm(formulaire){
		elements=$$('.obli');
		toto=true;
		elements.each(function(n){
		if($F(n)==""){
		n.focus();
		alert("Tous les champs avec une * sont obligatoires !")
		toto=false;
		throw $break;
		throw $break;
		}
		});
		return toto;
	}

function fermerOuvert(parent){
	menus = document.getElementsByClassName('ssMenu',parent);
	for(i=0; i<menus.length ; i++){
		//$('message').innerHTML += menus[i]+"<br />\n";
		if(menus[i].style.display != 'none'){
			Effect.toggle(menus[i],'BLIND',{duration:0.3,  queue:{scope:'myscope', position:'end'}});
		}
	}
}
function ajouterFavoris() {
	if ( navigator.appName != 'Microsoft Internet Explorer' )
		{ window.sidebar.addPanel("Site Officiel du Stade Toulousain","http://www.stadetoulousain.fr",""); }
	else { window.external.AddFavorite("http://www.stadetoulousain.fr","Site Officiel du Stade Toulousain"); }
}
function ajouterFavorisEN() {
	if ( navigator.appName != 'Microsoft Internet Explorer' )
		{ window.sidebar.addPanel("Stade Toulousain Offical Web Site","http://www.stadetoulousain.fr",""); }
	else { window.external.AddFavorite("http://www.stadetoulousain.fr","Site Officiel du Stade Toulousain"); }
}

function ajaxWaiting(){
	$('ajaxContent').update('<div id="ajaxWainting"> </div>');
}

function initAjaxBox(titre, classe){
	if(!classe){
		classe = '';
	}
	if(!$('ajaxContainer')){	
	html = '<div id="overlay"></div><div id="ajaxContainer" class="cadre noLogo '+classe+'"><div id="ajaxCloser" onclick="cacherOverlay();">Fermer</div><h2 id="ajaxTitre">'+titre+'</h2><div id="ajaxContent"></div><div class="finCadre">&nbsp;</div></div>';
	element = document.body;
	new Insertion.Top(element, html);
	}else{
		$('ajaxTitre').update(titre);	
	}
	hideSelectBoxes();
	hideFlash();
	// création de l'overlay et du conteneur
	
	ajaxWaiting();
	var arrayPageSize = getPageSize();
	Element.setWidth('overlay', arrayPageSize[0]);
	Element.setHeight('overlay', arrayPageSize[1]);
	Event.observe('overlay', 'click', cacherOverlay);
	// affichage des éléments
	new Effect.Appear('overlay', { duration: 0.1, from: 0.0, to: 0.8});
	new Effect.Appear('ajaxContainer', { duration: 0.1});
	new Draggable('ajaxContainer',{handle:'ajaxTitre'});
}


function recommander(){
	initAjaxBox('Recommander le site &agrave; un ami');
	// Ajax du formulaire
	var url = 'ajax/envoyer-ami.php';
	var target = 'ajaxContent';
	var myAjax = new Ajax.Updater(target, url);
	//new Draggable('ajaxContainer',{handle:'ajaxTitre'});
}
function doEnvoyerAmi(){
	
	nomFrom =   $F('nomSender');
	emailFrom = $F('emailSender');
	nomTo =     $F('nomReceiver');
	emailTo =   $F('emailReceiver');
	copie =   $F('copie');
	var url = 'ajax/do-envoyer-ami.php';
	var target = 'ajaxContent';
	var page = window.location;
	var pars = 'nomSender='+nomFrom+'&emailSender='+emailFrom+'&nomReceiver='+nomTo+'&emailReceiver='+emailTo+"&copie="+copie+"&page="+escape(page);
	//alert(pars);
	var myAjax = new Ajax.Updater(target, url, {method: 'POST', parameters: pars, onLoading : ajaxWaiting()});
	return false;
}
function newsletter(){
	initAjaxBox('Inscription à la newsletter');
	
	
	// Ajax du formulaire
	var url = 'ajax/newsletter.php';
	var target = 'ajaxContent';
	var myAjax = new Ajax.Updater(target, url);
	//new Draggable('ajaxContainer',{handle:'ajaxTitre'});
}
function doNewsletter(){
	
/*	nom =   $F($('nom'));
	prenom = $F($('prenom'));
	email =     $F($('email'));
	adresse =   $F($('adresse'));
	codePostal =   $F($('codePostal'));
	ville =   $F($('ville'));
	age =   $F($('age'));
	tel =   $F($('tel'));
	activite =   $F($('activite'));
	mobile =   $F($('mobile'));*/
	
	valeur = $('newsletter').serialize()
	
	var url = 'ajax/do-newsletter.php';
	var target = 'ajaxContent';
	var page = window.location;
	var pars = valeur+'&page='+page;
	//alert(pars);
	var myAjax = new Ajax.Updater(target, url, {method: 'post', parameters: pars, onLoading : ajaxWaiting()});
	return false;
}

function cacherOverlay(){
	new Effect.Fade('overlay', { duration: 0.3 });
	new Effect.Fade('ajaxContainer', { duration: 0.3 });
	showSelectBoxes();
	showFlash();
}

function ChargeActu(image, div, imagette){
	imagettes = $('actuPhoto').getElementsByClassName('imagette');
	for(i=0; i<imagettes.length; i++){
		makevisible(imagettes[i],1);
	}
	makevisible(imagette,0);
	//$('viewer').src = image;
	if (document.all){
		$('viewer').style.filter="blendTrans(duration=0.300)";
		//$('viewer').style.filter="blendTrans(duration=crossFadeDuration)";
		$('viewer').filters.blendTrans.Apply();
	}
	$('viewer').src = tab_image[image-1].src;
	if (document.all){
		$('viewer').filters.blendTrans.Play()
	}
	textesActus = $('texteActu').immediateDescendants();
	nodes = $A(textesActus);
	nodes.each(function(node){
		node.hide();
	});
	/*for ( keyVar in textesActus ) {
	  alert(textesActus[keyVar].name);
	  textesActus[keyVar].hide();
	}*/
	
	$(div).show();
}

function makevisible(cur,which){
	if(document.getElementById)
		{
		if (which==0)
			{
			if(document.all)
				cur.filters.alpha.opacity=100
			else
				cur.style.setProperty("-moz-opacity", 1, "");
			}
		else
			{
			if(document.all)
				cur.filters.alpha.opacity=30
			else
				cur.style.setProperty("-moz-opacity", .30, "");
			}
		}
	}
function afficherClassement(afficher,masquer){
	if($(masquer).visible()){
		Effect.BlindUp(masquer, { duration: 0.3, queue: 'end' });
		Effect.BlindDown(afficher, { duration: 0.3, queue: 'end' });
	}
	return false;
}

function AfficherJoueur(id) {
	//alert("afficher joueur : "+id);
	new Effect.Fade('fiche',{duration:0.3, queue:"front"});
	setTimeout("getFiche('"+id+"')",200);
	new Effect.Appear('fiche',{duration:0.3, queue:"end"});
	new Effect.ScrollTo('finFiche',{duration:1});

}
function AfficherJoueurEn(id) {
	//alert("afficher joueur anglais : "+id);
	new Effect.Fade('fiche',{duration:0.3, queue:"front"});
	setTimeout("getFicheEn('"+id+"')",200);
	new Effect.Appear('fiche',{duration:0.3, queue:"end"});
	new Effect.ScrollTo('finFiche',{duration:1});

}

function getEncadrement(){
	var url = 'ajax/encadrement.php';
	var pars = '';
	var target = 'conteneurFiche';
	var myAjax = new Ajax.Updater(target, url, {method: 'get', parameters: pars});
}

function getFiche(id){
	//alert("get fiche : "+id);
	var url = 'ajax/fiche-joueur.php';
	var pars = 'idJoueur='+id;
	var target = 'conteneurFiche';
	var myAjax = new Ajax.Updater(target, url, {method: 'get', parameters: pars});
}

function getFicheEn(id){
	//alert("get fiche : "+id);
	var url = 'ajax/fiche-joueur_anglais.php';
	var pars = 'idJoueur='+id;
	var target = 'conteneurFiche';
	var myAjax = new Ajax.Updater(target, url, {method: 'get', parameters: pars});
}

function texteDefile() {
   if (!defile) defile = document.getElementById('defile');
   if (defile) {
      if(pscrnt < ( - defile.offsetHeight) ){
         pscrnt = psinit;
                } else {
         pscrnt+= -1; // pixel par deplacement
      }
      defile.style.top = pscrnt+"px";
   }
}
function lancerFlash(){
	interval = setInterval("texteDefile()",30); // delai de deplacement 
}
function goPage(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}
function goPageQuizz(targ,selObj,restore){ //v3.0
  eval(targ+".location='quizz-71.html?categorie="+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}
function goPageQuizz2(targ,selObj,restore){ //v3.0
  eval(targ+".location='questionnaireQuizz-64.html?id_quizz="+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}
function goElection(targ,selObj,restore){ //v3.0
  eval(targ+".location='election-69.html?matchVote="+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

function visuJoueur(id){
	//alert(id);	
	var url = 'ajax/fiche-joueur.php';
	var pars = 'idJoueur='+id;
	var target = 'conteneurFiche';
	var myAjax = new Ajax.Updater(target, url, {method: 'get', parameters: pars});
}


function controle () {
document.quizz.submit.checked = false;
var reponse = 0;
elements = document.quizz.length;
var form = $('quizz');


//alert($('quizz').getInputs('radio').length );
nb_question = $('nb_question').value;

var i;
	for (i = 0; i < elements; i++) {
		if (document.quizz.elements[i].checked) {
reponse = reponse + eval(document.quizz.elements[i].checked);
       }
    }
 
    if (reponse == nb_question) {
   	  return true;
    } else {
      //if (reponse <= 9) {
	alert("Merci de répondre à toutes les questions");
	return false;
     /* } else {
	alert("Jouer au quizz");
	return false;
      }*/
    }
  }
/*
function changeCouleur(ligne, couleur){
	ligne.style.backgroundColor = couleur;
}*/
function changeCouleur(ligne){
	this.color = ligne.style.backgroundColor;
	ligne.style.backgroundColor ="#565656";
}

function rollTitre(ligne){
	this.color = ligne.style.backgroundColor;
	ligne.style.backgroundColor ="#2B1312";
}

function remetCouleur(ligne){
	//ligne.bgColor = '#FFFFFF';
	ligne.style.backgroundColor = this.color;
}
function afficherMusee(menu){
	fermerOuvertMusee();
	Effect.toggle(menu,'BLIND',{duration:0.3, queue:{scope:'myscope', position:'end'}});
	return false;

}
function fermerOuvertMusee(){
	menus = document.getElementsByClassName('musee');

	for(i=0; i<menus.length ; i++){
		//$('message').innerHTML += menus[i]+"<br />\n";
		if(menus[i].style.display != 'none'){
			Effect.toggle(menus[i],'BLIND',{duration:0.3,  queue:{scope:'myscope', position:'end'}});
		}
	}
}

function creerCompte(){
	initAjaxBox('Créer un compte');
	
	// Ajax du formulaire
	var url = 'ajax/creerCompte.php';
	var target = 'ajaxContent';
	var myAjax = new Ajax.Updater(target, url);
}

function doCreerCompte(){
	valeur = $('creerCompte').serialize()
	
	var url = 'ajax/do-creer-compte.php';
	var target = 'ajaxContent';
	var page = window.location;
	var pars = valeur+'&page='+page;
	//alert(pars);
	var myAjax = new Ajax.Updater(target, url, {method: 'post', parameters: pars, onLoading : ajaxWaiting()});
	return false;
}

function verifForm(form){
	elements = $$('.obli');
	toto = true;
	elements.each(function(n){
		if($F(n) == ""){
			n.focus();
			
			alert("Certains champs sont obligatoires !")
			toto = false;
			throw $break;
			throw $break;
			
		}
	});
	
	return toto;
}

function verifDateNaissanceCop(date){
	var champDate = date;
	date = date.value;
	var date_regexp=/^(\d{1,2}\/){2}\d{4}$/;
	if (!date.match(date_regexp)){
		//message('Veuillez entrer une date valide (format jj/mm/aaaa).');
		return false;
	}else{
		var now = new Date();
		var d = date.split('/')	;
		
		var now = new Date();
		var nowSec = now.getTime();
		
		
		var jour = d[0];
		var mois = d[1]-1;
		var annee = d[2];
		
		var dateNaiss = new Date(annee, mois, jour);
		
		var age = (nowSec - dateNaiss.getTime());
		
		var dateAgeMax = new Date(now.getFullYear() - 12, mois, jour);
		var dateAgeMin = new Date(now.getFullYear() - 6, mois, jour);
	
		if(dateNaiss > dateAgeMax && age < dateAgeMin){
			//alert("OK max : "+dateAgeMax+" agemin : "+dateAgeMin);
			return true;
		}else{
			alert("Votre enfant doit être agé de 6 à 12 ans pour venir au Cop'");
			//return false;	
		}
	}
		
}
