var resultsMap = {};

function submitAppForm(str, type) {
	jQuery.ajax({
		type: "POST",
		url: "getRecApplication.php",
		dataType: "html",
		data: str + '&type=' + type,
		success: function(response){
			var results = response;
			//$('#rightSide').append(response);
			results = results.split(';');
			for(i in results) {
				var values = results[i].split(':');
				var key = values[0];
				resultsMap[key] = values[1];
			}

			if(type == 'products') {
				var totalGallons = resultsMap['Total-Gallons'];
				var totalJugs = resultsMap['Total-Jugs']; 
				var totalBioGallons = resultsMap['Total-Bio-Gallons']; 
				var totalBioJugs = resultsMap['Total-Bio-Jugs']; 
				var totalCarbonGallons = resultsMap['Total-Carbon-Gallons']; 
				var totalCarbonJugs = resultsMap['Total-Carbon-Jugs']; 
				var totalCost = resultsMap['Total-Cost'];
								
				$('#bioGal').val(totalBioGallons);
				$('#bioJugs').val(totalBioJugs);
				$('#carbonGal').val(totalCarbonGallons);
				$('#carbonJugs').val(totalCarbonJugs);
				$('#totalGal').val(totalGallons);
				$('#totalJugs').val(totalJugs);
				$('#totalCost').val(totalCost);
				
				/*$('.recommendationsFullYear').show();*/
				$('.recommendedApp').show();
				$('.appNote').show();
			} else if(type == 'service') {
				var totalServiceEstimate = resultsMap['Total-Service-Estimate'];
				$('#totalServiceCost').val(totalServiceEstimate);
				$('.serviceEstimate').show();
			}
		},
		error: function(){
			alert("Error occured during while retrieving recommended oz/feet<sup>2</sup> information from the server.");
		},
		complete: function(){
			resize();
	   }
	});
}

$(function() {
	if(page == 'application.php') {
		//$('input[name=reset]').trigger('click');
		$('.recommendedApp').hide();
		$('.serviceEstimate').hide();
		$('.appNote').hide();

		$('#quoteForm').hide();
		$('.estimate').click(function() {
			var type = $(this).text();
			if(type == 'Products' || type == 'product') {
				$('#quoteForm').hide();
				$('#recAppForm').show('slow');
			} else if(type == 'Service' || type == 'Service Estimate') {
				$('#recAppForm').hide();
				$('#quoteForm').show('slow');
			}
		});
	
	
		$('#recAppForm').submit(function(event) {
			event.preventDefault();
			var str = $("#recAppForm").serialize();
				submitAppForm(str, 'products');
		});	
		
		$('#quoteForm').submit(function(event) {
			event.preventDefault();
			var str = $("#quoteForm").serialize();
				submitAppForm(str, 'service');
		});
	}
});