
// CD 2006.07.19 start

//todo: keep currencyRateList up to date.
// + is safe to add new currencyName/Rate/Symbol at the end (currency lists must always have same size)
var currencyNameList = new Array("eur", "ron", "ufo", "nzd", "ukp", "usd");
var currencyRateList = new Array(0.79, 2.873, 222.218, 1.6476, 0.5484, 1.0);
var currencySymbolList = new Array(" €", " RON", " FOR", " NZD", " £", " $");

function getCurrencyRate(currencyName) {
	for (var i=0; i<currencyNameList.length; i++)
	if (currencyNameList[i]==currencyName) return currencyRateList[i];
	return currencyRateList[currencyRateList.length]; //return value for "usd" by default; should never be the case but just in case...
}

function getCurrencySymbol(currencyName) {
	for (var i=0; i<currencyNameList.length; i++)
	if (currencyNameList[i]==currencyName) return currencySymbolList[i];
	return currencySymbolList[currencyRateList.length]; //return "$" by default; should never be the case but just in case...
}


function changeCurrencyByName(newCurrencyName){
	g_currencyName = newCurrencyName;
	changeCurrancy(getCurrencyRate(newCurrencyName));
}

// CD 2006.07.19 end

//Need to add some type of security here
 //copyrighted May 2006 Distributed LP ELECTRIC - Romania
 //puts num into currency format
