// Form scripts

var emailAddressPattern = /^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/
		
function validateForm() {
	var isValid = true;
	var missingInfo = "";
	if (!validateInputBox(document.theForm.First_name.value)) {
		isValid = false;
		errorOn('firstname');
		missingInfo += "\n  -  First name";
	}
	if (!validateInputBox(document.theForm.Second_name.value)) {
		isValid = false;
		errorOn('secondname');
		missingInfo += "\n  -  Second name";
	}	
	if (!validateInputBox(document.theForm.Phone.value)) {
		isValid = false;
		errorOn('phone');
		missingInfo += "\n  -  Phone";
	}	
	if (!emailAddressPattern.test(document.theForm.email.value)) {
		isValid = false;
		errorOn('email');
		missingInfo += "\n  -  Valid email address";
	}
	if (!isValid) {
		alert("The following information is missing:\n" + missingInfo + "\n\nPlease correct and click \"send\" again.");
	}
	return isValid;
}

function updateItem(id) {
	switch (id) {
		case 'firstname':
			if (validateInputBox(document.theForm.First_name.value))
				errorOff('firstname');
			break;
		case 'secondname':
			if (validateInputBox(document.theForm.Second_name.value))
				errorOff('secondname');
			break;
		case 'phone':
			if (validateInputBox(document.theForm.Phone.value))
				errorOff('phone');
			break;
		case 'email':
			if (emailAddressPattern.test(document.theForm.email.value))
				errorOff('email');
	}
}

function validateInputBox(text) {
	return (text.length > 0);
}

function errorOn(id) {
	document.getElementById(id).style.color = '#cc0000';
}

function errorOff(id) {
	document.getElementById(id).style.color = '#000000';
}
