$(document).ready
(
	function()
	{
		// We are running Javascript, so we need to display the number of appliances drop menu
		$("#number_of_appliances").show();
		//$("#button_qf_QuickQuote_next").hide();
		
		// JS is on, so hide the headers until we know we are trying to select more than one
		var iNumSelApps = $("#how_many_appliance :selected").val();

		for ( var i = 1; i <= 8; i ++ )
		{
			if ( i <= iNumSelApps )
			{
				$('#appliance'+i).show();
				$("label[for='duration']").show();
				$("#appliance_hdrs").fadeIn('fast');
				/*$("#single_appliance_selected")
					.fadeIn('fast')
					.append('<br />');*/
			}
			else
			{

				// Hide the appliances we do not need
				$('#appliance'+i).hide();
				// and reset the values to nothing - else they will confuse the quote engine
				$('#appliance'+i+'_appliance_type').val(''); //alert ($('#appliance'+i+'_appliance_type').val);
				$('#appliance'+i+'_manufacturer').val(''); //alert($('#appliance'+i+'_manufacturer').val);
			}

		}
		
		if ( iNumSelApps == 1 )
		{
			$("#single_appliance_selected")
				.fadeIn('fast')
				.append('<br />');
		}

		$("#how_many_appliance").change
		(
			function()
			{
				
			   // assign the value to a variable, so you can test to see if it is working
			    var selectVal = $("#how_many_appliance :selected").val();

				for ( var i = 1; i <= 8; i ++ )
				{
					if ( i <= selectVal )
					{
						$('#appliance'+i).fadeIn('fast');
						$("label[for='duration']").fadeIn('fast');
					}
					else
					{
						// Hide the appliances we do not need
						$('#appliance'+i).fadeOut('fast');
						// and reset the values to nothing - else they will confuse the quote engine
						$('#appliance'+i+'_appliance_type').val(''); //alert ($('#appliance'+i+'_appliance_type').val);
						$('#appliance'+i+'_manufacturer').val(''); //alert($('#appliance'+i+'_manufacturer').val);

						//$("label[for='duration']").hide();
					}
				}

				// If we only have the one single appliance, then we must ask the question of how long the policy
				//	should be.. so we need to enable viewing of the element.
				if ( selectVal == 0 )
				{
					$("#appliance_hdrs").fadeOut('fast');
					$("#single_appliance_selected").hide();
				}
				else if ( selectVal == 1 )
				{
					$("#appliance_hdrs").fadeIn('fast');
					$("#single_appliance_selected")
						.fadeIn('fast')
						.append('<br />');
					
					//Remove the CRT option temporary
					//$("#appliance1_appliance_type").find('option:contains(CRT Television)').remove();
				}
				else if ( selectVal > 1 )
				{
					$("#appliance_hdrs").fadeIn('fast');
					$("#single_appliance_selected").hide();
					//$(new Option('CRT Television', 'CRT Television')).appendTo('#appliance1_appliance_type');
				} // if else statements end

				// do the calculatory update for the premium
				if (typeof xajax_updatePremium == 'function')
				{
					xajax_updatePremium(xajax.getFormValues("QuickQuote"));
					return false;
				}

				return true;
			} // function
		);

		/**
		 * When the submit button is clicked, we need to ensure that the selected amount of appliances
		 * have been correctly populated.
		 * If so - then we submit the form
		 * If not - we simply return, without allowing them to continue.
		 *
		 * When JS is off, we simply continue and collect whatever populated data is available
		 *
		 */
		$('#button_qf_QuickQuote_next').click
		(
			function() {

				var selectVal = $("#how_many_appliance :selected").val();
				if (selectVal == 0) {
					alert('You need to select at least one appliance\n');
					return false;
				}

				var bContinue = true;

					for ( var i = 1; i <= selectVal ; i ++ ) {

						if ($('#appliance'+i+'_manufacturer').val()=='' || $('#appliance'+i+'_appliance_type').val()=='') {
							bContinue = false;
						}
					}
					if (bContinue==false) //If we have an error throw alert
					{
						alert('The application form is incomplete.\nPlease ensure appliances are selected!\n');
						return false;
					}
					
			} // function
		);


		var appliance;
		var applianceChosen = 0;
		for (appliance=1;appliance<=8;appliance=appliance+1)
		{

			if ( $('#appliance'+appliance+'_manufacturer').val()!='' || $('#appliance'+appliance+'_appliance_type').val()!='')
			{
				applianceChosen = applianceChosen + 1;
			}

		}


		if (applianceChosen > 0 && typeof xajax_updatePremium == 'function')
		{
			xajax_updatePremium(xajax.getFormValues("QuickQuote"));
		}

		if (applianceChosen > 1)
		{
			$("#row-duration").hide();
		}

		$("#duration,#appliance1_manufacturer,#appliance2_manufacturer,#appliance3_manufacturer,#appliance4_manufacturer,#appliance5_manufacturer,#appliance6_manufacturer,#appliance7_manufacturer,#appliance8_manufacturer").change
		(
			function ()
			{
				if (typeof xajax_updatePremium == 'function')
				{
					xajax_updatePremium(xajax.getFormValues("QuickQuote"));
					return false;
				}
				return true;
			}
		);

		$("#appliance1_appliance_type,#appliance2_appliance_type,#appliance3_appliance_type,#appliance4_appliance_type,#appliance5_appliance_type,#appliance6_appliance_type,#appliance7_appliance_type,#appliance8_appliance_type").change
		(
			function ()
			{
				if (typeof xajax_updatePremium == 'function')
				{
					xajax_updatePremium(xajax.getFormValues("QuickQuote"));
					return false;
				}
				return true;
			}
		);
		
	} // function - ready
	
);

