There are two ways to use alert() to assist in debugging a script that isn't working properly. One way is to use it to trace the path through the code that is followed when the script is run. The second use is to display the values that certain fields contain at selected points within the script.
To use alert() to trace the path followed through the code simply insert alert statements into various spots in your script and give each a different value. Two of the more obvious alternatives are to either just number the alerts - alert(1), alert(2) etc. - or to use letters of the alphabet - alert('a'), alert('b') etc - so that you can tell exactly where in the code that you are when each alert is produced. Provided that you have at least one alert within each function, each if and else, and each loop, you will be able to track the exact path followed through the code and compare that to the path that you would expect the particular test to follow.
Alerts can also be used to check the value held in a particular field at a specific spot in the code. The simplest way to do this is to simply include the name of the field to be examined inside the alert - alert(fieldname) - however if you are also using alerts for trackign the path followed at the same time or are trying to check the value in several fields then having the alert display which field has the value will assist you in distinguishing between alerts - alert('fieldname = ' + fieldname).