// 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 -- ');
}
}







//------------------- Son-Of-Sucker-Fish IE Hack -------------------//

sfHover = function() {
          var sfEls = document.getElementById("nav").getElementsByTagName("li");
          // for each list item in the menu...
          for (var i=0; i < sfEls.length; i++) {
                   // Is this IE7?  If so, use onmouseleave to fix the fact that onmouseout won't fire
                   is_IE7 = navigator.appVersion.indexOf("MSIE 7.0") != -1;
                   sfEls[i].onmouseover = function() {
                             this.className+=" sfHover";
                             // is this a top-level menu item?
                             var child_ul = this.getElementsByTagName('ul')[0];
                             if (child_ul && is_IE7){
                                      // fix for IE7
                                      child_ul.style.position = 'static';
                             }
                   }
                   sfEls[i].onmouseleave = function() {
                             // is this a top-level menu item?
                             var child_ul = this.getElementsByTagName('ul')[0];
                             if (child_ul && is_IE7){
                                      // fix for IE7
                                      child_ul.style.position = 'absolute';
                                      child_ul.style.left = '-9000px';
                             }
                   }
                   sfEls[i].onmouseout = function() {
                             this.className=this.className.replace(new RegExp(" sfHover\\b"), "");
                   }
          }
}
if (window.attachEvent) window.attachEvent("onload", sfHover);







// -------------------- 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;
}
