﻿﻿var qtype = { "RPC":1, "WEIGHT":2,"REVENUE":3};
var parameters;
/*resultEntry Object*/
function ResultEntryStructure(co2Value, po4Value,r11Value,c2H4Value,so2Value){
 this.co2Value=co2Value;
 this.po4Value=po4Value;
 this.r11Value=r11Value;
 this.c2H4Value=c2H4Value;
 this.so2Value=so2Value;
}
/*Result Datastructure for relative and absolute results*/
function EmissionCompareResult(){
    this.cardBoardResult=new ResultEntryStructure(0,0,0,0,0);
    this.rpcResult=new ResultEntryStructure(0,0,0,0,0);
}
/*Result Datastructure for actual Cardboard,RPC and Total Quantities */
function EmissionsTotalResult(){
    this.compareResult=new EmissionCompareResult();
    this.getTotalResult=function(){
            return new ResultEntryStructure(
                    this.compareResult.cardBoardResult.co2Value + this.compareResult.rpcResult.co2Value,
                    this.compareResult.cardBoardResult.po4Value + this.compareResult.rpcResult.po4Value,
                    this.compareResult.cardBoardResult.r11Value + this.compareResult.rpcResult.r11Value,
                    this.compareResult.cardBoardResult.c2H4Value + this.compareResult.rpcResult.c2H4Value,
                    this.compareResult.cardBoardResult.so2Value + this.compareResult.rpcResult.so2Value);
                }
}
/*Result DataStructure object for Absolute and Relative values */
function AbsoluteRelativeResult(){
this.absoluteResult=new ResultEntryStructure(0,0,0,0,0);
this.relativeResult=new ResultEntryStructure(0,0,0,0,0);
}

