Event.observe(window, 'load', function(e){	
	gestionPage = new Inscription();
});

var Inscription = Class.create(); 
Inscription.prototype = { 

	initialize: function() {
		this.pseudo = $('pseudo');
		this.mdp = $('mdp') ;
		this.mdp_conf = $('mdp_conf') ;
		this.birth = $('birthyear');
		this.sexe = $('sexe');
		this.country = $('country');
		this.email = $('email');	

		this.pseudo_img = $('pseudo_img');
		this.mdp_img = $('mdp_img') ;
		this.mdp_conf_img = $('mdp_conf_img') ;
		this.birth_img = $('birthyear_img');
		this.sexe_img = $('sexe_img');
		this.country_img = $('country_img');
		this.email_img = $('email_img');	
		
		this.rules = $('rules');
				
		this.form = $('inscription_form');	
		this.init();
		
	},
	
	init: function(){
		Event.observe(this.form,'submit',this.submit.bindAsEventListener(this));
		Event.observe(this.pseudo,'blur',this.checkPseudo.bindAsEventListener(this));		
		Event.observe(this.mdp,'blur',this.checkMdp.bindAsEventListener(this));
		Event.observe(this.mdp_conf,'blur',this.checkMdpConf.bindAsEventListener(this));
		Event.observe(this.birth,'change',this.checkBirth.bindAsEventListener(this));
		Event.observe(this.country,'blur',this.checkCountry.bindAsEventListener(this));
		Event.observe(this.email,'blur',this.checkEmail.bindAsEventListener(this));
		this.control();
	},
	
	control: function()
	{
		if(!this.pseudo.value.isVide())
			this.checkPseudo();
			
		if(!this.email.value.isVide())
			this.checkEmail();
		
		if(!this.mdp.value.isVide())	
		{
			this.checkMdp();		
		}
		this.checkBirth();
		this.checkCountry();		
	},
	
	controlKo: function(elt,img)
	{	
		img.src = img.src.replace('ok','ko');
		elt.addClassName('ko');
	},
	
	controlOk: function(elt,img)
	{
		img.src = img.src.replace('ko','ok');
		elt.removeClassName('ko');
	},	
	
	checkPseudo: function(){
		var login = this.pseudo.value ;
		var t =this ;
  		new Ajax.Request( 'ajax/home/page/inscription.ajax.php',
 		{	method: 'post',
 			postBody: 'action=checkLogin&login='+encodeURIComponent(login),
 			onComplete:function(r){
 				var valide = r.responseText;
		 		if( login.isVide() || valide == 'ko' || login.length<4 || login.length>24)
					t.controlKo(t.pseudo,t.pseudo_img);
				else
					t.controlOk(t.pseudo,t.pseudo_img);				
  			}
 		});		
 		
			
	},
	
	checkMdp: function(){
		var mot_de_passe = this.mdp.value ;
		if( mot_de_passe.isVide() || mot_de_passe.length<4 || mot_de_passe.length>24)
			this.controlKo(this.mdp,this.mdp_img);
		else
			this.controlOk(this.mdp,this.mdp_img);
		this.checkMdpConf();
	},	

	checkMdpConf: function(){
		var mot_de_passe = this.mdp.value ;
		var confirmation = this.mdp_conf.value ;
		if( confirmation.isVide() || mot_de_passe.isVide() || mot_de_passe != confirmation )
			this.controlKo(this.mdp_conf,this.mdp_conf_img);
		else
			this.controlOk(this.mdp_conf,this.mdp_conf_img);
	},	

	checkBirth: function(){
		var birth = this.birth.value ;
		if( birth.isVide() )
			this.controlKo(this.birth,this.birth_img);
		else
			this.controlOk(this.birth,this.birth_img);
	},
	
	checkCountry: function(){
		var country = this.country.value ;
		if( country.isVide() )
			this.controlKo(this.country,this.country_img);
		else
			this.controlOk(this.country,this.country_img);
	},
	
	checkEmail: function(){
		var email = this.email.value ;
		var t = this ;
  		new Ajax.Request( 'ajax/home/page/inscription.ajax.php',
 		{	method: 'post',
 			postBody: 'action=checkMail&mail='+encodeURIComponent(email),
 			onComplete:function(r){
 				var valide = r.responseText;		
				if(valide=='ko' || !email.isEmail())
					t.controlKo(t.email,t.email_img);
				else
					t.controlOk(t.email,t.email_img);
  			}
 		});			

	},			

	check: function(){
		var cpt = $$('.ko');
		if(cpt.length == 0)
			return true ;
		else 
			return false ;			
	},

	submit: function(e){
	//	if(!this.check()){
	//		e.stop();
	//		alert = new Alert('<p class="failed">Merci de vérifier vos informations, certaines sont incorrects</p>');
	//	}	
		
		if(!this.rules.checked)
		{
			e.stop();
			alert = new Alert('<p class="failed">Vous devez accepter les conditions générale pour pouvoir vous inscrire.</p>');
		}
	}	
}	
	
	
	
	


