﻿//-- COMMON FUNCTIONS



var debug = false;

var debugTextArea = debug? document.getElementById("txtDebug") : null;



function debugWrite(text)

{

	debugTextArea.value += text + "\n";

}



function uz(o)

{

    if(isNaN(o))

    {

        alert("Please only use numeric values" + o);

        return 0

    }

        

    return o == null ? 0 : Number(o);

}



function GetValueById(id)

{

    return GetAnyValueById(id, true);

}



function GetAnyValueById(id, isNumeric)

{

   try

   {

    if(isNumeric)

        return uz(document.getElementById(id).value);                

    else

        return document.getElementById(id).value;

   }

   catch(err)

   {

        if(debug) alert("there was a problem getting the value of "+ id + ": " + err);

   }

}



function GetStringById(id)

{

    return document.getElementById(id).value;

}



function SetDocText(id, value){

    document.getElementById(id).innerHTML = value;

}



function DecimalValue(value)

{

    return Math.round(value*100)/100  

}



function DecimalAsString(value)

{

    var i = parseFloat(value);

    if(isNaN(i)) { i = 0.00; }

    var minus = '';

    if(i < 0) { minus = '-'; }

    i = Math.abs(i);

    i = parseInt((i + .005) * 100);

    i = i / 100;

    s = new String(i);

    if(s.indexOf('.') < 0) { s += '.00'; }

    if(s.indexOf('.') == (s.length - 2)) { s += '0'; }

    s = minus + s;

    return s;

}



//-- END COMMON FUNCTIONS



