// JavaScript Document

//define all the regular expressions
reg = new Array();
reg['phone']=/^(\d{11})|(\d{5}\s{1}\d{6})$/;
reg['email']=/^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/;
reg['name']=/^[A-Za-z]+$/;
reg['job']=/^[A-Za-z/ &\(\)]+$/;
reg['address']=/^[A-Za-z0-9\b\r\n ]+$/;
		
// use this function to check the data against the expressions
function checkreg(field,valued){
	regexp=reg[field];		
	if (regexp.test(valued)){
		return true;
	} else {
		return false;
	}		
}
		
// function to change the form when an error is detected
function error(row){
	document.getElementById(row).style.backgroundColor="#ff0033";
	document.getElementById(row+"error").style.display="block";
}
		
//function to correct the form to ensure the error messages go away again
function correct(row){
	document.getElementById(row).style.backgroundColor="#ffffff";
	document.getElementById(row+"error").style.display="none";
}
		
// this is the main checking function that does all the work
function checkcode(val,elem,check){
	//check to see if its a W3C DOM enabled browser
	if (document.getElementById){
		//set var valued to value of the form element
		valued=document.getElementById(elem).value;
		//send data to be checked then initiate secondary procedures
		if (checkreg(check,valued)==true){
			// return true if data is correct
			correct ("row"+val);
			return true;
		} else {
			//return false if problem with data
			error("row"+val);
			return false;
		}
	} else {
		// return true to enable customers with non W3C DOM browsers still to use the site
		return true;
	}
}
		
//joincheck function - this just basically relies on the previous functions to check whether data from the join form is complete and can be submitted
function joincheck(){
	var checked=0;
	if (checkcode(2,'fname','name')==false){
		checked=1;
	}
	if (checkcode(3,'sname','name')==false){
		checked=1;	
	}
	if(checkcode(4,'job','job')==false){			
		checked=1;
	}
	if(checkcode(5,'organisation','job')==false){
		checked=1;
	}
	if(checkcode(6,'address','address')==false){
		checked=1;
	}			
	if(checkcode(7,'phone','phone')==false){
		checked=1;
	}
	if(checkcode(8,'email','email')==false){
		checked=1;
	}
	// v. important!! check that they have agreed data protection.
	if (joinform.agreedataprotection.checked==false){
		if (document.getElementById){
			error("row9");
		} 
		checked=1;	
	} else {
		if (document.getElementById){
			correct("row9");
		}
	}
	if(checked==1){
		return false
	} else {
		return true
	}		
}
		