/****************************************************
* Home Gas Tariff Calculator						*
*													*
*****************************************************/

var calc_titles = [], tariffs = [], column_titles = [], annual_standing_charges = [], tier1_units = [], tier1_prices = [], tier2_prices = [], capacity_charges = [], winters_saver_credits = [], user_type_threshold = [], lru_threshold = [];

var numberOfCalculators = 1;		//***** SET TO 2 IF NEW PRICES CHANGE *******//




/****************************************************
* Calculator Information - info, prices, etc		*
* array[0] == new info								*
* array[1] == old info								*
*													*
*****************************************************/

calc_titles[0] = 'New Tariff Prices effective from 1st October 2011 (inc. VAT 13.50%)';

tariffs[0] = ['Standard Tariff', 'No Standing Charge Tariff', 'Winter Saver Tariff', 'Large Residential User Tariff'];

column_titles[0] = ['Tariff', 'Total<br />Annual<br />Cost<br />&euro;', 'Annual<br />Standing<br />Charge<br />&euro;', 'Tier 1/<br />Bundled Units<br /><br />kWh', 'Tier 1<br />Price<br /><br />c/kWh', 'Tier 2<br />Price<br /><br />c/kWh', 'Capacity<br />Charge<br /><br />&euro;/kWh'];

annual_standing_charges[0] = ['82.89', 'n/a', '255.31', '91.94'];

tier1_units[0] = ['', '2870', '3174', ''];

tier1_prices[0] = ['5.432', '8.321', '', '3.371'];

tier2_prices[0] = ['', '5.432', '5.432', ''];

capacity_charges[0] = ['', '', '', '2.096'];

winters_saver_credits[0] = ['', '', '172.41', ''];//152.63

user_type_threshold[0] = 73000;

lru_threshold[0] = 3750;






/****************************************************
* Prototype functions definitions					*
*													*
*****************************************************/

function Stretch(Q, L, c) { var S = Q;
	if (c.length>0){ while (S.length<L) { S = c+S; } }
 	return S;
}

function StrU(X, M, N) { // X>=0.0
	var T, S=new String(Math.round(X*Number("1e"+N)));
	if (S.search && S.search(/\D/)!=-1) { return ''+X; }
	with (new String(Stretch(S, M+N, '0')))
	return substring(0, T=(length-N)) + '.' + substring(T);
}

function Sign(X) { return X<0 ? '-' : ''; }

function StrS(X, M, N) { return Sign(X)+StrU(Math.abs(X), M, N) }

Number.prototype.toFixed= function(n){ return StrS(this,1,n)};





/****************************************************
* Add Commas to Numbers above 1000					*
*													*
*****************************************************/

function commafy(num){
	var sNum, rNum = '';
	if(!isNaN(num)){
		sNum = num.toString();
		if(num<1000){ rNum = num; }
		if(num>=1000 && num<1000000){
			rNum = sNum.substring(0, sNum.length-3) + ',' + sNum.substring(sNum.length-3, sNum.length);
		} else if(num>=1000000 && num<1000000000){
			rNum = sNum.substring(0, sNum.length-6) + ',' + sNum.substring(sNum.length-6, sNum.length-3) + ',' + sNum.substring(sNum.length-3, sNum.length);
		} else if(num>=1000000000){
			rNum = sNum.substring(0, sNum.length-9) + ',' + sNum.substring(sNum.length-9, sNum.length-6) + ',' + sNum.substring(sNum.length-6, sNum.length-3) + ',' + sNum.substring(sNum.length-3, sNum.length);
		}
	} else {
		rNum = num;
	}
	
	return rNum;
}




/****************************************************
* Validate Information								*
*													*
*****************************************************/

function validateInfo(i){
	if(!column_titles[0]){ alert('Please enter column titles'); return false; }
	if(!column_titles[i]){ column_titles[i] = column_titles[0]; }
	if(!tariffs[0]){ alert('Please enter tariff names'); return false; }
	if(!tariffs[i]){ tariffs[i] = tariffs[0]; }
	if(!annual_standing_charges[i]){ alert('Please enter annual standing charges for calculator ' + i); return false; }
	if(!tier1_units[i]){ alert('Please enter tier 1 units for calculator ' + i); return false; }
	if(!tier1_prices[i]){ alert('Please enter tier 1 prices for calculator ' + i); return false; }
	if(!tier2_prices[i]){ alert('Please enter tier 2 prices for calculator ' + i); return false; }
	if(!capacity_charges[i]){ alert('Please enter capacity charges for calculator ' + i); return false; }
	if(!winters_saver_credits[i]){ alert('Please enter winters saver credits for calculator ' + i); return false; }
	if(!user_type_threshold[0]){ alert('Please enter user type threshold'); return false; }
	if(!user_type_threshold[i]){ user_type_threshold[i] = user_type_threshold[0]; }
	if(!lru_threshold[0]){ alert('Please enter large residential threshold'); return false; }
	if(!lru_threshold[i]){ lru_threshold[i] = lru_threshold[0]; }
	
	return true;
}





