// this code is required to suppress the Enter key when used in a Text or Select ??or TextArea?? field
// nominally, the key would be replaced by Tab, but that is not browser-independent.
//
// so, we will try to shift focus to the next field in Tab Order

document.onkeypress = ReplaceEnterKeyWithTab;

function ReplaceEnterKeyWithTab(e)  {
    var evt = (e) ? e : ((event) ? event : null);
    var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
 //alert("Enter key rtn");
    if ((evt.keyCode == 13) ) {
 //alert("Enter key detected");
        if (TextTypeField(node,false))  {
// alert("current field is text type");
            var targ = NextInTabOrder(node);
            if (!(targ == null))  {
// alert("moving focus to "+targ.name);
                 targ.focus();
                 }
            return false;
            }
        }
    return true;
    }


function TextTypeField(fld, asDest)  {
    // is this a suitable field for 'Enter' replacement ...
    // pressing Enter within a select or text field should get treated as a Tab
    // ... in which case find the next text-type field to land in
    if (fld.type.toLowerCase() == "text") return true;
    if (fld.type.toLowerCase() == "password") return true;
    if (asDest) { if (fld.type.toLowerCase() == "textarea") return true; }
    if (fld.type.substr(0,6).toLowerCase() == "select") return true;
    return false;
    }


function NextInTabOrder(fld)  {
    // find next in tab order in document after the given field fld
    var idx = fld.tabIndex;
    var min_txt_idx=999999, max_txt_idx=-1, nxt_txt_idx=999999, min_txt_fld, max_txt_fld, nxt_txt_fld;
    var frm = fld.form;
    var tgt, result;
    result = null;
    for (var i=0;i<frm.length;i++)  {
        tgt = frm.elements[i];
        if ((TextTypeField(tgt,true)) && (!(tgt.name == fld.name))) {
// alert("Found text field: "+tgt.name+"   idx: "+tgt.tabIndex);
            if (((tgt.tabIndex) > idx) && ((tgt.tabIndex) < nxt_txt_idx))  {
                nxt_txt_idx = tgt.tabIndex;
                nxt_txt_fld = tgt;
                }
            if (tgt.tabIndex > max_txt_idx)  {
                max_txt_idx = tgt.tabIndex;
                max_txt_fld = tgt;
                }
            if (tgt.tabIndex < min_txt_idx)  {
                min_txt_idx = tgt.tabIndex;
                min_txt_fld = tgt;
                }

            }
        }
    if (nxt_txt_idx == 999999)  {
        // nothing higher in tab order ... go back to start
        if (min_txt_idx == 999999)  {
            // no other text field to land on
            return null;
            }
        result = min_txt_fld;
        } else {
        result = nxt_txt_fld;
        }
//    alert("Next in tab order: "+result.name);
    return result;

    }
