//**************************************************************************
// JavaScript utility functions
//
// checkRadio(s) - determine if a radio option has been selected,
//                 returns index of selected option or -1 if no selection
// formatDecimals(s) - format an amount, return with two decimal positions
// goToNextForm(s [,s]) - go to the next .html or .jsp form
// isDomBrowser - determine if a browser is W3C DOM-capable
//
//**************************************************************************
// Copyright (c) 2002, Bits & Bytes Programming, Inc.
// ALL RIGHTS RESERVED
// http://www.web400.com
//**************************************************************************
// You may freely use these utilities in any programs or files that 
// you develop, provided that you include the copyright notice 
// shown above.
//**************************************************************************
   

        //****************************************************************
        // determine if a radio option has been selected
        //
        // arguments
        //   radio - name of the radio button set to check
        //
        // returns
        //   index of selected option (0..n)
        //   -1 if no selected option
        //****************************************************************
        function checkRadio(radio) {
        
            for (var i=0; i < radio.length; i++) {
            
                if (radio[i].checked) {
                    return i;
                }
            }
            
            return -1;
        }


        //****************************************************************
        // format an amount so that it is returned with two decimal
        // positions.
        //
        // arguments 
        //   amount - amount to be formatted. May already contain
        //            decimal point and decimal place digits.
        //****************************************************************
        function formatDecimals(amount) {
        
            var regexpDec2 = /[.]\d\d/;
            var regexpDec1 = /[.]\d/;
            var regexpDec0 = /[.]/;
        
            if (regexpDec2.test(amount)) {
                return amount;
            }
            
            else if (regexpDec1.test(amount)) {
                return amount + "0";
            }
                
            return amount + ".00";
        }
        

        //****************************************************************
        // go to the next form
        //
        // arguments
        //    formName - name of the form to go to
        //    fromForm - (optional) name of form submitting from, if more 
        //               than one form in the <body> section.
        //               If the fromForm name is not supplied, forms[0]
        //               (the first or only form) is used.
        //****************************************************************
        function goToForm(formName, fromForm) {
        
            var form = fromForm ? fromForm : 0;
            
            document.forms[form].action = formName;
            document.forms[form].submit();
        }


        //****************************************************************
        // isDomBrowser - determine if the reported user agent (browser)
        //                is a W3C DOM-capable browser
        //
        // returns
        //    true  - browser is one of those known to be DOM capable
        //            MSIE 5+
        //            Netscape6 (function does not test for other verions)
        //    false - browser is not known to be DOM capable
        //****************************************************************
        
        function isDomBrowser() {

            var ua         = navigator.userAgent;
            var msie       = ua.indexOf('MSIE');
            var netscape   = ua.indexOf('Netscape6');
            var opera      = ua.indexOf('Opera');            
            
            //************************************************************
            // Opera - reports as MSIE, but not fully DOM-capable
            //************************************************************            
            if (opera > 0) {
                return false;
            }

            //************************************************************
            // Microsoft Internet Explorer - check for major version >= 5
            //************************************************************            
            if (msie > 0) {
            
                var majorVersion = parseInt(ua.substring(msie + 5, ua.indexOf(".", msie)));
                
                if (majorVersion >= 5) {
                    return true;
                }
                                    
                else {
                    return false;
                }
            }

            //************************************************************
            // Netscape6 
            //************************************************************            
            if (netscape > 0) {
                return true;
            }

            //************************************************************
            // Not MSIE 5+ or Netscape6, not known to be DOM-capable
            //************************************************************            
            return false;
        }                             
        
