
/* :::::::::: RegisrationHelper :::::::::::::: */
var RegisrationHelper = Class.create(); 
RegisrationHelper.prototype =
{
    initialize: function()
    {
    },
    
    init: function (form_name)
    {
         this.form = $(form_name);
         this.msg = $('msg');
         this.result = new Hash({});
         this.locksubmit = true;
         this.lockajax = 0; 

         var thisCopy = this;
         var inputs = this.form.select('input[type="text"]', 'input[type="password"]');
             inputs.each(function(el)     
             {
                if(el.id=='login') thisCopy.input_login = el;
                else if(el.id=='email') thisCopy.input_email = el;
                else if(el.id=='password') thisCopy.input_password1 = el;
                else if(el.id=='password2') thisCopy.input_password2 = el;
                else if(el.id=='captcha') thisCopy.input_captcha = el;
                
                
                el.observe('keyup', function(event)  { thisCopy.onKeyPress( event, el ); });
             });
         
        
         this.input_lic = $('lic');    
      
    },

    submit: function ()
    {
        if(this.checkForm( this.msg)) this.form.submit();        
        return false;
    },
                               
    showMessage: function( text )
    {
        if(text.blank())
            this.msg.update(''); 
        else
            this.msg.update(text);
    },
    
    onKeyPress: function(event, input) {
      switch(event.keyCode) {
       case Event.KEY_TAB:
       case Event.KEY_ESC:
       case Event.KEY_RETURN: 
       case Event.KEY_LEFT:
       case Event.KEY_RIGHT:
       case Event.KEY_UP:
       case Event.KEY_DOWN:
       case Event.KEY_HOME:
       case Event.KEY_END:
       case Event.KEY_PAGEUP:
       case Event.KEY_PAGEDOWN:
       case Event.KEY_INSERT:
         return;
      }
      this.checkForm(input);
    },
      
    checkForm: function( input )
    {
        this.locksubmit = true;
       
        if(this.result.get('login') != 1 || input.id=='login')
        {   
            if( this.result.get('login') == 0 && input.id!='login')
            {
                this.showMessage('Sorry, this LOGIN already exist into our database');  
                return false;
            } 

            input = (input.id=='login'? input : this.input_login );
            if( this.checkLogin( input )) {
                this.result.set('login', 1);
            } else {
                this.result.unset('login'); return false; 
            }
        }

        if(this.result.get('email') != 1 || input.id=='email')
        {
/*            if( this.result.get('email') == 0 && input.id!='email')
            {
                this.showMessage('Sorry, this email already exist into our database. <br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;If you forgot password use forgot service');  
                return false;
            }
                */
            input = (input.id=='email'? input : this.input_email );
            if( this.checkEmail( input ) ) {
                this.result.set('email', 1);
            } else {
                this.result.unset('email'); return false; 
            }
        }   
             
        if(this.result.get('pass1') != 1 || input.id=='password')
        {
            input = (input.id=='password' ? input : this.input_password1 );
            if( this.checkPassword1( input ) ) {
                this.result.set('pass1', 1);
                this.result.set('pass2', 0); //recheck pass1 == pass2
            } else {
                this.result.unset('pass1'); return false; 
            }
        }
        if(this.result.get('pass2') != 1 || input.id=='password2')
        {
            input = (input.id=='password2' ? input : this.input_password2 );
            if( this.checkPassword2( input ) ) {
                this.result.set('pass2', 1);
            } else {
                this.result.unset('pass2'); return false; 
            }
        }

        if(this.result.get('avatar') == undefined)
        {
            var av = $('avatar').value.toString();
            if(av.blank())
            {
                //this.showMessage('добавьте КАРТИНКУ');
                //return false;
            }
            else
            {
                this.result.set('avatar', 1);
            }
        }  
        
        if(this.result.get('captcha') != 1 || input.id=='captcha')
        {   
            input = (input.id=='captcha'? input : this.input_captcha );
            if( input.value.length == 5 ) {
                this.result.set('captcha', 1);
            } else if( input.value.length != 5 ) {
                this.showMessage('Please enter the verification NUMBER');      
                this.result.unset('captcha'); return false; 
            }
        }              
               
        if( ( this.input_lic.checked) == false )
        {
           this.showMessage('Please, read the TERMS OF SERVICE');  
           return false; 
        }
           
        this.showMessage('');
        this.locksubmit = false; 
        return true;
    },

    checkLogin: function( input )
    {
         do
         {
            if(input.value.length == 0)
            {
                this.showMessage('Please, choose your LOGIN');  
                break;
            }
            if(input.value.length<3)
            {
                this.showMessage('LOGIN must contain at least 3 alphanumeric char');  
                break;
            }
            
            var reg = new RegExp ("^[a-zA-Z0-9_]*$", 'i');
            if( reg.test(input.value) == false )
            {
                this.showMessage('LOGIN must content only alphanumeric chars and digital');  
                break;
            }
            
            this.ajaxCheck(input);
                              
            return true;
         }while(false);
         return false;       
    },

    checkEmail: function( input )
    {
         do
         {
            if(input.value.length == 0)
            {
                this.showMessage('Please, enter your EMAIL address');  
                break;
            }
            if(!this.isEmail(input.value))
            {
                this.showMessage('EMAIL is not correct');  
                break;
            }
            
            this.ajaxCheck(input);
                              
            return true;
         }while(false);
         return false;       
    },

    checkPassword1: function( input )
    {
         do
         {
            if(input.value.length == 0)
            {
                this.showMessage('Please, enter your PASSWORD');  
                break;
            }
            if(input.value.length<3)
            {
                this.showMessage('PASSWORD must contain at least 3 alphanumeric char');  
                break;
            }
                              
            return true;
         }while(false);
         return false;       
    },

    checkPassword2: function( input )
    {
         do
         {
            if(input.value.length == 0)
            {
                this.showMessage('Please, confirm your PASSWORD');  
                break;
            }
            if(input.value!= this.input_password1.value)
            {
                this.showMessage('Error in PASSWORD CONFIRMATION');  
                break;
            }
                              
            return true;
         }while(false);
         return false;       
    },
    
    isEmail: function(item)
    {
        var at="@";
        var dot=".";
        var lat=item.indexOf(at);
        var litem=item.length;
        var ldot=item.indexOf(dot);

           var reg= new RegExp ("^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[-0-9a-z_^\\.]+\\.[a-z]{2,6}$", 'i');
           if (!reg.test(item)) {
                   return false;
           }
        if (item.indexOf(at)==-1) return false;
        if (item.indexOf(at)==-1 || item.indexOf(at)==0 || item.indexOf(at)==litem) return false;
        if (item.indexOf(dot)==-1 || item.indexOf(dot)==0 || item.indexOf(dot) >= litem - 2) return false;
        if (item.indexOf(at,(lat+1))!=-1) return false;
        if (item.substring(lat-1,lat)==dot || item.substring(lat+1,lat+2)==dot) return false;
        if (item.indexOf(dot,(lat+2))==-1) return false;
        if (item.indexOf(" ")!=-1)  return false;

        return true;
    },
    
    ajaxCheck: function(input)
    {
        //if(this.lockajax == 1)
        //    return;
    
        //this.lockajax = 1;
    
        new Ajax.Request('/user/ajax_registation_check/', {
            method: 'post',
            parameters: {'type': input.id, 'value': input.value},
            asynchronous: true,

            onSuccess: function(response)
            {
                var res = response.responseText;
                //this.lockajax = 0; 
                if(res == 0) {
                    this.result.set(input.id, 0);
                    this.checkForm( this.msg );
                }
                else if(res == 1)
                    this.result.set(input.id, 1);
                    
            }.bind(this),

            onFailure: function() 
            {                
            }.bind(this)
        } );   
    },
    
    onLoadAvatarFinish: function ( responseText ) 
    {
        var response = eval('('+responseText+')');
        if(response.error == 0) {
            $('avatarImage').src = response.msg;
            var av = $('avatar');
                av.value = response.avatar;
            this.checkForm( av );
        }
        else {
            this.showMessage(response.msg);
        }
    }     
}

function onLoadAvatarStart(){
    register.showMessage('Please, wate...');
}

function onLoadAvatarFinish( response ) {
    register.onLoadAvatarFinish(response);
}
