var desc_ottica;
var desc_finitura;
var desc_colore;
var code_ottica="";
var code_colore="";
var code_finitura="";
var code_alimentatore="";
var power;
var lumen;
var coeff_watt;
var coeff_lumen;
var led_number;
var item_code;

//funzione per la generazione del codice
function makeCode(type, string, item_description){

  var string_array=string.split("|");
  var code=string_array[0];
  var description=string_array[1];
  var attribute1=string_array[2];
  var attribute2=string_array[3];
  var number=string_array[4];
  
  if (type=='colore'){
	 var input = document.forms['myCode'].colore;
   code_colore = code;
   desc_colore = description;
   power = attribute1;
   lumen = attribute2;
   led_number = number;
  }
  if (type=='ottica'){  
   var input = document.forms['myCode'].ottica;
   code_ottica = code;
   desc_ottica = description;
  }
  if (type=='finitura'){
	 var input = document.forms['myCode'].finitura;  
	 code_finitura = code;
	 desc_finitura = description;
  }  
  if (type=='alimentazione'){
	 var input = document.forms['myCode'].alimentazione;  
	 //per l'alimentatore il codice completo da 4 cifre lo scrivo sulla descrizione
	 code_alimentatore = description;
	 //per l'alimentatore se flag_photometry=1 allora scrivo il codice articolo nel campo numero led
	 item_code = number;
	 coeff_watt = attribute1;
	 coeff_lumen = attribute2;
  }
	 
	input.value=code;

  makeDescription(item_description);
  
  makeCodeCurves(led_number, code_ottica, code_colore, code_alimentatore, item_code);	
  
  calculateItem(led_number, power, lumen, coeff_watt, coeff_lumen);
  
}

//funzione per la visualizzazione del codice
function showCode(type, string, item_description){
  var string_array=string.split("|");
  var code=string_array[0];
  
  if (type=='colore')
    var input = document.forms['myCode'].colore;

  if (type=='ottica') 
    var input = document.forms['myCode'].ottica;

  if (type=='finitura')
	   var input = document.forms['myCode'].finitura;  
  
  if (type=='alimentazione')
	  var input = document.forms['myCode'].alimentazione;  
 
	input.value=code; 
}

//funzione per la visualizzazione del codice
function hideCode(type, string, item_description){
  var string_array=string.split("|");
  var code=string_array[0];
  
  if (type=='colore'){
    var input = document.forms['myCode'].colore;
    code=code_colore;      
  }

  if (type=='ottica'){ 
    var input = document.forms['myCode'].ottica;
    code=code_ottica;     
  }

  if (type=='finitura'){
	 var input = document.forms['myCode'].finitura;  
   code=code_finitura;     
  }
    
  if (type=='alimentazione'){
	  var input = document.forms['myCode'].alimentazione;
    code=code_alimentatore.substr(2,2);      
  }  
 
	input.value=code; 
}

//funzione per la generazione della descrizione
function makeDescription(item_description){
  var description;
  description = item_description;
  
  if (desc_ottica) 
    description += ", " + desc_ottica;
  if (desc_finitura) 
    description += ", " + desc_finitura;
  if (desc_colore) 
    description += ", " + desc_colore;
    
  var input_description = document.forms['myCode'].description_complete;
  input_description.value=description;  
  //document.getElementById('itemDescription').innerHTML=description;
}

//funzione per la generazione del codice delle immagini per la fotometria
function makeCodeCurves(led_number, code_ottica, code_colore, code_alimentatore, item_code){
  var codeCurves;
  var scriptImages;
  var input_photometry = document.forms['myCode'].photometry_code;
  
  if (led_number && code_ottica && code_colore && code_alimentatore){
    if (item_code)
      codeCurves = item_code+"_"+led_number+"_"+code_colore+"_"+code_ottica+"_"+code_alimentatore;
    else
      codeCurves = led_number+"_"+code_colore+"_"+code_ottica+"_"+code_alimentatore; 
  }
 
  if (codeCurves){  
    //compongo il codice per la visualizzazione delle due immagini e del link
    scriptImages = "<img src='images/catalog/curves/projection/"+codeCurves+".jpg' title='projection'>";
    scriptImages += "<img src='images/catalog/curves/polar/"+codeCurves+".jpg' title='polar'>";
    scriptImages += "<br/><br/>";
    scriptImages += "<a href='images/catalog/curves/ldt/"+codeCurves+".ldt' target='_blank'><img src='images/ldt_icon.gif' title='download file ldt'></a>";
    scriptImages += "<b style='color:#ff9900;'>&nbsp;<&nbsp;Scarica il file LDT della fotometria</b>";
    
    document.getElementById('code_curves1').style.display='block';
    document.getElementById('code_curves2').style.display='block';
    document.getElementById('code_curves_content').innerHTML=scriptImages; 
    input_photometry.value=codeCurves;   
  } 

}

//funzione per il calcolo dei lumen e della potenza
function calculateItem(led_number, power, lumen, coeff_watt, coeff_lumen){
  var total_power;
  var total_lumen;
  var string_false='Dati non sufficienti';
  var input_power = document.forms['myCode'].power;
  var input_lumen = document.forms['myCode'].lumen;
  
  total_power=arrotonda((led_number*power*coeff_watt),2);
  string_power='W '+total_power;
  
  total_lumen=arrotonda((led_number*lumen*coeff_lumen),2);
  string_lumen='Lm '+total_lumen;
  
  if (total_power)
    input_power.value=string_power;
  else
    input_power.value=string_false;
  
  if (total_lumen)
    input_lumen.value=string_lumen;  
  else
    input_lumen.value=string_false;
}

function arrotonda(num, dec){ 
    dec= (dec)?dec:2; // default: due decimali 
    ee = Math.pow(10,dec); 
    nn = Math.floor(num*ee); 
    vv = ""; // default per 0 
    if(nn>0) { 
        vv = String(nn/ee); 
        vv += (vv.indexOf(".")>=0)?"":"."; 
        while (vv.indexOf(".")>vv.length-dec-1) { vv += "0" } 
    } 
    return vv;
} 

