/***************************************************************\
 * ElementInfo.js                                              *
 *                                                             *
 * Copyright (c) 2008 Heaps of Flavour Pty Ltd                 *
 * $Id: ElementInfo.js,v 1.1.2.1 2008/02/01 04:10:56 steve Exp $ *
 *                                                             *
 * A class for adding default informative text to text fields. *
 *                                                             *
\***************************************************************/

function ElementInfo(field, text) {
   this.txt = text;
   this.origCol = field.style.color || '#000000';
   this.displayed = true;
   this.f = field;
   this.showInfo = function() {
      this.f.value = this.txt;
      this.f.style.color = '#808080';
      this.displayed = true;
   };
   this.hideInfo = function() {
      this.f.value = '';
      this.f.style.color = this.origCol;
      this.displayed = false;
   };
};

/* Adds info text to a field */
function addElementInfo(field, text) {
   if (!field) return;
   if (field.type != 'text') return;

   field.info = new ElementInfo(field, text);
   field.onclick = function() {
      if (this.info.displayed && this.value == this.info.txt) { 
         this.info.hideInfo();
      }
   };
   field.onfocus = field.onclick;
   field.onblur = function() {
      if (this.value == '') {
         this.info.showInfo();
      }
   };
   field.title = text;

   // show the info text initially
   field.onblur();
}

/* Removes info text from all fields in the form - useful when submitting the form */
function removeElementInfo(frm) {
   if (!frm) return;
   if (!frm.elements) return;
   // reset field values
   for (var i = 0; i < frm.elements.length; i++) {
      if (frm.elements[i].type != 'text') continue;
      if (frm.elements[i].info && frm.elements[i].info.displayed) {
         if (frm.elements[i].info.hideInfo());
      }
   }
}
