// If text within a label is clicked, the associated form field is brough into focus.

function focusLabels() {
  if (!document.getElementsByTagName) return false;
  var labels = document.getElementsByTagName("label");		// Get all the label elements in the document.
  for (var i=0; i<labels.length; i++) {
    if (!labels[i].getAttribute("for")) continue;					// If the label has a for attribute, attach an event handler.
    labels[i].onclick = function() {								// When the label is clicked on, extract the value of the for attribute.
      var id = this.getAttribute("for");							// This value is the id of a form element.
      if (!document.getElementById(id)) return false;			// Make sure the form element exists.
      var element = document.getElementById(id);				// Bring that form element into focus.
      element.focus();
    }
  }
}

// Default values are automatically removed whenever a field is brought into focus.
// If user moves on without entering information the default value is replaced.

function resetFields(whichform) {
  for (var i=0; i<whichform.elements.length; i++) {			// Loop through all elements in the form.
    var element = whichform.elements[i];
    if (element.type == "submit") continue;					// If the element is a submit button, move on
    															//      to the next iteration of the loop.
    if (element.type == "radio") continue;						// If the element is a radio button, on to next.
    if (!element.defaultValue) continue;						// If the element doesn't have a default value,
    															//      move on to the next iteration.
    element.onfocus = function() {							// Otherwise, add an event handler for when
    															//      the element is brought into focus:
    if (this.value == this.defaultValue) {						// * Set the value of the element to empty.
      this.value = "";
     }
    }
    element.onblur = function() {								// Add another event handler for when the
    															//      element no longer has focus.
      if (this.value == "") {									// * If the value of the element is empty, change
        this.value = this.defaultValue;							//      it back to its default value.
      }
    }
  }
}

// Loops through each Form object and passes each one to resetFields function.

function prepareForms() {
  for (var i=0; i<document.forms.length; i++) {
    var thisform = document.forms[i];
    resetFields(thisform);
    thisform.onsubmit = function() {
    	return validateForm(this);
    }
  }
}

// Takes an element from a form as its single argument; returns true if filled in and false if bypassed.

function isFilled(field) {
	if (field.value.length < 1 || field.value == field.defaultValue) {
		return false;
	} else {
		return true;
	}
}

// Actual function to validate the form.

function validateForm(whichform) {
	for (var i=0; i<whichform.elements.length; i++) {		// Loop through the elements array of the form.
		var element = whichform.elements[i];
		if (element.className.indexOf("printifvalue") != -1) {		// If 'printifvalue' is in className property
																//      pass element to isFilled function.
			if (!isFilled(element)) {							// If isFilled returns false, assign an empty value
				element.value = "";							//      to prevent 'example' text from being sent.
				continue;
			}
		}
	}
	return true;
}

addLoadEvent(focusLabels);
addLoadEvent(prepareForms);