/* ******************************************************************************
*this is external javaScript file attached to form in camping.htm and lodges.htm
*in www.smytham.co.uk aplication
*scripted by Richard Wiseman 
*created on 14/07/2010
*validation for all reply forms
*********************************************************************************/

var errorMsg;
var newErrorText;
var newText;
var oldError;

var dataToTest;
var testRegExp;
var msgEx;

var strName;
var strEmail;
var strRoad;
var strPhone;
var strPostCode;

var phoneTest = /^([\d]{9,13})$/;
var roadTest = /^([\w ])*$/;
var wordTest = /^[a-zA-Z ]{5,30}$/;
var shortWordTest = /^[a-zA-Z ]{0,30}$/;
var wordNumberTest = /^[ \w]{1,30}$/;
var commentsTest = /^[ \w\s]{0,300}$/;
var emailTest = /^[a-zA-Z0-9._\-]+@[a-zA-Z0-9.\-]+\.[a-zA-Z]{2,4}$/;
var numberTest = /^[\d]{9,13}$/;
var postCodeTest = /^[A-Z0-9 ]{4,9}$/;//this regexp allows for europe and all of uk codes
var notZeroTest = /^[1-9]/;
var cardNumberTest = /^([0-9]{4} ){3}[0-9]{4}$/;
var cardIssueTest = /^[\d]{0,2}$/;
var cardDateTest = /^[\d]{2}$/;
var cardCscTest = /^[0-9]{3}$/;
var agreeTest = /^1$/;
	

var strWishToBook=0;
// makes visable fields relivent to radio button 
function loadBookingForm(num){
	var strStuff=document.getElementsByTagName('*');//get all tags and put in strStuff
	for(var i=0; i<strStuff.length; i++) {
		if(strStuff[i].className=='enquiry') {
			if(num==0){// make an enquiry
				strStuff[i].style.display='';	
				strWishToBook=0;
			}
			else{
				strStuff[i].style.display='none';	
			}
		}
		if(strStuff[i].className=='booking') {
			if(num==1){// wish to book
				strStuff[i].style.display='';	
				strWishToBook=1;
			}
			else{
				strStuff[i].style.display='none';	
			}
		}
	}
	payNow(strCost);
}

function popUp(url) {//camping conditions
	newwindow=window.open(url,'name','height=400,width=500,scrollbars=yes,resizable=yes');
	if (window.focus) {newwindow.focus()}
	return false;
}
	
