// Javascript form validation


function fValidateForm(){
	var gName = document.forms.contactForm.contactName.className;
	var gPhone = document.forms.contactForm.contactPhone.className;
	var gCompany = document.forms.contactForm.contactCompany.className;
	var gEmail = document.forms.contactForm.contactEmail.className;
	
	
	if(gName == "correct" && gPhone == "correct" && gCompany == "correct" && gEmail == "correct"){
		document.contactForm.submit();
	}else{
		if(gName == "notCorrect" || gPhone == "notCorrect" || gCompany == "notCorrect" || gEmail == "notCorrect"){
		alert("please correct all of the fields in RED");
		}
		
		if(gName == "initial" || gPhone == "initial" || gCompany == "initial" || gEmail == "initial"){
		alert("please fill out all of the forms");
		}
		
		return false;
	}
	
	
}

function fValidateSubscribe(){
	var gNewsletter = document.forms.headerSub.newsletter.className;
	
	if(gNewsletter == "correct"){
		document.headerSub.submit();
	}else{
		if(gNewsletter == "notCorrect"){
		alert("please correct your email adress");
		}
		
		if(gNewsletter == "initial"){
		alert("please put in your email address");
		}
		
		return false;
	}
}

function fValidate(gField, gValue){
	var gPattern = gValue
	
	if(gPattern.test(gField.value)){
		gField.className = "correct";
	}else{
		gField.className = "notCorrect";

	}
	
}




function fPageLoaded(){
	
	
	var gNewsletter = document.forms.headerSub.newsletter;
	
	gNewsletter.onblur = function(){
		if(gNewsletter.value == ""){
			gNewsletter.value = "Email Address";
			gNewsletter.className = "initial"; 
		}else{
			fValidate(gNewsletter, /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/);
		}
	};
	
	gNewsletter.onfocus = function(){
		if(gNewsletter.className == "initial"){
			gNewsletter.value = "";
			gNewsletter.className = "focus";
		}
	};
	
	
	var fFormCheck = document.getElementById("contactForm");
	
	if (fFormCheck != null){
	
	var gName = document.forms.contactForm.contactName;
	var gPhone = document.forms.contactForm.contactPhone;
	var gCompany = document.forms.contactForm.contactCompany;
	var gEmail = document.forms.contactForm.contactEmail;
	
	//on blur actions//////////////
	
	gName.onblur = function(){
		if(gName.value == ""){
			gName.value = "Full Name";
			gName.className = "initial"; 
		}else{
			fValidate(gName, /[a-zA-Z]/);
		}
	};
	
	gPhone.onblur = function(){
		if(gPhone.value == ""){
			gPhone.value = "Phone Number (with area code)";
			gPhone.className = "initial"; 
		}else{
			fValidate(gPhone, /[0-9]/);
		}
	};
	
	gCompany.onblur = function(){
		if(gCompany.value == ""){
			gCompany.value = "Company Name";
			gCompany.className = "initial"; 
		}else{
			fValidate(gCompany, /[a-zA-Z]/);
		}
	};
	
	gEmail.onblur = function(){
		if(gEmail.value == ""){
			gEmail.value = "Email Address";
			gEmail.className = "initial"; 
		}else{
			fValidate(gEmail, /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/);
		}
	};
	
	
	//on focus actions////////////
	
	gName.onfocus = function(){
		if(gName.value == "Full Name"){
			gName.value = "";
			gName.className = "focus";
		}
	};
	
	gPhone.onfocus = function(){
		if(gPhone.value == "Phone Number (with area code)"){
			gPhone.value = "";
			gPhone.className = "focus";
		}
	};
	
	gCompany.onfocus = function(){
		if(gCompany.value == "Company Name"){
			gCompany.value = "";
			gCompany.className = "focus";
		}
	};
	
	gEmail.onfocus = function(){
		if(gEmail.value == "Email Address"){
			gEmail.value = "";
			gEmail.className = "focus";
		}
	};
	
	}
	
	
}


window.onload = function() {
	fPageLoaded();
}
