﻿function checkForm() {
	var element;
	var intErrorCount = 0;
	var strErrorMessage = "";
	var strFocusElement = "";
	var blnResult = true;

	var elementValue = $("#ctl00_mphContent_ipEmailAddress").attr("value");

	if (elementValue == "") {
		intErrorCount++;
		strErrorMessage += 'Please enter an e-mail address\n';
		if (strFocusElement == "") strFocusElement = "ipEmailAddress";
	} else {
		if (!emailIsValid(elementValue)) {
			intErrorCount++;
			strErrorMessage += 'Please enter a valid e-mail address\n';
			if (strFocusElement == "") strFocusElement = "ipEmailAddress";
		}
	}

	if ($("#ctl00_mphContent_ipName").attr("value") == "") {
		intErrorCount++;
		strErrorMessage += 'Please enter your name\n';
		if (strFocusElement == "") strFocusElement = "ipName";
	}

	if (
		($("#ctl00_mphContent_ipTelHome").attr("value") == "") &&
		($("#ctl00_mphContent_ipTelMobile").attr("value") == "") &&
		($("#ctl00_mphContent_ipTelWork").attr("value") == "")) {
		intErrorCount++;
		strErrorMessage += 'Please enter a contact number\n';
		if (strFocusElement == "") strFocusElement = "ipTelHome";
	}

	if (
		($("#ctl00_mphContent_ipChildName1").attr("value") == "") &&
		($("#ctl00_mphContent_ipChildAge1").attr("value") == "") &&
		($("#ctl00_mphContent_ipChildGender1").attr("value") == "")) {
		intErrorCount++;
		strErrorMessage += 'Please enter your first child\'s details\n';
		if (strFocusElement == "") strFocusElement = "ipChildName1";
	} else {
		if ($("#ctl00_mphContent_ipChildName1").attr("value") == "") {
			intErrorCount++;
			strErrorMessage += 'Please enter your first child\'s name\n';
			if (strFocusElement == "") strFocusElement = "ipChildName1";
		}

		if ($("#ctl00_mphContent_ipChildAge1").attr("value") == "") {
			intErrorCount++;
			strErrorMessage += 'Please enter your first child\'s age\n';
			if (strFocusElement == "") strFocusElement = "ipChildAge1";
		}

		if ($("#ctl00_mphContent_ipChildGender1").attr("value") == "") {
			intErrorCount++;
			strErrorMessage += 'Please enter your first child\'s gender\n';
			if (strFocusElement == "") strFocusElement = "ipChildGender1";
		}
	}

	if ($("#ctl00_mphContent_ipTermsAndConditions").attr("checked") != true) {
		intErrorCount++;
		strErrorMessage += 'Please confirm that you have read and agreed to Chalfont Nannies terms and conditions.\n';
		if (strFocusElement == "") strFocusElement = "ipTermsAndConditions";
	}

	switch (intErrorCount) {
		case 0:
			blnResult = true;
			break;
		case 1:
			alert("Please complete the following field:\n" + strErrorMessage);
			blnResult = false;
			break;
		default:
			alert("Please complete the following fields:\n\n" + strErrorMessage);
			blnResult = false;
			break;
	}
	if (!blnResult) {
		$("input#ctl00_mphContent_" + strFocusElement)[0].focus();
	}
	return blnResult;
}
