var arrFields = new Array("Dealership","title", "firstname",  "surname", "email", "vehicle_model", "registration_number", "desctiption_of_part");
var arrTitles = new Array("Dealership","Title", "First Name", "Surname", "Email", "Vehicle model", "Registration number", "Description of part");
function checkForm(thisForm){

	var hasError = 0;
	var hasError2 = 0;
	var hasError3 = 0;
	var hasError4 = 0;
	var missingFields = "";
	var errorFields = "";

	highlightColor = "#ffc";
	defaultColor = "#ffffff";
	thisForm = thisForm;

	for(i=0; i<arrFields.length; i++){
		theField = arrFields[i]
		theTitle = arrTitles[i]
		if(isNaN(thisForm[theField].length) || ! isNaN(thisForm[theField].selectedIndex)){
			if(thisForm[theField].value == ""){
				missingFields += theTitle + "\n";
				thisForm[theField].style.background = highlightColor;
				if(hasError == 0){thisForm[theField].focus()}
				hasError = 1;
			}else{
				thisForm[theField].style.background = defaultColor;
			}
		}else{
			var hasNodeSelected = 0;
			for(j=0; j<thisForm[theField].length; j++){
				if(thisForm[theField][j].checked){
					hasNodeSelected++;
				}
			}
			if(hasNodeSelected == 0){
				hasError = 1;
				missingFields += theTitle + "\n";
			}
		}
	}

	thisEmail = thisForm.email
	mail=thisEmail.value;
	atloc=(mail.indexOf("@"));
	if (atloc==-1) {
		thisEmail.style.background = highlightColor;
		errorFields += 'Please enter a valid e-mail address.' + "\n";
		hasError4 = 1;
	}else if (mail.indexOf(".",atloc)==-1) {
		thisEmail.style.background = highlightColor;
		errorFields += 'Please enter a valid e-mail address.' + "\n";
		hasError4 = 1;
	}else if (mail.indexOf(' ') > 0){
		thisEmail.style.background = highlightColor;
		errorFields += 'Please ensure that your email address does not contain spaces.' + "\n";
		hasError4 = 1;
	}

	if(hasError == 1){
		alert('Please complete the following fields:\n' + missingFields)
		return false;
	}else if (hasError4 == 1){
		alert(errorFields);
		return false;
	}else{
		return true;
	}

}
