// JavaScript Document
function formEvents(){
	//get all forms on page
	var allforms = document.getElementsByTagName('form');
	
	
	//loop through forms
	for(i=0;i<allforms.length;i++){
		
		//get all input tags in current form
		var thisform = allforms[i].getElementsByTagName('input');
		var message = allforms[i].getElementsByTagName('textarea');
		
		//loop through those inputs
		for(j=0;j<thisform.length;j++){
			//qualify that the input is a text input, and has a title, and isn't being excluded by using the 'noevents' class
			if(thisform[j].title && thisform[j].title != '' && thisform[j].type != 'image' && thisform[j].type != 'button' && thisform[j].type != 'submit' && thisform[i].className != 'noevents'){
				//assign the title to the value
				thisform[j].value = thisform[j].title;
				message[0].value = message[0].title;
				//events that will remove/replace default values into fields
				thisform[j].onfocus = function(){if(this.title == this.value){this.value = '';}}
				thisform[j].onblur = function(){if(this.value == ''){this.value = this.title;}}
				message[0].onfocus = function(){if(this.title == this.value){this.value = '';}}
				message[0].onblur = function(){if(this.value == ''){this.value = this.title;}}
			}
			
		}
	}
}

function validateForm(formname){
		//init an error message
			err = 'The following errors have occurred: \n';
			err_count = 0;
			
			var name = eval('document.'+formname+'.fromName.value');
			var from = eval('document.'+formname+'.sendFrom.value');
			var subj = eval('document.'+formname+'.subject.value');
			var body = eval('document.'+formname+'.sendBody.value');
			

		//validate form fields
		if(!isEmail(from)){
			err += 'Your Email is not valid.\n';
			err_count++;
		}
		if(!isText(name)){
			err += 'Your Name is not valid.\n';
			err_count++;
		}
		if(!isText(subj)){
			err += 'The Subject is not valid.\n';
			err_count++;
		}
		if(!isText(body)){
			err += 'The Message is not valid.\n';
			err_count++;
		}
		
		
		if(err_count == 0){
			eval('document.'+formname+'.submit()');
			return true;
		}else{
		alert(err);
		return false;
		}
	}
	
	function isEmail(str){
	if(str == '') return false;
	var re = /^[^\s()<>@,;:\/]+@\w[\w\.-]+\.[a-z]{2,}$/i
	return re.test(str);
	}
	
	function isText(str){
		if(str == '') return false;
		return true;
	}
	
function stripWhitespace(str, replacement){// NOT USED IN FORM VALIDATION
	if (replacement == null) replacement = '';
	var result = str;
	var re = /\s/g
	if(str.search(re) != -1){
		result = str.replace(re, replacement);
	}
	return result;
}






