var investments = new Array();
investments[1] = 35000;
investments[2] = 1800;
investments[3] = 8000;
investments[4] = 850;
investments[5] = 2000;
investments[6] = 1800;

var fuel = new Array();
fuel[1] = 14000;
fuel[2] = 5300;
fuel[3] = 5500;
fuel[4] = 730;
fuel[5] = 2560;
fuel[6] = 1830;

var GHG = new Array();
GHG[1] = 35;
GHG[2] = 14;
GHG[3] = 13.5;
GHG[4] = 2;
GHG[5] = 5;
GHG[6] = 4;

function validateInputLength(value)
{
	if (value.replace(/^\s+|\s+$/g, '')=="" || value.length==0)
		return(false);
	return(true);
}

function displayCurrentRebateTotal(id)
{
	var quantity = $('input#zone_input_' +id).attr("value");
	if(quantity == '')
		quantity =0;
	if($('#claim_' +id).length > 0)
		claim = parseInt($('#claim_' +id).html());
	else
		claim = 0;
	quantity = parseInt(quantity) + claim; 
	var rebate = $('input#amt_' +id).attr("value");
	var total = quantity * rebate;
	
	if (total < 0)
		$('div#total_' +id).attr("innerHTML","$0");
	else
		$('div#total_' +id).attr("innerHTML","$" +numberFormat(total));
	
	$('div#rebate_' +id).attr("innerHTML","$" +numberFormat(rebate));
}

function loopCurrentRebateTotals()
{
	var ids = $('input#categories_ids').attr("value");

	var ids_array = new Array();
	ids_array = ids.split(",");
	ids_array_count = ids_array.length;
	
	for (i = 0; i < ids_array_count; i++)
	{
		var current_quantity = $('input#zone_input_' +ids_array[i]).attr("value").replace('$','').replace(' ','').replace(/,/gi,'');
		if (current_quantity > 0 && validateInputLength(current_quantity)==true)
			displayCurrentRebateTotal(ids_array[i]);
	}
}

function calculateTotalNumberofInput()
{
	var total = 0;
	var ids = $('input#categories_ids').attr("value");
	
	var ids_array = new Array();
	ids_array = ids.split(",");
	ids_array_count = ids_array.length;
	
	for (i = 0; i < ids_array_count; i++)
	{
		var current_total = $('input#zone_input_' +ids_array[i]).attr("value").replace('$','').replace(' ','').replace(/,/gi,'');
		
		if (current_total > 0 && validateInputLength(current_total)==true)
			total = parseInt(total) + parseInt(current_total);
	}
	
	if (typeof(display_fleet_analysis)!=="undefined")
		if (total>=25 && display_fleet_analysis==0)
		{
			display_fleet_analysis = 1;
			//$('a.fleet_analysis').click();
			
			if (current_blurb!=7)
			{
				current_blurb = 6;
				cycleBackgroundImage();
			}
		}
}

function calculateTotalPlanEOITotals()
{
	var total = 0;
	var total_investment = 0;
	var total_fuel = 0;
	var total_GHG = 0;
	var ids = $('input#eoi_categories_ids').attr("value");
	
	var ids_array = new Array();
	ids_array = ids.split(",");
	ids_array_count = ids_array.length;
	
	/*####################### Claims 'My Plan' Calculation Displays ##############################*/
	for (i = 0; i < ids_array_count; i++)
	{
		var current_quantity = $('input#eoi_amt_' +ids_array[i]).attr("value").replace('$','').replace(' ','').replace(/,/gi,'');
		if(validateInputLength(current_quantity)==true)
		{
			if($('#claim_' +ids_array[i]).length > 0)
				claim = parseInt($('#claim_' +ids_array[i]).html());
			else
				claim = 0;
			claim = parseInt($('#claim_' +ids_array[i]).html());
			//current_quantity = parseInt(current_quantity) + claim; 
			//alert(current_quantity);
			if (current_quantity > 0 )
			{
				total_investment = (parseInt(current_quantity) * investments[ids_array[i]]) + parseInt(total_investment);
				total_fuel = (parseInt(current_quantity) * fuel[ids_array[i]]) + parseInt(total_fuel);
				total_GHG = (parseInt(current_quantity) * GHG[ids_array[i]]) + parseInt(total_GHG);
			}
		}
	}
	/*####################### Claims 'My Plan' Calculation Displays ##############################*/
	
	var total_fuel = numberFormat(total_fuel,",",0) +" L/yr";
	$('div.eoi_total_fuel').attr("innerHTML","<strong class=\"important\">Estimated Fuel Savings:</strong><br>" +total_fuel);
	
	var total_GHG = numberFormat(total_GHG,",",1) +" Tonnes/yr";
	$('div.eoi_total_GHG').attr("innerHTML","<strong class=\"important\">GHG Reductions:</strong><br>" +total_GHG);
}

