function LoginFormController(settings,rpcClientObject)
{
  this._construct(settings,rpcClientObject);
}
(function(){
  var _this= null;
  
  LoginFormController.prototype={
  	form: null,
  	formContainer: null,
    pageContainer: null,
    loadingPageContainer: null,
    
    data: {
      login: '',
      password: '',
      rememberMe: false,
      autologin: false,
      auto: false,
      onCompetitorSuccess: '',
      onEmployerSuccess: ''
    },
  	
  	loginValidator: null,
  	passwordValidator: null,
    
    eventListeners: {},
    rpcClientObject: null,
    
    showError: null,
    showSuccess: null,
    successLink: null,
    
    Events: {
      onLogin: 'onLogin',
      onRememberMeClick: 'onRememberMeClick',
      onAutologinClick: 'onAutologinClick'
    },

    addEventListener: function(customEvent,listener)
    {
      if (typeof(_this.eventListeners[customEvent])=='undefined')
        _this.eventListeners[customEvent]=[];
        
      _this.eventListeners[customEvent].push(listener);
      return false;
    },
    
    notifyListeners: function(evt,customEvent,customData)
    {
    	if (typeof(_this.eventListeners)=='undefined' || typeof(_this.eventListeners[customEvent])=='undefined')
    		return;
    	var i=0;
    	for (i=0;i<_this.eventListeners[customEvent].length;i++)
    	{
    		_this.eventListeners[customEvent][i](evt,customData);
    	}
    },
    
  	_construct: function(settings,rpcClientObject)
  	{
  	  _this=this;
  	  _this.rpcClientObject=rpcClientObject;
  	
  		if (typeof(settings)!='undefined')
  		{
  		  if (typeof(settings.formName)!='undefined') _this.form=document.forms[settings.formName];
  		  if (typeof(settings.formContainerId)!='undefined'){ 
          var tmpEffect=new Spry.Effect.Fade(settings.formContainerId, {duration: 2000, toggle: false});
          tmpEffect.start();
        }
  		  
  		  if (typeof(settings.loginBtnId)!='undefined') Spry.Utils.addEventListener(settings.loginBtnId,'click',_this.onLoginBtn);
  		  if (typeof(settings.rememberMeCheckboxId)!='undefined') Spry.Utils.addEventListener(settings.rememberMeCheckboxId,'click',_this.onRememberMeClick);
  		  if (typeof(settings.autologinCheckboxId)!='undefined') Spry.Utils.addEventListener(settings.autologinCheckboxId,'click',_this.onAutologinClick);
  		  
  		  if (typeof(settings.loginFieldId)!='undefined') _this.loginValidator=new Spry.Widget.ValidationTextField(settings.loginFieldId, "none", {maxChars:40, validateOn:["blur"]});
  		  if (typeof(settings.passwordFieldId)!='undefined') _this.passwordValidator=new Spry.Widget.ValidationPassword(settings.passwordFieldId, {minChars:4, maxChars:10, validateOn:["blur"]});
          if (typeof(settings.successLinkId)!='undefined') _this.successLink=document.getElementById(settings.successLinkId);

        if (typeof(settings.onCompetitorSuccess)!='undefined') _this.data.onCompetitorSuccess=settings.onCompetitorSuccess;
        if (typeof(settings.onEmployerSuccess)!='undefined') _this.data.onEmployerSuccess=settings.onEmployerSuccess;

        if (typeof(settings.errorContainerId)!='undefined'){
          _this.showError=new Spry.Effect.Blind(settings.errorContainerId, {duration: 100, from: '0%', to: '100%', toggle: false});
          _this.hideError=new Spry.Effect.Blind(settings.errorContainerId, {duration: 1, from: '100%', to: '0%', toggle: false});
          _this.hideError.start();
        }
        
        if (typeof(settings.successMsgContainerId)!='undefined'){
          _this.showSuccessMsg=new Spry.Effect.Blind(settings.successMsgContainerId, {duration: 100, from: '0%', to: '100%', toggle: false});
          var hideSuccessMsg=new Spry.Effect.Blind(settings.successMsgContainerId, {duration: 1, from: '100%', to: '0%', toggle: false});
          hideSuccessMsg.start();
        }
        // H //
        if (typeof(settings.PageContainer)!='undefined'){
          _this.showPageContainer=new Spry.Effect.Blind(settings.PageContainer, {duration: 500, from: '0%', to: '100%', toggle: false});
          _this.hidePageContainer=new Spry.Effect.Blind(settings.PageContainer, {duration: 500, from: '100%', to: '0%', toggle: false});
          //_this.hidePageContainer.start();
        }
        
        if (typeof(settings.loadingPageContainer)!='undefined'){
          _this.showLoadingPageContainer=new Spry.Effect.Blind(settings.loadingPageContainer, {duration: 100, from: '0%', to: '100%', toggle: false});
          _this.hideLoadingPageContainer=new Spry.Effect.Blind(settings.loadingPageContainer, {duration: 100, from: '100%', to: '0%', toggle: false});
          
        }
        
  		}
  		
  		if (!_this.form) throw "Login form object not found.";
      
      if (_this.data.login=MyTools.getCookie('login')){
      
            if (_this.data.password=MyTools.getCookie('password')){
                _this.form.lf_pass.value=_this.data.password;
            }
             
        _this.form.lf_login.value=_this.data.login;
        _this.form.lf_remember_me.checked=true;
        
        if (_this.data.auto=MyTools.getCookie('auto'))
        {
          _this.form.lf_autologin.checked=true;
        }else{
          _this.form.lf_autologin.checked=false;
        }
      } else{
        _this.form.lf_login.value='';
        _this.form.lf_remember_me.checked=false;
      }/**/
  	},
  	
  	resetControls: function()
  	{
  	  if (_this.loginValidator) _this.loginValidator.reset();
  	  if (_this.passwordValidator) _this.passwordValidator.reset();
  	},
    
    busyForm: function()
    {
        _this.form.lf_login.disabled();
        _this.form.lf_pass.disabled();
    },
    notBusyForm: function (){
        _this.form.lf_login.enabled();
        _this.form.lf_pass.enabled();
    },
    
    prepareData: function()
    {
      var error=false
    
      if (_this.loginValidator.validate()) _this.data.login=_this.form.lf_login.value; else error|=true;
      if (_this.passwordValidator.validate()) _this.data.password=_this.form.lf_pass.value; else error|=true;
      
      if (error) return false;
      
      if (_this.form.lf_remember_me.checked) {
         _this.data.rememberMe=true;
         var oDate = new Date();
        oDate.setFullYear(oDate.getFullYear() + 1);
        document.cookie = "password="+_this.form.password.value+";expires="+ oDate.toGMTString();  // установка cookie
         
      }else{
        _this.data.rememberMe=false;
        document.cookie = "password=";  // установка cookie
      }
      
      if (_this.form.lf_autologin.checked) _this.data.autologin=true; else _this.data.autologin=false;
      
      //_this.data.onSuccess=_this.form.action;
      
      return true;
    },
  	
	  onLoginBtn: function(evt)
	  {
//	  	
        
      if (_this.prepareData())
      {
         _this.form.login.disabled = true;
         _this.form.password.disabled = true;
         _this.hideError.start();
         
         //_this.form.lf_remember_me.checked
         
        _this.rpcClientObject.setData(_this.data,_this.onDataReady);
        _this.rpcClientObject.call('controllers_login.login',5);
        _this.notifyListeners(evt,_this.onLogin,_this);
      }
      
      
	  	evt.preventDefault();
	  },
	  
	  onRememberMeClick: function(evt)
	  {
	  	//alert('Remember me');
      
      if (!_this.form.lf_remember_me.checked)
      {
        MyTools.deleteCookie('login','/');
        MyTools.deleteCookie('password','/');
        //_this.form.lf_autologin.checked=false;
      }else{
        var oDate = new Date();
        oDate.setFullYear(oDate.getFullYear() + 1);
        document.cookie = "password="+_this.form.password.value+";expires="+ oDate.toGMTString();  // установка cookie
        //document.writeln(document.cookie);

      }
	  },
	  
	  onAutologinClick: function(evt)
	  {
	  	//alert('Autologin');
      
      if (_this.form.lf_autologin.checked)
      {
        _this.form.lf_remember_me.checked=true;
      }else{
        MyTools.deleteCookie('auto','/');
      }
	  },
      
    onDataReady: function(response)
    {
      _this.form.login.disabled = false;
      _this.form.password.disabled = false;
      
      
      if (response.error)
      {
        alert(response.error);
        return;
      }
      
      if (response.result.errors)
      {
        _this.showError.element.innerHTML='';
        if (response.result.errors.EBadParams) _this.showError.element.innerHTML+=response.result.errors.EBadParams;
        if (response.result.errors.ENoSuchAccount) _this.showError.element.innerHTML+=response.result.errors.ENoSuchAccount;
        if (response.result.errors.EAccountNotActivated) _this.showError.element.innerHTML+=response.result.errors.EAccountNotActivated;
        if (response.result.errors.EAccountIsBanned) _this.showError.element.innerHTML+=response.result.errors.EAccountIsBanned;
        if (response.result.errors.EAlreadyOnlineAccount) _this.showError.element.innerHTML+=response.result.errors.EAlreadyOnlineAccount;
        //_this.loginValidator.addClassName(_this.loginValidator.element,'textfieldInvalidFormatState');
        //_this.passwordValidator.addClassName(_this.passwordValidator.element,'passwordCustomState');
        
        _this.showError.start();
        
      }
      
      if (response.result.type){
        switch (response.result.type){
          case 'competitor': _this.successLink.href=_this.data.onCompetitorSuccess; break;
          case 'employer': _this.successLink.href=_this.data.onEmployerSuccess; break;
        }
        
        _this.showSuccessMsg.start();
      }
      
       // H //
      //_this.hideLoadingPageContainer.start();
      //_this.showPageContainer.start();
    }
  }
})();