// JavaScript Document

addLoadEvent(prepareForms);

// -------------------- Add Load Event -------------------- //
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}






// Hide Email Addresses from Harvesters
function hideaddy(em1, em2) {
if (em1 && em2) {
document.write('<a href=\"mailto:' + em1 + '@' + em2 +'.org\">' + em1 + '@' + em2 + '.org</a>');
} else {
document.write(' -- No email address supplied -- ');
}
}











// -------------------- Prepare all forms to be cleared out and validated  -------------------- //
function prepareForms() {
  for (var i=0; i<document.forms.length; i++) {
    var thisform = document.forms[i];
    resetFields(thisform);
    thisform.onsubmit = function() {
      return validateForm(this);
    }
  }
}








// -------------------- Clear out form fields -------------------- //
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.style.color ="#888888";
	
    element.onfocus = function() {
    if (this.value == this.defaultValue) {
      this.value = "";
	 //this.style.color = "#000000";
     }
    }
    element.onblur = function() {
      if (this.value == "") {
        this.value = this.defaultValue;
		//this.style.color = "#888888";
      }
    }
  }
}








// -------------------- Make sure form is filled with a non-default value -------------------- //
function isFilled(field) {
		if (field.value.length < 1 || field.value == field.defaultValue) {
			return false;
		} 
		
		else {
			return true;
		}
}


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


// -------------------- Form Validation -------------------- //
function validateForm(whichform) {
  for (var i=0; i<whichform.elements.length; i++) {
    var element = whichform.elements[i];
    if (element.className.indexOf("required") != -1) {
      if (!isFilled(element)) {
        alert("Please fill in the "+element.name+" field.");
        return false;
      }
    }
    if (element.className.indexOf("email") != -1) {
      if (!isEmail(element)) {
		
        alert("The "+element.name+" field must be a valid email address.");
        return false;
      }
    }
  }
  return true;
}
	
	
	
	
	
	
