/* ************************************
*this is external javaScript file attached to form.htm 
*in smytham.co.uk aplication
*scripted by Richard Wiseman 
* on 14/02/2008
*/
//function to return an error message ajacent to the field
	//function to return an error message ajacent to the field
function errorText(errorMsg,errorLocation){
	newText=document.createElement("span")
	newErrorText=document.createTextNode(errorMsg)
	newText.appendChild(newErrorText)
	
	getLocation=document.getElementById(errorLocation)
	allPara=getLocation.getElementsByTagName("span")
	oldError=allPara.item(0)
	
	getLocation.replaceChild(newText,oldError)
}	

/*    function to check that required data is present, just calls all checking functions	   */
function checkAll(replyForm){
	if(checkSurname(replyForm)&&checkPhoneNo(replyForm)&&checkStreetName(replyForm)&&checkPostCode(replyForm)&&checkEmail(replyForm)){
	errorText("Data OK ","errorMsgMain");	
	return true;
	}
	else{
		errorText("Please check your entries above ","errorMsgMain");	
		return false;
	}
}//end checkAll function

// values sent to function in the order (the data to test, the RegExp to test against,error msg for no data, error msg example, the lacation to dispolay the error message)
var errorMsgNone=" "
function checkData(dataToTest,testRegExp,msgNoData,msgEx,msgLocal){
	if(dataToTest.value==""){
		errorText(msgNoData,msgLocal)
		return false;
		errorText(msgNoData,"errorMessage")
	}	
	else{
		if(testRegExp.test(dataToTest.value)){
			errorText(errorMsgNone,msgLocal);
			return true;
		}
		else{
		errorText(msgEx,msgLocal)
		errorText(msgEx,"errorMessage")
		return false;
		
		
		}
	}
}//end checkData function

//function check if surname field is empty
function checkSurname(replyForm){
	checkData(document.getElementById('surname'),/^\S/,"Please enter your Surname!","Please enter your Surname!","errorMsgSurname")
}

function checkPhoneNo(replyForm){
	checkData(document.getElementById('phoneNumber'),/^((\d{9,12}))$/,"please enter your phone number!","re-enter your phone number! No spaces","errorMsgPhone")
}

function checkStreetName(replyForm){
	checkData(document.getElementById('streetName'),/^([A-Za-z ])*$/,"Please enter your street name!","re-enter your street name! It must start with a letter","errorMsgStreet")
}

function checkPostCode(replyForm){
	checkData(document.getElementById('postCode'),/^([A-Z0-9 ]{4,10})$/,"please enter you post code!"," Please re-enter using cap's and numbers!","errorMsgPostCode")
}

function checkEmail(replyForm){
	checkData(document.getElementById('emailAddress'),/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/,"please enter your email address!","re-enter data eg; abcd@abcd.com","errorMsgEmail")
}
