// Checks whether any fields which are required are missing and alerts the user
function checkSubmit(){

var name = document.getElementById('fullname').value;
var company = document.getElementById('company').value;
var email = document.getElementById('email').value;
var tel = document.getElementById('telephone').value;
var address = document.getElementById('address').value;
var county = document.getElementById('county').value;
var postcode = document.getElementById('postcode').value;
var subject = document.getElementById('subject').value;
var enquiry = document.getElementById('enquiry').value;

var check = 0;
var missing='';

if ((name==null)||(name=="")){
check = 1;	
missing = missing + 'Name, ';
}

if ((company==null)||(company=="")){
check = 1;	
missing = missing + 'Company, ';
}

if ((email==null)||(email=="")){
check = 1;	
missing = missing + 'E-Mail, ';
}

if ((tel==null)||(tel=="")){
check = 1;	
missing = missing + 'Telephone, ';
}

if ((address==null)||(address=="")){
check = 1;	
missing = missing + 'Address, ';
}

if ((county==null)||(county=="")){
check = 1;
missing = missing + 'County, ';
}

if ((postcode==null)||(postcode=="")){
check = 1;
missing = missing + 'Postcode, ';
}

if ((subject==null)||(subject=="")){
check = 1;	
missing = missing + 'Subject, ';
}

if ((enquiry==null)||(enquiry=="")){
check = 1;	
missing = missing + 'Enquiry, ';
}

if (check == 1){
	alert('The following fields are missing: ' + missing);
}else{
	if (emailcheck(email)==true){
		document.enquiryform.submit()
	}
	
}
	

}

// Checks whether a given string has a valid email format i.e. someone@somewhere.com
function emailcheck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   alert("Please enter a valid email addresss.")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Please enter a valid email addresss.")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("Please enter a valid email addresss.")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("Please enter a valid email addresss.")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("Please enter a valid email addresss.D")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("Please enter a valid email addresss.")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    alert("Please enter a valid email addresss.")
		    return false
		 }

 		 return true					
	}
