function ShellC(a_prompts)
{
  this.s_output_id = "#shell_content";
  this.s_input_id = "#shell_input_text";
  this.shell_input_reflex ="#shell_content .shell_input_reflex";
  //this.n_delay = 50;
  //this.n_current_char = 0;
  this.s_text = "";
  this.a_data = new Array();
  this.shell_cursor_id = ".shell_cursor";
  this.shell_surface = "#monitor_surface";
  this.a_prompt_data = a_prompts;
  this.n_prompt_data = this.a_prompt_data.length - 1;
  this.n_datapos = 0;
  this.callback = "";
  this.b_stop = false;
}//fn




ShellC.prototype.set_prompt_data = function(a_prompts)
{
  this.a_prompt_data = a_prompts;
  this.n_prompt_data = this.a_prompt_data.length - 1;
  this.n_datapos = 0;  
}//fun


ShellC.prototype.display = function(s_content, b_all)
{
  if (b_all) {o_shell.clean();};
  $("#shell_pre_content").append(s_content);
}//fun

ShellC.prototype.clean = function()
{
  $("#shell_content").html("");
  $("#shell_pre_content").html("");
  $("#shell_intro").html("");
  o_shell.scroll_to(0);
}//fun


ShellC.prototype.off = function()
{
  o_shell.b_stop = true;
  $("#monitor_scroll_content").html("");
}//fun

ShellC.prototype.show_loader = function()
{
  $("#shell_loader").css("display", "block");    
}//fun

ShellC.prototype.hide_loader = function()
{
  $("#shell_loader").css("display", "none");    
}//fun

ShellC.prototype.redirect = function(s_url, n_time)
{
  o_shell.b_stop = true;
  o_shell.clean();
  o_shell.show_loader();
  setTimeout("change_page('"+ s_url+ "')", n_time);
}//fun




ShellC.prototype.reinit_error = function(a_prompts, s_error)
{
  o_shell.b_stop = false;
  o_shell.set_prompt_data(a_prompts);
  o_shell.start(true);
  o_shell.display_error(s_error);
}//fun

ShellC.prototype.display_error = function(s_error)
{
  $("#shell_pre_content").append("<br><span class='shell_error'>"+ s_error+ "</span>");
}//fun

ShellC.prototype.scroll_to = function(n_top0_end1)
{
  var o_divmon = document.getElementById("monitor_scroll_content");
  
  if (n_top0_end1 == 0)
  {
    o_divmon.scrollTop = n_top0_end1; 
  }//if
  else
  {
    o_divmon.scrollTop = o_divmon.scrollHeight;    
  }//if  
}//fun

ShellC.prototype.start = function(b_restart)
{
  o_shell.n_datapos = 0;
  o_shell.scroll_to(0);
  $(o_shell.s_input_id).val("");
  $(o_shell.s_output_id).html("");
  $("#shell_pre_content").html("");
    
  if (!b_restart)
  {
    
    $("#shell_port").css("display", "block");
    
    listen_action_key("shell_input_text", 13, "o_shell.analyze");
    
    $(o_shell.shell_surface).click(function()
    {
      $(o_shell.s_input_id).focus();
    });
    
    $(o_shell.s_input_id).blur(function()
    {
      o_shell.hide_cursor();
    });         

    $(o_shell.s_input_id).focus(function()
    {
      o_shell.show_cursor();
    });             
  }

  $(o_shell.s_input_id).focus();
  o_shell.listen_input_field();  
  o_shell.rundata();
}//fn


ShellC.prototype.rundata = function()
{
  var n_x = o_shell.n_datapos;
  var s_field = o_shell.a_prompt_data[n_x]["l"];
  $(o_shell.s_output_id).html($("#prompt4shell").html());
  $(o_shell.s_output_id+ " .shell_dataprompt").html(s_field);
}//fun


