function checkForErrorDivs(){
  // get divs with class name 'error'
 var errorDivs = $('.error');
 // loop through all divs with class name 'error'
 for (i=0; i<errorDivs.length; i++){
	if (errorDivs[i]){
	  $('div.error').remove();
	  $('p.error').remove();	
	}
  }	
}

function verify(form){
  
  checkForErrorDivs();
  
  // define name field
  var nameField = document.getElementById("Name");
  // check to see if it's empty
  if (nameField.value==""){
	// add element for error text to be displayed
    var errorName = document.createElement("div");
	// Add the error text
    var errorNameText = document.createTextNode("*Please enter your name");
	// Add the text to the created element (div)
	errorName.appendChild(errorNameText);
	// give it a class of error (red text)
    errorName.className = "error";
	// define the contact form
	var contactForm = document.getElementById("contactform");
	// get the input field with name info
	var nameField = contactForm.getElementsByTagName("input")[0];
	// insert the error text node before the input field in red text
	nameField.parentNode.insertBefore(errorName, nameField);
	// focus on the field
    form.Name.focus();
	// don't submit the form yet
    return false;
  }
  
  var emailAddress = document.getElementById("emailaddress");
  if (emailAddress.value==""){
	
    var errorAddr = document.createElement("div");
	// Add error text
	var errorAddrText = document.createTextNode("*Please enter your email address");
	// add text to the new element
	errorAddr.appendChild(errorAddrText);
	// give it a class, error (red text)
	errorAddr.className = "error";
	// define contact form
	var contactForm = document.getElementById("contactform");
	// get the input field with address info
	var emailField = contactForm.getElementsByTagName("input")[1];
	// insert before input of email address
	emailField.parentNode.insertBefore(errorAddr, emailField);
	// focus on field
	form.emailaddress.focus();
	// don't submit the form
	return false;
  }
  
  var phone = document.getElementById("phone");
  if (phone.value=="" || phone.value.length < 10){
	
    var errorAddr = document.createElement("div");
	// Add error text
	var errorAddrText = document.createTextNode("*Please enter your phone #");
	// add text to the new element
	errorAddr.appendChild(errorAddrText);
	// give it a class, error (red text)
	errorAddr.className = "error";
	// define contact form
	var contactForm = document.getElementById("contactform");
	// get the input field with address info
	var phoneField = contactForm.getElementsByTagName("input")[2];
	// insert before input of email address
	phoneField.parentNode.insertBefore(errorAddr, phoneField);
	// focus on field
	form.phone.focus();
	// don't submit the form
	return false;
  }
  
  var comments = document.getElementById("comments");
  if (comments.value=="" || comments.value == null){
	
    var errorAddr = document.createElement("div");
	// Add error text
	var errorAddrText = document.createTextNode("*Please tell me what you want to talk about");
	// add text to the new element
	errorAddr.appendChild(errorAddrText);
	// give it a class, error (red text)
	errorAddr.className = "error";
	// define contact form
	var contactForm = document.getElementById("contactform");
	// get the input field with address info
	var commentsField = contactForm.getElementsByTagName("textarea")[0];
	// insert before input of email address
	commentsField.parentNode.insertBefore(errorAddr, commentsField);
	// focus on field
	form.comments.focus();
	// don't submit the form
	return false;
  }
  
  var ruHuman = document.getElementById("ruhuman");  
  var codeField = document.getElementById("code");
  // this field should be empty, if it's not then a spambot has probably filled it out
  if (codeField.value != ""){
	var contactForm = document.getElementById("contactform");
	var errorRUHuman = document.createElement("p");
	// alert(codeField.value);
	var errorRUHumanText = document.createTextNode("This form has not been submitted. Call for questions or more information.");
	errorRUHuman.appendChild(errorRUHumanText);
	errorRUHuman.className = "error";
	// display not submitted info
	contactForm.appendChild(errorRUHuman);
    return false;	    
  }
  // alert("This form is just an example. It will not send info anywhere while on this demoSite.");
  return true;
}

jQuery(function($) {

// check form fields on exit of field to remove error field
$('input').blur(function(){
  if ($(this).val().length > 0){
	  checkForErrorDivs();
  }
});

// after form is processed, php returns a page anchor (#)
  var path = window.location;
  // if php returns an error, go to this page with error message
  if (path == 'http://www.matthewgoodrich.com/contact.php#error'){
	// write the error message
	$('#contactusbox').before('<a id="error"></a><div id="errordiv"><p class="red"><b>*Error: Please check the information entered on the previous page for accuracy.</b></p></div> ');
  }
  if (path == 'http://www.matthewgoodrich.com/index.php#error'){
	// write the error message
	$('#quickcontactbox').before('<a id="error"></a><p class="red"><b>*Error: Please check the information entered on the form for accuracy.</b></p> ');
  }
  // if php returns ok, go to this page with thank you message
  if (path == 'http://www.matthewgoodrich.com/contact.php#thankyou'){
	// add element to dom above the contact us box 
	$('#contactusbox').before('<a id="thankyou"></a><div id="thankyoudiv"><p class="green"><b>Thank you for your information, we will contact you as soon as possible.</b></p></div>');
  } 
  if (path == 'http://www.matthewgoodrich.com/index.php#thankyou'){
	// write the error message
	$('#quickcontactbox').before('<a id="thankyou"></a><p class="red"><b>Thank you for your information, we will contact you as soon as possible.</b></p> ');
  } 
  
  
});
