function parseCurrency(fltValue, strDefault, intPrecision, blnAllowblank)
{
  if (fltValue=='')
    return (blnAllowblank) ? '' : strDefault;
  else
  {
    fltValue = fltValue.replace(/ /g, "");      
    fltValue = fltValue.replace(/,/g, "");        
    fltValue = parseFloat(fltValue);
    
    if (isNaN(fltValue)) 
      return(strDefault);
    else  
      return (fltValue.toFixed(intPrecision));
  }
}

function commaCurrency(fltValue)
{
	var n = new String(fltValue);
	var strReturn = "";

	while(n.length > 3)
	{
		strReturn = "," + n.substr(n.length-3) + strReturn;
		n = n.substr(0,n.length-3);
	}
	
	strReturn = n + strReturn;
	return ((strReturn.substr(0,1)==",") ? strReturn.substr(1,strReturn.length-1) : strReturn);
}


function CalculateEmissions()
{
	EmissionTonnes = 0.0;
	EmissionCost = 0.0;
	
	EmissionTonnes += parseInt(document.getElementById("Electricity").value) * 0.43;
	EmissionTonnes += parseInt(document.getElementById("Gas").value) * 0.19;
	EmissionTonnes += parseInt(document.getElementById("Oil").value) * 2.68;	
	
	EmissionTonnes /= 1000; // Convert KG to tonnes
	
	EmissionCost += parseInt(document.getElementById("Electricity").value) * parseFloat(document.getElementById("CostElec").value) / 100;
	EmissionCost += parseInt(document.getElementById("Gas").value) * parseFloat(document.getElementById("CostGas").value) / 100;
	EmissionCost += parseInt(document.getElementById("Oil").value) * parseFloat(document.getElementById("CostOil").value) / 100;	
	
	document.getElementById("emission_result").innerHTML = parseCurrency(EmissionTonnes.toString(), EmissionTonnes, 2, false)+" Tonnes CO<sub>2</sub><br />&pound;" + commaCurrency(parseCurrency(EmissionCost.toString(), EmissionCost, 0, false));
	
	// Savings
	EmissionTonnes *= 0.2;
	EmissionCost *= 0.2;
	
	document.getElementById("saving_result").innerHTML = parseCurrency(EmissionTonnes.toString(), EmissionTonnes, 2, false)+" Tonnes CO<sub>2</sub><br />&pound;" + commaCurrency(parseCurrency(EmissionCost.toString(), EmissionCost, 0, false));
}


function CalculateQuestionResults()
{
  var Result = 0;
  
  for (Index = 1 ; Index <= document.getElementById('question_count').value ; Index++)
  {
	  eval("ObjRadio = document.question_form.result"+Index);
	  
	  HasBeenChecked = false;
	  
	  for (ResultCounter = 0 ; ResultCounter < ObjRadio.length ; ResultCounter++)
	  {
		  if (ObjRadio[ResultCounter].checked)
		  {
			  HasBeenChecked = true;
		      Result += parseInt(ObjRadio[ResultCounter].value);
		  }
	  }
	  
	  if (!HasBeenChecked) return(-1) // A section hasn't been completed
  }
  
  return(Result);
}