FormHelper = Class.create();
FormHelper.prototype = {
        initialize: function(formObject, options)
        {
            //initialize the form element
            if(typeof(formObject) != 'object')
            {
                if($(formObject))
                {
                    this.form = $(formObject);
                }
            }
            else if(typeof(formObject) == 'object')
            {
                this.form = formObject;
            }
            
            //check to ensure we have a form object
            if(typeof(this.form) != 'object')
            {
                throw 'Form object not exists!';
                //exit the object creation
                return false;
            }
            
            //set the options
            if(this.setOptions)
            {
                this.setOptions(options);
            }
            else
            {
                this.options = options || {};
            }
            
            //defaulting the options if nessesery
            this.options.clearOnFocus       = (this.options.clearOnFocus == true ? true : false);
            this.options.fillIfEmpty        = (this.options.fillIfEmpty == true ? true : false);
            this.options.focusFirstElement  = (this.options.focusFirstElement == true ? true : false);
            this.options.disable            = (this.options.disable == true ? true : false);
            
            //disable the form if asked
            if(this.options.disable)
            {
                this.disable();
            }
            
            //get the form input elements by the type attribute
            this.formElements = Form.getElements(this.form);
            
            //save the form input elements default value state to the new attribute: oldvalue
            for(var i=0; i < this.formElements.length; i++)
            {
                this.formElements[i].setAttribute('oldvalue', this.formElements[i].value);
            }
            
            //focus the first form element if configured
            if(this.options.focusFirstElement && this.options.disable == false)
            {
                Form.focusFirstElement(this.form);
            }
            
            //if the clearOnFocus option is configured to true
            //assign the onfocus function to all the elements
            if(this.options.clearOnFocus)
            {
                for(var i=0; i < this.formElements.length; i++)
                {
                    this.clearOnFocus(this.formElements[i]);
                }
            }
            
            //if the fillIfEmpty option is configured to true
            //assign the onblur function to all the elements
            if(this.options.fillIfEmpty)
            {
                for(var i=0; i < this.formElements.length; i++)
                {
                    this.fillIfEmpty(this.formElements[i]);
                }
            }
        },
        
        //function clearOnFucus
        //clears the value attribute in the DOM when focusing the element
        clearOnFocus: function(inputElement)
        {
            inputElement.onfocus = function(){
                                       this.clear();
                                   }
        },
        
        //function fillIfEmpty
        //fills the value attribute in the DOM when the user blurs the element and the value attribute is not present
        fillIfEmpty: function(inputElement)
        {
            inputElement.onblur = function(){
                                      classAttr = this.readAttribute('class');
                                      if(classAttr == 'error_input')
                                      {
                                          this.removeAttribute('class');
                                      }
                                      if(!this.present())
                                      {
                                          this.value = this.readAttribute('oldvalue');
                                      }
                                      if(this.present())
                                      {
                                          this.setAttribute('oldvalue', this.value);
                                      }
                                  };
        },
        
        //function to disable an element or the form
        disable: function(name)
        {
            if(!name){
                this.form.disable();
            } else {
                this.form[name].disable();
            }
        },
        
        //function to enable an element or the form
        enable: function(name)
        {
            if(!name){
                this.form.enable();
            } else {
                this.form[name].enable();
            }
        },
        
        //function to focus on an element
        focusElement: function(name)
        {
            this.form[name].focus();
        },
        
        //get the current value from the element
        getElementValue: function(name)
        {
            return this.form[name].getValue();
        },
        
        //check if a element contains a value or not
        isPresent: function(name)
        {
            return this.form[name].present();
        },
        
        //return the serialized value of the current form
        serialize: function()
        {
            return this.form.serialize();
        },
        
        //clear the value of an element
        clearElement: function(name)
        {
            this.form[name].clear();
        },
        
        //return all the form elements
        getElements: function()
        {
             return this.formElements;
        }
}