function formatCurrency(num) {
  var i,l,d;
  var nums;
  var ret;
  num = intx(num);
  nums = String(Math.round(num*100));
  while (nums.length <3) nums = "0" + nums;
  l = nums.length-3;
  ret = "";
//  ret = "," + nums.charAt(l+1) + nums.charAt(l+2);
  d=0;
  for (i=l; i>=0; i--) {
    ret = nums.charAt(i) + ret;
    d++;
    if (d==3 && i>0) {
      ret="." + ret;
      d=0;
    }
  }
	// CD: return currencySymbol + value
  ret = ret + getCurrencySymbol(g_currencyName);
  return ret;
}
function formatCurrency2(num) {
  var i,l,d;
  var nums;
  var ret;
  num = intx(num);
  nums = String(Math.round(num*100));
  while (nums.length <3) nums = "0" + nums;
  l = nums.length-3;
  ret = "";
  ret = "," + nums.charAt(l+1) + nums.charAt(l+2);
  d=0;
  for (i=l; i>=0; i--) {
    ret = nums.charAt(i) + ret;
    d++;
    if (d==3 && i>0) {
      ret="." + ret;
      d=0;
    }
  }
  ret = "$" + ret;
  return ret;
}
//convertes to int but double like
function cint(val){
	if ((val== null)||(val== "")){
		val= parseInt(0);	
	}
 	else{ val = parseInt(val*100)/100;}
	return val;
}
//shows €2.23
function formatCurrency3(num) {
  var i,l,d;
  var nums;
  var ret;
  nums = String(Math.round(num*100));
  while (nums.length <3) nums = "0" + nums;
  l = nums.length-3;
  ret = "";
  ret = "," + nums.charAt(l+1) + nums.charAt(l+2);
  d=0;
  for (i=l; i>=0; i--) {
    ret = nums.charAt(i) + ret;
    d++;
    if (d==3 && i>0) {
      ret="." + ret;
      d=0;
    }
  }
  ret = "€" + ret;
  return ret;
}
//intx
function intx(val){
	if ((val== null)||(val== "")){
		val= parseInt(0);	
	}
 	else if (val == parseInt(val)){
 		 val = parseInt(val);}
 	else {val = parseInt(val)+1;}
	return val;
}
//Add a new select box
function newBox(BoxNumber, ValueBox, NameBox, ToBox) {
	var MenuDrop = new Option();
	MenuDrop.value = ValueBox;
	MenuDrop.text = NameBox;
	ToBox[BoxNumber] = MenuDrop;
}
// RUN ALL THE CALCS
function runCalcs(){
 if (setupRun != 1){
 EffSetup();
PVRoofgenerator();
PowerProducedMaxAvg();
ChangePrices(); 
costsDifference();
updateGraph();
 }
}
//find the Utility and Environmental Savings
function costsDifference(){
    if (document.form.UtilityValue.value == "No"){
    	pricePerkWh = 0;} 
	currentMonthly = intx(cint(document.form.CurrentMonthUtilCosts.value)*Currency);
	document.form.CurrentMonthUtil.value = formatCurrency(currentMonthly );
	document.form.CurrentYearUtil.value = formatCurrency(currentMonthly *12);
	document.form.CurrentLifeUtil.value = formatCurrency(currentMonthly *12*30);
	PowerProduced = intx(document.form.TotalPowerMonthly.value);
	PowerNeeded = intx(document.form.PowerNeededAvg.value);
	PowerLeft =  PowerNeeded-PowerProduced ;
	currentCo2 = cO2 * PowerNeeded;
	if (PowerLeft>0){
		document.form.NewMonthUtilCalc.value = pricePerkWh *PowerLeft;
		document.form.NewMonthUtil.value = formatCurrency(intx(pricePerkWh *PowerLeft*Currency) );
		document.form.NewYearUtil.value = formatCurrency(intx(pricePerkWh *PowerLeft*Currency) *12);
		document.form.NewLifeUtil.value = formatCurrency(intx(pricePerkWh *PowerLeft *Currency)*12*30);
		document.form.SavingsMonthUtilCalc.value = currentMonthly  -(pricePerkWh *PowerLeft);
		document.form.SavingsMonthUtil.value =formatCurrency(currentMonthly  -intx(pricePerkWh *PowerLeft*Currency));
		document.form.SavingsYearUtil.value =  formatCurrency((currentMonthly  -intx(pricePerkWh *PowerLeft*Currency))*12);
		document.form.SavingsLifeUtil.value =formatCurrency(((currentMonthly  -intx((pricePerkWh *PowerLeft*Currency )))*12*30));
		//co2
		document.form.CO2NewMonth.value=intx(cO2 * PowerLeft );
		document.form.CO2NewYear.value=intx(cO2 * PowerLeft )*12;
		document.form.CO2NewLife.value=intx(cO2 * PowerLeft )*12*30;
		document.form.CO2SavedMonth.value=intx(cO2 * (PowerNeeded-PowerLeft) );
		document.form.CO2SavedYearly.value=intx(cO2 * (PowerNeeded-PowerLeft) )*12;
		document.form.CO2SavedLife.value=intx(cO2 * (PowerNeeded-PowerLeft)) *12*30;
	}
	else {
	    document.form.NewMonthUtilCalc.value = 0;
		document.form.NewMonthUtil.value = formatCurrency(0);
		document.form.NewYearUtil.value = formatCurrency(0);	
		document.form.NewLifeUtil.value = formatCurrency(0);
		document.form.SavingsMonthUtilCalc.value = document.form.CurrentMonthUtilCosts.value
		document.form.SavingsMonthUtil.value = document.form.CurrentMonthUtil.value;
		document.form.SavingsYearUtil.value = document.form.CurrentYearUtil.value;
		document.form.SavingsLifeUtil.value = document.form.CurrentLifeUtil.value;
		//co2
		document.form.CO2NewMonth.value=0;
		document.form.CO2NewYear.value=0;
		document.form.CO2NewLife.value=0;
		document.form.CO2SavedMonth.value= document.form.CO2OldMonth.value;
		document.form.CO2SavedYearly.value= document.form.CO2OldYear.value;
		document.form.CO2SavedLife.value =document.form.CO2OldLife.value ;
	}
ChangePrices();	
}
//find Power NEeded  avg min max Weather
function generatePowerNeededAvg(){
	PowerNeededMin = 9999;
	PowerNeededMax = 0;
	PowerNeededAvg= 0;
	PowerNeededArray[0] = cint(document.form.PowerNeededJan.value);
	PowerNeededArray[1] = cint(document.form.PowerNeededFeb.value);
	PowerNeededArray[2] = cint(document.form.PowerNeededMar.value);
	PowerNeededArray[3] = cint(document.form.PowerNeededApr.value);
	PowerNeededArray[4] = cint(document.form.PowerNeededMay.value);
	PowerNeededArray[5] = cint(document.form.PowerNeededJun.value);
	PowerNeededArray[6] = cint(document.form.PowerNeededJul.value);	
	PowerNeededArray[7] = cint(document.form.PowerNeededAug.value);
	PowerNeededArray[8] = cint(document.form.PowerNeededSep.value);
	PowerNeededArray[9] = cint(document.form.PowerNeededOct.value);
	PowerNeededArray[10] = cint(document.form.PowerNeededNov.value);
	PowerNeededArray[11] = cint(document.form.PowerNeededDec.value);
 	for (var i=0; i<12; i++){
		 PowerNeededAvg=PowerNeededAvg+ PowerNeededArray[i];
		if(PowerNeededArray[i] < PowerNeededMin){
			PowerNeededMin = PowerNeededArray[i];} 
		if(PowerNeededArray[i] > PowerNeededMax ){
			PowerNeededMax = PowerNeededArray[i];} 		
		}
	document.form.PowerNeededMin.value= PowerNeededMin;
	document.form.PowerNeededMax.value=PowerNeededMax;
	PowerNeededAvg = cint(PowerNeededAvg/12)
	document.form.PowerNeededAvg.value= PowerNeededAvg ;
	document.form.CO2OldMonth.value= intx(cO2 * PowerNeededAvg );
	document.form.CO2OldYear.value= intx(cO2 * PowerNeededAvg )*12;
	document.form.CO2OldLife.value =intx(cO2 * PowerNeededAvg )*12*30;
	document.form.CurrentMonthUtilCosts.value = intx(pricePerkWh * PowerNeededAvg );
	document.form.CurrentMonthUtil.value= formatCurrency(intx(pricePerkWh * PowerNeededAvg) );
	document.form.CurrentYearUtil.value= formatCurrency(intx(pricePerkWh * PowerNeededAvg) *12);
	document.form.CurrentLifeUtil.value =formatCurrency(intx(pricePerkWh * PowerNeededAvg) *12*30); 
}