/****************************************************
* Show Supply Point Capacity Errors on user input	*
*													*
*****************************************************/

function showSpcErrors(num){
	if(num === 0){
		alert('Please enter the appropriate Supply Point Capacity. This figure must be less than 3,750 kWh');
	} else if(num == 1){
		alert('These tariffs are not available to customers with a Supply Point Capacity of more than 3,750 kWh. Please see the Fuel Variation Tariff for more information.');
	}
	
	$('spc-input').focus();
}





/****************************************************
* Clear Results										*
*													*
*****************************************************/

function clearResults(){
	for(var i=0; i<numberOfCalculators; i++){
		if($('annual-cost-tariff-' + 0 + '-' + i)){ $('annual-cost-tariff-' + 0 + '-' + i).innerHTML = ''; }
		if($('annual-cost-tariff-' + 1 + '-' + i)){ $('annual-cost-tariff-' + 1 + '-' + i).innerHTML = ''; }
		if($('annual-cost-tariff-' + 2 + '-' + i)){ $('annual-cost-tariff-' + 2 + '-' + i).innerHTML = ''; }
		if($('annual-cost-tariff-' + 3 + '-' + i)){ $('annual-cost-tariff-' + 3 + '-' + i).innerHTML = ''; }
	}
	$('spc-holder').setStyle('display', 'none');
}





/****************************************************
* Update Annual Costs								*
*													*
*****************************************************/

function getCost(num, spc){
	var i, j, fig = num.toFloat();
	var spcFig = false;
	
	if(spc){ spcFig = spc.toFloat(); }
	
	if(isNaN(fig)){ clearResults(); return; }
	
	for(i=0; i<numberOfCalculators; i++){
		if(user_type_threshold[i]){
			
			if(fig > 73000){ $('spc-holder').setStyle('display', 'inline'); }
			else { $('spc-holder').setStyle('display', 'none'); }
						
			if($('annual-cost-tariff-' + 0 + '-' + i) && annual_standing_charges[i][0] && tier1_prices[i][0]){
				if(fig <= user_type_threshold[i]){
					$('annual-cost-tariff-' + 0 + '-' + i).innerHTML = Math.max((annual_standing_charges[i][0].toFloat() + (fig * (tier1_prices[i][0].toFloat()/100))), 0).toFixed(2);					
				} else {
					$('annual-cost-tariff-' + 0 + '-' + i).innerHTML = 'n/a';
				}
			}
			
			if($('annual-cost-tariff-' + 1 + '-' + i) && tier1_units[i][1] && tier1_prices[i][1] && tier2_prices[i][1]){
				if(fig <= user_type_threshold[i]){
					if(fig < tier1_units[i][1].toFloat()){
						$('annual-cost-tariff-' + 1 + '-' + i).innerHTML = Math.max((fig * (tier1_prices[i][1].toFloat()/100)), 0).toFixed(2);
					} else {
						$('annual-cost-tariff-' + 1 + '-' + i).innerHTML = Math.max(((tier1_units[i][1].toFloat() * (tier1_prices[i][1].toFloat()/100)) + ((fig - tier1_units[i][1].toFloat()) * (tier2_prices[i][1].toFloat()/100))), 0).toFixed(2);
					}
				} else {
					$('annual-cost-tariff-' + 1 + '-' + i).innerHTML = 'n/a';
				}
			}
			
			if($('annual-cost-tariff-' + 2 + '-' + i) && annual_standing_charges[i][2] && winters_saver_credits[i][2] && tier2_prices[i][2]){
				if(fig <= user_type_threshold[i]){
					$('annual-cost-tariff-' + 2 + '-' + i).innerHTML = Math.max(Math.max(((fig * (tier2_prices[i][2].toFloat()/100)) + annual_standing_charges[i][2].toFloat() - winters_saver_credits[i][2].toFloat()), (annual_standing_charges[i][2].toFloat())), 0).toFixed(2);
				} else {
					$('annual-cost-tariff-' + 2 + '-' + i).innerHTML = 'n/a';
				}
			}
			
			if($('annual-cost-tariff-' + 3 + '-' + i) && annual_standing_charges[i][3] && tier1_prices[i][3] && capacity_charges[i][3]){
				if(fig <= user_type_threshold[i]){
					$('annual-cost-tariff-' + 3 + '-' + i).innerHTML = 'n/a';
				} else {
					if(spcFig){
						if(spcFig > lru_threshold[i]){
							showSpcErrors(1);
							showSpcErrors(0);
							$('annual-cost-tariff-' + 3 + '-' + i).innerHTML = '';
							return;
						}
						
						$('annual-cost-tariff-' + 3 + '-' + i).innerHTML = Math.max((annual_standing_charges[i][3].toFloat() + (fig * (tier1_prices[i][3].toFloat()/100)) + (spcFig * capacity_charges[i][3].toFloat())), 0).toFixed(2);
						
					} else {
						$('annual-cost-tariff-' + 3 + '-' + i).innerHTML = '';
						showSpcErrors(0);						
					}
				}
			}
		}
	}
}






