var iteration = 0;
var total_sum_value = 0;
var total_vat_value = 0;

function addRowToTable(name){
	var tbl = document.getElementById(name);
	var lastRow = tbl.rows.length;
	iteration = lastRow - 1;
	var row = tbl.insertRow(lastRow);
	var el = document.createElement('span');
	el.innerHTML = lastRow+'.';
	var cell = row.insertCell(0);
	cell.setAttribute('width', '20');
	cell.setAttribute('align', 'center');
	cell.setAttribute('valign', 'top');
	cell.setAttribute('className', 'lentele1');
	cell.appendChild(el);
	var el = document.createElement('input');
	el.setAttribute('type', 'text');
	el.setAttribute('name', 'name['+iteration+']');
	el.setAttribute('id', 'name['+iteration+']');
	el.setAttribute('className', 'txt100');
	var cell = row.insertCell(1);
	cell.setAttribute('align', 'left');
	cell.setAttribute('valign', 'top');
	cell.setAttribute('className', 'lentele1');
	cell.appendChild(el);
	var el = document.createElement('input');
	el.setAttribute('type', 'text');
	el.setAttribute('name', 'mate['+iteration+']');
	el.setAttribute('id', 'mate['+iteration+']');
	el.setAttribute('className', 'txt100');
	el.setAttribute('value', 'vnt.');
	var cell = row.insertCell(2);
	cell.setAttribute('width', '50');
	cell.setAttribute('align', 'center');
	cell.setAttribute('valign', 'top');
	cell.setAttribute('className', 'lentele1');
	cell.appendChild(el);
	var el = document.createElement('input');
	el.setAttribute('type', 'text');
	el.setAttribute('name', 'count['+iteration+']');
	el.setAttribute('id', 'count['+iteration+']');
	el.setAttribute('className', 'txt100');
	el.setAttribute('value', '0');
	el.setAttribute('title', iteration);
	el.attachEvent('onkeyup', calculate_sum);
	var cell = row.insertCell(3);
	cell.setAttribute('width', '60');
	cell.setAttribute('align', 'center');
	cell.setAttribute('valign', 'top');
	cell.setAttribute('className', 'lentele1');
	cell.appendChild(el);
	var el = document.createElement('input');
	el.setAttribute('type', 'text');
	el.setAttribute('name', 'price['+iteration+']');
	el.setAttribute('id', 'price['+iteration+']');
	el.setAttribute('className', 'txt100');
	el.setAttribute('value', '0.00');
	el.setAttribute('title', iteration);
	el.attachEvent('onkeyup', calculate_sum);
	var cell = row.insertCell(4);
	cell.setAttribute('width', '70');
	cell.setAttribute('align', 'center');
	cell.setAttribute('valign', 'top');
	cell.setAttribute('className', 'lentele1');
	cell.appendChild(el);
	var el = document.createElement('span');
	el.innerHTML = '0.00';
	var cell = row.insertCell(5);
	cell.setAttribute('id', 'sum['+iteration+']');
	cell.setAttribute('width', '90');
	cell.setAttribute('align', 'center');
	cell.setAttribute('valign', 'top');
	cell.setAttribute('className', 'lentele2');
	cell.appendChild(el);
}

function calculate_sum(){
	good_sum(document.getElementById('count['+event.srcElement.title+']').value,document.getElementById('price['+event.srcElement.title+']').value,'sum['+event.srcElement.title+']');
}

function good_sum(object1,object2,object3){
	var curr_good_sum = parseFloat(object1) * parseFloat(object2)
	document.getElementById(object3).innerHTML = curr_good_sum.toFixed(2);
	total_sum();
}

function total_sum(){
	total_sum_value = 0;
	for(i=0;i<=iteration;i++) {
		total_sum_value = parseFloat(total_sum_value) + (parseFloat(document.getElementById('count['+i+']').value) * parseFloat(document.getElementById('price['+i+']').value));
	}
	document.getElementById('sum_not_vat').innerHTML = total_sum_value.toFixed(2);
	total_vat();
}

function total_vat(){
	total_vat_value = 0;
	total_vat_value = (parseFloat(total_sum_value) * (document.getElementById('buy_vat_index').value/100)); //Sutarties suma"
	document.getElementById('vat').innerHTML = total_vat_value.toFixed(2);;	
	total_vat_sum();
}

function total_vat_sum(){
	total_vat_sum_all = parseFloat(total_sum_value) + parseFloat(total_vat_value);
	document.getElementById('total_sum_all').innerHTML = total_vat_sum_all.toFixed(2);
	var total_vat_sum_cent = total_vat_sum_all.toFixed(2);
	document.getElementById('aassdd').innerHTML = convert_number(total_vat_sum_cent.toString(),'aassdd','ltl');
}

function kio_suma_zodziais(object1,object2,object3){
	var total_vat_with_cent = document.getElementById(object1).value+"."+document.getElementById(object2).value;
	document.getElementById(object3).innerHTML = convert_number(total_vat_with_cent.toString(),object3,'ltl');
}