//find solar avg min max Weather
function generateSolarAvg(){
	SolarMin = 9999;
	SolarMax = 0;
	SolarAvg= 0;
	SolarArray[0] = cint(document.form.SolarJan.value);
	SolarArray[1] = cint(document.form.SolarFeb.value);
	SolarArray[2] = cint(document.form.SolarMar.value);
	SolarArray[3] = cint(document.form.SolarApr.value);
	SolarArray[4] = cint(document.form.SolarMay.value);
	SolarArray[5] = cint(document.form.SolarJun.value);
	SolarArray[6] = cint(document.form.SolarJul.value);	
	SolarArray[7] = cint(document.form.SolarAug.value);
	SolarArray[8] = cint(document.form.SolarSep.value);
	SolarArray[9] = cint(document.form.SolarOct.value);
	SolarArray[10] = cint(document.form.SolarNov.value);
	SolarArray[11] = cint(document.form.SolarDec.value);
 	for (var i=0; i<12; i++){
		 SolarAvg=SolarAvg+ SolarArray[i];
		if(SolarArray[i] < SolarMin){
			SolarMin = SolarArray[i];} 
		if(SolarArray[i] > SolarMax ){
			SolarMax = SolarArray[i];} 		
		}
	document.form.SolarMin.value= SolarMin;
	document.form.SolarMax.value=SolarMax;
	document.form.SolarAvg.value= cint(SolarAvg/12);
}