function calculateTotalExpressionOfInterest()
{
	var total = 0;
	var total_investment = 0;
	var total_fuel = 0;
	var total_GHG = 0;
	var ids = $('input#categories_ids').attr("value");
	var pay_to_maximum = $('input#pay_to_maximum').attr("value");
	var claim_total = 0;
	var reserved_total = 0;
	
	var ids_array = new Array();
	ids_array = ids.split(",");
	ids_array_count = ids_array.length;
	
	for (i = 0; i < ids_array_count; i++)
	{
		var claim_amt = 0;
		rebate = $('input#amt_' +ids_array[i]).attr("value").replace('$','').replace(' ','').replace(/,/gi,'');
		if($('div#claim_' +ids_array[i]).length > 0)
			claim_amt = $('div#claim_' +ids_array[i]).attr("innerHTML").replace('$','').replace(' ','').replace(/,/gi,'');
		var reserved_amt = $('input#zone_input_' +ids_array[i]).attr("value").replace('$','').replace(' ','').replace(/,/gi,'');
		if(reserved_amt == "")
		{
			reserved_amt = 0;
		}
		if(rebate > 0)
		{
			if(reserved_amt>0)
				reserved_total = parseInt(reserved_total) + (parseInt(rebate) * parseInt(reserved_amt));
			if(claim_amt>0)
				claim_total = parseInt(claim_total) + (parseInt(rebate) * parseInt(claim_amt));
			total = reserved_total + claim_total;
			//alert("Total:" + total + " Reserved:"+reserved_total+ " Claim:"+claim_total+" rebate:"+rebate);
		}
	/*	var current_total = $('div#total_' +ids_array[i]).attr("innerHTML").replace('$','').replace(' ','').replace(/,/gi,'');
		if (current_total > 0 && validateInputLength(current_total)==true)
			total = parseInt(total) + parseInt(current_total);
	*/
	}
	
	var total_used = "";
	var percentage = Math.round(((parseInt(total)/parseInt(pay_to_maximum))*100));
	var percentage_reserved = Math.round(((parseInt(reserved_total)/parseInt(pay_to_maximum))*100));
	var percentage_claim = Math.round(((parseInt(claim_total)/parseInt(pay_to_maximum))*100));
	
	var total_used = "$" +numberFormat(total,",");
	
	/*####################### Rebate Calculation Displays ##############################*/
	$('div.total_rebate').attr("innerHTML","Total Rebate " +total_used);
	
	for (i = 0; i < ids_array_count; i++)
	{
		var current_quantity = $('input#zone_input_' +ids_array[i]).attr("value").replace('$','').replace(' ','').replace(/,/gi,'');
		if(validateInputLength(current_quantity)==true)
		{
			if($('#claim_' +ids_array[i]).length > 0)
				claim = parseInt($('#claim_' +ids_array[i]).html());
			else
				claim = 0;
			current_quantity = parseInt(current_quantity) + claim; 
			//alert(current_quantity);
			if (current_quantity > 0 )
			{
				total_investment = (parseInt(current_quantity) * investments[ids_array[i]]) + parseInt(total_investment);
				total_fuel = (parseInt(current_quantity) * fuel[ids_array[i]]) + parseInt(total_fuel);
				total_GHG = (parseInt(current_quantity) * GHG[ids_array[i]]) + parseInt(total_GHG);
			}
		}
	}	
	
	var total_investment = "$" +numberFormat(total_investment,",");
	$('div.total_investment').attr("innerHTML","Estimated Investment " +total_investment);

	var total_fuel = numberFormat(total_fuel,",",0) +" L/yr";
	$('div.total_fuel').attr("innerHTML","Estimated Fuel Savings " +total_fuel);
	/*####################### Rebate Calculation Displays ##############################*/
	
	if (parseInt(total)>0 && percentage>20)
		var total_used = total_used +" Used";
	
	if (parseInt(total) > parseInt(pay_to_maximum))
		var total_used = "$" +numberFormat(pay_to_maximum,",") +" Limited Reached";
		
	//$('div#total').attr("innerHTML","<div class=\"progress\"><div class=\"progress_bar\">" +total_used + "</div></div>Currently using <strong>$" +numberFormat(total,",") +"</strong> of <strong>$" +numberFormat(pay_to_maximum,",") +"</strong> limit.");
	$('div#total').attr("innerHTML","<div class=\"progress\"><div class=\"claim_progress_bar\"></div><div class=\"progress_bar\"></div></div>Currently using <strong>$" +numberFormat(total,",") +"</strong> of <strong>$" +numberFormat(pay_to_maximum,",") +"</strong> limit.");
	$('input#pay_total').attr("value",total);
	
	if (parseInt(total) > parseInt(pay_to_maximum))
	{
		$('div#total').append(" <span class=\"error\">Over limit by $" +numberFormat((parseInt(total) - parseInt(pay_to_maximum)),",") +".</span>");
		$('div.total_rebate').attr("innerHTML","Total Rebate $" +numberFormat(pay_to_maximum,","));
		$('div.progress_bar').css("width","100%");
		$('div.progress_bar').css("background-color","#FF0000");
		$('div.progress_bar').css("background","url('/images/bar_red.jpg')");
	}
	else
		/* $('div.progress_bar').css("width",percentage +"%"); */
		$('div.progress_bar').css("width",percentage_reserved +"%");
		$('div.claim_progress_bar').css("width",percentage_claim +"%");
		
	
	/*####################### Claims EOI Calculation Displays ##############################*/
	if (typeof(calculate_claims_eoi)!=="undefined")
	{
		
		if (parseInt(total) > parseInt(pay_to_maximum))
			$('div.total_rebate').attr("innerHTML","<strong>Total</strong> $" +numberFormat(pay_to_maximum,",") +"<br><span class=\"error\">* You've reached your reservation limit of $" +numberFormat(pay_to_maximum,",")+"</span>");
		else
			$('div.total_rebate').attr("innerHTML","<strong>Total</strong> " +total_used.replace('Used',''));
		$('div.total_fuel').attr("innerHTML","<strong class=\"important\">Estimated Fuel Savings:</strong><br>" +total_fuel);
		
		var total_GHG = numberFormat(total_GHG,",",1) +" Tonnes/yr";
		$('div.total_GHG').attr("innerHTML","<strong class=\"important\">GHG Reductions:</strong><br>" +total_GHG);
	}
	/*####################### Claims EOI Calculation Displays ##############################*/
}

