/*******************************************************************************************
 * formValidate
 * Written by Nick Livingstone (amends by Craig Francis)
 * Perform basic form validation before sending to the server.
 *******************************************************************************************/

//--------------------------------------------------
// Validation function

	function formValidate() {

		//--------------------------------------------------
		// Script initialisation

			firstError = 10000;
			var errorCount = 0;
			var field;

		//--------------------------------------------------
		// Type

			var rdType1 = document.getElementById('rfldYou');
			var rdType2 = document.getElementById('rfldYouPartner');
			var rdType3 = document.getElementById('rfldYouChildren');
			var rdType4 = document.getElementById('rfldYouPartnerChildren');
			var coverDetails = document.getElementById('coverDetails');
			var rowAdditionalChildren = document.getElementById('rowAdditionalChildren');
            var cbAdditionalChildren = document.getElementById('cbAdditionalChildren');
			
			if (rdType1 && rdType2 && rdType3 && rdType4) {
				if (rdType1.checked == false && rdType2.checked == false && rdType3.checked == false && rdType4.checked == false) {
					var tx = 'Please select the cover type you require.';
					errorCount++;
					appendQuoteError(document.getElementById('error'),tx);

				}
				if ((rdType3.checked == true || rdType4.checked == true) && (cbAdditionalChildren.value == 0)) {
					var tx = 'Number of children must be greater than 0 for this type of policy.';
					errorCount++;
					appendQuoteError(getParent(rowAdditionalChildren, 'div'), tx);

				}
			}

		//--------------------------------------------------
		// Return the validation result, and if necessary
		// scroll to the first error.

			if (errorCount == 0) {
			 	return true; // No errors, all is well
			} else {
				window.scrollTo(0, firstError);
				return false;
			}

	}

//--------------------------------------------------
// Removes error specifically from the quote type radio buttons.
function removeQuoteError() {
    var error = document.getElementById('error');
    if (error) {
        error.style.display = 'none';
    }
}

//--------------------------------------------------
// When the page loads

	addLoadEvent(function() {

		//--------------------------------------------------
		// Ensure the browser can run this validation

			if (!document.getElementsByTagName) {
				return;
			}
			

		//--------------------------------------------------
		// Set the default (findPosY) location for the
		// first error - is this necessary?

			firstError = 10000;

		//--------------------------------------------------
		// Attach the form validation function

			var form = document.getElementById('quoteCalculator');
			var rdType1 = document.getElementById('rfldYou');
			var rdType2 = document.getElementById('rfldYouPartner');
			var rdType3 = document.getElementById('rfldYouChildren');
			var rdType4 = document.getElementById('rfldYouPartnerChildren');
			if (rdType1 && rdType2 && rdType3 && rdType4) {
			   rdType1.onfocus=removeQuoteError;
			   rdType2.onfocus=removeQuoteError;
			   rdType3.onfocus=removeQuoteError;
			   rdType4.onfocus=removeQuoteError;
			}
			 
			if (form) {
				form.onsubmit = formValidate;
				if (document.getElementById('rfldYouChildren').checked || document.getElementById('rfldYouPartnerChildren').checked) {
				    document.getElementById('rowAdditionalChildren').style.display="block";
				} else {
				    document.getElementById('rowAdditionalChildren').style.display="none";
				}
				var content = document.getElementById('content');
				var contentBottom = document.getElementById('footer');
				
				if (content && contentBottom) {
					content.style.paddingBottom = '0';
					contentBottom.style.position = 'relative';
				}
			  	
				
			}

	});