/*EmissionCalculationResult Object*/
function EmissionCalculationResult(){
  
  //pool composition values
  this.transportationCardBoardCount=0;
  this.transportationRPCCount=0;
  this.getTransportationTotalCount=function(){
    return this.transportationCardBoardCount+this.transportationRPCCount;
  }
  //Ergebnisse Umweltwirkungen: Annahme Pool besteht aus 100% Einwegkartonkisten
  this.allCardboardValues=new ResultEntryStructure(0,0,0,0,0);
  
  //Ergebnisse Umweltwirkungen: Annahme 100% Mehrwegtransportbehälter
  this.allRPCValues=new ResultEntryStructure(0,0,0,0,0);
 
 //Ergebnisse Umweltwirkungen für definierte Poolzusammensetzung
  this.actualEmissionResult=new EmissionsTotalResult();
  
  //Einsparpotential bei Wechsel auf 100% Mehrweg im Vergleich zur definierten Poolzusammensetzung
  this.saveWhenAllRPCResult= new AbsoluteRelativeResult();
  
  //Bereits eingespart für die definierte Poolzusammensetzung im Vergleich zu 100% Einweg/
  this.alreadySavedEmissions=new AbsoluteRelativeResult();
  
}
/*calculator object */
function EmissionCalculator(){ // Constructor function for the calculator

    this.absolute=function(value)
    {
        if(isNaN(value)) {
            return value;
        }else{
            if(value < 0){
                return (value * -1);
            }else{
                return value;
            }
        }
    }

  this.calculateResult=function(qtype,quantity,rpcPerc,rpcLife,rpcFillings, kgConversionValue, EuroConversionValue){
     var result=new EmissionCalculationResult();
     var transportCount = 0;
     var rotationsPerRPCLifetime = rpcLife*rpcFillings
     
     if(parameters==null || !parameters){
       parameters= new CalculationParameterFactory().getCalculationDataContainer();
     }  
    //Setting Parameters for FillingWeightPerCrate and MerchandiseValuePerCrate
    //was an extention - will probably be able to be changed by User
    if (kgConversionValue!=0)
      parameters.FillingWeightPerCrate=kgConversionValue;
    if (EuroConversionValue!=0)
      parameters.MerchandiseValuePerCrate=EuroConversionValue; 
    
    
    if(qtype == 1){
      //qtype==1: TransportCount same as quantity
      transportCount = quantity;
    }
    else { 
      if(qtype==2){
      //qtype==2: Tonnage
        transportCount=quantity/parameters.FillingWeightPerCrate; 
      }else{
        //qtype==3: Umsatz
        transportCount=quantity/parameters.MerchandiseValuePerCrate;
      }
    }
    
    result.transportationCardBoardCount = transportCount * ((100-rpcPerc)/100);
    result.transportationRPCCount = transportCount * (rpcPerc/100);
    
    result.allCardboardValues.co2Value = parameters.CarbonCo2Value * transportCount;
    result.allCardboardValues.po4Value = parameters.CarbonPo4Value * transportCount;
    result.allCardboardValues.r11Value = parameters.CarbonR11Value * transportCount;
    result.allCardboardValues.c2H4Value = parameters.CarbonC2h4Value * transportCount;
    result.allCardboardValues.so2Value = parameters.CarbonSo2Value * transportCount;
    
    result.allRPCValues.co2Value= ((rotationsPerRPCLifetime*parameters.GWP.Creation.sParameter + parameters.GWP.Creation.tParameter)*transportCount) + 
    ((rotationsPerRPCLifetime*parameters.GWP.Usage.sParameter + parameters.GWP.Usage.tParameter)*transportCount) +
    ((rotationsPerRPCLifetime*parameters.GWP.Eol.sParameter + parameters.GWP.Eol.tParameter)*transportCount);
    
    result.allRPCValues.po4Value= ((rotationsPerRPCLifetime*parameters.EP.Creation.sParameter + parameters.EP.Creation.tParameter)*transportCount) + 
    ((rotationsPerRPCLifetime*parameters.EP.Usage.sParameter + parameters.EP.Usage.tParameter)*transportCount) +
    ((rotationsPerRPCLifetime*parameters.EP.Eol.sParameter + parameters.EP.Eol.tParameter)*transportCount);    
    
    result.allRPCValues.r11Value= ((rotationsPerRPCLifetime*parameters.ODP.Creation.sParameter + parameters.ODP.Creation.tParameter)*transportCount) + 
    ((rotationsPerRPCLifetime*parameters.ODP.Usage.sParameter + parameters.ODP.Usage.tParameter)*transportCount) +
    ((rotationsPerRPCLifetime*parameters.ODP.Eol.sParameter + parameters.ODP.Eol.tParameter)*transportCount);   

    result.allRPCValues.c2H4Value= ((rotationsPerRPCLifetime*parameters.POCP.Creation.sParameter + parameters.POCP.Creation.tParameter)*transportCount) + 
    ((rotationsPerRPCLifetime*parameters.POCP.Usage.sParameter + parameters.POCP.Usage.tParameter)*transportCount) +
    ((rotationsPerRPCLifetime*parameters.POCP.Eol.sParameter + parameters.POCP.Eol.tParameter)*transportCount);      

    result.allRPCValues.so2Value= ((rotationsPerRPCLifetime*parameters.AP.Creation.sParameter + parameters.AP.Creation.tParameter)*transportCount) + 
    ((rotationsPerRPCLifetime*parameters.AP.Usage.sParameter + parameters.AP.Usage.tParameter)*transportCount) +
    ((rotationsPerRPCLifetime*parameters.AP.Eol.sParameter + parameters.AP.Eol.tParameter)*transportCount);    
    
    result.actualEmissionResult.compareResult.rpcResult.co2Value=result.allRPCValues.co2Value*(rpcPerc/100);
    result.actualEmissionResult.compareResult.rpcResult.po4Value=result.allRPCValues.po4Value*(rpcPerc/100);
    result.actualEmissionResult.compareResult.rpcResult.r11Value=result.allRPCValues.r11Value*(rpcPerc/100);
    result.actualEmissionResult.compareResult.rpcResult.c2H4Value=result.allRPCValues.c2H4Value*(rpcPerc/100);
    result.actualEmissionResult.compareResult.rpcResult.so2Value=result.allRPCValues.so2Value*(rpcPerc/100);
    
    result.actualEmissionResult.compareResult.cardBoardResult.co2Value=result.allCardboardValues.co2Value*((100-rpcPerc)/100);
    result.actualEmissionResult.compareResult.cardBoardResult.po4Value=result.allCardboardValues.po4Value*((100-rpcPerc)/100);
    result.actualEmissionResult.compareResult.cardBoardResult.r11Value=result.allCardboardValues.r11Value*((100-rpcPerc)/100);    
    result.actualEmissionResult.compareResult.cardBoardResult.c2H4Value=result.allCardboardValues.c2H4Value*((100-rpcPerc)/100);
    result.actualEmissionResult.compareResult.cardBoardResult.so2Value=result.allCardboardValues.so2Value*((100-rpcPerc)/100);
    
    var actPoolSumResult=result.actualEmissionResult.getTotalResult();
    result.saveWhenAllRPCResult.relativeResult.co2Value=(actPoolSumResult.co2Value-result.allRPCValues.co2Value)/actPoolSumResult.co2Value;
    result.saveWhenAllRPCResult.relativeResult.po4Value=(actPoolSumResult.po4Value-result.allRPCValues.po4Value)/actPoolSumResult.po4Value;
    result.saveWhenAllRPCResult.relativeResult.r11Value=(actPoolSumResult.r11Value-result.allRPCValues.r11Value)/actPoolSumResult.r11Value;       
    result.saveWhenAllRPCResult.relativeResult.c2H4Value=(actPoolSumResult.c2H4Value-result.allRPCValues.c2H4Value)/actPoolSumResult.c2H4Value;
    result.saveWhenAllRPCResult.relativeResult.so2Value=(actPoolSumResult.so2Value-result.allRPCValues.so2Value)/actPoolSumResult.so2Value;      

    result.saveWhenAllRPCResult.absoluteResult.co2Value=actPoolSumResult.co2Value-result.allRPCValues.co2Value;
    result.saveWhenAllRPCResult.absoluteResult.po4Value=actPoolSumResult.po4Value-result.allRPCValues.po4Value;
    result.saveWhenAllRPCResult.absoluteResult.r11Value=actPoolSumResult.r11Value-result.allRPCValues.r11Value;
    result.saveWhenAllRPCResult.absoluteResult.c2H4Value=actPoolSumResult.c2H4Value-result.allRPCValues.c2H4Value;
    result.saveWhenAllRPCResult.absoluteResult.so2Value=actPoolSumResult.so2Value-result.allRPCValues.so2Value;
    
    
    result.alreadySavedEmissions.relativeResult.co2Value=(actPoolSumResult.co2Value-result.allCardboardValues.co2Value)/result.allCardboardValues.co2Value;
    result.alreadySavedEmissions.relativeResult.po4Value=(actPoolSumResult.po4Value-result.allCardboardValues.po4Value)/result.allCardboardValues.po4Value;
    result.alreadySavedEmissions.relativeResult.r11Value=(actPoolSumResult.r11Value-result.allCardboardValues.r11Value)/result.allCardboardValues.r11Value;       
    result.alreadySavedEmissions.relativeResult.c2H4Value=(actPoolSumResult.c2H4Value-result.allCardboardValues.c2H4Value)/result.allCardboardValues.c2H4Value;
    result.alreadySavedEmissions.relativeResult.so2Value=(actPoolSumResult.so2Value-result.allCardboardValues.so2Value)/result.allCardboardValues.so2Value;      

    result.alreadySavedEmissions.absoluteResult.co2Value=result.allCardboardValues.co2Value-actPoolSumResult.co2Value;
    result.alreadySavedEmissions.absoluteResult.po4Value=result.allCardboardValues.po4Value-actPoolSumResult.po4Value;
    result.alreadySavedEmissions.absoluteResult.r11Value=result.allCardboardValues.r11Value-actPoolSumResult.r11Value;
    result.alreadySavedEmissions.absoluteResult.c2H4Value=result.allCardboardValues.c2H4Value-actPoolSumResult.c2H4Value;
    result.alreadySavedEmissions.absoluteResult.so2Value=result.allCardboardValues.so2Value-actPoolSumResult.so2Value;



    //ensure Positve Values -> developed within release for Furitligistica 2011 -> Ticket #33 (Ifco)
    result.saveWhenAllRPCResult.relativeResult.co2Value= this.absolute(result.saveWhenAllRPCResult.relativeResult.co2Value);
    result.saveWhenAllRPCResult.relativeResult.po4Value= this.absolute(result.saveWhenAllRPCResult.relativeResult.po4Value);
    result.saveWhenAllRPCResult.relativeResult.r11Value= this.absolute(result.saveWhenAllRPCResult.relativeResult.r11Value);       
    result.saveWhenAllRPCResult.relativeResult.c2H4Value= this.absolute(result.saveWhenAllRPCResult.relativeResult.c2H4Value);
    result.saveWhenAllRPCResult.relativeResult.so2Value= this.absolute(result.saveWhenAllRPCResult.relativeResult.so2Value);      

    result.saveWhenAllRPCResult.absoluteResult.co2Value= this.absolute(result.saveWhenAllRPCResult.absoluteResult.co2Value);
    result.saveWhenAllRPCResult.absoluteResult.po4Value= this.absolute(result.saveWhenAllRPCResult.absoluteResult.po4Value);
    result.saveWhenAllRPCResult.absoluteResult.r11Value= this.absolute(result.saveWhenAllRPCResult.absoluteResult.r11Value);
    result.saveWhenAllRPCResult.absoluteResult.c2H4Value= this.absolute(result.saveWhenAllRPCResult.absoluteResult.c2H4Value);
    result.saveWhenAllRPCResult.absoluteResult.so2Value= this.absolute(result.saveWhenAllRPCResult.absoluteResult.so2Value);
    
    
    result.alreadySavedEmissions.relativeResult.co2Value= this.absolute(result.alreadySavedEmissions.relativeResult.co2Value);
    result.alreadySavedEmissions.relativeResult.po4Value= this.absolute(result.alreadySavedEmissions.relativeResult.po4Value);
    result.alreadySavedEmissions.relativeResult.r11Value= this.absolute(result.alreadySavedEmissions.relativeResult.r11Value);       
    result.alreadySavedEmissions.relativeResult.c2H4Value= this.absolute(result.alreadySavedEmissions.relativeResult.c2H4Value);
    result.alreadySavedEmissions.relativeResult.so2Value= this.absolute(result.alreadySavedEmissions.relativeResult.so2Value);      

    result.alreadySavedEmissions.absoluteResult.co2Value= this.absolute(result.alreadySavedEmissions.absoluteResult.co2Value);
    result.alreadySavedEmissions.absoluteResult.po4Value= this.absolute(result.alreadySavedEmissions.absoluteResult.po4Value);
    result.alreadySavedEmissions.absoluteResult.r11Value= this.absolute(result.alreadySavedEmissions.absoluteResult.r11Value);
    result.alreadySavedEmissions.absoluteResult.c2H4Value= this.absolute(result.alreadySavedEmissions.absoluteResult.c2H4Value);
    result.alreadySavedEmissions.absoluteResult.so2Value= this.absolute(result.alreadySavedEmissions.absoluteResult.so2Value);



    //End Calculation
    //do calc here
    return result;
  }
  
}