//-- CARBON CALCULATOR



    function CalculateHouseholdOffset()

    {

        UserHouseholdFuel = new HouseholdFuel();

        UserHouseholdFuel.GetValues();

        UserHouseholdFuel.GetACBP();

        

        UserTravelByCar1 = new TravelByCar();

        UserTravelByCar1.GetValues("1st");

        UserTravelByCar1.GetACBP();



        UserTravelByCar2 = new TravelByCar();

        UserTravelByCar2.GetValues("2nd");

        UserTravelByCar2.GetACBP();



        ShortHaulFlights = new TravelByAir('ShortHaul');

        ShortHaulFlights.GetValues();

        ShortHaulFlights.GetACBP();

        

        MediumHaulFlights = new TravelByAir('MediumHaul');

        MediumHaulFlights.GetValues();

        MediumHaulFlights.GetACBP();

        

        LongHaulFlights = new TravelByAir('LongHaul');

        LongHaulFlights.GetValues();

        LongHaulFlights.GetACBP();

        

        Total = DecimalValue(

            (UserHouseholdFuel.ACBPElectricity +

             UserHouseholdFuel.ACBPNaturalGas +

             UserHouseholdFuel.ACBPLPG + 

             UserHouseholdFuel.ACBPHeatingOil +

             UserTravelByCar1.ACBPForCar +

             UserTravelByCar2.ACBPForCar +

             ShortHaulFlights.ACBFForFlights +

             MediumHaulFlights.ACBFForFlights +

             LongHaulFlights.ACBFForFlights) / 1000);

             

        SetDocText("litTotalCarbonFootprint", Total);

        



        if(debug == true)

        {

            debugWrite("UserHouseholdFuel.ACBPElectricity = " + UserHouseholdFuel.ACBPElectricity);

            debugWrite("UserHouseholdFuel.ACBPNaturalGas = " + UserHouseholdFuel.ACBPNaturalGas);

            debugWrite("UserHouseholdFuel.ACBPLPG = " + UserHouseholdFuel.ACBPLPG);

            debugWrite("UserHouseholdFuel.ACBPHeatingOil = " + UserHouseholdFuel.ACBPHeatingOil);

            

            debugWrite("UserTravelByCar1.GetACBP = " + UserTravelByCar1.ACBPForCar);

            debugWrite("UserTravelByCar2.GetACBP = " + UserTravelByCar2.ACBPForCar);

            

            debugWrite("ShortHaulFlights.GetACBP = " + ShortHaulFlights.ACBFForFlights);

            debugWrite("MediumHaulFlights.GetACBP = " + MediumHaulFlights.ACBFForFlights);

            debugWrite("LongHaulFlights.GetACBP = " + LongHaulFlights.ACBFForFlights);

        }    

    }

    

    function ValidateBusinessForm()

    {

        var _organisation,

        _address,

        _postcode,

        _telephone,

        _email,

        _txtReportingPeriodStart,

        _txtReportingPeriodEnd;

        

        _organisation = GetAnyValueById("organisation",false);

        _address = GetAnyValueById("address",false);

        _postcode = GetAnyValueById("postcode",false);

        _telephone = GetAnyValueById("telephone",false);

        _email = GetAnyValueById("email",false);

        _confirmEmail = GetAnyValueById("emailConfirm",false);        

        _txtReportingPeriodStart = GetAnyValueById("txtReportingPeriodStart",false);

        _txtReportingPeriodEnd = GetAnyValueById("txtReportingPeriodEnd",false);

        

        

        if(debug == false){

            if(_organisation.length <= 0)

            {

                alert("Please enter your company name.");

                return false;

            }



            if(_address.length <= 10)

            {

                alert("Please enter your full company address.");

                return false;

            }



            if(_postcode.length < 6)

            {

                alert("Please enter your company postcode.");

                return false;

            }

            

            if(_telephone.length <= 8)

            {

                alert("Please enter a valid company company phone number.");

                return false;

            }

            

            if(_email.length <= 5 || echeck(_email) == false)

            {

                alert("Please enter a valid company contact email address.");

                return false;

            }

            else

            {

            	if(_email != _confirmEmail)

            	{

            		alert("Please enter make sure you have confirmed your email address correctly");

            		return false;

            	}

            }

            

            

            if(isDate(_txtReportingPeriodStart) != 0)

            {

                alert("Please enter a valid reporting start date.");

                return false;

            }  

            

            if(isDate(_txtReportingPeriodStart) != 0)

            {

                alert("Please enter a valid reporting end date.");

                return false;

            }                                    

        }

        

        return true;

        

    }

    

    function CalculateBusinessOffset()

    {

        if(ValidateBusinessForm() == true){

            

            UserBusinessFuel = new BusinessFuel();

            UserBusinessFuel.GetValues();

            UserBusinessFuel.GetACBP();

            

            var Vehicle = new TravelByCar();

            

            if(isNaN(GetAnyValueById('txtPetrol')))

            {

	            UserPetrolVehiclesC02  = 0;

	            alert("Please only enter a numeric value for petrol vehicle fuel spend, we have taken you current spend as zero");

            }

            else

            {

	            UserPetrolVehiclesC02 = Vehicle.CalculateC02("Petrol", GetAnyValueById('txtPetrol'));

            }

            

            if(isNaN(GetAnyValueById('txtDiesel')))

            {

	            UserDieselVehiclesC02 = 0;

	            alert("Please only enter a numeric value for diesel vehicle fuel spend, we have taken you current spend as zero");

            }

            else

            {

            	UserDieselVehiclesC02 = Vehicle.CalculateC02("Diesel", GetAnyValueById('txtDiesel'));

            }

            

            if(isNaN(GetAnyValueById('txtLPG')))

            {

	            UserLPGVehiclesC02 = 0;

	            alert("Please only enter a numeric value for LPG vehicle fuel spend, we have taken you current spend as zero");

            }

            else

            {	

	            UserLPGVehiclesC02 = Vehicle.CalculateC02("LPG", GetAnyValueById('txtLPG'));

            }

            

            ShortHaulFlights = new TravelByAir('ShortHaul');

            ShortHaulFlights.GetValues();

            ShortHaulFlights.GetACBP();

            

            MediumHaulFlights = new TravelByAir('MediumHaul');

            MediumHaulFlights.GetValues();

            MediumHaulFlights.GetACBP();

            

            LongHaulFlights = new TravelByAir('LongHaul');

            LongHaulFlights.GetValues();

            LongHaulFlights.GetACBP();

            

            /*

            var vehicles1 = GetValueById('txt1stCarVehicles');

            var vehicles2 = GetValueById('txt2ndCarVehicles');

            var vehicles3 = GetValueById('txt3rdCarVehicles');

            */

            

            Total = DecimalValue(

                (UserPetrolVehiclesC02 +

                 UserDieselVehiclesC02 +

                 UserLPGVehiclesC02 +

                 ShortHaulFlights.ACBFForFlights +

                 MediumHaulFlights.ACBFForFlights +

                 LongHaulFlights.ACBFForFlights +

                 UserBusinessFuel.ACBPByOfficeSize + 

                 UserBusinessFuel.ACBPElectricity +

                 UserBusinessFuel.ACBPNaturalGas  

                  )  / 1000) ;

	        

	        

            if(Total < 10)

            {

                Total = 10;

                alert("The minimum purchase for business carbon offsets is 10 Tonnes. Please note that as your carbon offset has been calculated at less than 10 Tonnes you will be invoiced for the minimum amount.");

            }

            

            SetDocText("litTotalCarbonFootprint", Total);

            

            document.getElementById("hdCO2Result").value = Total;

            document.getElementById("hdPriceResult").value = DecimalValue(Total* Number(7.23));

			//- new VAT rate
            document.getElementById("hdPriceResultIncVAT").value = DecimalValue(Total * Number(8.32));
			
			//- old VAT rate 
			// document.getElementById("hdPriceResultIncVAT").value = DecimalValue(Total * Number(8.50));

            

            if(debug == true)

            {

                

                debugWrite("ReportingPeriod = " + UserBusinessFuel.ReportingPeriod);

                debugWrite("OfficeType = " + UserBusinessFuel.OfficeType);

                debugWrite("CalculateBy = " + UserBusinessFuel.CalculateBy);

                debugWrite("ACBPByOfficeSize = " + UserBusinessFuel.ACBPByOfficeSize);

                

                debugWrite("OfficeSizeMetres = " + UserBusinessFuel.OfficeSizeMetres);

                debugWrite("OfficeSizeFeet = " + UserBusinessFuel.OfficeSizeFeet);

                

                debugWrite("OfficeSizeFeet = " + UserBusinessFuel.OfficeSizeFeet);

                

                debugWrite("ShortHaulFlights.GetACBP = " + ShortHaulFlights.ACBFForFlights);

                debugWrite("MediumHaulFlights.GetACBP = " + MediumHaulFlights.ACBFForFlights);

                debugWrite("LongHaulFlights.GetACBP = " + LongHaulFlights.ACBFForFlights);



            }    

        }

    }    



    // -- Business Fuel

    function BusinessFuel()

    {

        this.CalculateBy;

        this.ElectricityInKWh, this.ElectricityInPounds;

        this.NaturalGasInKWh, this.NaturalGasInPounds;

        this.OfficeType, this.OfficeSizeMetres, this.OfficeSizeFeet;

        this.ReportingPeriod; 

        

        this.ACBPElectricity = 0;

        this.ACBPNaturalGas = 0;

        

        this.ACBPByOfficeSize = 0;

        

        this.OfficeType = 1; 

        this.OfficeSizeMetres = 0; 

        this.OfficeSizeFeet = 0;

        

        this.ReportingPeriod = GetReportingPeriod(); 

        

    }



    BusinessFuel.prototype.GetValues = function()

    {

        this.CalculateBy = (document.getElementById("radOfficeEnergy1").checked == true) ? "ByFuelUsage" : "ByOfficeSize";

        

        if(this.CalculateBy == "ByFuelUsage")

        {

	        this.ElectricityInKWh = GetValueById("txtElectricityKWhPerYear");

	        this.ElectricityInPounds = GetValueById("txtElectricityPoundsPerYear");

	        this.NaturalGasInKWh = GetValueById("txtNaturalGasKWhPerYear");

	        this.NaturalGasInPounds = GetValueById("txtNaturalGasPoundsPerYear");

        		

        }

        else

        {

	        this.OfficeType = GetValueById("ddlOfficeType");

	        this.OfficeSizeMetres = GetValueById("txtOfficeSizeMetres");

	        this.OfficeSizeFeet = GetValueById("txtOfficeSizeFeet");

        }

        

        this.ReportingPeriod = GetReportingPeriod();

        

		OptionalFormElement(this.ElectricityInKWh , this.ElectricityInPounds , 1, "Electiricity value will be taken from the 'Electricity in KWh' field, as you have proviced values for both KWh and pounds");

        OptionalFormElement(this.NaturalGasInKWh , this.NaturalGasInPounds , 1, "Natural Gas value will be taken from the 'Natural gas in KWh' field, as you have proviced values for both KWh and pounds");



    }

    

    BusinessFuel.prototype.GetACBP = function()

    {

       //ByFuelUsage / ByOfficeType

       if(this.CalculateBy == "ByFuelUsage")

       {

            if(this.ElectricityInKWh > 0)

                this.ACBPElectricity =  DecimalValue(this.ElectricityInKWh * 0.43);

            else

                this.ACBPElectricity = DecimalValue((this.ElectricityInPounds / 0.077) * 0.43);

                

            if(this.NaturalGasInKWh > 0)

                this.ACBPNaturalGas = DecimalValue(this.NaturalGasInKWh * 0.19);

            else

                this.ACBPNaturalGas = DecimalValue((this.NaturalGasInPounds / 0.029) * 0.19);    

        }

        else

        {

            this.ACBPNaturalGas = 0;

            this.ACBPElectricity = 0;

            

            

            if(this.OfficeSizeMetres > 0)

            {

                this.ACBPByOfficeSize = DecimalValue(this.GetOfficeSizeMultiplier() * this.OfficeSizeMetres * (this.ReportingPeriod / 365));

                

                if(debug) debugWrite("ACBPByOfficeSize (m)" + this.ACBPByOfficeSize);

            }    

            else

            {

                

                if(this.OfficeSizeFeet == 0)

                    alert('Please enter a value for the size of you office');

                else

                {    

                    this.ACBPByOfficeSize = DecimalValue(this.GetOfficeSizeMultiplier() * (this.OfficeSizeFeet / 10.76) * (this.ReportingPeriod / 365));

                    

                    if(debug) debugWrite("ACBPByOfficeSize (f)" + this.ACBPByOfficeSize);

                }

            }

        }

        

    }

    

    BusinessFuel.prototype.GetOfficeSizeMultiplier = function()

    {

        switch(this.OfficeType)

        {

            case 1:

              return 43;

              break    

            case 2:

              return 55;

              break

            case 3:

              return 109;

              break            

            case 4:

              return 171;

              break                   

            default:

              return 43;

        } 

    }

    //end Business Fuel



    // -- Household Fuel 

    function HouseholdFuel()

    {

        this.ElectricityInKWh, this.ElectricityInPounds;

        this.NaturalGasInKWh, this.NaturalGasInPounds;

        this.LPGInLitres, this.LPGInPounds;

        this.HeatingOilInLitres, this.HeatingOilInPounds;

        

        this.ACBPLPG = 0;

        this.ACBPElectricity = 0;

        this.ACBPNaturalGas = 0;

        this.ACBPHeatingOil = 0;

        

    }

    

    HouseholdFuel.prototype.GetValues = function()

    {

        this.ElectricityInKWh = GetValueById("txtElectricityKWhPerYear");

        this.ElectricityInPounds = GetValueById("txtElectricityPoundsPerYear");

        this.NaturalGasInKWh = GetValueById("txtNaturalGasKWhPerYear");//txtNaturalGasKWhPerYear : txtNaturalGasKWhPerYear

        this.NaturalGasInPounds = GetValueById("txtNaturalGasPoundsPerYear");

        this.LPGInLitres = GetValueById("txtLPGKWhPerYear");

        this.LPGInPounds = GetValueById("txtLPGLitresPerYear");

        this.HeatingOilInLitres = GetValueById("txtHeatingOilLitresPerYear");

        this.HeatingOilInPounds = GetValueById("txtHeatingOilPoundsPerYear");

        

        OptionalFormElement(this.ElectricityInKWh , this.ElectricityInPounds , 1, "Electiricity value will be taken from the 'Electricity in KWh' field, as you have proviced values for both KWh and pounds");

        OptionalFormElement(this.NaturalGasInKWh , this.NaturalGasInPounds , 1, "Natural Gas value will be taken from the 'Natural gas in KWh' field, as you have proviced values for both KWh and pounds");

        OptionalFormElement(this.LPGInLitres , this.LPGInPounds , 1, "LPG value will be taken from the 'LPG in Litres' field, as you have proviced values for both pounds and litres");

        OptionalFormElement(this.HeatingOilInLitres , this.HeatingOilInPounds , 1, "Heating Oil value will be taken from the 'Heating Oil in Litres' field as you have provided values for both pounds and litres");

        



    }

    

    HouseholdFuel.prototype.GetACBP = function()

    {

        

        if(this.ElectricityInKWh > 0)

            this.ACBPElectricity =  DecimalValue(this.ElectricityInKWh * 0.43);

        else

            this.ACBPElectricity = DecimalValue((this.ElectricityInPounds / 0.098) * 0.43);



        if(this.NaturalGasInKWh > 0)

            this.ACBPNaturalGas = DecimalValue(this.NaturalGasInKWh * 0.19);

        else

            this.ACBPNaturalGas = DecimalValue((this.NaturalGasInPounds / 0.036) * 0.19);



        if(this.LPGInLitres > 0)

            this.ACBPLPG = DecimalValue((this.LPGInLitres * 7.4) *.21); 

        else

            this.ACBPLPG = DecimalValue((this.LPGInPounds / 0.045) * 0.21);



        if(this.HeatingOilInLitres > 0)

            this.ACBPHeatingOil = DecimalValue(this.HeatingOilInLitres * 0.25);

        else

            this.ACBPHeatingOil = DecimalValue((this.HeatingOilInPounds / 0.033) * 0.25);

    }

    

    //-- end Household Fuel

    

    //-- Travel by Car

    function TravelByCar()

    {

        this.RefuelDurationInDays, this.AveragePricePerRefuel;

        this.FuelType;

        this.ACBPForCar;

    }

    

    TravelByCar.prototype.GetValues = function(item)

    {

    	

        this.RefuelsPerMonth = GetValueById("txt"+ item +"CarRefuels");

        this.AveragePricePerRefuel = GetValueById("txt"+ item +"CarAvgCostPerRefuel");

        this.FuelType = GetStringById("ddl"+ item +"CarFuelType");

        

    }

    

    TravelByCar.prototype.GetACBP = function()

    {

        if(this.FuelType == 'Petrol')

            this.ACBPForCar = DecimalValue((((this.RefuelsPerMonth * 12 * this.AveragePricePerRefuel)/0.96) * 9.6) * 0.24);

        

        if(this.FuelType == 'Diesel')

            this.ACBPForCar = DecimalValue((((this.RefuelsPerMonth * 12 * this.AveragePricePerRefuel)/0.96) * 10.3) * 0.25);

        

        if(this.FuelType == 'LPG')

            this.ACBPForCar = DecimalValue((((this.RefuelsPerMonth * 12 * this.AveragePricePerRefuel)/0.45) * 7.4) * 0.21);

    }

    

    TravelByCar.prototype.CalculateC02 = function(fuelType, price)

    {

        switch(fuelType)

        {

            case "Petrol":

                return DecimalValue(((price /0.96) * 9.6) * 0.24)

            break;

            case "Diesel":

                return DecimalValue(((price /0.96) * 10.3) * 0.25)

            break;

            case "LPG":

                return DecimalValue(((price /0.45) * 7.4) * 0.21);

            break;

            default: 

                alert('Fuel Type not recognised');                       

        }

    }

    

    //-- end Travel by Car

    

    //-- Travel by Air

    function TravelByAir(haulType)

    {

        this.HaulType = haulType;

        this.NumberOfFlights;

        

        this.ACBFForFlights;

    }

    

    TravelByAir.prototype.GetValues = function()

    {

        this.NumberOfFlights = GetValueById("txtFlights"+  this.HaulType);

    }

    

    TravelByAir.prototype.GetACBP = function()

    {

        if(this.HaulType == "ShortHaul")

            this.ACBFForFlights = DecimalValue(this.ACBFForFlights = this.NumberOfFlights * 250); 

        

        if(this.HaulType == "MediumHaul")

            this.ACBFForFlights = DecimalValue(this.ACBFForFlights = this.NumberOfFlights * 1500); 

        

        if(this.HaulType == "LongHaul")

            this.ACBFForFlights = DecimalValue(this.ACBFForFlights = this.NumberOfFlights * 2800); 

    }

    //-- end Travel By Air

    