//Calculate Solar Power Produced
function ChangeSolar(){
 	str = document.form.Panels.value;
 	var vlength = str.length;
	var pos=str.indexOf("/");
	price = str.substring(0,pos); 
	var panelSize = str.substring(pos+1,vlength);
	SolarSize = document.form.SolarPanelSize.value;
	SolarQuantity = intx((SolarSize*1000)/panelSize);
	document.form.SolarQuantity.value = SolarQuantity ;
	document.form.SolarRoofCost.value = formatCurrency(price * SolarQuantity*Currency);
	document.form.SolarPanelSize.value= panelSize * SolarQuantity/1000;
	document.form.SolarPanelCostCalcs.value=parseInt(price * SolarQuantity*100)/100;
	 document.form.SolarRoofName.value=document.form.Panels.options[document.form.Panels.selectedIndex].text;
runCalcs();
}
//calc solar output
function PVRoofgenerator(){
    document.form.SolarRoofCost.value = formatCurrency(document.form.SolarPanelCostCalcs.value*Currency);
    calcRoofData = 30*document.form.SolarPanelSize.value * EffFactor;
    RoofPowerOutput[0] = intx(calcRoofData *document.form.SolarJan.value); 
    RoofPowerOutput[1] = intx(calcRoofData *document.form.SolarFeb.value); 
    RoofPowerOutput[2] = intx(calcRoofData *document.form.SolarMar.value); 
    RoofPowerOutput[3] = intx(calcRoofData *document.form.SolarApr.value); 
    RoofPowerOutput[4] = intx(calcRoofData *document.form.SolarMay.value); 
    RoofPowerOutput[5] = intx(calcRoofData *document.form.SolarJun.value); 
    RoofPowerOutput[6] = intx(calcRoofData *document.form.SolarJul.value); 
    RoofPowerOutput[7] = intx(calcRoofData *document.form.SolarAug.value); 
    RoofPowerOutput[8] = intx(calcRoofData *document.form.SolarSep.value); 
    RoofPowerOutput[9] = intx(calcRoofData *document.form.SolarOct.value); 
    RoofPowerOutput[10] = intx(calcRoofData *document.form.SolarNov.value); 
    RoofPowerOutput[11] = intx(calcRoofData *document.form.SolarDec.value); 
    //Change outputstring 
    vRoofPowerOutput = RoofPowerOutput[0]+"/"+ RoofPowerOutput[1] +"/"+  RoofPowerOutput[2] +"/"+  RoofPowerOutput[3] +"/"+ RoofPowerOutput[4]+"/"+RoofPowerOutput[5] +"/"+RoofPowerOutput[6] +"/"+ RoofPowerOutput[7]+"/"+ RoofPowerOutput[8]    +"/"+ RoofPowerOutput[9]  +"/"+  RoofPowerOutput[10]+"/"+RoofPowerOutput[11] 
    document.form.vRoofPowerOutput.value = vRoofPowerOutput ;
}
//TotalPowerProduced
function PowerProducedMaxAvg(){
	AvgPowerProduced=0;
	MaxPowerProduced=0;
	PowerProduced =0;
	for (var i=0; i<12; i++){
	  PowerProduced= cint(RoofPowerOutput[i])
	  AvgPowerProduced = AvgPowerProduced +PowerProduced;
	  if(MaxPowerProduced < PowerProduced){
	  	MaxPowerProduced = PowerProduced ;}	}
	 AvgPowerProduced = AvgPowerProduced /12;
	 MaxPowerProduced =MaxPowerProduced +10;
	 document.form.TotalPowerMonthly.value = intx(AvgPowerProduced);
	 document.form.PercentToNeeded.value=intx((AvgPowerProduced/cint(document.form.PowerNeededAvg.value))*100);
}
//changes pricing
function ChangePrices(){ 
document.form.SolarRoofName.value = document.form.Panels.options[document.form.Panels.selectedIndex].text ;
 TotalCosts =   cint(document.form.SolarPanelCostCalcs.value)
TotalOther = cint(Support *TotalCosts); 
document.form.SupportCalcs.value = TotalOther ;
document.form.TotalCostsCalcs.value = TotalCosts+TotalOther ;
document.form.TotalCosts.value=formatCurrency((TotalCosts+TotalOther)*Currency);
document.form.ComponentCosts.value=formatCurrency(TotalOther);
document.form.ComponentCostsCalcs.value=TotalOther;
fMonthlyCosts();
}
function floor(number)
{
  return Math.floor(number*Math.pow(10,2))/Math.pow(10,2);
}
function fMonthlyCosts(){
TotalCosts= document.form.TotalCostsCalcs.value;
InterestRate = cint(document.form.InterestRate.value);
Years = cint(document.form.FinanceYears.value);
 Payment = ((TotalCosts*(1+(InterestRate*Years)))/Years)/12;
document.form.MonthlyPaymentFinance.value=formatCurrency(floor(Payment)*Currency);
  }


 //change solar Size
 function ChangeSolarSize(val){
	var str = document.form.Panels.value;
	var vlength = str.length;
	var pos=str.indexOf("/");
	price = str.substring(0,pos); 
	var panelSize = str.substring(pos+1,vlength);
	SolarQuantity = intx(val*1000/panelSize);
	document.form.SolarQuantity.value = SolarQuantity ;
	document.form.SolarRoofCost.value = formatCurrency(price * SolarQuantity*Currency);
	document.form.SolarPanelCostCalcs.value=parseInt(price * SolarQuantity*100)/100;
	runCalcs();
}
function ChangeSolarQuantity(val){
 	str = document.form.Panels.value;
 	var vlength = str.length;
	var pos=str.indexOf("/");
	price = str.substring(0,pos); 
	var panelSize = str.substring(pos+1,vlength);
	SolarSize = document.form.SolarPanelSize.value;
	SolarQuantity = intx(val);
	document.form.SolarQuantity.value = SolarQuantity ;
	document.form.SolarRoofCost.value = formatCurrency(price * SolarQuantity*Currency);
	document.form.SolarPanelCostCalcs.value=parseInt(price * SolarQuantity*100)/100;
	document.form.SolarPanelSize.value= panelSize * SolarQuantity/1000;
	runCalcs();
}