$('input.quantity_input').keyup(function(e) {
	var keynum;
	if(window.event)
		keynum = e.keyCode;
	else if(e.which)
		keynum = e.which;
	
	if ((keynum==8 || keynum==46 || keynum==9 || typeof(keynum)=="undefined") || (keynum > 47 && keynum < 58) || (keynum >= 96 && keynum <= 105) || keynum==144)
	{
		var id = $(this).attr("id").replace('zone_input_','');
		
		displayCurrentRebateTotal(id);
		calculateTotalExpressionOfInterest();
		calculateTotalNumberofInput();
		return(true);
	}
	else
	{
		var id = $(this).attr("id").replace('zone_input_','');
		var value = $('input#zone_input_' +id).attr("value");
		value = value.substring(0,(value.length-1));
		
		$('input#zone_input_' +id).attr("value",value);
		
		return(false);
	}
});

$('input.quantity_input').blur(function() {
	var id = $(this).attr("id").replace('zone_input_','');
	displayCurrentRebateTotal(id);
	calculateTotalExpressionOfInterest();
});

$('input#reserve_button').click(function() {
	var total_quantity = 0;
	
	var ids = $('input#categories_ids').attr("value");
	var ids_array = new Array();
	ids_array = ids.split(",");
	ids_array_count = ids_array.length;
	
	for (i = 0; i < ids_array_count; i++)
	{
		var current_quantity = $('input#zone_input_' +ids_array[i]).attr("value");
		if (current_quantity > 0 && validateInputLength(current_quantity)==true)
			total_quantity = parseInt(total_quantity) + parseInt(current_quantity);
	}
	
	if (total_quantity==0)
		alert("Sorry, you must enter some quantites before you can reserve your funds.");
	else
		document.forms["eoi_form"].submit();
});