/*    function to check that required data is present, just calls all checking functions	   */
function checkAll(campForm){

	if(strWishToBook==0){
		if (checkData(wordTest,document.campForm.name1.value,"Please enter your name","errorMessage")
		&&checkData(emailTest,document.campForm.email.value,"Please enter your e-mail. eg. abcd@abcdef.co.uk","errorMessage")
		&&checkData(numberTest,document.campForm.phoneDay.value,"Please enter your Phone Number with no spaces","errorMessage")
		&&checkData(commentsTest,document.campForm.comments.value,"Only letters and numbers are permitted in the comments box","errorMessage")){
			return true;
		}
		else{
			return false;
		}
	}
	else if(strWishToBook==1){
	
	
		calculate();//to check the price is right
		
		//end new bit
		if (checkData(wordTest,document.campForm.name1.value,"Please enter your name","errorMessage")
		&&checkData(emailTest,document.campForm.email.value,"Please enter your e-mail. eg. abcd@abcdef.co.uk","errorMessage")
		&&checkData(numberTest,document.campForm.phoneDay.value,"Please enter your Phone Number with no spaces","errorMessage")
		&&checkData(numberTest,document.campForm.phoneAlt.value,"Please enter your Alternative Phone Number with no spaces","errorMessage")
		&&checkData(wordNumberTest,document.campForm.house.value,"Please enter your house name or number","errorMessage")
		&&checkData(wordNumberTest,document.campForm.road.value,"Please enter your road name","errorMessage")
		&&checkData(postCodeTest,document.campForm.postCode.value,"Please enter your post code using only cap's and numbers","errorMessage")
		&&checkData(shortWordTest,document.campForm.name2.value,"Please only use letters to enter names","errorMessage")
		&&checkData(shortWordTest,document.campForm.name3.value,"Please only use letters to enter names","errorMessage")
		&&checkData(shortWordTest,document.campForm.name4.value,"Please only use letters to enter names","errorMessage")
		&&checkData(shortWordTest,document.campForm.name5.value,"Please only use letters to enter names","errorMessage")
		&&checkData(shortWordTest,document.campForm.name6.value,"Please only use letters to enter names","errorMessage")
		/*&&checkData(shortWordTest,document.campForm.name7.value,"Please only use letters to enter names")
		&&checkData(shortWordTest,document.campForm.name8.value,"Please only use letters to enter names")
		&&checkData(shortWordTest,document.campForm.name9.value,"Please only use letters to enter names")
		&&checkData(shortWordTest,document.campForm.name10.value,"Please only use letters to enter names")
		&&checkData(shortWordTest,document.campForm.name11.value,"Please only use letters to enter names")*///these fields only appear in the camping form need a work around
		&&checkData(commentsTest,document.campForm.comments.value,"Only letters and numbers are permitted in the comments box","errorMessage")
		&&checkData(notZeroTest,document.campForm.priceMsg.value,"Please check your booking details","errorMessage")// test that price has been worked out
		&&checkData(notZeroTest,document.campForm.cardType.value,"Please a card type","errorMessage")//card type
		&&checkData(cardNumberTest,document.campForm.cardNumber.value,"Input card number with spaces","errorMessage")//card number
		&&checkData(wordTest,document.campForm.cardName.value,"Enter the name as it appears on the card","errorMessage")//card name
		&&checkData(cardIssueTest,document.campForm.cardIssue.value,"Only enter numbers in the issue number field, or leave blank","errorMessage")//card name
		&&checkData(cardDateTest,document.campForm.cardStartDay.value,"Please check card valid from date","errorMessage")//card valid from
		&&checkData(cardDateTest,document.campForm.cardStartMonth.value,"Please check card valid from date","errorMessage")//card valid from
		&&checkData(cardDateTest,document.campForm.cardExDay.value,"Please check expirey date","errorMessage")//card expirey
		&&checkData(cardDateTest,document.campForm.cardExMonth.value,"Please check expirey date","errorMessage")//card expirey
		&&checkData(cardCscTest,document.campForm.csc.value,"Please check card security number","errorMessage")// card csc
		){
			if(document.campForm.agree.checked!=true){
				errorText('You need to agree to our booking conditions to proceed','errorMessage');
				return false;
			}
			else{
				return true;
			}
			
		}
		else{
			return false;
		}
	}
	else{
		return false
	}
}//end checkAll function

function checkOwningData(owningForm){

	if (checkData(wordTest,document.owningForm.name1.value,"Please enter your name","errorMessage")
	&&checkData(emailTest,document.owningForm.email.value,"Please enter your e-mail. eg. abcd@abcdef.co.uk","errorMessage")
	&&checkData(numberTest,document.owningForm.phoneDay.value,"Please enter your Phone Number with no spaces","errorMessage")
	&&checkData(commentsTest,document.owningForm.comments.value,"Only letters and numbers are permitted in the comments box","errorMessage")){
		return true;
	}
	else{
		return false;
	}
}//end checkOwningData function



function checkData(dataTest,dataLocal,errorMsg,errorMsgPlace){
var errorMsgLocal=errorMsgPlace
	if(dataTest.test(dataLocal)){
		return true
	}
	else{
		errorText(errorMsg,errorMsgLocal);
		return false;
	}
}

function errorText(errorMsg,errorMsgLocal){

	newText=document.createElement("span");
	newErrorText=document.createTextNode(errorMsg);
	newText.appendChild(newErrorText);
	
	getLocation=document.getElementById(errorMsgLocal);
	var allPara=getLocation.getElementsByTagName("span");
	oldError=allPara.item(0);
	
	getLocation.replaceChild(newText,oldError);
}	

