	
	// basic form validation script.
	// - no type checking or regex filtering.
	
	$(function ()
	{

		function resetOrderDetailGroup ( )
		{
			var _eFieldset = this;
			var _aChildren = $( _eFieldset ).children();
			// reset the form
			$.each ( _aChildren, function ( _nIndex, _eFormElement )
			{
				var _eElement = $( _eFormElement )[0];
				if ( $(_eFormElement ).is("select") ) _eElement.selectedIndex = 0;
				if ( $(_eFormElement ).is("input") || $(_eFormElement ).is("textarea") )
				{
					$(_eFormElement ).val("");
				}									
			});
		}
		
		function getOrderDetailGroup ( sFieldsetId )
		{
			var _aTitles = [];	
			var _aValues = [];
			var _aDetails = $( sFieldsetId + " input[@title]," + sFieldsetId + " select[@title]," + sFieldsetId +" textarea[@title]" );
			$.each ( _aDetails, function ( _nIndex, _eInput )
			{
				var sInput = null;
				if ( $(_eInput).is( "select" ) )
				{
					var eSelect = $(_eInput)[0];
					sInput = eSelect[eSelect.selectedIndex].text;
				}
				else sInput = $.trim ( $( _eInput ).val( ) );
				if ( sInput == "" ||  sInput == null || sInput.indexOf("please choose...") != -1)
				{
					_aTitles.push ( $(_eInput).attr("title") );
				}
				else 
				{
					_aValues.push( sInput );
				}
			});
			return { errors: _aTitles, values: _aValues };
		}

		// relative to the checkbox elements
		function toggleOrderItemVisibility ( )
		{
			var sId = this.id;
			var bChecked = this.checked;
			var sTargetId = "#" + this.id + "Layer";
			if ( bChecked ) $( sTargetId ).slideDown("fast");
			else 
			{
				var bDispose = true;
				var sProduct = sId.substring(6,(sId.length-1));
				var sQuestion = "Are you sure you don't want to order any " + sProduct + "s?";
				var oOrderItem = getOrderDetailGroup ( sTargetId );						
				if ( oOrderItem.values.length > 0 ) bDispose = confirm ( sQuestion );
				if ( bDispose ) // reset the pertinent product form elements 
				{				
					$( sTargetId ).slideUp( "fast", resetOrderDetailGroup );
				}
				// dont do anything, i.e. 'keep' the the order details gathered
				else return false;
			}
		}

		function notifyError ( _sMessage, _aDetails )
		{
			if ( _aDetails ) _sMessage += "\n\n    " + _aDetails.join("\n    ");		
			throw new Error ( _sMessage );
		}

		function validateOrderForm ()
		{
			var _aCheckboxes = $("#productChoices input");	
			var _aSelectedProducts = [];			
			// check to see if any products have been selected
			$.each ( _aCheckboxes, function ( _nIndex, _eCheckbox )
			{	
				if ( _eCheckbox.checked ) _aSelectedProducts.push( "#" + this.id + "Layer" );
			});

			// if no items are selected, stop now
			if ( _aSelectedProducts.length == 0 )
			{
				var sErrorMsg =  "Please make a product selection before attempting to submit the order form!";
				notifyError ( sErrorMsg );
			}

			// validate selected orderItems and make sure they are filled out correctly
			$.each ( _aSelectedProducts, function ( _nIndex, _sFieldsetId )
			{
				var oOrderItem = getOrderDetailGroup ( _sFieldsetId )
				if ( oOrderItem.errors.length > 0 ) 
				{
					var sErrorMsg =  "Please make sure you have correctly provided the following information:";
					notifyError ( sErrorMsg, oOrderItem.errors );
				}
			});

			// validate customer details			
			var oCustomerDetails = getOrderDetailGroup ( "#customerDetails" );
			if ( oCustomerDetails.errors.length > 0 ) 
			{
				var sErrorMsg = "Please make sure to supply all the required contact and payment details before \nresubmitting your order:";
				notifyError ( sErrorMsg, oCustomerDetails.errors );
			}

			// if they've got this far, the order is acceptable.
			return true;
		}			

		// initialize event handlers in the global scope to avoid closures
		$.each ( $("#productChoices input") , function ( )
		{
			this.checked = false;
			$( this ).click ( toggleOrderItemVisibility );
		});

		$( "#cancelBtn" ).click( function ()
		{
			window.location.replace ("http://www.jenniescandles.com/index.html");
		});
		
		$("#nextBtn").click( function ()
		{
			try { 
				validateOrderForm () 
			}
			catch ( oError )	{
				alert ( oError.message );
				return false;
			}
		});		

	});