var cfr;
var pfr;
var nCostOfEnegry;
var nCostOfWater;
var nCostOfWaste;
var ncos;
var nnos;
var nShowerheadCost;

var OldWaterUsed;
var NewWaterUsed;
var WaterSaved;
var EnergyCost;
var WaterCost;
var WasteCost;
var WaterCostSaving;
var EnergyCostSavings;
var TotalCostSaving;
var Investment;
var InvestmentPayback;
var txtPayday;
var txtPaydate;
var txtCarbonFoot;
var CarbonFoot;




/*Initalization */
nCostOfEnegry = 0.0375;
nCostOfWater = 0.86;
nCostOfWaste = 1.50;





/* FUNCTION WORK STARTS */
function fnProposedFlowRate()
{
	cfr = parseFloat(document.getElementById("CurrentFlowRate").options[document.getElementById("CurrentFlowRate").selectedIndex].value);
	pfr = parseFloat(document.getElementById("ProposedFlowRate").options[document.getElementById("ProposedFlowRate").selectedIndex].value);	
	
	
	if(cfr <= pfr)
	{
		alert("Proposed flow rate must be less than the current rate");
		document.getElementById("CurrentFlowRate").focus();
	}
}


/* function for enegry */
function fnCboEnegry(data)
{
	if(data.selectedIndex == 1)
	{
		document.getElementById("txtCostOfEnergy").style.display = "block";
		document.getElementById("txtCostOfEnergy").value = nCostOfEnegry;
	}
	else
	{
		document.getElementById("txtCostOfEnergy").style.display = "none";
		nCostOfEnegry =  0.0214;
	}
}

function fnTxtEnegry(data)
{
	nCostOfEnegry = data.value;
}



/* function for water */
function fnCboWater(data)
{
	if(data.selectedIndex == 1)
	{
		document.getElementById("txtCostofWater").style.display = "block";
		document.getElementById("txtCostofWater").value = nCostOfWater;
	}
	else
	{
		document.getElementById("txtCostofWater").style.display = "none";
		nCostOfWater = 0.76;
	}
}

function fnTxtWater(data)
{
	nCostOfWater = data.value;
}


/* function for Water */
function fnCboWaste(data)
{
	if(data.selectedIndex == 1)
	{
		document.getElementById("txtCostOfWaste").style.display = "block";
		document.getElementById("txtCostOfWaste").value = nCostOfWaste;
	}
	else
	{
		document.getElementById("txtCostOfWaste").style.display = "none";
		nCostOfWaste = 1.29;
	}
}

function fnTxtWaste(data)
{
	nCostOfWaste = data.value;
}


/* Number of shower heads */
function fnNumberOfShowerheads(data)
{
	k = document.getElementById("ShowerheadChoice")
	switch(k.options[k.selectedIndex].value)
	{
		case "X - Stream":
			ncos = 22.6;
			break;
						
		case "TriSpa":
			ncos = 54.35;
			break;
			
		case "Skincare":
			ncos = 32.17;
			break;
			
		case "5 Star Resort":
			ncos = 47.82;
			break;
			
		case "BodySpa":
			ncos = 51.2;
			break;
			
		case "Elite Skin Care":
			ncos = 39.12;
			break;	
			
		case "IntelliShower":
			ncos = 54.35;
			break;	
			
			
	}
	
	nnos = parseFloat(data.value) * ncos;
	nShowerheadCost = nnos
	document.getElementById("ShowerheadCost").value = (nnos);
	//document.getElementById("txtInvestment").innerHTML = Math.ceil(Math.round(nnos * 100)/100);
}



/* Main click */

function mainclick()
{
	if(parseFloat(document.getElementById("NumberOfShowerheads").value) < 1)
	{
		alert("Please make some selections before asking for a savings calculation");
		return false;
	}
k = document.getElementById("ShowerheadChoice")
	cfr = parseFloat(document.getElementById("CurrentFlowRate").options[document.getElementById("CurrentFlowRate").selectedIndex].value);
	pfr = parseFloat(document.getElementById("ProposedFlowRate").options[document.getElementById("ProposedFlowRate").selectedIndex].value);	

	
	nNumberOfShowerheads = parseFloat(document.getElementById("NumberOfShowerheads").value);
	nShowersPerDay = parseFloat(document.getElementById("ShowersPerDay").options[document.getElementById("ShowersPerDay").selectedIndex].value);
	nLengthOfShower = parseFloat(document.getElementById("LengthOfShower").options[document.getElementById("LengthOfShower").selectedIndex].value);
	nDaysPerWeek = parseFloat(document.getElementById("DaysPerWeek").options[document.getElementById("DaysPerWeek").selectedIndex].value);
	nDiv = (52 / 1000);
	
	
	
	
	OldWaterUsed = parseFloat(nNumberOfShowerheads) * nShowersPerDay * nLengthOfShower * nDaysPerWeek * nDiv * cfr;	
	NewWaterUsed = parseFloat(nNumberOfShowerheads) * nShowersPerDay * nLengthOfShower * nDaysPerWeek * nDiv * pfr;
	WaterSaved = parseFloat(OldWaterUsed) - NewWaterUsed ;
	
	txtWaterSaved = WaterSaved;
	
	WaterCost = nCostOfWater;
	WasteCost = nCostOfWaste;
	
	WaterCostSaving = (parseFloat(WaterSaved) * WaterCost) + parseFloat(parseFloat(WaterSaved) * WasteCost);
	
	
	ntxtWaterCostSaving = WaterCostSaving.toFixed(2);
	
	
	EnergyCost = nCostOfEnegry;
	
	EnergyCostSaving = (parseFloat(WaterSaved) / 0.031) * EnergyCost
	ntxtEnergyCostSaving = EnergyCostSaving.toFixed(2);
	
	
	TotalCostSaving = parseFloat(WaterCostSaving) + EnergyCostSaving
	txtTotalSavings = TotalCostSaving.toFixed(2);
	
	
	Investment = document.getElementById("ShowerheadCost").value;
	Investment = parseFloat(Investment);
	Investment = Investment.toFixed(2);
	InvestmentPayback = (parseFloat(Investment) / parseFloat(TotalCostSaving)) * 364
	txtInvestmentPayback = InvestmentPayback;
	
	CarbonFoot = parseFloat(txtWaterSaved)/0.031*0.19/1000 + parseFloat(txtWaterSaved)*0.00029;
	CarbonFoot = CarbonFoot.toFixed(2);
	
	function addDays(myDate,days) {
return new Date(myDate.getTime() + days*24*60*60*1000);

}
txtPayday = addDays(new Date(),InvestmentPayback);

var weekday=new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday")
var monthname=new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec")
txtPaydate = (weekday[txtPayday.getDay()] + " ") + (txtPayday.getDate() + ". ") + (monthname[txtPayday.getMonth()] + " ") + (txtPayday.getFullYear())

document.getElementById("txtPayday").innerHTML =  txtPaydate;

	
	
	document.getElementById("txtWaterSaving").innerHTML = Math.round(txtWaterSaved * 1000)/1000;
	document.getElementById("txtWaterCostSaving").innerHTML = (ntxtWaterCostSaving);
	document.getElementById("txtEnergyCostSaving").innerHTML =  (ntxtEnergyCostSaving);
	document.getElementById("txtAnnualCostSaving").innerHTML =  (txtTotalSavings );
	document.getElementById("txtInvestment").innerHTML = (Investment);
	document.getElementById("txtPayback").innerHTML = Math.round(txtInvestmentPayback);
	document.getElementById("txtCarbonFoot").innerHTML = (CarbonFoot);
}



