var lagoonShape = '';
var resultsMap = {};

function setupOptionClick() {
	$('select#whatCrop option').click(function() {
		var group = $(this).val();
		getRecOz(group, cropType);
	});
}

function submitAppForm(str) {
	jQuery.ajax({
		type: "POST",
		url: "getRecApplication.php",
		dataType: "html",
		data: str,
		success: function(response){
			$('.recommendationsFullYear').show();
			$('.recommendationsSingleApp').show();
			$('.appNote').show();
			var results = response;
			results = results.split(';');
			for(i in results) {
				var values = results[i].split(':');
				var key = values[0];
				resultsMap[key] = values[1];
			}
			
			var totalGallons = resultsMap['Total-Gallons']; 
			var totalGallonsPerApplication = resultsMap['Total-Gallons-Per-Application'];
			var totalJugs = resultsMap['Total-Jugs']; 
			var totalJugsPerApplication = resultsMap['Total-Jugs-Per-Application']; 
			var totalBioGallons = resultsMap['Total-Bio-Gallons']; 
			var totalBioGallonsPerApplication = resultsMap['Total-Bio-Gallons-Per-Application']; 
			var totalBioJugs = resultsMap['Total-Bio-Jugs']; 
			var totalBioJugsPerApplication = resultsMap['Total-Bio-Jugs-Per-Application']; 
			var totalCarbonGallons = resultsMap['Total-Carbon-Gallons']; 
			var totalCarbonGallonsPerApplication = resultsMap['Total-Carbon-Gallons-Per-Application']; 
			var totalCarbonJugs = resultsMap['Total-Carbon-Jugs']; 
			var totalCarbonJugsPerApplication = resultsMap['Total-Carbon-Jugs-Per-Application']; 
			var totalCost = resultsMap['Total-Cost']; 
			var totalCostPerApplication = resultsMap['Total-Cost-Per-Application']; 
			var totalCostPerAcre = resultsMap['Total-Cost-Per-Acre']; 
			var totalCostPerAcrePerApplication = resultsMap['Total-Cost-Per-Acre-Per-Application']; 
			//var totalGallonsWater = resultsMap['Total-Gallons-Water']; 
			//var totalGallonsWaterPerApplication = resultsMap['Total-Gallons-Water-Per-Application'];;
			//var roundedTotalGallons = resultsMap['Rounded-Total-Gallons']; 
			//var roundedTotalGallonsPerApplication = resultsMap['Rounded-Total-Gallons-Per-Application']; 
			//var roundedTotalBioGallons = resultsMap['Rounded-Total-Bio-Gallons']; 
			//var roundedTotalBioGallonsPerApplication = resultsMap['Rounded-Total-Bio-Gallons-Per-Application']; 
			//var roundedTotalCarbonGallons = resultsMap['Rounded-Total-Carbon-Gallons'];
			//var roundedTotalCarbonGallonsPerApplication = resultsMap['Rounded-Total-Carbon-Gallons-Per-Application'];
			//var roundedTotalCost = resultsMap['Rounded-Total-Cost']; 
			//var roundedTotalCostPerApplication = resultsMap['Rounded-Total-Cost-Per-Application']; 
			//var roundedTotalCostPerAcre = resultsMap['Rounded-Total-Cost-Per-Acre'];
			//var roundedTotalCostPerAcrePerApplication = resultsMap['Rounded-Total-Cost-Per-Acre-Per-Application']; 
			//var roundedTotalGallonsWater = resultsMap['Rounded-Total-Gallons-Water'];
			
			$('#bioGalSingleApp').val(totalBioGallonsPerApplication);
			$('#bioJugsSingleApp').val(totalBioJugsPerApplication);
			$('#carbonGalSingleApp').val(totalCarbonGallonsPerApplication);
			$('#carbonJugsSingleApp').val(totalCarbonJugsPerApplication);
			$('#totalGalSingleApp').val(totalGallonsPerApplication);
			$('#totalJugsSingleApp').val(totalJugsPerApplication);
			$('#totalCostSingleApp').val(totalCostPerApplication);
			$('#costPerAcreSingleApp').val(totalCostPerAcrePerApplication);
			//$('#totalGalWaterSingleApp').val(totalGallonsWaterPerApplication);
							
			$('#bioGal').val(totalBioGallons);
			$('#bioJugs').val(totalBioJugs);
			$('#carbonGal').val(totalCarbonGallons);
			$('#carbonJugs').val(totalCarbonJugs);
			$('#totalGal').val(totalGallons);
			$('#totalJugs').val(totalJugs);
			$('#totalCost').val(totalCost);
			$('#costPerAcre').val(totalCostPerAcre);
			//$('#totalGalWater').val(totalGallonsWater);
		},
		error: function(){
			alert("Error occured during while retrieving information from the server.");
		},
		complete: function(){
			resize();
	   }
	});
}

function getVolume(data) {

	jQuery.ajax({
		type: "POST",
		url: "getVolume.php",
		dataType: "html",
		data: data,
		success: function(response){
			//$('#rightSide').append(response)
			$('#recAcreFt').val(response);
			$('#acreFt').val(response);
			$('#submitRow').show();
		},
		error: function(){
			alert("Error occured during while retrieving information from the server.");
		},
		complete: function(){
			resize();
	   }
	});
}

/*function getCrops(type) {
	jQuery.ajax({
		type: "POST",
		url: "getCrops.php",
		dataType: "html",
		data: "type=" + type,
		success: function(response){
			$("select#whatCrop").append(response);
			setupOptionClick();
		},
		error: function(){
			alert("Error occured during while retrieving crop information from the server.");
		}
	});
}*/

function getFields(shape) {
	$('.shapeRow').hide();
	if(shape == 'irRectangle') {
		$('#length1').show();
		$('#length2').show();
		$('#width1').show();
		$('#width2').show();
	} else if(shape == 'rectangle') {
		$('#length1').show();
		$('#width1').show();
	} else if(shape == 'circle') {
		$('#diameter').show();
	} else if(shape == 'triangle') {
		$('#side1').show();
		$('#side2').show();
		$('#side3').show();
	}
	resize();
}

$(function() {
	//$('input[name=reset]').trigger('click');
	$('.recommendationsFullYear').hide();
	$('.recommendationsSingleApp').hide();
	$('#length1').hide();
	$('#length2').hide();
	$('#width1').hide();
	$('#width2').hide();
	$('#side1').hide();
	$('#side2').hide();
	$('#side3').hide();
	$('#diameter').hide();
	$('#prevDepthRow').hide();
	$('#submitRow').hide();
	$('.appNote').hide();
	$('input[name=shape]').focus(function() {
		lagoonShape = $(this).val();
		getFields(lagoonShape);
	});
	$('img.shape').click(function() {
		var imgShape = $(this).attr('id');
		$('#' + imgShape + 'Bu').attr('checked', 'checked');
		getFields(imgShape);
	});
	/*$('input[name=cropType]').focus(function() {
		cropType = $(this).val();
		$("select#whatCrop").empty();
		getCrops(cropType);
	});*/
	
	$('input[name=treatmentType]').click(function() {							  
		$('#prevDepthRow').hide();
		if($(this).attr('id') == 'followUp') {
			$('#prevDepthRow').show();
		}
		resize();
	});
	
	$('#calculateVol').click(function() {
		var str = $("#recAppForm").serialize();							  
		getVolume(str);
	});
	
	$('#recAppForm').submit(function(event) {
		event.preventDefault();
		var str = $("#recAppForm").serialize();
			submitAppForm()(str);
	});	
});
