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


function focusLabels() {
  if (!document.getElementsByTagName) return false;
  var labels = document.getElementsByTagName("label");
  for (var i=0; i<labels.length; i++) {
    if (!labels[i].getAttribute("for")) continue;
    labels[i].onclick = function() {
      var id = this.getAttribute("for");
      if (!document.getElementById(id)) return false;
      var element = document.getElementById(id);
      element.focus();
    }
  }
}

function resetFields(whichform) {
  for (var i=0; i<whichform.elements.length; i++) {
    var element = whichform.elements[i];
    if (element.type == "submit") continue;
    if (!element.defaultValue) continue;
    element.onfocus = function() {
    if (this.value == this.defaultValue) {
      this.value = "";
     }
    }
    element.onblur = function() {
      if (this.value == "") {
        this.value = this.defaultValue;
      }
    }
  }
}

function validateForm(whichform) {
  for (var i=0; i<whichform.elements.length; i++) {
    var element = whichform.elements[i];
    var loading = document.getElementById("loading_img");
    var details = document.getElementById("details");
    if (element.className.indexOf("require") != -1) {
      if (!isFilled(element)) {
		details.style.display='block';  
		loading.style.display='none';
		alert("Please fill in the "+element.id+" field.");
        return false;
      }
    }
    if (element.className.indexOf("email") != -1) {
      if (!isEmail(element)) {
		details.style.display='block';  
		loading.style.display='none';		
        alert("The "+element.id+" field must be a valid email address.");
        return false;
      }
    }
  }
  loading.style.display='block';
  return true;
}

function isFilled(field) {
  if (field.value.length < 1 || field.value == field.defaultValue) {
    return false;
  } else {
    return true;
  }
}

function isEmail(field) {
  if (field.value.indexOf("@") == -1 || field.value.indexOf(".") == -1) {
    return false;
  } else {
    return true;
  }
}

function prepareForms() {
	if (!document.getElementById("details")) return false;
	var thisform = document.getElementById("details");
	resetFields(thisform);
	thisform.onsubmit = function() {
		document.getElementById('details').style.display='none';  
		if (!document.getElementById("loading_img")){
			var htwotag = document.getElementsByTagName('h2');
			for (var i=0; i<htwotag.length; i++) {
				displayLoading(htwotag[i]);
			}
		}
		return validateForm(this);
	}
}

function displayLoading(element) {
  var image = document.createElement("img");
  image.setAttribute("src","/images/loading.gif");
  image.setAttribute("alt","Loading...");
  image.setAttribute("id","loading_img");
  element.appendChild(image);
}

addLoadEvent(prepareForms);