ShellC.prototype.analyze = function()
{
  var s_data = $(o_shell.s_output_id).html();
  $(o_shell.s_output_id).html("");
  //alert(s_data);
   
  //append visualy the content to previous 
  $("#shell_pre_content").append(s_data);
  $("#shell_pre_content .shell_cursor").remove();
  
  //clean buffer
  var s_buff_val = $(o_shell.s_input_id).val();
  $(o_shell.s_input_id).val("");
  
  
  //scroll div to bottom
  o_shell.scroll_to(1);
  
  //get data
  o_shell.a_data[o_shell.a_prompt_data[o_shell.n_datapos]["d"]] = s_buff_val;
  
  //if valid data
  var b_error = false;
  var s_fun = o_shell.a_prompt_data[o_shell.n_datapos]["f"];
  if (s_fun != "")
  {
    eval("var s_result = "+ s_fun+ "()");
    if (s_result != "")
    {
      b_error = true;
      o_shell.display_error(s_result);      
    }//if
  }//if
  
  
  if (b_error == false)
  {
  
    if (o_shell.n_datapos < o_shell.n_prompt_data)
    {
      o_shell.n_datapos++;
    }//if
    else
    {
      if (o_shell.callback != "")
      {
        eval(o_shell.callback+ "()");
      }//if
      else
      {
        o_shell.start(true);
      }//el
    }//el
  }//if  
  
  if (o_shell.b_stop == false)
  {
    o_shell.rundata();
  }//if
}//fn

ShellC.prototype.hide_cursor = function()
{
  $(this.shell_cursor_id).css("visibility", "hidden");  
}//fn

ShellC.prototype.show_cursor = function()
{
  $(this.shell_cursor_id).css("visibility", "visible");  
}//fn


/*
ShellC.prototype.type_loop = function(s_callback)
{
  $(this.s_output_id).append(this.s_text[this.n_current_char]);
  this.n_current_char++;
  
  if (this.n_current_char>=this.s_text.length)
  {
    this.n_current_char=0;
    if (s_callback != null)
    {
      eval(s_callback+ "()");
    }
    //setTimeout("o_shell.type_loop()", 5000);
  }
  else
  {
    setTimeout("o_shell.type_loop('"+ s_callback+ "')", this.n_delay);
  }
}

ShellC.prototype.type = function(s_text, s_callback)
{
  this.s_text = s_text;
  this.type_loop(s_callback);
}//fun
*/
ShellC.prototype.ignore_key = function(e)
{
  var b_resp = false;
  var KEY_LEFT = 37;var KEY_RIGHT = 39;var KEY_UP = 38;var KEY_DOWN = 40;var KEY_TAB = 11;
  var n_code = e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0;
  if ((n_code == KEY_LEFT) || (n_code == KEY_RIGHT) || (n_code == KEY_UP) || (n_code == KEY_DOWN))
  {
    b_resp = true; 
    e = null;
  }//if
  return(b_resp);
}//fun



ShellC.prototype.listen_input_field = function()
{ 
  $(o_shell.s_input_id).keyup(function(e)
  {
    
    if (o_shell.ignore_key(e) == false)
    {
      var s_content = $(o_shell.s_input_id).val();
      if (o_shell.a_prompt_data[o_shell.n_datapos]["t"] == "pass")
      {
        s_content = o_shell.str_pad("", s_content.length, "*", "STR_PAD_LEFT");
      }//if
      $(o_shell.shell_input_reflex).html(s_content);
    }//if
    else
    {
      $(o_shell.s_input_id).val("");
      $(o_shell.shell_input_reflex).html("");
      //$(o_shell.s_input_id).select();
      
    }
  });  
}//fun

ShellC.prototype.set_callback = function(s_callback)
{
  o_shell.callback = s_callback;    
}



ShellC.prototype.str_pad = function(input, pad_length, pad_string, pad_type) {
  return(str_pad(input, pad_length, pad_string, pad_type));    
}
