//helps initiate all functions on window load

function addLoadEvent(func){
	var oldonload=window.onload;
	if(typeof window.onload !='function'){
		window.onload=func;
	}
	else{
		window.onload=function(){
			oldonload();
			func();
		}
	}
}




//this function resets the fields as they are clicked on

function resetFields(whichform){
	for(var i=0; i<whichform.elements.length; i++){
		var element=whichform.elements[i];
		if(element.type=="submit")continue;
		if(!element.defaultValue)continue;
		element.onfocus=function(){
			if(this.value==this.defaultValue){
				this.value="";
			}
		}
		element.onblur=function(){
			if(this.value==""){
				this.value=this.defaultValue;
			}
		}
	}
}

//this function checks the fields of the form

function prepareForms(){
	for(var i=0; i<document.forms.length; i++){
		var thisform=document.forms[i];
		resetFields(thisform);
		thisform.onsubmit=function(){
			return validateForm(this);
	}
}
}
addLoadEvent(prepareForms);


//this function chekcs if a field is filled
function isFilled(field){
	if(field.value.length<1 ||field.value==field.defaultValue){
		return false;
	}else{
		return true;
	}
}

//this function checks to see if the characters needed for an email exist
function isEmail(field){
	if(field.value.indexOf("@")==-1||field.value.indexOf(".")==-1)
	{
		return false;
	}else{
		return true;
	}
}
//this function makes sure the fields are filled and if not gives the user a warning
function validateForm(whichform){
	for (var i=0; i<whichform.elements.length; i++){
		var element=whichform.elements[i];
		if(element.className.indexOf("required")!=-1){
									 if(!isFilled(element)){
										 alert("please fill in the "+element.name+" field.");
										 return false;
									 }
									 }
									 if(element.className.indexOf("email") !=-1){
										 if(!isEmail(element)){
											 alert
											 ("The "+element.name+" field must be a valid e-mail address.");
											 return false;
										 }
									 }
	}
	return true;
}





