Auto Calculating Dynamic Feilds..

Hi!!
This is some of the code I am using to add dynamic rows when user clicks the Add Row button.I want to show the calculated amount like:
line_total=qty*unit_price;
sub_total=total of line_total;
total=sub_total+tax-advance;

So far I came up with the function as follows.It works very well on the first row, but not on the other dynamically added rows.
Similarly, I dont know how I can calculate the sub_total value by adding line_total feilds altogether.Please help me out