/*Parameter Structure - containing parameters for calculation Object*/
function CalulationSimpleParameterStructure(ConservativValue, TechnicValue){
 this.aParameter=0;
 this.cParameter=0;
 this.acParameter=0;
 this.tParameter=0;
 this.sParameter=0;
 
 this.ConservatvValue = ConservativValue;
 this.TechnicValue = TechnicValue;
 
 this.InitStrucuteParameterValues=function(ConservativTotalValue, TechnicTotalValue){
  this.aParameter=this.ConservatvValue/ConservativTotalValue;
  this.cParameter=this.TechnicValue/TechnicTotalValue;
  this.acParameter=this.aParameter/this.cParameter;
  this.sParameter=(1/50)*((this.TechnicValue/TechnicTotalValue)-(this.ConservatvValue/ConservativTotalValue));
  this.tParameter=this.aParameter-(50*this.sParameter);
 } ;
}

function CalculationComplexParameterStructure(calcSimpleStructCreation, calcSimpleStructUsage, calcSimpleStructEol, ConservativTotalValue, TechnicTotalValue){
  this.Creation = calcSimpleStructCreation;
  this.Usage = calcSimpleStructUsage;
  this.Eol = calcSimpleStructEol;
  
  this.ConservativTotalValue = ConservativTotalValue;
  this.TechnicTotalValue = TechnicTotalValue;
  
  this.InitStrucuteParameterValues=function(){
    this.Creation.InitStrucuteParameterValues(this.ConservativTotalValue, this.TechnicTotalValue);
    this.Usage.InitStrucuteParameterValues(this.ConservativTotalValue, this.TechnicTotalValue);
    this.Eol.InitStrucuteParameterValues(this.ConservativTotalValue, this.TechnicTotalValue);
  };
}
function CalculationParameterDataContainerStructure(GwpStructure, EPStructure, ODPStructure, POCPStructure,APStructure){
  this.GWP=GwpStructure;
  this.EP=EPStructure;
  this.ODP=ODPStructure;
  this.POCP=POCPStructure;
  this.AP=APStructure;
  
  this.CarbonCo2Value=0;
  this.CarbonPo4Value=0;
  this.CarbonR11Value=0;
  this.CarbonC2h4Value=0;
  this.CarbonSo2Value=0;
  
  this.FillingWeightPerCrate=0;
  this.MerchandiseValuePerCrate=0;
  
  this.InitStrucuteParameterValues=function(){
    this.GWP.InitStrucuteParameterValues();
    this.EP.InitStrucuteParameterValues();
    this.ODP.InitStrucuteParameterValues();
    this.POCP.InitStrucuteParameterValues();
    this.AP.InitStrucuteParameterValues();
  };
  
  this.InitGeneralCalculationValues= function(CarbonCo2Value,CarbonPo4Value,CarbonR11Value,CarbonC2h4Value,CarbonSo2Value, FillingWeightPerCrate,MerchandiseValuePerCrate){
    this.CarbonCo2Value=CarbonCo2Value;
    this.CarbonPo4Value=CarbonPo4Value;
    this.CarbonR11Value=CarbonR11Value;
    this.CarbonC2h4Value=CarbonC2h4Value;
    this.CarbonSo2Value=CarbonSo2Value;
    
    this.FillingWeightPerCrate=FillingWeightPerCrate;
    this.MerchandiseValuePerCrate=MerchandiseValuePerCrate;
  };  
  
  
}

