var inputs = null;
var labels = null;
var textareas = null;

function loadLabels(formId) {
  var formElt = this.document.getElementById(formId);
  inputs = formElt.getElementsByTagName("input");
  labels = formElt.getElementsByTagName("label");
  textareas = formElt.getElementsByTagName("textarea");
  
  if (textareas[0].value == "") {
    textareas[0].value = getLabelByInputId(labels,textareas[0].id);
    addEvent(textareas[0],'focus',clearLabel);
    addEvent(textareas[0],'blur',getLabel);
  }

  for (var i = 0; i < inputs.length; i++) {
    if(inputs[i].id == "copy" || !(inputs[i].id))
       continue;
    if (inputs[i].value != "")
        continue;
    inputs[i].value = getLabelByInputId(labels,inputs[i].id);
    addEvent(inputs[i],'focus',clearLabel);
    addEvent(inputs[i],'blur',getLabel);
  }

  addEvent(formElt,'submit',clearLabels);

  addEvent(window,'unload',EventCache.flush);
}

function getLabel(e){
  var targ = null;
  if (e.target) targ = e.target;
  else if (e.srcElement) targ = e.srcElement;
  if (targ.nodeType == 3) // defeat Safari bug
    targ = targ.parentNode;
  if (targ.value == "") {
      targ.value = getLabelByInputId(labels,targ.id);
   }
}

function clearLabel(e) {
  var targ = null;
  if (e.target) targ = e.target;
  else if (e.srcElement) targ = e.srcElement;
  if (targ.nodeType == 3) // defeat Safari bug
    targ = targ.parentNode;
  if (targ.value == getLabelByInputId(labels,targ.id)) {
      targ.value = "";
   }
}

function clearLabels(e) {
  if (textareas[0].value == getLabelByInputId(labels,textareas[0].id))
        textareas[0].value = "";
  for (var i = 0; i < inputs.length; i++) {
    if(inputs[i].id == "copy")
        continue;
    if (inputs[i].value == getLabelByInputId(labels,inputs[i].id))
        inputs[i].value = "";
  }
}

function getLabelByInputId(labels, id) {
      for (var i = 0; i < labels.length; i++) {
        if (id == labels[i].htmlFor){
          return labels[i].firstChild.data;
        }
      }
      return "";
}

function addEvent( obj, type, fn ) {
	if (obj.addEventListener) {
		obj.addEventListener( type, fn, false );
		EventCache.add(obj, type, fn);
	}
	else if (obj.attachEvent) {
		obj["e"+type+fn] = fn;
		obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
		obj.attachEvent( "on"+type, obj[type+fn] );
		EventCache.add(obj, type, fn);
	}
	else {
		obj["on"+type] = obj["e"+type+fn];
	}
}

var EventCache = function(){
	var listEvents = [];
	return {
		listEvents : listEvents,
		add : function(node, sEventName, fHandler){
			listEvents.push(arguments);
		},
		flush : function(){
			var i, item;
			for(i = listEvents.length - 1; i >= 0; i = i - 1){
				item = listEvents[i];
				if(item[0].removeEventListener){
					item[0].removeEventListener(item[1], item[2], item[3]);
				};
				if(item[1].substring(0, 2) != "on"){
					item[1] = "on" + item[1];
				};
				if(item[0].detachEvent){
					item[0].detachEvent(item[1], item[2]);
				};
				item[0][item[1]] = null;
			};
		}
	};
}();
