Mesh.LoginControl = Class.create();
Object.extend(Mesh.LoginControl.prototype, {
  initialize:       function(options){
                      this.errorText    = $(options.errorText);
                      this.username     = $(options.username);
                      this.password     = $(options.password);
                      this.loading      = $(options.loading);

                      this.forgotpswd   = $(options.forgotpswd);
                      this.forgotform   = $(options.forgotform);
                      //this.forgotarrow  = $(options.forgotarrow);
                      this.sendbutton   = $(options.sendbutton);
                      this.useremail    = $(options.useremail);
                      this.notmember    = $(options.notmember);

                      this.working      = false;
					
                      Event.observe($(options.loginButton), 'click', this.submitLogin.bind(this));
                      Event.observe(this.username, 'keypress', Mesh.Control.checkEnter(this.submitLogin.bind(this)));
                      Event.observe(this.password, 'keypress', Mesh.Control.checkEnter(this.submitLogin.bind(this)));
                      Event.observe(this.forgotpswd, 'click', this.showPasswordForm.bind(this));
                      Event.observe(this.sendbutton, 'click', this.sendingForgotPassword.bind(this));
                    },
  startWorking:     function(){
                      this.username.disabled     = true;
                      this.password.disabled     = true;

                      this.working = true;
                      this.hideErrors();
                    },
  stopWorking:      function(){
                      this.working = false;
                      this.username.disabled = false;
                      this.password.disabled = false;
                    },
  submitLogin:      function(){
                      if(this.working)
                        return false;
                        
                      var username = this.username.value;
                      var password = this.password.value;

                      if(username.match(/^\s*$/)){
                        //this.displayError('Please enter a username!');
                        //this.username.value = 'Please enter a username!';
                        alert('Please enter a username!');
                        this.username.focus();
                        return false;
                      }
                      if(password.match(/^\s*$/)){
                        //this.displayError('Please enter a password!');
                        //this.password.value = 'Please enter a password!';
                        alert('Please enter a password!');
                        this.password.focus();
                        return false;
                      }
                      this.startWorking();
                      new Ajax.Request('callbacks/login.php',
                                       { method:     'post',
                                         parameters: 'action=login&username='+escape(username)+'&password='+escape(password)+
                                                     '&x='+(new Date()).valueOf(),
                                         onComplete: this.loginReturn.bind(this) });
                    },
  loginReturn:      function(request){
                      try {
                        var login = eval('('+request.responseText+')');
                        if(!login.success){
                        	alert(login.errmsg);
                          //this.displayError(login.errmsg);
                          this.stopWorking();
                        } else {
                        	//console.log(login.errmsg)
                          window.location.href = "index.php"
                        }
                      } catch(e){
                        //if(console) console.log(e);
                        //this.displayError("There was an error while processing your login. Please try again.");
                        alert("There was an error while processing your login. Please try again.");
                        this.stopWorking();
                      }
                    },
  displayError:     function(errmsg){
                      this.errorText.innerHTML     = errmsg;
                      this.errorText.style.display = 'block';
                    },
  hideErrors:       function(){
                      this.errorText.style.display = 'none';
                    },
  showPasswordForm: function(){
  						 
  	                  if (this.forgotform.style.display == "none") {
      			            this.forgotform.style.display = "block";
      			            this.notmember.style.display = "none";
      			            //this.forgotarrow.src='media/tpl/expand-minus.gif';
      			          } else {
      				        this.forgotform.style.display = "none";
      				        this.notmember.style.display = "block";
      				        //this.forgotarrow.src='media/tpl/expand-plus.gif';
      			          }
                    },

  sendingForgotPassword:   function(){
                             if(!this.useremail.value)
                             {
                               alert("Please enter your registration email!");
                             	return;
                             }
                             new Ajax.Request('callbacks/login.php',
                                       { method:     'post',
                                         parameters: 'action=forgotpassword&email='+escape(this.useremail.value)+
                                                     '&x='+(new Date()).valueOf(),
                                         onComplete: this.forgotReturn.bind(this) });
                           },

  forgotReturn:          function(request){
                           try {
                            var login = eval('('+request.responseText+')');
                            if(!login.success){
                              alert('Email does not exist');
                            } else {
                              window.location.href = "forgotpassword.php";
                            }
                          } catch(e){
                            //if(console) console.log(e);
                            alert('Email does not exist');
                          }
                         }

});