/* autolabel-password1.js
   $Id: autolabel-password1.js,v 1.2 2008/12/19 18:00:32 dferruggia Exp $ */

// Graytext color is currently set to "#999"
// Search for that (in 2 places) if you want to change it

function addLoadEvent(func){
	var oldonload = window.onload;
	if (typeof window.onload != 'function'){
		window.onload = func;
	} else {
		window.onload = function(){
			oldonload();
			func();
		}
	}
}

function labelFields() {
	if (!document.getElementsByTagName){ return; }
	var allfields = document.getElementsByTagName("input");	
	for (var i=0; i<allfields.length; i++){ 	// loop through all input tags and add events
		initField(allfields[i]);
	}
	var allfields2 = document.getElementsByTagName("textarea");	
	for (var i=0; i<allfields2.length; i++){ 	// loop through all textarea tags and add events
		initField(allfields2[i]);
	}
}

function initField(field) {
	if (field && field.getAttribute("rel") && !field.getAttribute("rel").indexOf("autolabel") != -1) { //prevent misfire
		var graycolor = "#999";
		if ((field.type == "password" || field.type == "text" || field.type == "textarea") && (field.value != null)) {	// include text boxes, exclude empty ones
			field.style.color = graycolor;
			field.graytext = field.value;
			field.onfocus = function () {
				if (this.value==this.graytext){
					this.style.color="#000";
					this.value="";
				} else {
					this.select();
				}
			}
			field.onblur = function () {
				if (this.value=="") {
					this.style.color=graycolor;
					this.value=this.graytext;
				}
			}
		}
	}
}

function initOverLabels () {
  if (!document.getElementById) return;      

  var labels, id, field;

  // Set focus and blur handlers to hide and show 
  // labels with 'overlabel' class names.
  labels = document.getElementsByTagName('label');
  for (var i = 0; i < labels.length; i++) {

    if (labels[i].className == 'overlabel') {

      // Skip labels that do not have a named association
      // with another field.
      id = labels[i].htmlFor || labels[i].getAttribute('for');
      if (!id || !(field = document.getElementById(id))) {
        continue;
      } 

      // Change the applied class to hover the label 
      // over the form field.
      labels[i].className = 'overlabel-apply';

      // Hide any fields having an initial value.
      if (field.value !== '') {
        hideLabel(field.getAttribute('id'), true);
      }

      // Set handlers to show and hide labels.
      field.onfocus = function () {
        hideLabel(this.getAttribute('id'), true);
        this.select();
      };
      field.onblur = function () {
        if (this.value === '') {
          hideLabel(this.getAttribute('id'), false);
        }
      };

      // Handle clicks to label elements (for Safari).
      labels[i].onclick = function () {
        var id, field;
        id = this.getAttribute('for');
        if (id && (field = document.getElementById(id))) {
          field.focus();
        }
      };

    }
  }
};

function hideLabel (field_id, hide) {
  var field_for;
  var labels = document.getElementsByTagName('label');
  for (var i = 0; i < labels.length; i++) {
    field_for = labels[i].htmlFor || labels[i].getAttribute('for');
    if (field_for == field_id) {
      labels[i].style.textIndent = (hide) ? '-1000px' : '0px';
      return true;
    }
  }
}
function passwordInit()
{
	setTimeout(initOverLabels,50);
}

addLoadEvent(labelFields); //Has to be in this order
addLoadEvent(passwordInit);
