function addFormEvent(func) {
  if (!document.getElementById | !document.getElementsByTagName) return
  var oldonload=window.onload
  if (typeof window.onload != 'function') { window.onload=func }
  else {
    window.onload=function() { oldonload(); func() }
  }
}



// --------------------- borders for text type inputs ------------------------------ //
function attachBorderAndSelect(id){
  var obj0 = document.getElementById(id)
  obj0.onfocus= function() {this.style.border="2px solid #f00"; this.select();}
  obj0.onblur = function() {
                  this.style.border="2px inset #ccc"
                  if (this.value=="") {this.value=this.defaultValue}
                  this.value=this.value
                }
}
// ----------------------------------------------------------------------------- //


// ------------------------------ border for submit buttons --------------------------- //
function attachBorderToButton(id){
  var obj0 = document.getElementById(id)
  obj0.onfocus= function() {this.style.border="2px solid #f00"}
  obj0.onblur = function() {this.style.border="2px outset #fff"}
}
// ----------------------------------------------------------------------------- //


// text type inputs
 attachBorderAndSelect('name');
 attachBorderAndSelect('email');
 attachBorderAndSelect('phone'); 

// textfield
 attachBorderAndSelect('message');


// submit
 attachBorderToButton('Submit');


// -------------------------- form validation ------------------------- //

function validEmail(email) {
			invalidChars = " /:,;"
	
			if (email == "") {
				return false
			}
			for (i=0; i<invalidChars.length; i++) {
				badChar = invalidChars.charAt(i)
				if (email.indexOf(badChar,0) > -1) {
					return false
				}
			}
			atPos = email.indexOf("@",1)
			if (atPos == -1) {
				return false
			}
			if (email.indexOf("@",atPos+1) > -1) {
				return false
			}
			periodPos = email.indexOf(".",atPos)
			if (periodPos == -1) {
				return false
			}
			if (periodPos+3 > email.length)	{
				return false
			}
			return true
		}
		
		function formChecker() {

 			 if (document.contactform.name.value=="") {
			return true;
			}
        }
		
         function messChecker() {
  			if (document.contactform.message.value=="") {
			return true;
			}
		
  		} 
	

		function submitIt(contactform) {
			
			if (formChecker(contactform.name.value)) {
				alert("Please enter a name")
				contactform.name.focus()
				contactform.name.select()
				return false
			}
			
			
			if (!validEmail(contactform.emailadd.value)) {
				alert("Please enter a valid email address")
				contactform.emailadd.focus()
				contactform.emailadd.select()
				return false
			}
	
			
			if (messChecker(contactform.message.value)) {
				alert("Please enter a message")
				contactform.message.focus()
				contactform.message.select()
				return false
			}
	
			return true
}