$.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) {
        	//$(o+":contains(' ')")?alert('has space'):alert('no space');
        	c= $(o).attr('class');
          if(c.substring(0,c.indexOf(' ')) == 'username') { this.username(o) };
          if(c.substring(0,c.indexOf(' ')) == 'mandatory') { this.mandatory(o) };
          if(c.substring(0,c.indexOf(' ')) == 'pwd') { this.password(o) };
          if(c.substring(0,c.indexOf(' ')) == 'email') { this.email(o) };
          if(c.substring(0,c.indexOf(' ')) == 'dob') { this.dob(o) };          
          if(c.substring(0,c.indexOf(' ')) == 'country') { this.country(o) };
          if(c.substring(0,c.indexOf(' ')) == 'any') { this.any(o) };
          if(c.substring(0,c.indexOf(' ')) == 'pseudo_valid') { this.pseudoValid(o) };
          /*if(o.name == 'password') { this.password(o) };
          if(o.name == 'email') { this.email(o) };
          if(o.name == 'dob') { this.dob(o) };*/
        },
        pseudoValid: function(o) {
	        //sms = minString($(o),5);
	        if(sms!=true && $(o).val().length > 0 && $(o).val().length <= 4) {
	        	this.doError(o,sms);
	        	//alert(sms);
	        	return false;
	        }else{
            	this.doSuccess(o);
	        }
        },
        username: function(o) {
        sms = minString($(o),4);
        if(sms!=true) {
        	this.doError(o,sms);
        	return false;
        }
          var user = /[(\*\(\)\[\]\+\.\,\/\?\:\;\'\"\`\~\\#\$\%\^\@\\&\<\>)+]/;
           if (!$(o).val().match(user)) {
             return doValidate(o);
           } else {
           this.doError(o,'Aucun caract�re sp�cial n\'est autorisé');
           return false;
           };
        },
        mandatory: function(o) {
        	if($(o).val()==""){
        		this.doError(o,'Merci de remplir également cette case');
           		return false;
        	}
            this.doSuccess(o);
        },
        password: function(o) {
        sms = minString($(o),4);
        if(sms!=true) {
        	this.doError(o,sms);
        	return false;
        }
          //var pass = /[(\*\(\)\[\]\+\.\,\/\?\:\;\'\"\`\~\\#\$\%\^\&\<\>)+]/;
           //if (!o.value.match(pass)) {
             //doValidate(o);
              this.doSuccess(o);
            //} else {           
            // this.doError(o,'Aucun caractère spécial n\'est autorisé');
            //};
        },
        email: function(o) {        
          var email  = /^([a-zA-Z0-9_\.\-]{2,4})+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
          var emailadd=$('#mail_inscription').val();
           if (emailadd.match(email)) {           	
              this.doSuccess('your_email');          
            } else {  
            //alert('kk');          
              this.doError(('your_email'),'not a valid email');             
            };
        },
        dob: function(o) {
          var dob  = /(0[1-9]|1[012])+\/(0[1-9]|[12][0-9]|3[01])+\/(19|20)\d\d/;
            if ($(o).val(o) !== "" || $(o).value.match(dob)) {
              this.doSuccess(o);
            } else {
              this.doError(o,'format de date invalide');
              return false;
            };
        },
        any: function(o) {
              this.doSuccess(o);
        },
        country: function(o) {
          var empty  = '';
            if (jQuery.trim(o.value)!=empty) {
              this.doSuccess(o);
            } else {
              this.doError(o,'Merci de choisir '+$(o).attr('alt'));
            };
        },
        doSuccess: function(o) {
              $('#' + o + '_img').html('<img class="img_info" src="/_design/accept.gif" border="0" style="float:left;" />');
              //$('#' + $(o).attr('id') ).removeClass("error");
              $('#' + o + '_msg').html(" ");
              //$('#' + $(o).attr('id') ).addClass("success");
     	},
	     doError: function(o,m) {	     		  	   
	              $('#'+ o +'_img').html('<img class="img_info" src="/_design/exclamation.gif" border="0" style="float:left;" />');
	              //$('#your_email_img') .addClass("error");
	              
	              $('#'+ o +'_msg').html(m);
	              //$('#your_email_smg').removeClass("success");
	     }
     };

};
$.fn.match = function(origin,repeat) {	
	$('#' + $(repeat).attr('id') + '_img').html('<img class="img_info" src="/_design/loading.gif" border="0" style="float:left;" />');
	if(($(repeat).val() == '' || $(origin).val() == '')){
		$('#' + $(repeat).attr('id') + '_img').html('<img class="img_info" src="/_design/exclamation.gif" border="0" style="float:left;" />');
		$(repeat).addClass("error");
		$(repeat).removeClass("success");
		$('#' + $(repeat).attr('id') + '_msg').html("Merci d'indiquer un mot de passe");
	}
	else if ($(origin).val() == $(repeat).val()) {
          $('#' + $(repeat).attr('id') + '_img').html('<img src="/_design/accept.gif" border="0" style="float:left;" />');
          $(repeat).removeClass("error");
          $(repeat).addClass("success");
          $('#' + $(repeat).attr('id') + '_msg').html("");
     } else {
       $('#' + $(repeat).attr('id') + '_img').html('<img class="img_info" src="/_design/exclamation.gif" border="0" style="float:left;" />');
       $(repeat).addClass("error");
       $(repeat).removeClass("success");
       $('#' + $(repeat).attr('id') + '_msg').html("Les mots de passe ne sont pas identiques, merci de recommencer.");
     };
};



$(document).ready(function()
{
  $(".validated").keyup(function() {
          $(this).validate.init(this);
  });
  /*$(".validated").blur(function() {
          $(this).validate.init(this);
  });*/

  $("#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");});
  $(".validated").mouseover(function() {
          $(this).addClass("selected");
  });
  $(".validated").mouseout(function() {
          $(this).removeClass("selected");
  });
  
});
