
function AllEmbedLabels()
{
   this.all	= new Array();

   this.add	= function ( instance )
   {
      this.all.push( instance );
      return this.all.length - 1;
   }
}

var pageEmbedLabels	= new AllEmbedLabels();


function embedLabel( elementName, label )
{
  return new EmbedLabel( elementName, label );
}

function EmbedLabel( elementName, label )
{
   this.id		= pageEmbedLabels.add( this );

   this.label		= label;

   this.labelID		= "EmbedLabel" + this.id;
   this.fieldElement	= null;

   this.labelOn		= 0;

   this.buttonDisabled		= -1;
   this.buttonImage         = '';
   this.buttonImageOnSrc	= '';
   this.buttonImageOffSrc	= '';

   /**
    * Insert element so form can be located by tracing up the DOM tree
    */
   document.write( "<span id='" + this.labelID + "' style='position: absolute; visibility: hidden;'></span>" );

   this.labelElement	= document.getElementById( this.labelID );

   /**
    * Locate form field
    */
   parentForm	= locateParentElement( "FORM", this.labelElement );

   nameElements		= document.getElementsByName( elementName );

   for( var i = 0; i < nameElements.length && !this.fieldElement; i++ )
   {
      if( parentForm == locateParentElement( "FORM",  nameElements[i] ) )
      {
	 this.fieldElement	= nameElements[i];
      }
   }

   if( !this.fieldElement )
   {
      return null;
   }

   pageEvents.setElement( this.fieldElement, "onfocus", "pageEmbedLabels.all[ " + this.id + "].focus()"  );
   pageEvents.setElement( this.fieldElement, "onblur", "pageEmbedLabels.all[ " + this.id + "].blur()" );

   pageEvents.setElement( this.fieldElement, "onchange", "pageEmbedLabels.all[ " + this.id + "].change()" );
   pageEvents.setElement( this.fieldElement, "onkeyup", "pageEmbedLabels.all[ " + this.id + "].change()" );	// FF
   pageEvents.setElement( this.fieldElement, "onkeypress", "pageEmbedLabels.all[ " + this.id + "].change()" );	// IE

   this.focus = function ()
   {
      if( this.labelOn )
      {
	 this.fieldElement.value	= "";
	 this.labelOn	= 0;
      }
   }

   this.blur = function ()
   {
      if( ! this.fieldElement.value.length )
      {
	 this.fieldElement.value	= this.label;
	 this.labelOn	= 1;
      }
   }

   this.change = function ()
   {
/*
	  if( this.fieldElement.value.length && this.buttonDisabled != 0 )
      {
	 this.buttonImage.src		= this.buttonImageOnSrc;
	 this.buttonImage.disabled	= '';
	 this.buttonImage.style.cursor	= 'pointer';
	 this.buttonDisabled		= 0;
      }
      else if( ! this.fieldElement.value.length && this.buttonDisabled != 1 )
      {
	 this.buttonImage.src		= this.buttonImageOffSrc;
	 this.buttonImage.disabled	= 'disabled';
	 this.buttonImage.style.cursor	= 'default';
	 this.buttonDisabled		= 1;
      }
*/
   }

   this.activateButtonImage = function ( buttonElement, onSrc, offSrc )
   {
      this.buttonImage		= buttonElement;
      this.buttonImageOnSrc	= onSrc;
      this.buttonImageOffSrc	= offSrc;
      this.focus();
      this.change();
      this.blur();
   }

   this.blur();
}
