var accCount = [];
var previous_r = "";
var previous_w = null;
var chosen = "";
var chosenClassName = "";
function res()
{ 
	document.vero.reset(); 
} 
if(window.addEventListener )
{
	addEventListener("unload", res, false );
}
function handleRow(useDiscount, weight )
{
  // haetaan CO2 ja lasketaan veroprosentti
  var thisCO2 = vero.paastot.value;
  var taxPros = 0;
  if( thisCO2 != null )
    taxPros = Math.round( (4.88 + 0.122 * thisCO2)*10 ) / 10;
  var originalPrice = 0;
  var tax = 0;
  var ylva = 0;
  var taxValue = 0;
  var sumPrice = 0;
  var kaikkiveroton=0;
  var kaikkivero=0;
  var kaikkiverolla=0;
  // lasketaan ja kirjoitetaan jokaiseen varusteeseen
	for( i=1; i<=accCount; i++ )
	{
 		// alert('accPrice_1_'+i);
		originalPrice = document.getElementById( 'accPrice_1_'+i ).value;
		while(originalPrice.indexOf(" ")>0)
		{
			originalPrice = originalPrice.replace(' ', '');
		}
		while(originalPrice.indexOf(",")>0)
		{
			originalPrice = originalPrice.replace(',', '.');
		}
		if( originalPrice != null & originalPrice>0)
		{
			kaikkiveroton=eval(parseFloat(kaikkiveroton)+parseFloat(originalPrice));
			ylva = originalPrice * 0.945;
			taxValue = Math.round( (ylva / (1- taxPros/100) )*100 ) / 100;
			tax = Math.floor( (taxValue * taxPros / 100)*100 ) / 100;
			sumPrice = Math.round( (originalPrice*1 + tax*1)*100 ) / 100;
			document.getElementById( 'accTax_1_'+i ).innerHTML = tax.toFixed(2).replace('.', ',');
			document.getElementById( 'accSum_1_'+i ).innerHTML = "<b>"+sumPrice.toFixed(2).replace('.', ',')+"</b>";
		}
		else
		{
			document.getElementById( 'accTax_1_'+i ).innerHTML = "";
			document.getElementById( 'accSum_1_'+i ).innerHTML = "";
		}
	}  
	ylva = parseFloat(kaikkiveroton * 0.945);
	taxValue = Math.round( (ylva / (1- taxPros/100) )*100 ) / 100;
	tax = Math.floor( (taxValue * taxPros / 100)*100 ) / 100;
	sumPrice = Math.round( (kaikkiveroton*1 + tax*1)*100 ) / 100;
	document.getElementById( 'kaikkiveroton' ).innerHTML = kaikkiveroton;
	document.getElementById( 'kaikkivero' ).innerHTML = tax.toFixed(2).replace('.', ',');
	document.getElementById( 'kaikkiverolla').innerHTML = "<b>"+sumPrice.toFixed(2).replace('.', ',')+"</b>";
}
function addRow()
{
accCount++;
//here we point the tbody variable to the table 
var tbody = document.getElementById("verotaulukko").getElementsByTagName("TBODY")[0]; 
//here we create a row element 
var row = document.createElement("TR");
if(accCount%2)
{
	// row.setAttribute("class","first");
	row.className="first";
}
else
{
//	row.setAttribute("class","second");
	row.className="second";
}
//here we create the first cell element 
var t0 = document.createElement("TD"); 
t0.innerHTML='<img src="/vv-auto/Audi3.nsf/spacer.gif"   width="20" height="19" border="0">';
var t1 = document.createElement("TD"); 
//here we popuate the first cell with 'Test1' 
t1.innerHTML='<input  type="text" name="accExpl_1_'+accCount+'" value="Varuste '+accCount+'" style="width: 100%;">';
//here we create the second cell element 
var t2 = document.createElement("TD"); 
//here we popuate the first cell with email@server.com' 
t2.innerHTML='<input type="text" id="accPrice_1_'+accCount+'" value="Syötä hinta" size="5" style="width: 100%;" onClick="if( this.value==\'Syötä hinta\') this.value=\'\'">';
var t3 = document.createElement("TD"); 
t3.innerHTML='<div style="text-align: right;" id="accTax_1_'+accCount+'">&nbsp;</div>';
var t4 = document.createElement("TD"); 
t4.innerHTML='<div style="text-align: right; padding-right: 10px;" id="accSum_1_'+accCount+'">&nbsp;</div>';
//here we append the cells to the created row element 
row.appendChild(t0) 
row.appendChild(t1) 
row.appendChild(t2) 
row.appendChild(t3) 
row.appendChild(t4) 
//here we append the created row element to the table 
tbody.appendChild(row) 
document.getElementById( 'kaikkiveroton' ).innerHTML = "";
document.getElementById( 'kaikkivero' ).innerHTML = "";
document.getElementById( 'kaikkiverolla').innerHTML = "";
// alert(document.getElementById( 'verotaulukko').innerHTML);
}
