$.fn.copyTo = function(to) {
    var to = $(to);
    for ( var i = 1; i < arguments.length; i++ )
        to.set( arguments[i], this.get(0)[ arguments[i] ] );
    return this;
};

new function() {
       // $.fn.validate = validate() {};
    $.fn.validate = {
        init: function(o) {
          if(o.name == 'username') { this.username(o) };
          if(o.name == 'password') { this.password(o) };
          if(o.name == 'email') { this.email(o) };
          if(o.name == 'dob') { this.dob(o) };
		  if(o.name == 'cpintervention') { this.cpintervention(o) };
		  if(o.name == 'frequencea') { this.frequencea(o) };
		  if(o.name == 'frequenceb') { this.frequenceb(o) };
		  if(o.name == 'prixa') { this.prixa(o) };
		  if(o.name == 'prixb') { this.prixb(o) };
		  if(o.name == 'besoinprecis') { this.besoinprecis(o) };
		  if(o.name == 'membre') { this.membre(o) };
		  if(o.name == 'statut') { this.statut(o) };
		  if(o.name == 'civilite') { this.civilite(o) };
		  if(o.name == 'nom') { this.nom(o) };
		  if(o.name == 'prenom') { this.prenom(o) };
		  if(o.name == 'adresse') { this.adresse(o) };
		  if(o.name == 'codepostal') { this.codepostal(o) };
		  if(o.name == 'ville') { this.ville(o) };
		  if(o.name == 'telephone1') { this.telephone1(o) };
		  if(o.name == 'telephone2') { this.telephone2(o) };
        },
		
        cpintervention: function(o) {
          var cpintervention  = /^([0-9]){5}/;				// 5 chiffres ; ne peut rester vide
           if (o.value.match(cpintervention)) {
              doSuccess(o);
            } else {
              doError(o,'un code postal a 5 chiffres');
            };
        },
		
		
		  frequencea: function(o) {
          var frequencea  = /^([0-9])/;					// chiffre(s) ; ne peut rester vide
           if (o.value.match(frequencea)) {
              doSuccess(o);
            } else {
              doError(o,'le nombre d\'heures est incorrect');
            };
        },
		
		frequenceb: function(o) {
		  var frequenceb  = /[^0-9]/;					// pas de chiffre ; ne peut rester vide
           if (o.value.match(frequenceb)) {
              doSuccess(o);
            } else {
              doError(o,'la fréquence est incorrecte');
            };
        },
		
		
		besoinprecis: function(o) {
		  var besoinprecis  = /[^a-z]/;					// que des lettres
           if (o.value.match(besoinprecis)) {
              doSuccess(o);
            } else {
              doError(o,'vous devez faire un choix');
            };
        },

		membre: function(o) {
		  var membre  = /[^a-z]/;						// que des lettres
           if (o.value.match(membre)) {
              doSuccess(o,'les membres n\'ont pas à saisir les 9 champs qui suivant');
            } else {
              doError(o,'vous devez faire un choix');
            };
        },
		
		statut: function(o) {
		  var statut = /Particulier|Professionnel|particulier|professionnel/;						// choix
           if (o.value.match(statut)) {
              doSuccess(o);
            } else {
              doError(o,'vous devez choisir: particulier/professionnel');
            };
        },
		
		
		civilite: function(o) {
		  var civilite  = /Mme|Mlle|Mr|mme|mlle|mr/;		// choix
           if (o.value.match(civilite)) {
              doSuccess(o);
            } else {
              doError(o,'vous devez choisir: Mme/Mlle/Mr');
            };
        },
		
		nom: function(o) {
		  var nom = /[^a-z]/;							// que des lettres
           if (o.value.match(nom)) {
              doSuccess(o);
            } else {
              doError(o,'format invalide');
            };
        },
		
		
		prenom: function(o) {
		  var prenom  = /[^a-z]/;						// que des lettres
           if (o.value.match(prenom)) {
              doSuccess(o);
            } else {
              doError(o,'format invalide');
            };
        },
		
		
		adresse: function(o) {
		  var adresse  = "";
           if (o.value.match(adresse)) {
              doSuccess(o);
            } else {
              doError(o,'format invalide');
            };
        },
		
		
		codepostal: function(o) {
		  var codepostal  = /^([0-9])/;					// que des chiffres
           if (o.value.match(codepostal)) {
              doSuccess(o);
            } else {
              doError(o,'un code postal a 5 chiffres');
            };
        },
		
		ville: function(o) {
		  var ville  = /[^a-z]/;						// que des lettres
           if (o.value.match(ville)) {
              doSuccess(o);
            } else {
              doError(o,'format invalide');
            };
        },
		
		
		telephone1: function(o) {
		  var telephone1  = /^([0-9])/;					// que des chiffres
           if (o.value.match(telephone1)) {
              doSuccess(o);
            } else {
              doError(o,'un numéro de telephone a 10 chiffres');
            };
        },
		
		
		telephone2: function(o) {
		  var telephone2  = /^([0-9])/;					// que des chiffres
           if (o.value.match(telephone2)) {
              doSuccess(o);
            } else {
              doError(o,'un numéro de téléphone a 10 chiffres');
            };
        },
		
		
		prixa: function(o) {
          var prixa  = /^([0-9])/;						// que des chiffres
           if (o.value.match(prixa)) {
              doSuccess(o);
            } else {
              doError(o,'le nombre d\'heures minimun est incorrect');
            };
        },
		
		prixb: function(o) {
          var prixb  = /^([0-9])/;						// que des chiffres
           if (o.value.match(prixb)) {
              doSuccess(o);
            } else {
              doError(o,'le nombre d\'heures maximum est incorrect');
            };
        },
	
     
		
        username: function(o) {
          var user = /[(\*\(\)\[\]\+\.\,\/\?\:\;\'\"\`\~\\#\$\%\^\&\<\>)+]/;
           if (!o.value.match(user)) {
             doValidate(o);
            } else {
             doError(o,'no special characters allowed');
            };
        },
        password: function(o) {
          var pass = /[(\*\(\)\[\]\+\.\,\/\?\:\;\'\"\`\~\\#\$\%\^\&\<\>)+]/;
           if (!o.value.match(pass)) {
             doValidate(o);
            } else {
             doError(o,'les caractères spéciaux ne sont pas autorisés');
            };
        },
        email: function(o) {
          var email  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
           if (o.value.match(email)) {
              doSuccess(o);
            } else {
              doError(o,'email invalide');
            };
        },
        dob: function(o) {
          var dob  = /(0[1-9]|1[012])+\/(0[1-9]|[12][0-9]|3[01])+\/(19|20)\d\d/;
            if (o.value.match(dob)) {
              doSuccess(o);
            } else {
              doError(o,'not a valid date');
            };
        }
     };

     function doSuccess(o) {
              $('#' + o.id + '_img').html('<img src="images/accept.gif" border="0" style="float:left;" />');
              $('#' + o.id + '_li').removeClass("error");
              $('#' + o.id + '_msg').html("");
              $('#' + o.id + '_li').addClass("success");
     }

     function doError(o,m) {
              $('#' + o.id + '_img').html('<img src="images/exclamation.gif" border="0" style="float:left;" />');
              $('#' + o.id + '_li').addClass("error");
              $('#' + o.id + '_msg').html(m);
              $('#' + o.id + '_li').removeClass("success");
     }
     //private helper, validates each type after check
     function doValidate(o) {
        	$('#' + o.id + '_img').html('<img src="images/loading.gif" border="0" style="float:left;" />');
        	$.post('ajax.php', { id: o.id, value: o.value }, function(json) {
                  	eval("var args = " + json);
                        if (args.success == true)
                  	{
                  	  doSuccess(args);
                  	}
                  	else
                  	{
                          doError(args,args.msg);
                  	}
                  });
    };

};
$.fn.match = function(m) {
	$('#' + this.get(0).id + '_img').html('<img src="images/loading.gif" border="0" style="float:left;" />');
	if ($(this).get(0).val() == $(m.match).val()) {
          $('#' + this.get(0).id + '_img').html('<img src="images/accept.gif" border="0" style="float:left;" />');
          $(m.error).removeClass("error");
          $(m.error).addClass("success");
          $('#' + this.get(0).id + '_msg').html("");
        } else {
          $('#' + this.get(0).id + '_img').html('<img src="images/exclamation.gif" border="0" style="float:left;" />');
          $(m.error).addClass("error");
          $(m.error).removeClass("success");
          $('#' + this.get(0).id + '_msg').html("Les 2 mots de passe ne sont pas identiques");
        };
};
$(document).ready(function()
{

  $("//[@class=validated]/input").blur(function() {
          $(this).validate.init(this);
  });
  

  $("#confirmpass").blur(function() {
          $(this).match({match: '#password', error: '#confirmpass_li'});
  });


  $("#password").keyup(function() {
          $(this).copyTo("#password_copy","value");
  });

  // This Used To Be HOVER, But I.E. Didnt Like It
  //$(".form li").hover(function(){$(this).addClass("selected");},function(){$(this).removeClass("selected");});
  $(".form li").mouseover(function() {
          $(this).addClass("selected");
  });
  $(".form li").mouseout(function() {
          $(this).removeClass("selected");
  });
});