$(document).ready(function() {
	
	if ($('#calculator'))
	{
		calc = new SizingCalculator();
		
		// add suction options
		var suction = calc.options.suction;
		for (i=0; i<suction.length; i++)
		{
			var opt = new Option();
		 	$(opt).val(suction[i]).text(suction[i]);
			$('#f_suction').append(opt);
		}

		// add discharge options
		var discharge = calc.options.discharge;
		for (i=0; i<discharge.length; i++)
		{
			var opt = new Option();
		 	$(opt).val(discharge[i]).text(discharge[i]);
			$('#f_discharge').append(opt);
		}

		$('#btn_calulate_size').click(function(e) {
			e.preventDefault();
			
			results = calc.findCompressor(
				$('#f_suction').val(),
				$('#f_discharge').val(),
				$('#f_flow').val(),
				$('input[name=mount]:checked').val(),
				$('input[name=drive]:checked').val(),
				$('#f_flow_unit').val()
			);

			if (results.status == 'ok')
			{
				calc.writeResults(results.link);	
			}
			else
			{
				calc.writeResults('<span class="error">' + results.message + '</span>');	
			}

		});	
	}
});

SizingCalculator = function ()
{
	this.options = {
		suction : [0, 1, 2, 3, 4, 5, 10, 15, 20, 30, 40, 50],
		discharge : [10, 15, 20, 25, 30, 35, 75, 100, 125, 150, 200, 250, 300, 350, 400]
	}
	
	this.dataTable = {
		m45 : {
			0 : { 75: 4.8, 100: 4.8, 125: 4.6, 150: 4.0, 200: 3.7 },
			5 : { 75: 6.1, 100: 5.7, 125: 5.7, 150: 5.6, 200: 5.2, 250: 4.9 },
			10 : { 75: 7.4, 100: 7.3, 125: 7.3, 150: 7.2, 200: 6.8, 250: 6.3 },
			20 : { 75: 10.6, 100: 10.5, 125: 10.5, 150: 10.4, 200: 8.9, 250: 7.9 },
			30 : { 100: 13.8, 125: 13.7, 150: 12.0, 200: 10.3, 250: 9.4 },
			40 : { 100: 17.0, 125: 15.9, 150: 14.0, 200: 11.5, 250: 10.5 },
			50 : { 100: 20.2, 125: 19.0, 150: 16.7, 200: 13.7, 250: 11.6 },
			meta : {
				link : "You need a <a href=\"/products/low-horsepower/\">45 HP</a> gas compressor.",
				link_portable : "You need a <a href=\"/products/portable/\">portable 45 HP</a> gas compressor.",
				link_electric : "You need an <a href=\"/products/electric/\">electric 45 HP</a> gas compressor."
			}
		}, 
		m70 : { 
			0 : { 75: 7.9, 100: 7.8, 125: 7.8, 150: 7.7, 200: 7.0 },
			5 : { 75: 10.9, 100: 10.8, 125: 10.8, 150: 10.7, 200: 10.0, 250: 8.7 },
			10 : { 75: 14.0, 100: 13.9, 125: 13.8, 150: 13.4, 200: 11.7, 250: 10.7 },
			20 : { 75: 20.0, 100: 19.9, 125: 17.8, 150: 16.21, 200: 13.9, 250: 13.5 },
			30 : { 100: 26.1, 125: 22.1, 150: 18.4, 200: 16.2, 250: 15.0 },
			40 : { 100: 28.7, 125: 26.2, 150: 22.7, 200: 18.1, 250: 16.5 },
			50 : { 100: 20.2, 125: 31.3, 150: 27.0, 200: 21.6, 250: 18.1 },
			meta : {
				link : "You need a <a href=\"/products/low-horsepower/\">70 HP</a> gas compressor.",
				link_portable : "You need a <a href=\"/products/portable/\">portable 70 HP</a> gas compressor.",
				link_electric : "You need an <a href=\"/products/electric/\">electric 70 HP</a> gas compressor."
			}
		}, 
		m99 : { 
			0 : { 100: 14.4, 125: 13.5, 150: 12.0, 200: 9.7 },  	   	   	   	 
			5 : { 100: 18.1, 125: 17.0, 150: 15.6, 200: 13.1, 250: 11.1 }, 	  	  	 
			10 : { 100: 22.1, 125: 19.0, 150: 18.1, 200: 15.6, 250: 13.7, 300: 9.1, 350: 7.9, 400: 6.9 },
			20 : { 100: 27.5, 125: 25.0, 150: 22.5, 200: 19.3, 250: 17.0, 300: 12.5, 350: 10.1, 400: 9.8 },
			30 : { 100:	34.3, 125: 32.7, 150: 28.2, 200: 23.1, 250: 19.4, 300: 14.2, 350: 13.7, 400: 11.3 },
			40 : { 125: 38.5, 150: 34.3, 200: 26.1, 250: 22.7, 300: 17.8, 350: 14.8, 400: 13.0 },
			50 : { 125: 41.9, 150: 40.6, 200: 31.2, 250: 25.7, 300: 19.4, 350: 18.0, 400: 14.9 },
			60 : { 300: 21.7, 350: 20.1, 400: 17.6 },
			70 : { 300: 26.1, 350: 21.9, 400: 19.0 },
			80 : { 300: 28.3, 350: 24.8, 400: 21.0 },
			90 : { 300: 31.2, 350: 27.7, 400: 22.6 },
			meta : {
				link : "You need a <a href=\"/products/low-horsepower/\">99 HP</a> gas compressor.",
				link_portable : "You need a <a href=\"/products/portable/\">portable 99 HP</a> gas compressor.",
				link_electric : "You need an <a href=\"/products/electric/\">electric 99 HP</a> gas compressor."
			}
		},
		m188 : { 
			0 : { 100: 26.6, 125: 21.9, 150: 20.2, 200: 15.8, 250: 12.1 },  	   	   	 
			5 : { 100: 31.1, 125: 28.7, 150: 24.9, 200: 20.2, 250: 14.9 },	  	  	 
			10 : { 100: 38.9, 125: 32.6, 150: 31.5, 200: 22.5, 250: 19.3, 300: 15.4 }, 	  	 
			20 : { 100: 53.2, 125: 42.2, 150: 38.6, 200: 28.9, 250: 24.3, 300: 23.1, 350: 20.4, 400: 17.3 },
			30 : { 100: 66.2, 125: 52.8, 150: 45.9, 200: 37.9, 250: 31.5, 300: 28.1, 350: 23.9, 400: 22.4 },
			40 : { 125: 66.1, 150: 57.4, 200: 42.0, 250: 35.9, 300: 30.7, 350: 27.8, 400: 27.2 },
			50 : { 125: 84.4, 150: 69.3, 200: 49.0, 250: 43.1, 300: 36.9, 350: 33.5, 400: 28.5 },
			60 : { 200: 57.6, 250: 49.3, 300: 41.9, 350: 37.2, 400: 31.5 },
			70 : { 200: 65.2, 250: 55.8, 300: 45.9, 350: 40.4, 400: 33.1 },
			80 : { 200: 72.5, 250: 61.6, 300: 51.7, 350: 44.1, 400: 37.4 },
			90 : { 200: 82.2, 250: 68.6, 300: 57.6, 350: 48.6, 400: 41.7 },
			meta : { 
				link : "You need a <a href=\"/products/mid-horsepower/\">188 HP</a> gas compressor.",
				link_portable : false,
				link_electric : "You need an <a href=\"/products/electric/\">electric 188 HP</a> gas compressor."
			}
		},
		xlp45 : {
			0 : { 10: 12.7, 15: 11.6, 20: 10.9 },  	   	   	 
			1 : { 10: 14.2, 15: 13.0, 20: 12.0 },	  	  	 
			2 : { 10: 15.9, 15: 14.5, 20: 13.2 }, 	  	  	 
			3 : { 10: 17.1, 15: 16.0, 20: 14.7 }, 	  	  	 
			4 : { 10: 18.7, 15: 17.5, 20: 16.2 }, 	  	  	 
			5 : { 10: 20.2, 15: 18.8, 20: 17.6, 25: 16.3 }, 	  	 
			10 : { 15: 26.3, 20: 25.0, 25: 23.5, 30: 22.1 }, 	 
			15 : { 20: 32.8, 25: 31.5, 30: 29.5, 35: 28.2 },
			meta : {			
				link : "You need a <a href=\"/products/xlp/\">45 HP XLP</a> Low Pressure Booster.",
				link_portable : false,
				link_electric : "You need an <a href=\"/products/electric/\">electric 45 HP XLP</a> Low Pressure Booster."
			}
		},
		xlp70 : {
			0 : { 10: 24.8, 15: 23.3, 20: 22.1 },  	   	   	 
			1 : { 10: 27.6, 15: 25.9, 20: 24.3 }, 	  	  	 
			2 : { 10: 30.2, 15: 28.5, 20: 26.6 }, 	  	  	 
			3 : { 10: 32.7, 15: 31.0, 20: 29.4 }, 	  	  	 
			4 : { 10: 35.5, 15: 33.7, 20: 32.0 }, 	  	  	 
			5 : { 10: 38.0, 15: 36.2, 20: 34.6, 25: 32.5 }, 	  	 
			10 : { 15: 49.8, 20: 47.7, 25: 45.6, 30: 43.6 }, 	 
			15 : { 20: 61.4, 25: 59.2, 30: 57.2, 35: 55.1 },
			meta : {
				link : "You need a <a href=\"/products/xlp/\">70 HP XLP</a> Low Pressure Booster.",
				link_portable : false,
				link_electric : "You need an <a href=\"/products/electric/\">electric 70 HP XLP</a> Low Pressure Booster."
			}
		},
		xlp99 : { 
			0 : { 10: 35.7, 15: 33.5, 20: 32.4 }, 	   	   	 
			1 : { 10: 39.5, 15: 37.5, 20: 35.7 }, 	  	  	 
			2 : { 10: 42.8, 15: 40.8, 20: 38.4 },	  	  	 
			3 : { 10: 46.4, 15: 44.7, 20: 42.6 }, 	  	  	 
			4 : { 10: 49.9, 15: 48.6, 20: 46.0 }, 	  	  	 
			5 : { 10: 53.8, 15: 51.7, 20: 49.9, 25: 46.8 }, 	  	 
			10 : { 15: 70.5, 20: 67.7, 25: 64.8, 30: 63.0 }, 	 
			15 : { 20: 87.4, 25: 84.8, 30: 81.9, 35: 79.9 },
			meta : { 
				link : "You need a <a href=\"/products/xlp/\">99 HP XLP</a> Low Pressure Booster.",
				link_portable : false,
				link_electric : "You need an <a href=\"/products/electric/\">electric 99 HP XLP</a> Low Pressure Booster."
			}
		}
	}
	return true;
}

