How to write custome client and server side script

I call a function when mouse blur from one field.
I have two fields like as category and mat code.
The mat code is like as(Category prefix+count)
i want max of counts+1 in particular category wise
ex:If i select FP category
Mat code is:FP003
if i select Rm category
Mat code is Rm005

Please read this

I think people can help you on the forum if you are stuck, can’t write solutions for you.