function CompetitorRegistrationFormController(settings,rpcClientObject)
{
  this._construct(settings,rpcClientObject);
}
(function(){
  var _this= null;
  
  CompetitorRegistrationFormController.prototype={
    form: null,
    formContainer: null,
    
    data: {
      confirmPageURL: '',
      login: '',
      password: '',
      confirmPassword: '',
      email: '',
      firstName: '',
      secondName: '',
      lastName: '',
      sex: '',
      address: '',
      phone: '',
      firstNameIsVisible: false,
      secondNameIsVisible: true,
      lastNameIsVisible: true,
      sexIsVisible: true,
      addressIsVisible: true,
      phoneIsVisible: true,
      securityCode: ''
    },
    
    loginValidator: null,
    passwordValidator: null,
    retypePasswordValidator: null,
    emailValidator: null,
    firstNameValidator: null,
    secondNameValidator: null,
    lastNameValidator: null,
    sexValidator: null,
    addressValidator: null,
    phoneValidator: null,
    securityCodeValidator: null,
    
    showError: null,
    hideError: null,
    
    showSuccessMsg: null,
    hideButtonPanel: null,
    
    
    eventListeners: {},
    rpcClientObject: null,
    
    Events: {
      onCreate: 'onCreate'
    },

    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];
          Spry.Utils.addEventListener(_this.form,'submit',_this.onCreateBtn);
        }
        if (typeof(settings.formContainerId)!='undefined') _this.formContainer=document.getElementById(settings.formContainerId);
        
        if (typeof(settings.createBtnId)!='undefined') Spry.Utils.addEventListener(settings.createBtnId,'click',_this.onCreateBtn);
        
        if (typeof(settings.confirmPageURL)!='undefined') _this.data.confirmPageURL=settings.confirmPageURL;
        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.retypePasswordFieldId)!='undefined' && typeof(settings.passwordFieldId)!='undefined') _this.retypePasswordValidator=new Spry.Widget.ValidationConfirm(settings.retypePasswordFieldId, settings.passwordFieldId, {validateOn:["blur"]});
        if (typeof(settings.emailFieldId)!='undefined') _this.emailValidator=new Spry.Widget.ValidationTextField(settings.emailFieldId, "email", {maxChars:255, validateOn:["blur"]});
        if (typeof(settings.firstNameFieldId)!='undefined') _this.firstNameValidator=new Spry.Widget.ValidationTextField(settings.firstNameFieldId, "none", {isRequired:false, maxChars:255, validateOn:["blur"]});
        if (typeof(settings.secondNameFieldId)!='undefined') _this.secondNameValidator=new Spry.Widget.ValidationTextField(settings.secondNameFieldId, "none", {isRequired:false, maxChars:255, validateOn:["blur"]});
        if (typeof(settings.lastNameFieldId)!='undefined') _this.lastNameValidator=new Spry.Widget.ValidationTextField(settings.lastNameFieldId, "none", {isRequired:false, maxChars:255, validateOn:["blur"]});
        if (typeof(settings.sexFieldId)!='undefined') _this.sexValidator=new Spry.Widget.ValidationSelect(settings.sexFieldId, {isRequired:false, validateOn:["blur"]});
        if (typeof(settings.addressFieldId)!='undefined') _this.addressValidator=new Spry.Widget.ValidationTextField(settings.addressFieldId, "none", {isRequired:false, maxChars:255, validateOn:["blur"]});
        if (typeof(settings.phoneFieldId)!='undefined') _this.phoneValidator=new Spry.Widget.ValidationTextField(settings.phoneFieldId, "none", {isRequired:false, maxChars:255, validateOn:["blur"]});
        if (typeof(settings.securityCodeFieldId)!='undefined') _this.securityCodeValidator=new Spry.Widget.ValidationTextField(settings.securityCodeFieldId, "none", {validateOn:["blur"]});
        if (typeof(settings.errorContainerId)!='undefined'){
          _this.hideError=new Spry.Effect.Blind(settings.errorContainerId, {duration: 1, from: '100%', to: '0%', toggle: false});
          _this.showError=new Spry.Effect.Blind(settings.errorContainerId, {duration: 100, from: '0%', to: '100%', toggle: false});
        }
        
        if (typeof(settings.successMsgContainerId)!='undefined' && typeof(settings.buttonPannelId)!='undefined'){
          _this.hideButtonPanel=new Spry.Effect.Blind(settings.buttonPannelId, {duration: 1, from: '100%', to: '0%', toggle: false});
          _this.showSuccessMsg=new Spry.Effect.Blind(settings.successMsgContainerId, {duration: 100, from: '0%', to: '100%', toggle: false, setup: _this.hideButtonPanel.start});
          var tmpEffect=new Spry.Effect.Blind(settings.successMsgContainerId, {duration: 1, from: '100%', to: '0%', toggle: false});
          tmpEffect.start();
        }
      }
      
      if (!_this.form) throw "Competitor registration form object not found.";

      _this.form.crf_first_name_visible.checked=!_this.data.firstNameIsVisible;
      _this.form.crf_second_name_visible.checked=!_this.data.secondNameIsVisible;
      _this.form.crf_last_name_visible.checked=!_this.data.lastNameIsVisible;
      _this.form.crf_sex_visible.checked=!_this.data.sexIsVisible;
      _this.form.crf_address_visible.checked=!_this.data.addressIsVisible;
      _this.form.crf_phone_visible.checked=!_this.data.phoneIsVisible;/**/
      
      _this.hideError.start();

    },
    
    resetControls: function()
    {
      if (_this.loginValidator) _this.loginValidator.reset();
      if (_this.passwordValidator) _this.passwordValidator.reset();
      if (_this.retypePasswordValidator) _this.retypePasswordValidator.reset();
      if (_this.emailValidator) _this.emailValidator.reset();
      if (_this.firstNameValidator) _this.firstNameValidator.reset();
      if (_this.secondNameValidator) _this.secondNameValidator.reset();
      if (_this.lastNameValidator) _this.lastNameValidator.reset();
      if (_this.sexValidator) _this.sexValidator.reset();
      if (_this.addressValidator) _this.addressValidator.reset();
      if (_this.phoneValidator) _this.phoneValidator.reset();
      if (_this.securityCodeValidator) _this.securityCodeValidator.reset();
    },
    
    prepareData: function()
    {
      var error=false

      if (_this.loginValidator.validate()) _this.data.login=_this.loginValidator.input.value; else error|=true;
      if (_this.passwordValidator.validate()) _this.data.password=_this.passwordValidator.input.value; else error|=true;
      if (_this.retypePasswordValidator.validate()) _this.data.confirmPassword=_this.retypePasswordValidator.input.value; else error|=true;
      if (_this.emailValidator.validate()) _this.data.email=_this.emailValidator.input.value; else error|=true;
      if (_this.firstNameValidator.validate()) _this.data.firstName=_this.firstNameValidator.input.value; else error|=true;
      if (_this.secondNameValidator.validate()) _this.data.secondName=_this.secondNameValidator.input.value; else error|=true;
      if (_this.lastNameValidator.validate()) _this.data.lastName=_this.lastNameValidator.input.value; else error|=true;
      if (_this.sexValidator.validate()) _this.data.sex=_this.sexValidator.selectElement.value; else error|=true;
      if (_this.addressValidator.validate()) _this.data.address=_this.addressValidator.input.value; else error|=true;
      if (_this.phoneValidator.validate()) _this.data.phone=_this.phoneValidator.input.value; else error|=true;
      if (_this.securityCodeValidator.validate()) _this.data.securityCode=_this.securityCodeValidator.input.value; else error|=true;

      if (error) return false;
      
      if (_this.form.crf_first_name_visible.checked) _this.data.firstNameIsVisible=false; else _this.data.firstNameIsVisible=true;
      if (_this.form.crf_second_name_visible.checked) _this.data.secondNameIsVisible=false; else _this.data.secondNameIsVisible=true;
      if (_this.form.crf_last_name_visible.checked) _this.data.lastNameIsVisible=false; else _this.data.lastNameIsVisible=true;
      if (_this.form.crf_sex_visible.checked) _this.data.sexIsVisible=false; else _this.data.sexIsVisible=true;
      if (_this.form.crf_address_visible.checked) _this.data.addressIsVisible=false; else _this.data.addressIsVisible=true;
      if (_this.form.crf_phone_visible.checked) _this.data.phoneIsVisible=false; else _this.data.phoneIsVisible=true;
      
      return true;
    },
    
    onCreateBtn: function(evt)
    {
//      alert(_this.phoneValidator.input.value);
      _this.hideError.start();
                
      if (_this.prepareData())
      {
        _this.form.login.disabled = true;
        _this.form.password.disabled = true;
        _this.form.confirm_password.disabled = true;
        _this.form.email.disabled = true;
        _this.form.family.disabled = true;
        _this.form.name.disabled = true;
        _this.form.secondName.disabled = true;
        _this.form.gender.disabled = true;
        _this.form.organisation.disabled = true;
        _this.form.telephone.disabled = true;
        _this.form.security.disabled = true;
        //_this.form.ID_COMPETITOR_REGISTRATION_FORM_REGISTER_BUTTON.disabled = true;
        //_this.form.password.disabled = true;
        //_this.form.password.disabled = true;
        //_this.form.password.disabled = true;
        
        _this.rpcClientObject.setData(_this.data,_this.onDataReady);
        _this.rpcClientObject.call('controllers_registration_form.createCompetitorAccount',5);
      }
      
      evt.preventDefault();
      _this.notifyListeners(evt,_this.Events.onCreate,_this);
    },
    
    onDataReady: function(response)
    {
       _this.form.login.disabled = false;
        _this.form.password.disabled = false;
        _this.form.confirm_password.disabled = false;
        _this.form.email.disabled = false;
        _this.form.family.disabled = false;
        _this.form.name.disabled = false;
        _this.form.secondName.disabled = false;
        _this.form.gender.disabled = false;
        _this.form.organisation.disabled = false;
        _this.form.telephone.disabled = false;
        _this.form.security.disabled = false;
        //_this.form.ID_COMPETITOR_REGISTRATION_FORM_REGISTER_BUTTON.disabled = true;
        //_this.form.password.disabled = true;
        //_this.form.password.disabled = true;
        //_this.form.password.disabled = true;
        
      if (response.error)
      {
        _this.showError.element.innerHTML=response.error;
        _this.showError.start();
        
//        alert(response.error);
        return;
      }
      
      if (response.result.errors)
      {
          if (response.result.errors.ELogin)
            _this.loginValidator.addClassName(_this.loginValidator.element,'textfieldInvalidFormatState');
          if (response.result.errors.EPassword)
            _this.passwordValidator.addClassName(_this.passwordValidator.element,'passwordCustomState');
          if (response.result.errors.EConfirmPassword)
            _this.retypePasswordValidator.addClassName(_this.retypePasswordValidator.element,'confirmInvalidState');
          if (response.result.errors.EEmail)
            _this.emailValidator.addClassName(_this.emailValidator.element,'textfieldInvalidFormatState');
          if (response.result.errors.EFirstName)
            _this.firstNameValidator.addClassName(_this.firstNameValidator.element,'textfieldInvalidFormatState');
          if (response.result.errors.ESecondName)
            _this.secondNameValidator.addClassName(_this.secondNameValidator.element,'textfieldInvalidFormatState');
          if (response.result.errors.ELastName)
            _this.lastNameValidator.addClassName(_this.lastNameValidator.element,'textfieldInvalidFormatState');
          if (response.result.errors.ESex)
            _this.sexValidator.addClassName(_this.sexValidator.element,'selectInvalidState');
          if (response.result.errors.EAddress)
            _this.addressValidator.addClassName(_this.addressValidator.element,'textfieldInvalidFormatState');
          if (response.result.errors.EPhone)
            _this.phoneValidator.addClassName(_this.phoneValidator.element,'textfieldInvalidFormatState');
          if (response.result.errors.ESecurityCode)
            _this.securityCodeValidator.addClassName(_this.securityCodeValidator.element,'textfieldInvalidFormatState');


          if (response.result.errors.EExtendedMessage)
          {
            _this.showError.element.innerHTML=response.result.errors.EExtendedMessage;
            _this.showError.start();
          }
      } else {
        _this.hideButtonPanel.start();
        _this.showSuccessMsg.start();
      }
      
      
//      alert('Create');
    }
  }
})();