Mesh.RegistrationControl = Class.create();
Object.extend(Mesh.RegistrationControl.prototype, {

  initialize:    function(){
                     },
                 
  processReg:    function(options){
                  this.username   = $(options.username);
                  this.passwd     = $(options.passwd);
                  this.cfmpass    = $(options.cfmpass);
                  this.first      = $(options.first);
                  this.last       = $(options.last);
                  this.email      = $(options.email);
                  this.sex        = $(options.sex);
                  this.day        = $(options.day);
                  this.mth        = $(options.mth);
                  this.year       = $(options.year);
                  this.birthday   = $(options.dob);
                  this.dobsw      = $(options.dobsw);
                  this.age        = $(options.age);
                  this.input      = $(options.input);
                  this.errormsg   = $(options.errormsg);
                  this.emailerror = $(options.emailerror);
              	  this.htmltext   = $(options.htmltext);
              	  this.captcha    = $(options.captcha);
              
                  Event.observe(this.email, 'blur', this.checkEmail.bind(this));
                  Event.observe(this.username, 'blur', this.checkUsername.bind(this));
                  Event.observe(this.cfmpass, 'blur', this.checkPassword.bind(this));
                  Event.observe(this.year, 'change', this.assignAgeGroup.bind(this));
                  Event.observe(this.input, 'click', this.assignDOB.bind(this));
                  Event.observe(this.captcha, 'blur', this.checkCaptcha.bind(this));
                },

  processConfirm:      function(options){
                          this.errormsg   = $(options.errormsg);
                          this.input      = $(options.input);
                          this.country    = $(options.country);
                          this.state      = $(options.state);
                          this.postcode   = $(options.postcode);
                          Event.observe(this.postcode, 'blur', this.checkPostCode.bind(this));
                          Event.observe(this.input, 'click', this.submitConfirm.bind(this));
                        },

 
  checkPostCode:      function(){
                        if(this.postcode.value!=""){  
	  						new Ajax.Request('callbacks/registration.php',
	                          { method:     'post',
	                          parameters: 'action=checkPostCode&postcode='+escape(this.postcode.value),
	                          onComplete: this.userReturn.bind(this)
	                          });
                        	}
                        },
                     
  checkCaptcha:      function(){  	
                        if(this.captcha.value != ""){  
	  						new Ajax.Request('callbacks/registration.php',
	                          { method:     'post',
	                          parameters: 'action=checkCaptcha&captcha='+escape(this.captcha.value),
	                          onComplete: this.captchaReturn.bind(this)
	                          });
                        	}
                        },
  captchaReturn:	function(request){					   	
   							try {
   								 //$('hidcaptcha').value = this.captcha.value; 
                                  var register = eval('('+request.responseText+')');
                                  if(!register.success){
                                    this.displayError(register.errmsg,'iderrcaptcha');
                                    $('hidcaptcha').value = " ";
                                  } else {
                                   $('iderrcaptcha').innerHTML = '';
                                   $('hidcaptcha').value = this.captcha.value
                                   //$('hidcaptcha').value = register.success;
                                   
                                  }
                                } catch(e){
                                  if(console) console.log(e);
                                  this.displayError("There was an error while processing your registration(captcha). Please try again.","iderrcaptcha");
                                }
                              
					   },
  userReturn:         function(request){
                          var rdata = (request.responseText).split(":");
                          this.state.value = rdata[0];
                          this.errormsg.style.display = '';
                          this.errormsg.innerHTML = rdata[1];
                        },

  assignAgeGroup:      function(){
                          //alert('ha yoo '+this.year.value);
                          new Ajax.Request('callbacks/registration.php',
                          { method:     'post',
                          parameters: 'action=age&year='+escape(this.year.value),
                          onComplete: this.ageReturn.bind(this)
                          });
                        },

  ageReturn:         function(request){
                          var rdata = (request.responseText).split(":");
                          this.age.value = rdata[0];
                          this.errormsg.style.display = '';
                          this.errormsg.innerHTML = rdata[1];
                        },

  checkUsername:      function(){
                          new Ajax.Request('callbacks/registration.php',
                          { method:     'post',
                          parameters: 'action=check&username='+escape(this.username.value)+'&x='+(new Date()).valueOf() ,
                          onComplete: this.registrationReturn.bind(this) });
                      },

  checkEmail:         function(){
                            new Ajax.Request('callbacks/registration.php',
                            { method:     'post',
                            parameters: 'action=chkmail&email='+escape(this.email.value)+'&x='+(new Date()).valueOf(),
                            onComplete: this.registrationReturnEmail.bind(this) });
                       },

  registrationReturn:       function(request){
                              try {
                                var register = eval('('+request.responseText+')');
                                if(!register.success){
                                  this.displayError(register.errmsg,'iderrormsg');
                                  this.username.focus();
                                } else {
                                  this.errormsg.innerHTML = '';
                                }
                              } catch(e){
                                if(console) console.log(e);
                                this.displayError("There was an error while processing your username. Please try again.","advice-required-iduser");
                          
                              }
                            },

  registrationReturnEmail:       function(request){
                                        try {
                                          var register = eval('('+request.responseText+')');
                                          if(!register.success){
                                            this.displayError(register.errmsg,'idemailerror');
                                            this.email.focus();
                                          } else {
                                            this.errormsg.innerHTML = '';
                                          }
                                        } catch(e){
                                          if(console) console.log(e);
                                          this.displayError("There was an error while processing your email. Please try again.","advice-required-idemail");
                                        }
                                      },
                                    
  checkPassword:            function(){
                                  if(this.passwd.value != this.cfmpass.value){
                                    this.displayError('Password and Confirm Password did not match.','iderrormsg');
                                    this.passwd.focus();
                                  }
                                },

  
  displayError:             function(errmsg,id){
                                  if($(id) != null){
                                    $(id).innerHTML      = errmsg;
                                    $(id).style.display  = 'block';
                                    $(id).style.fontSize = '8pt';
                                    $(id).style.color    = '#FF0000';
                                  }
                                },


  assignDOB:                function() {
                                this.birthday.value = this.day.value + '/' + this.mth.value + '/' + this.year.value;
                                var valid = new Validation('idregform', {useTitles:true});
                                var result = valid.validate();                               
                                if((result==true) && ($('hidcaptcha').value == this.captcha.value)) {
                                  $('idregform').submit();
                                }
                              },

  submitConfirm:                function() {
                                        var valid = new Validation('idfrmconfirm', {useTitles:true});
                                        var result = valid.validate();
                                        if(result==true) {
                                          $('idfrmconfirm').submit();
                                        }
                                      }
});
