Didn't look very closely at your code. Mostly because its horribly unorganized and difficult to read.
Cookies are included in HTTP headers, therefore, setcookie may not be called after output has been sent from the script to the browser (as in echo, print or whatever else). Either call setcookie before output from the script or use output control functions to buffer script output.
RTFM!
http://www.php.net/setcookie
http://www.php.net/outcontrol
If this does nothing to correct the problem, make sure cookies are enabled in your browser (they're turned off by default in MSIE 6).
: )
Rich
::::::::::::::::::::::::::::::::::::::::::
The Spicy Peanut Project
http://www.spicypeanut.net
::::::::::::::::::::::::::::::::::::::::::