/****************************************************
* Build Calculator and add events/styles			*
*													*
*****************************************************/

function buildCalculator(){
	var i, j, k, div, input, table, tBody, tr, th, td, className, columns, html, id;
	for(i=0; i<numberOfCalculators; i++){
		if(i === 0){ className = 'new-info'; }
		else { className = 'old-info'; }
		
		if(!validateInfo(i)){ return false; }
		
		if(i === 0){
			div = new Element('div', {
				'id': 'consumption-holder'
			}).inject($('tariff-calculator'));
			
			div = new Element('div', {
				'id': 'consumption-label',
				'html': 'Annual Usage (KWh):'
			}).inject($('consumption-holder'));
			
			input = new Element('input', {
				'id': 'consumption-input',
				'events': {
					'keyup': function(){ getCost($('consumption-input').get('value'), $('spc-input').get('value')); }
				}
			}).inject($('consumption-holder'));		
			
			div = new Element('div', {
				'id': 'spc-holder',
				'styles': { 'display': 'none' }
			}).inject($('tariff-calculator'));
			
			div = new Element('div', {
				'id': 'spc-label',
				'html': 'Supply Point Capacity:'
			}).inject($('spc-holder'));
			
			input = new Element('input', {
				'id': 'spc-input',
				'events': {
					'keyup': function(){ getCost($('consumption-input').get('value'), $('spc-input').get('value')); }
				}
			}).inject($('spc-holder'));
		}
		
		table = new Element('table', {
			'id': 'calc-table-' + i,
			'class': 'calculator ' + className
		}).inject($('tariff-calculator'));
		
		tBody = new Element('tbody', {}).inject(table);
		
		columns = column_titles[i].length;
		
		if(calc_titles[i]){
			tr = new Element('tr', {}).inject(tBody);
			th = new Element('th', {
				'colspan': columns,
				'class': 'no-border-left',
				'html': calc_titles[i]
			}).inject(tr);			
		}
		
		tr = new Element('tr', { 'class': 'col_headings' }).inject(tBody);
		for(j=0; j<columns; j++){
			if(j === 0){ className = 'first'; }
			else if(j == 1){ className = 'annual-col'; }
			else if(j == columns - 1){ className = 'border-right'; }
			else { className = ''; }
			td = new Element('td', {
				'class': className,
				'html': column_titles[i][j]
			}).inject(tr);
		}
		
		for(j=0; j<tariffs[i].length; j++){
			tr = new Element('tr', {}).inject(tBody);
			for(k=0; k<columns; k++){
				if(k === 0){ className = 'first'; }
				else if(k == 1){ className = 'annual-col'; }
				else if(k == columns - 1){ className = 'border-right'; }
				else { className = ''; }
				
				if(k === 0){ if(tariffs[i][j]){ html = tariffs[i][j]; } else { html = ''; } }
				else if(k == 1){ html = ''; }
				else if(k == 2){ if(annual_standing_charges[i][j]){ html = annual_standing_charges[i][j]; } else { html = ''; } }
				else if(k == 3){ if(tier1_units[i][j]){ html = tier1_units[i][j]; } else { html = ''; } }
				else if(k == 4){ if(tier1_prices[i][j]){ html = tier1_prices[i][j]; } else { html = ''; } }
				else if(k == 5){ if(tier2_prices[i][j]){ html = tier2_prices[i][j]; } else { html = ''; } }
				else if(k == 6){ if(capacity_charges[i][j]){ html = capacity_charges[i][j]; } else { html = ''; } }
				
				if(k == 1){
					td = new Element('td', {
						'id': 'annual-cost-tariff-' + j + '-' + i,
						'class': className,
						'html': html
					}).inject(tr);
					
				} else {
					td = new Element('td', {
						'class': className,
						'html': html
					}).inject(tr);
				}
			}
		}
		
		div = new Element('div', {
			'class': 'clear'
		}).inject($('tariff-calculator'));
	}
}

window.addEvent('domready', function() { if($('tariff-calculator')){ buildCalculator(); } });
