/* Form validation functions */

/*
Clear default form value script- by JavaScriptKit.com
Featured on JavaScript Kit (http://javascriptkit.com)
*/

function clearText(thefield){
		if (thefield.defaultValue==thefield.value)
		thefield.value = ""
	}




	
	function deleteSpans(idToDelete) {
		var spans = document.getElementsByTagName("span")
		
		for( i=spans.length-1; i >= 0; i-- ) {
			var item = spans.item(i)
			if (item.id==idToDelete) {
				item.parentNode.removeChild(item)
			}
		}	
	}


	function checkStreetName(){

/* deletes red correction specific to the input we are checking each time the user 
leaves this input onblur so red corrections don't accumulate on same field */

	deleteSpans("red1")
	

}





function checkPostalCode(){

/* deletes red correction specific to the input we are checking each time the user 
leaves this input onblur so red corrections don't accumulate on same field */

	deleteSpans("red2")	


}







/* Function to validate Family name, phone number and email address when form is submitted */

function validateForm(theForm) {

/* deletes red corrections each time use submits form so red corrections don't accumulate on same field */

	deleteSpans("red")


	

/* test first name and last name fields are not empty and consist of letters, hyphens and spaces only */
 	 	
 	var submitErrorMsg = false
 	var reValidFamilyName = /^[a-zA-Z ]+[\- ]?[a-zA-Z ]*$/
 	var reValidFirstName = /^[a-zA-Z ]+[\- ]?[a-zA-Z ]*$/

 	
 	
 	if (document.contactDetailsForm.lastName.value=="") {
 		submitErrorMsg = true
 		redErrorMsg = "Please enter a last name."
 		familyErrorElement = document.createElement("span")
 		familyErrorElement.id = "red"
 		familyErrorElement.appendChild(document.createTextNode(redErrorMsg))
 		document.getElementById("lastNameRed").appendChild(familyErrorElement)
	 	}
	
	else if (!(reValidFamilyName.test(document.contactDetailsForm.lastName.value))) {
 		submitErrorMsg = true
 		redErrorMsg = "Letters, spaces and hyphens only"
 		familyErrorElement = document.createElement("span")
 		familyErrorElement.id = "red"
 		familyErrorElement.appendChild(document.createTextNode(redErrorMsg))
 		document.getElementById("lastNameRed").appendChild(familyErrorElement)
	 	}
	
	
	if (document.contactDetailsForm.firstName.value=="") {
 		submitErrorMsg = true
 		redErrorMsg = "Please enter a first name."
 		familyErrorElement = document.createElement("span")
 		familyErrorElement.id = "red"
 		familyErrorElement.appendChild(document.createTextNode(redErrorMsg))
 		document.getElementById("firstNameRed").appendChild(familyErrorElement)
	 	}
	
	else if (!(reValidFirstName.test(document.contactDetailsForm.firstName.value))) {
 		submitErrorMsg = true
 		redErrorMsg = "Letters, spaces and hyphens only"
 		familyErrorElement = document.createElement("span")
 		familyErrorElement.id = "red"
 		familyErrorElement.appendChild(document.createTextNode(redErrorMsg))
 		document.getElementById("firstNameRed").appendChild(familyErrorElement)
	 	}


/* test if fields for address, postcode, town, country are not empty  */


	if (document.contactDetailsForm.houseNumber.value=="") {
 		submitErrorMsg = true
 		redErrorMsg = "Please enter your address."
 		familyErrorElement = document.createElement("span")
 		familyErrorElement.id = "red"
 		familyErrorElement.appendChild(document.createTextNode(redErrorMsg))
 		document.getElementById("houseNumberRed").appendChild(familyErrorElement)
	 	}

	if (document.contactDetailsForm.postalCode.value=="") {
 		submitErrorMsg = true
 		redErrorMsg = "Please enter a Postcode or zip."
 		familyErrorElement = document.createElement("span")
 		familyErrorElement.id = "red"
 		familyErrorElement.appendChild(document.createTextNode(redErrorMsg))
 		document.getElementById("postalCodeRed").appendChild(familyErrorElement)
	 	}
	
	if (document.contactDetailsForm.city.value=="") {
 		submitErrorMsg = true
 		redErrorMsg = "Please enter a town or city."
 		familyErrorElement = document.createElement("span")
 		familyErrorElement.id = "red"
 		familyErrorElement.appendChild(document.createTextNode(redErrorMsg))
 		document.getElementById("cityRed").appendChild(familyErrorElement)
	 	}
	 	
	if (document.contactDetailsForm.country.value=="") {
 		submitErrorMsg = true
 		redErrorMsg = "Please enter a country."
 		familyErrorElement = document.createElement("span")
 		familyErrorElement.id = "red"
 		familyErrorElement.appendChild(document.createTextNode(redErrorMsg))
 		document.getElementById("countryRed").appendChild(familyErrorElement)
 	}



	if (document.contactDetailsForm.phoneNumber.value=="") {
		submitErrorMsg = true
 		redErrorMsg = "Please enter a valid phone number."
 		familyErrorElement = document.createElement("span")
 		familyErrorElement.id = "red"
 		familyErrorElement.appendChild(document.createTextNode(redErrorMsg))
 		document.getElementById("phoneRed").appendChild(familyErrorElement)							
	}
	
	/* test if email address is in the format xyzabc@bleah.co.uk  or xyzabc@bleah.com */

	var reValidEmailAddress = /^\w+([\.-]?\w+)@\w+([\.-]?\w+)*(\.\w{2,3})+$/

	if (!(reValidEmailAddress.test(document.contactDetailsForm.emailAddress.value))) {
		submitErrorMsg = true
 		redErrorMsg = "Please enter a valid email address."
 		familyErrorElement = document.createElement("span")
 		familyErrorElement.id = "red"
 		familyErrorElement.appendChild(document.createTextNode(redErrorMsg))
 		document.getElementById("emailAddressRed").appendChild(familyErrorElement)
	}

	if (submitErrorMsg) {
		alert("Certain information is missing or incomplete. Please check and correct the fields marked in red before pressing Submit") 
	}

	return !submitErrorMsg
}
