/**
Script Title: Form Validation
Script Author: Raymond Angana
Date Created: 3/10/09
This notice must stay intact for legal use
*/
window.addEventListener?window.addEventListener('load',function () {
	ray.init('myform');
	},false):window.attachEvent('onload',function () {
	ray.init('myform');
	}); // FF : IE
					
var ray = {
	defaults:['Name:','Phone Number:','Email Address:','Inquiry:'],
	
	init:function (el) {
		var elmnts = this.getID(el).elements;
		this.getID(el).onsubmit = function () {
			if (this.rname.value == '' || this.rname.value == ray.defaults[0]) {
				ray.err ('Name field is mandatory',this.rname);
				return false;
			}
			if (this.email.value == '' || this.email.value == ray.defaults[1]) {
				ray.err ('Email address field is mandatory',this.email);
				return false;
			}
			if (!this.email.value.match(/^([a-z0-9])([\w\.\-\+])+([a-z0-9])\@(([\w\-]?)+\.)+([a-z]{2,4})$/i)) {
				ray.err ('Email address field is invalid',this.email);
				return false;
			}
		}

		for (var i = 0 ; i < (elmnts.length - 2 ) ; i++) {
			elmnts[i].onfocus = function () {
				if (this.name=='rname')
					this.value=this.value==ray.defaults[0]?'':this.value==''?ray.defaults[0]:this.value;
				if (this.name=='phone')
					this.value=this.value==ray.defaults[1]?'':this.value==''?ray.defaults[1]:this.value;
				if (this.name=='email')
					this.value=this.value==ray.defaults[2]?'':this.value==''?ray.defaults[2]:this.value;
				if (this.name=='inquiry')
					this.value=this.value==ray.defaults[3]?'':this.value==''?ray.defaults[3]:this.value;
			}

			elmnts[i].onblur = function () {
				if (this.name=='rname')
					this.value=this.value==ray.defaults[0]?'':this.value==''?ray.defaults[0]:this.value;
				if (this.name=='phone')
					this.value=this.value==ray.defaults[1]?'':this.value==''?ray.defaults[1]:this.value;
				if (this.name=='email')
					this.value=this.value==ray.defaults[2]?'':this.value==''?ray.defaults[2]:this.value;
				if (this.name=='inquiry')
					this.value=this.value==ray.defaults[3]?'':this.value==''?ray.defaults[3]:this.value;
			}
		}
	},
				
	err:function (message,el) {
		alert(message);
		el.focus();
	},
			
	getID:function (el) {
		return document.getElementById(el);
	}
}