Urgent help:names.nsf authentication problems

Hi,I have a problem with authentication.we have a webapplication.Authentication is done using through agents in welcome.nsf .With the referer we validate and redirects to names.nsf and then to home page.We also use the custom login form in domcfg.This works fine but the problem is when someone tries to access the names.nsf directly passing username and password the same goes to our homepage they are able to access it.Please find code below.

To restrict this I have put some javascript code in custom login form onload event which redirects to some other page. The problem iam facing is before my onload event is fired the reason type and reason value of the login form values are populated and redirecting the page to home page.Can anyone tell me what happens when the i hit the names.nsf with the correct user id and password.

a)Will that hit custom login form?

b)Can i load the javascript first rather than the login form fields so that i can redirect by checking the referer from where the URL is being passed.

Thanks in advance!!!