$(function() {

  	$('input.textField').css({backgroundColor:"#FFFFFF"});
  	$('input.textField').focus(function(){    $(this).css({backgroundColor:"#ffffff"});  });
  	$('input.textField').blur(function(){    $(this).css({backgroundColor:"#ffffff"});  });
  	
  	$("form[name=formAdherents]").submit(function() {

		var name = $("input#idtCli").val();
		if (name == "") {
		  	return false;
		}
		var mdpasse = $("input#mdp").val();
		if (mdpasse == "") {
		  	return false;
		}
		$.ajax({
		  	type: "POST",
		  	url: "http://www.chatelet-theatre.com/2009-2010/inc/identification.php",
		  	data: ({idtCli : name, mdp:mdpasse}),
		  	async: false,
			success: function(retour) {
				if (retour=="Erreur") $("#errorIdent").fadeIn("slow", function(){$("#errorIdent").fadeOut("slow")});
				else { 
					window.location="http://www.chatelet-theatre.com/2009-2010/adherents,a,11";
					//location.reload() ;
				}
     		},
     		error: function (retour) { $("#errorIdent").fadeIn("slow", $("#errorIdent").fadeOut("fast"));}
		});
		
		return false;
		
	});
	
	
	$("form[name=formNewsletter]").submit(function() {

		var mail = $("input#mailNewsletter").val();
		if (mail == "") {
		  	return false;
		}
		
		$.ajax({
		  	type: "POST",
		  	url: "http://www.chatelet-theatre.com/2009-2010/inc/YMLP/ContactsAjouter.php",
		  	data: ({mail : mail}),
		  	async: false,
			success: function(retour) {
				
				if (retour=="errorMail") $("#errorAdresseNewsletter").fadeIn("slow", function(){$("#errorAdresseNewsletter").fadeOut("slow")});
				else { 
					
					$("#OKNewsletter").fadeIn("slow", function(){$("#OKNewsletter").fadeOut("slow")});
				}		
     		},
     		error: function (retour) { $("#errorInscripNewsletter").fadeIn("slow", $("#errorInscripNewsletter").fadeOut("fast"));}
		});
		
		return false;
		
	});
	$("form[name=formDesinscriptionNewsletter]").submit(function() {

		var mailDesinscription = $("input#mailDesinscriptionNewsletter").val();
		if (mailDesinscription == "") {
		  	return false;
		}
		
		$.ajax({
		  	type: "POST",
		  	url: "http://www.chatelet-theatre.com/2009-2010/inc/YMLP/ContactsDesabonner.php",
		  	data: ({mailDesinscription : mailDesinscription}),
		  	async: false,
			success: function(retour) {
				
				if (retour=="errorMail") $("#errorDesAdresseNewsletter").fadeIn("slow", function(){$("#errorDesAdresseNewsletter").fadeOut("slow")});
				else { $("#OKDesNewsletter").fadeIn("slow", function(){$("#OKDesNewsletter").fadeOut("slow")});}		
     		},
     		error: function (retour) { $("#errorDesInscripNewsletter").fadeIn("slow", $("#errorDesInscripNewsletter").fadeOut("fast"));}
		});
		
		return false;
		
	});
	
	$("form[name=ident_collectivite]").submit(function (){
			var sendpass=$("#passwd_collec").val();
			$.ajax({
					type: "POST",
					url: "http://www.chatelet-theatre.com/2009-2010/inc/identification_collectivites.php",
					data: ({passwd_collec:sendpass}),
					async: false,
					success: function(retour) {
							var reg=new RegExp("^ok$","i");
							if(retour.match(reg)){
								// on mets un message ok et on vide tout les valeurs saisie
								$("#passwd_collec").val('');
								$("#form_collect_identif").html("");
								$("#OKIdentCollect").fadeIn("slow");
								window.location="http://www.chatelet-theatre.com/2009-2010/collectivites/";
								return false;
							}else{
								$("#errorIdentCollect").fadeIn("slow", function(){ 
									setTimeout($("#errorIdentCollect").fadeOut("slow"),50000);
									});
								return false;
							}
							
					}
				});
			return false;
	});
	
	
});



