//
// ManagedTextbox.js
//
// JavaScript class for maintaining a Managed textbox using DOM HTML
// to swap between display and edit elements.
//
// April 2005  -pge
//
// Constructor:
//  ManagedText
//   ( baseid[, textvalue, fieldname, nextid, previd, nullsok, blurproc] )
//   baseid - Name of JavaScript variable declared for this class instance.
//   textvalue - Initial value.
//   fieldname - Optional Name of CGI form input field name.
//   nextid - Optional Name of JavaScript variable to navigate to next.
//   previd - Optional Name of JavaScript variable to navigate back to.
//   nullsok - Optional boolean for null handling.
//   blurproc - Optional procedure name that takes baseid as arguments.
//     Called at end of onblur event handler (when text changes).
//
// Principal methods:
//  embed() - Called to create Text Display at desired location in document.
//  setText() - Called to change Text Displayed.
//
// Modified:
//
//   8/31/7  - Added not showing text when used for password.  -pge
//
                                                                                
// Constructor.
function ManagedTextbox
(
  baseid,
  textvalue,
  fieldname,
  nextid,
  previd,
  nullsok,
  blurproc,
  password
)
{
  this.baseid = baseid;
  if ( textvalue ) this.textvalue = encodeURIComponent( textvalue );
  else
  {
    if ( nullsok ) this.textvalue = encodeURIComponent( "<null>" );
    else this.textvalue = encodeURIComponent( "New Value" );
  }
  this.prevvalue = this.textvalue;
  this.fieldname = fieldname;
  this.nextid = nextid;
  this.previd = previd;
  this.nullsok = nullsok;
  this.blurproc = blurproc;
  this.password = password;
  this.span = new Object();
  this.hiddenelement = new Object();
  this.displayelement = new Object();
  this.editelement = new Object();
}
                                                                                
// Dummy class instance needed in JS1.1 before methods can be defined.
new ManagedTextbox( "junk","junk" );
                                                                                