//-- END CARBON CALCULATOR



function SendInvoice()

{

    CalculateBusinessOffset();

//    if(DecimalValue(document.getElementById("hdCO2Result").value) < 10)

//    {

//        alert("The minimum purchase for business carbon offsets is 10 Tonnes. Please note that as your carbon offset be calculated at less than 10 Tonnes you will be invoiced for the minimum amount.");

//    }

    

    var DoInvoice = confirm("Are you sure you want to be invoiced by Down with Carbon Limited?")

    if(DoInvoice)

    {

        document.bizcalc.target = "";

        document.bizcalc.action = 'http://downwithcarbon.org.uk/cgi-bin/cgiemail/BizInvoice.txt';

        document.bizcalc.submit();

    }

}



function PrintReport()

{

        CalculateBusinessOffset();

//        if(DecimalValue(document.getElementById("hdCO2Result").value) < 10)

//        {

//            alert("The minimum purchase for business carbon offsets is 10 Tonnes. Please note that as your carbon offset be calculated at less than 10 Tonnes you will be invoiced for the minimum amount.");

//        }

        document.getElementById("hdDate").value = formatDate(new Date(), "dd/MM/yyyy")

        document.bizcalc.action = 'http://downwithcarbon.org.uk/cgi-bin/businessReport.pl';

        document.bizcalc.submit();

}



