well, yeah, you could get it to work, if you rewrote the script but there are no parameters to be entered, which would allow any control over your script -> not a udf.
sadly, I stared your script for 5 mins and came to this conclusion and gave up. guess how the noobs feel? ;\)

download KiXnet