function CalculationParameterFactory(){


  this.getGWPParameterStructure=function(){
    return new CalculationComplexParameterStructure( 
      new CalulationSimpleParameterStructure(337767.252352983,402037.192560709),
      new CalulationSimpleParameterStructure(906421.775010078,1812836.51013689),
      new CalulationSimpleParameterStructure(-137635.341104113,-163824.42569301),3333333,6666666);
  };

  this.getEPParameterStructure=function(){
    return  new CalculationComplexParameterStructure(
      new CalulationSimpleParameterStructure(105.062959981059,125.0542116758),
      new CalulationSimpleParameterStructure(786.866090071851,1573.72606881058),
      new CalulationSimpleParameterStructure(-50.2668193053313,-59.8315282837199),3333333,6666666);
    };

  this.getODPParameterStructure=function(){
    return new CalculationComplexParameterStructure(
      new CalulationSimpleParameterStructure(0.24108340698027,0.286956463186157),
      new CalulationSimpleParameterStructure(0.297232724324179,0.594463140138411), 
      new CalulationSimpleParameterStructure(-0.141078731444965,-0.167923020141947),3333333,6666666);
  };
  
  this.getPOCOPParameterStructure=function(){
  return new CalculationComplexParameterStructure(
      new CalulationSimpleParameterStructure(644.551519490916,767.195995324199),
      new CalulationSimpleParameterStructure(921.113318771726,1842.21948355574), 
      new CalulationSimpleParameterStructure(-493.773964207769,-587.728670990891),3333333,6666666);
  };
  
  this.getAPParameterStructure=function(){
    return new CalculationComplexParameterStructure(
      new CalulationSimpleParameterStructure(973.303440800333,1158.50243066226), 
      new CalulationSimpleParameterStructure(5220.47626873205,10440.911991722),
      new CalulationSimpleParameterStructure(-280.47183889209,-333.839677810671),3333333,6666666);
    };
  this.getCalculationDataContainer=function(){
    var CalcDataStructure = new CalculationParameterDataContainerStructure(this.getGWPParameterStructure(),
                                                          this.getEPParameterStructure(), 
                                                          this.getODPParameterStructure(),
                                                          this.getPOCOPParameterStructure(),
                                                          this.getAPParameterStructure());
    CalcDataStructure.InitStrucuteParameterValues();
    CalcDataStructure.InitGeneralCalculationValues(0.70795875070795900000,0.00130830000130830000,0.00000019200000019200,0.00066015000066015000,0.00634740000634740000,15,15)
    return CalcDataStructure;
  }
//  this.getGWPParameterStructure=function(){
//    return new CalculationComplexParameterStructure(
//      new CalulationSimpleParameterStructure(0.10133,0.06031,1.68028,0.14235,-0.00082),
//      new CalulationSimpleParameterStructure(0.27193,0.27193,1.00000,0.27193,0.00000),
//      new CalulationSimpleParameterStructure(-0.04129,-0.02457,1.68028,-0.05801,0.00033));
//  };

//  this.getEPParameterStructure=function(){
//    return  new CalculationComplexParameterStructure(
//      new CalulationSimpleParameterStructure(0.00003,0.00002,1.68028,0.00004,0.00000),
//      new CalulationSimpleParameterStructure(0.00024,0.00024,1.00000,0.00024,0.00000),
//      new CalulationSimpleParameterStructure(-0.00002,-0.00001,1.68028,-0.00002,0.00000));
//    };

//  this.getODPParameterStructure=function(){
//    return new CalculationComplexParameterStructure(
//      new CalulationSimpleParameterStructure(0.00000,0.00000,1.68028,0.00000,0.00000),
//      new CalulationSimpleParameterStructure(0.00000,0.00000,1.00000,0.00000,0.00000), 
//      new CalulationSimpleParameterStructure(0.00000,0.00000,1.68028,0.00000,0.00000));
//  };
//  
//  this.getPOCOPParameterStructure=function(){
//  return 
//    new CalculationComplexParameterStructure(new CalulationSimpleParameterStructure(0.00019,0.00012,1.68028,0.00027,0.00000),
//    new CalulationSimpleParameterStructure(0.00028,0.00028,1.00000,0.00028,0.00000), 
//    new CalulationSimpleParameterStructure(-0.00015,-0.00009,1.68028,-0.00021,0.00000));
//  };
//  
//  this.getAPParameterStructure=function(){
//    return new CalculationComplexParameterStructure(
//      new CalulationSimpleParameterStructure(0.00029,0.00017,1.68028,0.00041,0.00000), 
//      new CalulationSimpleParameterStructure(0.00157,0.00157,1.00000,0.00157,0.00000),
//      new CalulationSimpleParameterStructure(-0.00008,-0.00005,1.68028,-0.00012,0.00000));
//    };
//  this.getCalculationDataContainer=function(){
//    return new CalculationParameterDataContainerStructure(this.getGWPParameterStructure(),
//                                                          this.getEPParameterStructure(), 
//                                                          this.getODPParameterStructure(),
//                                                          this.getPOCOPParameterStructure(),
//                                                          this.getAPParameterStructure());
//  }
}
