function highlightBlue(theBox,theHighlight) {
	if(theHighlight) {
		theBox.style.borderColor='#003366';
	} else {
		theBox.style.borderColor='#7F99B2';
	}
}

function highlightRed(theBox,theHighlight) {
	if(theHighlight) {
		theBox.style.borderColor='#B00D01';
	} else {
		theBox.style.borderColor='#D78680';
	}
}

function checkBlank(myField,myName) {
	var result = true;
	if (myField.value == "") {
		alert('Please complete the "' + myName + '" field.');
		myField.focus();
		result = false;
	}
	return result;
}

function isEmailAddr(email) {
	var result = false;
	var theStr = new String(email);
	var index = theStr.indexOf("@");
	if (index > 0) {
		var pindex = theStr.indexOf(".",index);
		if ((pindex > index+1) && (theStr.length > pindex+1)) {
			result = true;
		}
	}
	return result;
}

function validEmail(formField,fieldLabel,required) {
	var result = true;
	if (required && !checkBlank(formField,fieldLabel)) {
		result = false;
	}
	if (result && ((formField.value.length < 3) || !isEmailAddr(formField.value)) ) {
		alert("Please make sure your e-mail address is entered correctly in the format name@domain.com.");
		formField.focus();
		result = false;
	}
	return result;
}

function validate(theForm) {
	return checkBlank(theForm.fname,"Name")
		&& validEmail(theForm.femail,"E-Mail",true)
		&& checkBlank(theForm.fmessage,"Message")
		&& checkBlank(theForm.freferral,"Referral");
}

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

function prepareInputsForHints() {
  var inputs = document.getElementsByTagName("input");
  for (var i=0; i<inputs.length; i++){
    if (inputs[i].parentNode.getElementsByTagName("span")[0]) {
      inputs[i].onfocus = function () {
        this.parentNode.getElementsByTagName("span")[0].style.display = "inline";
      }
      inputs[i].onblur = function () {
        this.parentNode.getElementsByTagName("span")[0].style.display = "none";
      }
    }
  }
  var textareas = document.getElementsByTagName("textarea");
  for (var m=0; m<textareas.length; m++){
    textareas[m].onfocus = function () {
      this.parentNode.getElementsByTagName("span")[0].style.display = "inline";
    }
    textareas[m].onblur = function () {
      this.parentNode.getElementsByTagName("span")[0].style.display = "none";
    }
  }
}

addLoadEvent(prepareInputsForHints);