function _Status() {

    this.cancel = function()
    {
        $('user_state_state').value        = $('user_state_current_value').value;
        $('status_editor').style.display   = 'none';
        $('empty_status').style.display    = 'none';
        $('current_status').style.display  = 'block';
    }

    this.cancel_box = function()
    {
        $('user_state_state').value      = $('user_state_current_value').value;
        $('status_editor').style.display = 'none';
    }
    
    this.save_local = function()
    {
        $('mainfieldset').style.display  = 'none';

        var notice = $('localuserstate');
        var url='/index.php/userstate/saveLocal.html';
        new Ajax.Request(url, {
          method: 'post',
          parameters: Form.serialize('UserStateForm'),
          onLoading: function() {
            var note = $('note');
            note.update('Updating Status...').setStyle({  });
          }
          ,onSuccess: function(transport) {
            notice.update(transport.responseText).setStyle({  });
            }
        });
    }

    this.clear_state_local = function()
    {
        $('mainfieldset').style.display = 'none';

        var notice = $('localuserstate');
        var url='/index.php/userstate/clearLocal.html';
        new Ajax.Request(url, {
          method: 'post',
          parameters: Form.serialize('UserStateForm'),
          onLoading: function() {
            var note = $('note');
            note.update('Clearing Status...').setStyle({  });
          }
          ,onSuccess: function(transport) {
            notice.update(transport.responseText).setStyle({  });
            }
        });
    }

    this.handle_key_press_local = function(evt)
    {
        if ((evt.which && evt.which == 13) || (evt.keyCode && evt.keyCode == 13))
        {
            var newstate = $('user_state_state').value.replace(/^\s+|\s+$/g,'');
            
            $('user_state_state').value = newstate;

            if ($('user_state_state').value == '')
            {
                State.clear_state_local();
            }
            else if ($('user_state_state').value == ($('user_state_current_value').value))
            {
                if ($('user_state_calling_template').value == 'box')
                    State.cancel_box();
                else State.cancel();
            }
            else
            {
                State.save_local();
            }
        }
        
    }
    
    this.load_focus = function()
    {
        if ($('user_state_state').value == '')
        {
            $('user_state_state').value = 'is ';
        }

        setTimeout('$(\'user_state_state\').focus();',1);
    }
    
    this.set_end = function(obj)
    {
        if (obj.createTextRange)
        {
            var FieldRange = obj.createTextRange();
            FieldRange.moveStart('character', obj.value.length);
            FieldRange.collapse();
            FieldRange.select();
        }
    }
    
}

var State = new _Status();
