function get(elem)
{
    return document.getElementById(elem) || false;    
}
var on = true;
Console = function (console)
{
    this.console = console;    
    this.limit = console.value.length;
    var elem = false;

    this.keyPress = function (event)
    {        
        if (!event)
        {
            event = window.event;
        }
        if (event.keyCode == 13)
        {
            if (on)
            {
                return elem.process();
            }
            else
            {
                return false;
            }
        }
        var currentPos = elem.doGetCaretPosition();
        if (currentPos <= this.limit) 
        {
            if (this.console.value.length <= this.limit) this.console.value += ' ';
            elem.setCaretPosition(this.limit + 1);
            return false;
        }
    }
    
    this.addString = function (str)
    {
        elem.console.value += ' ' + str;
    }
    this.Click = function ()
    {
        var currentPos = elem.doGetCaretPosition();
        if (currentPos <= 6) 
        {
            elem.setCaretPosition(6);
        }
    }    
    
    this.init = function (el)
    {
        elem = el;
    }
    
    this.process = function ()
    {
        var command = this.console.value.slice(this.limit+1);
        elem.processCommand(command);
        this.console.value += '\n@root>';
        this.limit = this.console.value.length;
        
        return false;
    }
    
    this.processCommand = function (command)
    {
        switch (command) 
        {
            case 'help' : this.console.value += '\nOptiWeb help:\n';
            this.console.value += 'login // log in to grafic-intarface command panel\n';
            this.console.value += 'stopOW // disable OptiWeb Content Management System\n';
            this.console.value += 'shutdown // shutdown apache service\n';
            this.console.value += 'exit // exit from this page\n';
            this.console.value += 'help // get help\n';
            break;
            case 'login' : this.console.value += '\nYou can not login from this IP-addres!';
            break;
            case 'stopOW' : 
                var expire_date = new Date();
                var ms_from_now = 60*60*1000;
                expire_date.setTime(expire_date.getTime() + ms_from_now);
                var time = expire_date.toGMTString();
                document.cookie = "stop=1; path=/; expires="+time;
                on = false;
                this.console.value += '\n\nstopping OptiWeb service...\n';
                this.console.value += 'OptiWeb was disabled!';
            break;
            case 'exit' : window.location = "/";
            break;
            case 'shutdown' : 
                var expire_date = new Date();
                var ms_from_now = 60*60*1000;
                expire_date.setTime(expire_date.getTime() + ms_from_now);
                var time = expire_date.toGMTString();
                document.cookie = "shutdown=1; path=/; expires="+time;
                on = false;
                this.console.value += '\n\nShutting down apache service...\n';
                this.console.value += 'Apache service was stopped!';                
            break;
            default : this.console.value += '\nUnknown command "' + command+ '"! Try "help" to get help.';
        }
    }
    
    this.doGetCaretPosition = function () 
    {
        var caretPos = 0;
        // IE Support
        if (document.selection) 
        {
            this.console.focus ();
            var sel = document.selection.createRange ();

            sel.moveStart ('character', -this.console.value.length);

            caretPos = sel.text.length;
        }
        // Firefox support
        else if (this.console.selectionStart || this.console.selectionStart == '0')
        {
            caretPos = this.console.selectionStart;
        }
        return (caretPos);
    }
    
    this.setCaretPosition = function (pos)
    {
        if(this.console.setSelectionRange)
        {
            this.console.focus();
            this.console.setSelectionRange(pos,pos);
        }
        else if (this.console.createTextRange) 
        {
            var range = this.console.createTextRange();
            range.collapse(true);
            range.moveEnd('character', pos);
            range.moveStart('character', pos);
            range.select();
        }
    }    
}

function shutdown ()
{
    alert('eee');
}

function init()
{
    var c = new Console(get('console'));
    c.init(c);
    get('console'). focus();
    get('console').onkeydown = function (event) {return c.keyPress(event);};
    get('console').onclick = c.Click;
    get('example').onclick = function () {return c.addString(this.innerHTML);}
    
}
window.onload = init;