// Variables which must be set by the user.
var firstFocusField = null;
var defaultFirstFocus = null;
var submitAction = "";  // This can be used to set the form action (if initially
                        // set to an empty string) before submitting the page.


// Look up the index of a form element given its name.
function getElementIndex(targetName) {
    for (var i = 0; i<document.forms[0].elements.length; i++) {
        if (document.forms[0].elements[i].name == targetName) {
            return i;
        }
    }
    return -1;
}


// Capture <Enter> keystrokes and treat them as tabs to the next form field.  If
// the form has reached the last field, then do any commands in the submitAction
// variable, then submit the form.
// This is called if the document.onkeypress  value is set to this function.
function enterSubmits(evnt) {

    // evnt is null in IE (use window.event).  Evnt is set for other browsers.
    var myEvent = (evnt) ? evnt : window.event;

    // target is the standard DOM method.  IE uses srcElement instead.
    var myTarget = (myEvent.target) ? myEvent.target : myEvent.srcElement;

    // If enter is captured in a textarea, then it will submit and not allow the
    // user to enter multiple lines of text.
    if ((myTarget.type == "text" || myTarget.type == "select-one" 
            || myTarget.type == "radio")
            && myEvent.keyCode == 13) {
        if (getElementIndex(myTarget.name) == document.forms[0].elements.length - 1) {
            eval(submitAction);
            document.forms[0].submit();
        } else {
            // Advance to next element.  
            // NOTE:  THERE CANNOT BE ANY HIDDEN ELEMENTS EXCEPT AT THE TOP OF THE FORM!
            document.forms[0].elements[getElementIndex(myTarget.name) + 1].focus();
        }
    }
    return true;
}


// Set the focus for the first error field, held in the firstFocusField variable.
// If the firstFocusField is null, then use the defaultFirstFocus variable, 
// containing the name of the first editable field on the form.
function setFirstFocus() {
    if (firstFocusField == null) {
        firstFocusField = defaultFirstFocus;
    }
    if (firstFocusField) {
        eval("document.forms[0]." + firstFocusField + ".focus()");
    }
}


// Set the default first focus field, used whenever there aren't any errors.
function setDefaultFirstFocus(field) {
    if (defaultFirstFocus == null) {
        defaultFirstFocus = field;
    }
}