function check_search_form(){
	var searchinput=$('#form_name').val();
	
	if(searchinput=="ex. : Norma, Placido"){
		return false;
	}else{
		$("form[name=formRecherche]").submit();
	}
}

function send_msg(idspectacle){
	var sendmail=$("#mail").val();
	var senddesti=$("#destinataire").val();
	var sendmessage=$("#message").val();
	var sendnl=0;
	var sendpromo=0;
	if($('#newsletterYes').is(':checked')){
		sendnl=1;
	}
	if($("#promoYes").is(':checked')){
		sendpromo=1;
	}
	$.ajax({
		  	type: "POST",
		  	url: "http://www.chatelet-theatre.com/2009-2010/inc/partage.php",
		  	data: ({id_spectacle : idspectacle,mail:sendmail,destinataire:senddesti,message:sendmessage,newsletter:sendnl,promo:sendpromo}),
		  	async: false,
			success: function(retour) {
					var reg=new RegExp("^ok$","i");
					if(retour.match(reg)){
						// on mets un message ok et on vide tout les valeurs saisie
						$("#mail").val('');
						$("#destinataire").val('');
						$("#message").val('');
						$('#newsletterYes').attr('checked', false);
						$("#promoYes").attr('checked', false);
						//$("#ResultatEnvoi").html("Votre message a bien été envoyé");
						$("#ResultatEnvoi").fadeIn("slow", function(){ 
							setTimeout($("#ResultatEnvoi").fadeOut("slow"),50000);
							});
					}else{
						alert(retour);
						return false;
					}
					
     		}
		});

}

function ask_brochure(){
	
	
	var sendmail=$("#mail").val();
	var sendnom=$("#nom").val();
	var sendprenom=$("#prenom").val();
	var sendnumero=$("#numero").val();
	var sendrue=$("#rue").val();
	var sendcomplement_adresse=$("#complement_adresse").val();
	var sendville=$("#ville").val();
	var sendcodepostal=$("#code_postal").val();
	var sendraison_sociale=$("#raison_sociale").val();
	var sendpays=$("#pays").val();
	var sendtelephone=$("#telephone").val();
	
	
	var sendnl=0;
	var sendpromo=0;
	if($('#newsletterYes').is(':checked')){
		sendnl=1;
	}
	if($("#promoYes").is(':checked')){
		sendpromo=1;
	}
	
	var nb_tranche_age=$(".tranche_age").size();
	var sendtranche_age='';
	for (i=1; i<=nb_tranche_age; i++){
		if($('#tranche_age_'+i).is(':checked')){
			sendtranche_age=i;
		}
	}
	
	var nb_manif_culturelle=$(".manif_culturelle").size();
	var sendmanif_culturelle='##';
	for (i=1; i<=nb_manif_culturelle; i++){
		if($('#manif_culturelle_'+i).is(':checked')){
			sendmanif_culturelle+=i+"##";
		}
	}
	
	var nb_motivation_spectacle =$(".motivation_spectacle").size();
	var sendmotivation_spectacle='##';
	for (i=1; i<=nb_motivation_spectacle; i++){
		if($('#motivation_spectacle_'+i).is(':checked')){
			sendmotivation_spectacle+=i+"##";
		}
	}
	
	var nb_mode_resa =$(".mode_resa").size();
	var sendmode_resa='##';
	for (i=1; i<=nb_mode_resa; i++){
		if($('#mode_resa_'+i).is(':checked')){
			sendmode_resa+=i+"##";
		}
	}
	
	$.ajax({
		  	type: "POST",
		  	url: "http://www.chatelet-theatre.com/2009-2010/inc/brochure.php",
		  	data: ({mail:sendmail,nom:sendnom,prenom:sendprenom,numero:sendnumero,rue:sendrue,complement_adresse:sendcomplement_adresse,code_postal:sendcodepostal,ville:sendville,pays:sendpays,raison_sociale:sendraison_sociale,telephone:sendtelephone,newsletter:sendnl,promo:sendpromo,tranche_age:sendtranche_age,manif_culturelle:sendmanif_culturelle,motivation_spectacle:sendmotivation_spectacle,mode_resa:sendmode_resa}),
		  	async: false,
			success: function(retour) {
					var reg=new RegExp("^ok$","i");
					if(retour.match(reg)){
						// on mets un message ok et on vide tout les valeurs saisie
						$("#mail").val('');
						$("#nom").val('');
						$("#prenom").val('');
						$("#numero").val('');
						$("#rue").val('');
						$("#complement_adresse").val('');
						$("#code_postal").val('');
						$("#ville").val('');
						$("#pays").val('');
						$("#raison_social").val('');
						$("#telephone").val('');
						$('#newsletterYes').attr('checked', false);
						$("#promoYes").attr('checked', false);
						$("#raison_sociale").val('');
						
						$("INPUT[type='checkbox']").attr('checked', false ); 
						$("INPUT[type='radio']").attr('checked', false ); 
						//$("#ResultatEnvoi").html("Votre message a bien été envoyé");
						
						$("#okIdent").fadeIn("slow", function(){ 
							setTimeout($("#okIdent").fadeOut("slow"),100000);
							});
					}else{
						alert(retour);
						return false;
					}					
     		}
		});

}

