$.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;
};
function checkMandatoryFields(mand){
    var allFilled = true;
for(var j=0; j<mand.length; j++){
var fld=document.getElementById(mand[j]);
if (!fld.value) {
//document.emailForm.log.value+="**"+fld.name; 
allFilled = false;} 
                       }
    if(!allFilled){
	      $("#NewsButton").attr("disabled","disabled");
    }
}
new function() {
       // $.fn.validate = validate() {};
    $.fn.validate = {
        init: function(o) {
          var mand=new Array("EMAIL","EMAIL2");
          if(o.id == 'EMAIL') { this.email(o);  checkMandatoryFields(mand); };
          if(o.id == 'EMAIL2') { this.email(o);  checkMandatoryFields(mand); };
        },
        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);
	      $("#NewsButton").removeAttr("disabled");
            } else {
              doError(o,'not a valid email');
	      $("#NewsButton").attr("disabled","disabled");
            };
	    
        },
        exist: function(o) {
          var email  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
          if (o.value.length > 5) {
              doSuccess(o);
	      $("#NewsButton").removeAttr("disabled");
            } else {
              doError(o,'not a valid email');
	      $("#NewsButton").attr("disabled","disabled");
            };        
        
        }

     };

     function doSuccess(o) {
              $('#' + o.id + '_img').html('<img src="/css/accept.gif" border="0" />');

     }

     function doError(o,m) {
              $('#' + o.id + '_img').html('<img src="/css/exclamation.gif" border="0"  />');
     }
     //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="/css/loading.gif" border="0" style="float:left;" />');
	if ($(this).get(0).val() == $(m.match).val()) {
          $('#' + this.get(0).id + '_img').html('<img src="/css/accept.gif" border="0" style="float:left;" />');
          $('#' + this.get(0).id + '_msg').html("");
        } else {
          $('#' + this.get(0).id + '_img').html('<img src="/css/exclamation.gif" border="0" style="float:left;" />');
          $('#' + this.get(0).id + '_msg').html("The passwords don't match, please try again");
        };
};
