thanks for the help guys, i've solved the problem using PL/PgSQL. oncei had "created" the language it was really rather simple.
thanks again