// requires jQuery

if (!('tangane' in this)) this.tangane = {};

tangane.prehome = function(trad)
{
  this.traductions = trad;  
  this.erreurs     = [];
  this.valeurs     = [];
  
  var self = this;
  
  // Installer les callbacks lorsque la page est chargee
  
  $(function(){   
    
    var i;    
    
    self.inscription = 
    {
      pseudo  : $('#inscription input[name="pseudo"]'),
      mail    : $('#inscription input[name="mail"]'),
      pays    : $('#inscription select[name="pays"]'),
      sexe    : $('#inscription select[name="sexe"]'),
      usr_birthday    : $('#inscription select[name="usr_birthday"]'),
      cherche : $('#inscription select[name="cherche"]'),
      code    : $('#inscription input[name="code"]'),
      check   : $('#inscription input[name="check"]')
    };
    
    self.errors = 
    {
      pseudo  : $('#error-pseudo'),
      mail    : $('#error-mail'),
      pays    : $('#error-pays'),
      sexe    : $('#error-sexe'),
      cherche : $('#error-cherche'),
      code    : $('#error-code'),
      check   : $('#error-check'),
      login   : $('#error-login')
    };
    
    self.connexion =
    {
      erreur : $('#connexion div.error'),
      mail   : $('#connexion input[name="mail"]'),
      pass   : $('#connexion input[name="pass"]')
    };
    
    $('#inscription').submit(
      function(){return self.checkInscription()}
    );    
    $('#connexion').submit(
      function(){return self.checkConnexion()}
    );         
    
    for ( i in self.erreurs ) {
      self.afficheErreur(self.erreurs[i]);
    }
    
    for ( i in self.valeurs ) {
      self[self.valeurs[i].form][self.valeurs[i].field].val(self.valeurs[i].val);
    }
  });
};

tangane.prehome.prototype.createSelectBirthday = function(obj)
{
	var date = new Date();
	var year = date.getFullYear();
	
	var content='';
	for (var i = 1910;i<=year-18;i++) {
		if (i == 1970)
			content += '<option value="'+ i +'" selected>';
		else 
			content += '<option value="'+ i +'">';
		
		content +=i +'</option>';
	}
	
	$(function(){
		$('#usr_birthday').html(content);
	});
};

tangane.prehome.prototype.afficheErreur = function(data)
{
  this.errors[data.form].html(data.text);      
};

tangane.prehome.prototype.valeur = function(form, field, val)
{
  this.valeurs.push({form:form,field:field,val:val});  
  return this;
};

tangane.prehome.prototype.erreur = function(form, txt)
{
  this.erreurs.push({form:form,text:txt});
  return this;
};

tangane.prehome.prototype.checkInscription = function()
{
  var success = true;
  
  for (var e in this.errors)    
    this.errors[e].html('');
  
  if (!this.inscription.check.is(':checked')) {
    this.afficheErreur({
      form : 'check',
      text : this.traductions.accepteConditions
    });
    
    success = false;
  }
  
  var pseudo = this.inscription.pseudo.val();
  
  if (pseudo.replace(/[^_0-9a-zA-Z]/g,'') != pseudo || pseudo.length < 2) {
        
    this.afficheErreur({
      form : 'pseudo',
      text : this.traductions.mauvaisPrenom
    });
    
    success = false;
  }
  
  var code = this.inscription.code.val();
  
  if (code.replace(/[^0-9]/g,'') != code || code.length < 2) {
    
    this.afficheErreur({
      form : 'code',
      text : this.traductions.mauvaisCode
    });
    
    success = false;
  }
  if (this.inscription.pays.val()==0) {
    
    this.afficheErreur({
      form : 'pays',
      text : this.traductions.paysObligatoire
    });
    
    success = false;
  }
  
  if (this.inscription.sexe.val() == 0) {
        
    this.afficheErreur({
      form : 'sexe',
      text : this.traductions.sexeObligatoire
    });
    
    success = false;
  }
    
  if (this.inscription.cherche.val() == 0) {
        
    this.afficheErreur({
      form : 'cherche',
      text : this.traductions.chercheObligatoire
    });
    
    success = false;
  }
  
  var mail = this.inscription.mail.val();
  
  if (mail.search(/.@./) == -1) {
    
    this.afficheErreur({
      form : 'mail',
      text : this.traductions.mauvaisMail
    });
    
    success = false;
  }
  
  return success;
};

tangane.prehome.prototype.checkConnexion = function()
{
  var success = true;
    
  for (var e in this.errors)    
    this.errors[e].html('');
    
  if (this.connexion.pass.val().length == 0){
        
    this.afficheErreur({
      form : 'login',
      text : this.traductions.mauvaispass
    });
    
    success = false;
  }
  
  var mail = this.connexion.mail.val();
  
  if (mail.search(/.@./) == -1) {
    
    this.afficheErreur({
      form : 'login',
      text : this.traductions.mauvaisMail
    });
    
    success = false;
  };
 
  return success;
};