// Class methods.
ManagedTextbox.prototype.embed = function( el )
{
  this.span = document.createElement( 'span' );
  this.span.id = this.baseid;
  this.span.className = 'mtb';
  this.buildText();
  var obj = document.getElementById( el );
  obj.appendChild( this.span );
}
ManagedTextbox.prototype.setText = function( textvalue )
{
  while( this.span.childNodes.length > 0 )
  {
    this.span.removeChild( this.span.firstChild );
  }
  this.prevvalue = this.textvalue;
  this.textvalue = encodeURIComponent( textvalue );
  this.buildText();
}
ManagedTextbox.prototype.mtb_a_click = function()
{
  if ( this.displayelement.firstChild.nodeType == 3 )
  {
    this.editelement.value = decodeURIComponent( this.textvalue );
    this.span.replaceChild( this.editelement, this.displayelement );
    this.editelement.select();
    this.editelement.focus();
  }
}
ManagedTextbox.prototype.mtb_text_blur = function( evt )
{
  evt = ( evt ) ? evt : ( ( window.event ) ? event : null );
  if ( evt )
  {
    var inp = ( evt.target ) ? evt.target :
               ( ( evt.srcElement ) ? evt.srcElement : null );
    if ( inp )
    {
      var obj = eval( inp.id.toString().split( "_" )[2] );
      var str;
      if ( inp.value.search( /^$|^ +$/ ) < 0 )
      {
        str = encodeURIComponent( inp.value );
      }
      else
      {
        if ( obj.nullsok ) str = encodeURIComponent( "<null>" );
        else str = obj.textvalue;
      }
      var str2 = decodeURIComponent( str );
      if ( str != obj.textvalue )
      {
        obj.prevvalue = obj.textvalue;
        obj.textvalue = str;
        if ( obj.hiddenelement ) obj.hiddenelement.value = str2;
      }
      var txt;
      if ( obj.password )
      {
        var str3 = "";
        for ( var i = 0; i < str2.length; i++ ) { str3 += "*"; }
        txt = document.createTextNode( str3 );
      }
      else { txt = document.createTextNode( str2 ); }
      while( obj.displayelement.childNodes.length > 0 )
      {
        obj.displayelement.removeChild( obj.displayelement.firstChild );
      }
      obj.displayelement.appendChild( txt );
      obj.span.replaceChild( obj.displayelement, inp );
      mtb_callRemote( inp.id );
     }
  }
}
ManagedTextbox.prototype.mtb_text_key = function( evt )
{
  evt = ( evt ) ? evt : ( ( window.event ) ? event : null );
  if ( evt )
  {
    var inp = ( evt.target ) ? evt.target :
               ( ( evt.srcElement ) ? evt.srcElement : null );
    if ( inp )
    {
      var c = ( evt.charCode ) ? evt.charCode :
                 ( ( evt.which ) ? evt.which : evt.keyCode );
      switch( c )
      {
        case mtb_cEnter :
          inp.blur();
          return false;
        case mtb_cEsc :
          mtb_resetText( inp.id );
          inp.blur();
          return false;
        case mtb_cRightAngleBracket :
          inp.blur();
          mtb_editNext( inp.id, false );
          return false;
        case mtb_cLeftAngleBracket :
          inp.blur();
          mtb_editNext( inp.id, true );
          return false;
        default :
          return true;
      }
    }
  }
  return true;
}
ManagedTextbox.prototype.buildText = function()
{
  var str = decodeURIComponent( this.textvalue );
  if ( this.fieldname )
  {
    this.hiddenelement = document.createElement( "input" );
    this.hiddenelement.type = "hidden";
    this.hiddenelement.id = "mtb_hidden_" + this.baseid;
    this.hiddenelement.name = this.fieldname;
    this.hiddenelement.value = str;
    this.span.appendChild( this.hiddenelement );
  }
  this.displayelement = document.createElement( "a" );
  this.displayelement.id = "mtb_a_" + this.baseid;
  this.displayelement.className = "mtb_display";
  this.displayelement.setAttribute
  ( "href", "javascript:" + this.baseid + ".mtb_a_click()" );
  var txt;
  if ( this.password )
  {
    var str2 = "";
    for ( var i = 0; i < str.length; i++ ) { str2 += "*"; }
    txt = document.createTextNode( str2 );
  }
  else
  { txt = document.createTextNode( decodeURIComponent( this.textvalue ) ); }
  this.displayelement.appendChild( txt );
  this.span.appendChild( this.displayelement );
  this.editelement = document.createElement( "input" );
  if ( this.password ) this.editelement.type = "password";
  else this.editelement.type = "text";
  this.editelement.id = "mtb_text_" + this.baseid;
  this.editelement.className = "mtb_edit";
  this.editelement.onblur = this.mtb_text_blur;
  this.editelement.onkeypress = this.mtb_text_key;
  this.editelement.value = decodeURIComponent( this.textvalue );
}
ManagedTextbox.prototype.toString = function()
{
  return decodeURIComponent( this.textvalue );
}
ManagedTextbox.prototype.valueOf = function()
{
  return decodeURIComponent( this.textvalue );
}
ManagedTextbox.prototype.previousValue = function()
{
  return decodeURIComponent( this.prevvalue );
}

// Global methods.
function mtb_resetText( el )
{
  var obj = eval( el.toString().split( "_" )[2] );
  if ( obj && obj.editelement )
  {
    obj.editelement.value = decodeURIComponent( obj.textvalue );
  }
}
function mtb_editNext( el, prev )
{
  var obj = eval( el.toString().split( "_" )[2] );
  if ( obj )
  {
    var nobj;
    if ( prev ) { if ( obj.previd ) nobj = eval( obj.previd ); }
    else { if ( obj.nextid ) nobj = eval( obj.nextid ); }
    if ( nobj ) { nobj.displayelement.click(); }
  }
}
function mtb_callRemote( el )
{
  var obj = eval( el.toString().split( "_" )[2] );
  if ( obj && obj.blurproc && obj.textvalue != obj.prevvalue )
  {
    eval( obj.blurproc + "('" + obj.baseid + "')" );
  }
}

// Keycodes.
var mtb_cBackSpace = 8;
var mtb_cTab = 9;
var mtb_cEnter = 13;
var mtb_cEsc = 27;
var mtb_cSpace = 32;
var mtb_cForwardSlash = 47;
var mtb_cColon = 58;
var mtb_cSemiColon = 59;
var mtb_cLeftAngleBracket = 91;
var mtb_cBackSlash = 92;
var mtb_cRightAngleBracket = 93;
var mtb_cDelete = 127;