SizingCalculator.prototype = {

	writeResults : function(results) {
		$('#calc_results').html(results);
	},
	
	findCompressor : function (suction, discharge, flow, type, drive, flow_unit) {
		var dt = this.dataTable;
		
		// convert flow rate 
		if (flow_unit == 'mcf')
		{
			flow = flow / 35.31;
		}
		
		//console.log(flow_unit, flow);
		
		try {
			for (obj in dt)
			{
				var comp = dt[obj];
				if ((comp[suction] != undefined) && (comp[suction][discharge] != undefined))
				{
					if (parseFloat(flow) < parseFloat(comp[suction][discharge]))
					{
						if (type == 'skid')
						{
							if (drive == 'engine')
							{
								return { 
										status: 'ok',
										link : comp.meta.link
									}
							} 
							else
							{
								return { 
										status: 'ok',
										link : comp.meta.link_electric
									}
							}
						}
						else
						{
							if (comp.meta.link_portable)
							{
								return { 
									status: 'ok',
									link : comp.meta.link_portable
								}
							}
							else
							{
								return {
									status: 'error',
									message: 'The conditions you have entered are outside of the parameters of this sizing program. Please consult with a Brahma representative.'
								}
							}
						}
					}
				}
			}
		} catch (e) {
			// console.log(e);
			return {
				status : 'error',
				message: 'The conditions you have entered are outside of the parameters of this sizing program. Please consult with a Brahma representative.'
			}
		}

		return {
			status : 'error',
			message: 'The conditions you have entered are outside of the parameters of this sizing program. Please consult with a Brahma representative.'
		}
	}
};