/* 
*  Copyright 2006-2010 Dynamic Site Solutions.
*  Free use of this script is permitted for commercial and non-commercial 
*  applications, subject to the requirement that this comment block be kept 
*  and not be altered.  The data and executable parts of the script may be 
*  changed as needed.  Dynamic Site Solutions makes no warranty regarding 
*  fitness of use or correct function of the script.  If you would like help
*  customizing this script or if you have other questions, contact 
*  "contact_us@dynamicsitesolutions.com".
*
*  Script by: Dynamic Site Solutions -- http://www.dynamicsitesolutions.com/
*  Last Updated: 2007-02-20
*/

function dss_addEvent(el,etype,fn) {
  if(el.addEventListener && (!window.opera || opera.version) &&
  (etype!='load')) {
    el.addEventListener(etype,fn,false);
  } else if(el.attachEvent) {
    el.attachEvent('on'+etype,fn);
  } else {
    if(typeof(fn) != "function") return;
    if(typeof(window.earlyNS4)=='undefined') {
      // to prevent this function from crashing Netscape versions before 4.02
      window.earlyNS4=((navigator.appName.toLowerCase()=='netscape')&&
      (parseFloat(navigator.appVersion)<4.02)&&document.layers);
    }
    if((typeof(el['on'+etype])=="function")&&!window.earlyNS4) {
      var tempFunc = el['on'+etype];
      el['on'+etype]=function(e){
        var a=tempFunc(e),b=fn(e);
        a=(typeof(a)=='undefined')?true:a;
        b=(typeof(b)=='undefined')?true:b;
        return (a&&b);
      }
    } else {
      el['on'+etype]=fn;
    }
  }
}

function clearText(thefield){
	if(thefield.defaultValue==thefield.value) thefield.value = '';
}
function restoreText(thefield){
	if(thefield.value=='') thefield.value = thefield.defaultValue;
}
function verify() {
  var f = document.forms['emailmsg'];
	var errorMsg = "Please fill in the following fields:\n\n";
	if(f.fromname.value == "") {
		errorMsg += "Your Name\n";
	}
	if(f.fromadr.value == "") {
		errorMsg += "Your Email Address\n";
	}
	if((f.msgtxt.value == f.msgtxt.defaultValue) || (f.msgtxt.value == "")) {
		errorMsg += "Your Message\n";
	}
	if(errorMsg != "Please fill in the following fields:\n\n") {
		alert(errorMsg);
		return false;
	}

var email = f.fromadr.value.toLowerCase();
var pattern = /^[-\.\w\+]+@((([a-z\d]([a-z\d-]*[a-z\d])*)\.)+([a-z]{2,})|((\d{1,3}\.){3}\d{1,3}))$/;
	if(!pattern.test(email)) {
		alert("Your email address does not appear to be valid.");
		return false;
	} else {
		return true;
	}
}
dss_addEvent(window,'load',function(){
  var f = document.forms['emailmsg'];
  f.onsubmit = verify;
  var msgtxt = f.elements['msgtxt'];
  msgtxt.onfocus = function(){clearText(this)}
  msgtxt.onblur = function(){restoreText(this)}
});

