sfHover = function() {
	var sfEls = document.getElementById("nav").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

function resetFields(whichform) {
  for (var i=0; i<whichform.elements.length; i++) {
    var element = whichform.elements[i];
    if (element.type == "submit") continue;
		element.onfocus = function() {
		    if ( (this.name == "m3username") && (this.value == "Name") ) {
		      this.value = "";
		    }
			if ( (this.name == "m3email") && (this.value == "Email") ) {
		      this.value = "";
		    }
	    }
	    element.onblur = function() {
	      if (this.value == "") {
	        if(this.name == "m3username") {
				this.value = "Name";
			}
			if(this.name == "m3email") {
				this.value = "Email";
			}
	      }
    }
  }
}

function validateForm(whichform) {
  for (var i=0; i<whichform.elements.length; i++) {
    var element = whichform.elements[i];
    if (element.name.indexOf("m3username") != -1) {
      if (!isFilled(element)) {
        alert("Please fill in Name.");
        return false;
      }
    }
    if (element.name.indexOf("m3email") != -1) {
      if (!isEmail(element)) {
        alert("Email must be valid.");
        return false;
      }
    }
  }
  return true;
}

function isFilled(field) {
  if (field.value.length < 1 || field.value == field.defaultValue || field.value == "Name") {
    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) return false;
  if (!document.getElementById("m3username")) return false;
  if (!document.getElementById("m3email")) return false;
  var yourName = document.getElementById("m3username");
  yourName.value = "Name";
  var yourEmail = document.getElementById("m3email");
  yourEmail.value = "Email";  
  for (var i=0; i<document.forms.length; i++) {
    var thisform = document.forms[i];
	resetFields(thisform);
    thisform.onsubmit = function() {
      return validateForm(this);
    }
  }
}


addLoadEvent(prepareForms);