//  Registration Block
var regFormHandler = Class.create()

regFormHandler.prototype = {
    initialize: function() {               
        this.email = $('aj_reg_id_email')
        this.password = $('aj_reg_id_password')
        this.repassword = $('aj_reg_id_repassword')
        this.protection = $('aj_reg_id_code')
        this.register = $('aj_reg_id_submit')        
		this.loading = $('aj_reg_id_loading')
		this.message = $('aj_reg_id_message')        		
        if (this.email && this.password && this.repassword && this.protection && this.register) {
            this.register.onclick = this.registerHandler.bind(this)                       
        }        
    },   

    clearForm: function() {
		this.email.value = ''
		this.password.value = ''
		this.repassword.value = ''
		this.protection.value = ''		
    },

    registerHandler: function() {            
    	    var query = ''
            query += this.email.name + '=' + encodeURIComponent(this.email.value) + '&'
            query += this.password.name + '=' + encodeURIComponent(this.password.value) + '&'
            query += this.repassword.name + '=' + encodeURIComponent(this.repassword.value) + '&'
            query += this.protection.name + '=' + encodeURIComponent(this.protection.value)
            new Ajax.Request('/AjaxProcessRegistration', { postBody: query, onSuccess: this.registrationOk.bind(this), onFailure: this.registrationFailed.bind(this), onException: this.registrationEx.bind(this) })        
    },

    registrationOk: function(transport) {        
        var answer = transport.responseText.evalJSON()        
        this.message.innerHTML = answer.message
    },

    registrationFailed: function(transport) {        
        this.message.innerHTML = 'Registration failed. Try again'
    },

    registrationEx: function() {        
        this.message.innerHTML = 'Exception'
    }    
}
//  Recovery Block

var recFormHandler = Class.create()

recFormHandler.prototype = {
    initialize: function() {               
        this.email = $('aj_rec_id_email')        
        this.protection = $('aj_rec_id_code')
        this.recovery = $('aj_rec_id_submit')        
		this.loading = $('aj_rec_id_loading')
		this.message = $('aj_rec_id_message')        		
        if (this.email && this.protection && this.recovery) {
            this.recovery.onclick = this.recoveryHandler.bind(this)                       
        }        
    },   

    clearForm: function() {
		this.email.value = ''		
		this.protection.value = ''		
    },

    recoveryHandler: function() {            
    	    var query = ''
            query += this.email.name + '=' + encodeURIComponent(this.email.value) + '&'            
            query += this.protection.name + '=' + encodeURIComponent(this.protection.value)
            new Ajax.Request('/AjaxProcessRecovery', { postBody: query, onSuccess: this.recoveryOk.bind(this), onFailure: this.recoveryFailed.bind(this), onException: this.recoveryEx.bind(this) })        
    },

    recoveryOk: function(transport) {        
        var answer = transport.responseText.evalJSON()        
        this.message.innerHTML = answer.message
    },

    recoveryFailed: function(transport) {        
        this.message.innerHTML = 'Recovery failed. Try again'
    },

    recoveryEx: function() {        
        this.message.innerHTML = 'Exception'
    }    
}