//-- OFFSETS



function CalculateOffsets(){

    

    OffsetCalulation = new Offsets;

    OffsetCalulation.GetValues();

    OffsetCalulation.Calculate();

    

    SetDocText("litPriceResult", DecimalAsString(OffsetCalulation.PriceResult));

    SetDocText("litTonnesResult", DecimalAsString(OffsetCalulation.TonnesResult));

    

    if(DecimalAsString(OffsetCalulation.PriceResult) > 0)

    {

        document.getElementById("amount").value = DecimalAsString(OffsetCalulation.PriceResult);

    }

    

    if(DecimalAsString(OffsetCalulation.PriceInput) > 0)

    {

        document.getElementById("amount").value = DecimalAsString(OffsetCalulation.PriceInput);

    }

    

    document.getElementById("cartId").value = formatDate(new Date(), "yMMdHHmms");

}





function Offsets()

{

    this.IsValid;

    

    this.TonnesInput; 

    this.PriceResult;

    

    this.PriceInput;

    this.TonnesResult; 

}



Offsets.prototype.GetValues = function()

{

    this.TonnesInput = GetValueById("txtTonnesInput");

    this.PriceInput = GetValueById("txtPriceInput");

}



Offsets.prototype.Calculate = function()

{

    if(this.TonnesInput != 0){

        this.PriceResult = DecimalValue(this.TonnesInput * Number(8.50));

        document.getElementById('txtTonnes').value = this.TonnesInput;

    }



    if(this.PriceInput != 0){

        this.TonnesResult = DecimalValue(this.PriceInput / Number(8.50));

        document.getElementById('txtTonnes').value = this.TonnesResult;

    }

    

}





