Custom script editing

Hello,

I want to add additional script to already created script for costing template.

I want to know how to write script for the below calculations.

Total 2 = Sum of all the fields of (Sewing+Labels+Trim/Accessories)
Total 3 = Sum of all the fields of (Embillishments+Packaging+Other Cost)
CMT = SMV Multiplied by RATE
TOTAL= (Total 1 + Total 2 + Total 3) + CMT
FINANCE 6 PERCENTAGE = 6% of TOTAL
FOB USA = TOTAL + FINANCE 6 PERCENTAGE

I have attached the screenshots of Costing template page and also the doctype created for tables.

Please help.

Thanks and Regards,
Sujay