//Update Graph
function updateGraph(){
	if (document.form.Utility.value="No"){
		PowerNeededGraph = intx(document.form.PowerNeededMin.value);}
	else {PowerNeededGraph = intx(document.form.PowerNeededAvg.value);}
	PowerProducedMaxAvg();
	if (MaxPowerProduced < PowerNeededGraph){ MaxPower =  PowerNeededGraph;}
	else{ MaxPower = MaxPowerProduced;}
	var strXML = "<graph basefont='Arial' bastfontsize='10' outCnvBaseFontSize='12' caption='Putere Produsa Proiectata' xAxisName='Luna' yAxisName='    KWh' canvasbgcolor='FFFFFF' gridbgcolor='FFFFFF' hovercapbg='FFFFFF' hovercapborder='FFFFFF' divlinecolor='FFFFFF' yaxisminvalue='0'  yaxismaxvalue='"+intx(MaxPower) +"'>";
	strXML =  strXML +  "<categories><category name='Jan' /><category name='Feb' /><category name='Mar' /><category name='Apr' /><category name='May' /><category name='Jun' /><category name='Jul' /><category name='Aug' /><category name='Sep' /><category name='Oct' /><category name='Nov' /><category name='Dec' /></categories>";
 		strXML =  strXML +  "<dataset seriesname='-KWh=' color='0099CC' showValue='0'>";
  		for (var i=0; i<12; i++){
   			strXML =  strXML +  "<set value='"+RoofPowerOutput[i]+"' />";}
    	strXML =  strXML +  "</dataset>";
    strXML =  strXML +  "<trendlines><line value='"+PowerNeededGraph+"' color='1C86EE' displayValue='100%       ' thickness='10' alpha='20'/>";
 	 strXML =  strXML +  "<line value='"+PowerNeededGraph/2+"' color='DA4210' displayValue='50%        ' thickness='10' alpha='20'/>";
	 strXML =  strXML +  "<line value='"+PowerNeededGraph/5+"' color='E3A21C' displayValue='20%        ' thickness='10' alpha='20'/>";
	 strXML =  strXML +  "</trendlines></graph>";
	document.form.FC2StackedColumn.SetVariable("_root.newData","1");
	document.form.FC2StackedColumn.SetVariable("_root.dataXML",strXML);
	document.form.FC2StackedColumn.GotoFrame(0);
}