function GetReportingPeriod()

{

    var startDate = document.getElementById('txtReportingPeriodStart').value;

    var endDate = document.getElementById('txtReportingPeriodEnd').value;

    

    var start = getDateFromFormat(startDate, "d/M/yyyy");

    var end = getDateFromFormat(endDate, "d/M/yyyy");

    

    return differenceInDays(start, end);

}





function ValidateForm()

{

    CalculateOffsets();

    

    var NameInput = document.getElementById('txtName');

    var EmailAddress = document.getElementById('txtEmail');

    

    if ((NameInput.value==null)||(NameInput.value=="")){

		alert("Please Enter your name");

		NameInput.focus();

		return false;

	}

    

	if ((EmailAddress.value==null)||(EmailAddress.value=="")){

		alert("Please Enter a valid Email Address");

		EmailAddress.focus();

		return false;

	}

	if (echeck(EmailAddress.value)==false){

		EmailAddress.value="";

		EmailAddress.focus();

		return false;

	}

	

	

	document.BuyOffsetForm.action = "https://select.worldpay.com/wcc/purchase";

	//document.BuyOffsetForm.action = "http://www.downwithcarbon.org.uk/cgi-bin/HandleOffsetPurchase.pl";

	document.BuyOffsetForm.submit();

}





/////