// scrolling spectacle


function findPos(val) {
       //alert (val);
       obj = val;
       var curleft = document.getElementById(val).offsetLeft || 0;
       var curtop = document.getElementById(val).offsetTop || 0;
       while (obj = obj.offsetParent) {
                curleft += obj.offsetLeft
                curtop += obj.offsetTop
       }
     // alert (curleft);
       return {x:curleft,y:curtop};
   }

var INTERVAL=0;



function smoothie2(test) {
	// alert (test);
     var test=test.substring(test.indexOf("#")+1);
   // alert (test);
    //var test=test.substring(1, test.indexOf(";"));
	//alert("#"+test);
	//alert ($("#"+test));
	var pos=findPos(test);
	
    var destx = pos.x; 
    var desty = pos.y
   //alert (destx+" x "+desty);
    //var thisNode = $li;
    /*while (thisNode.offsetParent && (thisNode.offsetParent != document.body)) {
      thisNode = thisNode.offsetParent;
      destx += thisNode.offsetLeft;
      desty += thisNode.offsetTop;
    }*/
  
   cypos = getCurrentYPos();


    ss_stepsize = parseInt((desty-cypos)/25);
   // desty=300;
   // cypos=0;
	//ss_stepsize=12;
	//alert (parseInt(ss_stepsize));
    
    
    //alert('scrollWindow('+ss_stepsize+','+desty+',"'+$li+'")');
    INTERVAL = setInterval('scrollWindow('+ss_stepsize+','+desty+',"'+test+'")',10);
	
	return false;
	
}

function   getCurrentYPos() {
    if (document.body && document.body.scrollTop)
      return document.body.scrollTop;
    if (document.documentElement && document.documentElement.scrollTop)
      return document.documentElement.scrollTop;
    if (window.pageYOffset)
      return window.pageYOffset;
    return 0;
 }
 
 function scrollWindow(scramount,dest,anchor) {
    wascypos = getCurrentYPos();
    isAbove = (wascypos < dest);
    window.scrollTo(0,wascypos + scramount);
    iscypos = getCurrentYPos();
    isAboveNow = (iscypos < dest);
    if ((isAbove != isAboveNow) || (wascypos == iscypos)) {
      // if we've just scrolled past the destination, or
      // we haven't moved from the last scroll (i.e., we're at the
      // bottom of the page) then scroll exactly to the link
      window.scrollTo(0,dest);
      // cancel the repeating timer
      clearInterval(INTERVAL);
      // and jump to the link directly so the URL's right
      location.hash = anchor;
    }
  }
