function calculate(rows, fieldset_id, selected_sub_index) {

  subtotal = 0;
  total = 0;

  for (i=0;i<rows;i++){
    
  	subtotal = total;
  	
  	amount = document.getElementById("amount_" + i).value;
  	price = document.getElementById("price_" + i).value;	
  
  	total = (amount * price) + subtotal;
  }

  if (document.getElementById("amount_" + fieldset_id) != null){
    document.getElementById('active_sub_object').style.visibility = 'visible'; 
    generate_sub_select(4, fieldset_id);
    document.getElementById("sub_object_select").selectedIndex = selected_sub_index;
  }

  if (document.getElementById("sub_sub_object_select") != null){
    
    if (document.getElementById("amount_" + fieldset_id).value != 0){
      if (document.getElementById("sub_object_select").selectedIndex != 0){
        extra = (document.getElementById("sub_sub_object_select").selectedIndex) * 2;
        total = total + extra;
      }
    }
  }
    
	total = total.toFixed(2);
	total = total.toString();
	total = total.replace(/[.]/, ',');

	document.getElementById("total").value =  total;
}

function numbersonly(myfield, e, dec){
	

	var key;
	var keychar;

	if (window.event)
	   key = window.event.keyCode;
	else if (e)
	   key = e.which;
	else
	   return true;
	keychar = String.fromCharCode(key);

	// control keys
	if ((key==null) || (key==0) || (key==8) ||
	    (key==9) || (key==13) || (key==27) )
	   return true;

	// numbers
	else if ((("0123456789").indexOf(keychar) > -1))
	   return true;

	// decimal point jump
	else if (dec && (keychar == "."))
	   {
	   myfield.form.elements[dec].focus();
	   return false;
	   }
	else
	   return false;
}