function echeck(str) {



		var at="@"

		var dot="."

		var lat=str.indexOf(at)

		var lstr=str.length

		var ldot=str.indexOf(dot)

		if (str.indexOf(at)==-1){

		   alert("Invalid E-mail Address")

		   return false

		}



		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){

		   alert("Invalid E-mail Address")

		   return false

		}



		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){

		    alert("Invalid E-mail Address")

		    return false

		}



		 if (str.indexOf(at,(lat+1))!=-1){

		    alert("Invalid E-mail Address")

		    return false

		 }



		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){

		    alert("Invalid E-mail Address")

		    return false

		 }



		 if (str.indexOf(dot,(lat+2))==-1){

		    alert("Invalid E-mail Address")

		    return false

		 }

		

		 if (str.indexOf(" ")!=-1){

		    alert("Invalid E-mail ID")

		    return false

		 }



 		 return true					

	}



if(debug)

    alert("Calculator Javascript loaded succesfully");



//-- END OFFSETS



//



    function CalculateCarbonFootprint()

    {

        UserHouseholdFuel = new HouseholdFuel();

        UserHouseholdFuel.GetValues();

        UserHouseholdFuel.GetACBP();

        

        UserTravelByCar1 = new TravelByCar();

        UserTravelByCar1.GetValues("1st");

        UserTravelByCar1.GetACBP();



        UserTravelByCar2 = new TravelByCar();

        UserTravelByCar2.GetValues("2nd");

        UserTravelByCar2.GetACBP();



        ShortHaulFlights = new TravelByAir('ShortHaul');

        ShortHaulFlights.GetValues();

        ShortHaulFlights.GetACBP();

        

        MediumHaulFlights = new TravelByAir('MediumHaul');

        MediumHaulFlights.GetValues();

        MediumHaulFlights.GetACBP();

        

        LongHaulFlights = new TravelByAir('LongHaul');

        LongHaulFlights.GetValues();

        LongHaulFlights.GetACBP();

                

        Total = DecimalValue(

            (UserHouseholdFuel.ACBPElectricity +

             UserHouseholdFuel.ACBPNaturalGas +

             UserHouseholdFuel.ACBPLPG + 

             UserHouseholdFuel.ACBPHeatingOil +

             UserTravelByCar1.ACBPForCar +

             UserTravelByCar2.ACBPForCar +

             ShortHaulFlights.ACBFForFlights +

             MediumHaulFlights.ACBFForFlights +

             LongHaulFlights.ACBFForFlights) / 1000);

             

        SetDocText("litTotalCarbonFootprint", Total);

        

        document.getElementById('hdOffset').value = Total;

        



        if(debug == true)

        {

            debugWrite("UserHouseholdFuel.ACBPElectricity = " + UserHouseholdFuel.ACBPElectricity);

            debugWrite("UserHouseholdFuel.ACBPNaturalGas = " + UserHouseholdFuel.ACBPNaturalGas);

            debugWrite("UserHouseholdFuel.ACBPLPG = " + UserHouseholdFuel.ACBPLPG);

            debugWrite("UserHouseholdFuel.ACBPHeatingOil = " + UserHouseholdFuel.ACBPHeatingOil);

            

            debugWrite("UserTravelByCar1.GetACBP = " + UserTravelByCar1.ACBPForCar);

            debugWrite("UserTravelByCar2.GetACBP = " + UserTravelByCar2.ACBPForCar);

            

            debugWrite("ShortHaulFlights.GetACBP = " + ShortHaulFlights.ACBFForFlights);

            debugWrite("MediumHaulFlights.GetACBP = " + MediumHaulFlights.ACBFForFlights);

            debugWrite("LongHaulFlights.GetACBP = " + LongHaulFlights.ACBFForFlights);

        }

        

        

    }

    

    function ResetFootprintvalue()

    {

	    SetDocText("litTotalCarbonFootprint", "0");

    }

    

    function OptionalFormElement(item1, item2, preferred, msg)

	{

		if(item1 > 0 && item2 > 0)

		{

			alert(msg);

			return preferred == 1? item1: item2;

		}

		else

		{

			return item1 == null? item2 : item1;

		}

	}

	

	String.prototype.trim = function() 

	{

		return this.replace(/^\s+|\s+$/g,"");

	}

	

	function $get(id)

	{

		return document.getElementById(id);

	}

	

	function ResetInput(elem)

	{

		if(!elem.hasReset)

		{

			elem.value = "";

			elem.hasReset = true;

		}

	}





