
    // =======================================================================
    // MOMO WINES:ORDER.JS
    // =======================================================================

    // -----------------------------------------------------------------------
    // Support Routines...
    // -----------------------------------------------------------------------

    function trim12 (str) {
	    var	str = str.replace(/^\s\s*/, ''),
		    ws = /\s/,
    		i = str.length;
	    while (ws.test(str.charAt(--i)));
    	return str.slice(0, i + 1);
    }
    //  From: http://blog.stevenlevithan.com/archives/faster-trim-javascript

    // -----------------------------------------------------------------------
    // Global Constants/Variables...
    // -----------------------------------------------------------------------

    var highlight_colour = '#FFFFDD' ;
    var error_colour     = '#FFDDDD' ;
    var ok_colour        = '#FFFFFF' ;

    // -----------------------------------------------------------------------

    var ignore_onblur = false ;
            //  The total amount is updated whenever a quantity field is
            //  exited (onblur).  But sometimes (such as when focusing
            //  another field), we don't want to do ths.

    // -----------------------------------------------------------------------

    function get_freight_cost() {
        return 9 ;      //  Freight is currently a fixed $9.00
    }

    // -----------------------------------------------------------------------

/*
    var bottles_and_cases = [
            'riesling'          ,
            'chardonnay'        ,
            'pinot_noir'        ,
            'sauvignon_blanc'   ,
            'pinot_gris'
            ] ;

    var special_offers = [
            'christmas_pack'
            ] ;
*/

    // -----------------------------------------------------------------------

    var total_amount , total_amount_element ;

    // -----------------------------------------------------------------------
    // get_quantity
    // -----------------------------------------------------------------------

    function get_quantity( field_name , question_alert ) {

        // -------------------------------------------------------------------
        // Returns:-
        //      --  #cases/bottles (0+), or;
        //      --  FALSE if value invalid (error message issued)
        // -------------------------------------------------------------------

        if ( ! document.order_form.elements[ field_name ] ) {
//          alert( 'ERROR: No such field!\n\nThe missing field is:  ' + field_name ) ;
//          return false ;
            return 0 ;
                //  If the field doesn't exist, then we assume that this is
                //  because the item concerned is sold out.
        }

        var quantity = trim12( document.order_form.elements[ field_name ].value ) ;

        quantity = quantity.replace( /^[0]+/g , '' ) ;
            //  Strip leading zeros...

        if (    quantity === ''
                ||
                quantity === '0'
                ||
                quantity === '-'
                ||
                quantity.toLowerCase() === 'none'
            ) {
            document.order_form.elements[ field_name ].value = '' ;
            document.order_form.elements[ field_name ].style.backgroundColor = ok_colour ;
            return 0 ;
        }

        if ( /[^0-9]/.test( quantity ) ) {

            if ( question_alert ) {

                document.order_form.elements[ field_name ].style.backgroundColor = highlight_colour ;

                ignore_onblur = true ;

                document.order_form.elements[ field_name ].focus() ;

                alert( 'Bad quantity!\n\nPlease either leave the field blank,\nor enter a number; 0, 1, 2, 3, etc...' ) ;

                document.order_form.elements[ field_name ].style.backgroundColor = '#FFFFFF' ;

                ignore_onblur = false ;

                return false ;

            } else {

                document.order_form.elements[ field_name ].style.backgroundColor = error_colour ;

                return 0 ;

            }

        }

        document.order_form.elements[ field_name ].value = quantity ;
        document.order_form.elements[ field_name ].style.backgroundColor = ok_colour ;
        return quantity ;

    }

    // -----------------------------------------------------------------------
    // get_product_price
    // -----------------------------------------------------------------------

    function get_product_price( price_name , question_alert ) {

        // -------------------------------------------------------------------
        // Returns:-
        //      --  The price per case/bottle, as a float.
        //      --  FALSE if value invalid (error message issued)
        // -------------------------------------------------------------------

        var price_el = document.getElementById( price_name ) ;

        // -------------------------------------------------------------------

        if ( ! price_el ) {
//          alert( 'ERROR: No such price!\n\nThe missing product price is:  ' + price_name ) ;
//          return false ;
            return 0.0 ;
                //  If the price doesn't exist, then we assume that this is
                //  because the item concerned is sold out.
        }

        // -------------------------------------------------------------------

        var price = price_el.innerHTML ;
            //  Should get (eg); $299

        // -------------------------------------------------------------------

        price = price.replace( /\$/ , '' ) ;
            //  Get rid of the leading "$" (if there is one)...

        // -------------------------------------------------------------------

        return parseFloat( price ) ;
            //  NOTE!
            //  -----
            //  Currently, the prices are all integer.  But we use
            //  parseFloat() instead of parseInt() - so that things will
            //  still work if prices like "$123.45" are used.

        // -------------------------------------------------------------------

    }

    // -----------------------------------------------------------------------
    // update_total
    // -----------------------------------------------------------------------

    function update_total() {
        return _update_total( false ) ;
    }

    // -----------------------------------------------------------------------
    // _update_total
    // -----------------------------------------------------------------------

    function _update_total( question_alert ) {

        // -------------------------------------------------------------------

        if ( ignore_onblur ) {
            return true ;
        }

        // -------------------------------------------------------------------

        var i , j , quantity , product_price ;

        total_amount = 0 ;

        // -------------------------------------------------------------------
        // BOTTLES & CASES
        // -------------------------------------------------------------------

        for ( i=0 , j=bottles_and_cases.length ; i<j ; i++ ) {

            // ---------------------------------------------------------------
            // Cases...
            // ---------------------------------------------------------------

            quantity = get_quantity( bottles_and_cases[i] + '_cases' , question_alert ) ;

            if ( question_alert && quantity === false ) {
                return false ;
            }

            // ---------------------------------------------------------------

            if ( quantity > 0 ) {

                // -----------------------------------------------------------

                product_price = get_product_price( bottles_and_cases[i] + '_case_price' , question_alert ) ;

                if ( question_alert && product_price === false ) {
                    return false ;
                }

                // -----------------------------------------------------------

                total_amount += quantity * product_price ;

                // -----------------------------------------------------------

            }

            // ---------------------------------------------------------------
            // Bottles...
            // ---------------------------------------------------------------

            quantity = get_quantity( bottles_and_cases[i] + '_bottles' , question_alert ) ;

            if ( question_alert && quantity === false ) {
                return false ;
            }

            // ---------------------------------------------------------------

            if ( quantity > 0 ) {

                // -----------------------------------------------------------

                product_price = get_product_price( bottles_and_cases[i] + '_bottle_price' , question_alert ) ;

                if ( question_alert && product_price === false ) {
                    return false ;
                }

                // -----------------------------------------------------------

                total_amount += quantity * product_price ;

                // -----------------------------------------------------------

            }

            // ---------------------------------------------------------------

        }

        // -------------------------------------------------------------------
        // SPECIAL OFFERS
        // -------------------------------------------------------------------

        for ( i=0 , j=special_offers.length ; i<j ; i++ ) {

            // ---------------------------------------------------------------

            quantity = get_quantity( special_offers[i] , question_alert ) ;

            // ---------------------------------------------------------------

            if ( question_alert && quantity === false ) {
                return false ;
            }

            // ---------------------------------------------------------------

            if ( quantity > 0 ) {

                // -----------------------------------------------------------

                product_price = document.order_form.elements[ special_offers[i] + '_price' ].value ;

                // -----------------------------------------------------------

                if ( ! product_price ) {

                    if ( question_alert ) {
                        alert( 'ERROR: No price!\n\nThe unpriced special offer is : ' + special_offers[i] ) ;
                    }

                } else {

                    total_amount += quantity * parseFloat( product_price ) ;

                }

                // -----------------------------------------------------------

            }

            // ---------------------------------------------------------------

        }

        // -------------------------------------------------------------------

        if ( total_amount > 0 ) {
            total_amount += get_freight_cost() ;
        }

        // -------------------------------------------------------------------

        total_amount = total_amount.toFixed(2) ;
            //  Make sure only/exactly two decimal places...

        // -------------------------------------------------------------------

        if ( ! total_amount_element ) {
            total_amount_element = document.getElementById( 'total_amount' ) ;
        }

        // -------------------------------------------------------------------

        if ( total_amount_element ) {
            total_amount_element.innerHTML = total_amount ;
        }

        // -------------------------------------------------------------------

        return true ;

        // -------------------------------------------------------------------

    }

    // -----------------------------------------------------------------------
    // submit_button_handler
    // -----------------------------------------------------------------------

    function submit_button_handler() {

        // -------------------------------------------------------------------

        var result = _update_total( true ) ;

        // -------------------------------------------------------------------

        if ( result === false ) {
            return ;
        }

        // -------------------------------------------------------------------

        if ( total_amount == 0 ) {

            var i , j ;

/*
            for ( i=0 , j=bottles_and_cases.length ; i<j ; i++ ) {
                el = document.order_form.elements[ bottles_and_cases[i] + '_cases' ] ;
                if ( el ) {
                    el.style.backgroundColor = highlight_colour ;
                }
                el = document.order_form.elements[ bottles_and_cases[i] + '_bottles' ] ;
                if ( el ) {
                    el.style.backgroundColor = highlight_colour ;
                }
            }
*/

            for ( i=0 , j=document.order_form.elements.length ; i<j ; i++ ) {
                if ( document.order_form.elements[i].onblur ) {
                    document.order_form.elements[i].style.backgroundColor = highlight_colour ;
                } else {
                    if ( document.order_form.elements[i].type !== 'hidden' ) {
                        break ;
                    }
                }
            }

            ignore_onblur = true ;

            document.order_form.elements[0].focus() ;

            if ( special_offers.length > 0 ) {
                alert( 'You haven\'t ordered anything!\n\nPlease specify the number of bottles, cases and/or special offers required.' ) ;
            } else {
                alert( 'You haven\'t ordered anything!\n\nPlease specify the number of bottles and/or cases required.' ) ;
            }

/*
            for ( i=0 , j=bottles_and_cases.length ; i<j ; i++ ) {
                el = document.order_form.elements[ bottles_and_cases[i] + '_cases' ] ;
                if ( el ) {
                    el.style.backgroundColor = '#FFFFFF' ;
                }
                el = document.order_form.elements[ bottles_and_cases[i] + '_bottles' ] ;
                if ( el ) {
                    el.style.backgroundColor = '#FFFFFF' ;
                }
            }
*/

            for ( i=0 , j=document.order_form.elements.length ; i<j ; i++ ) {
                if ( document.order_form.elements[i].onblur ) {
                    document.order_form.elements[i].style.backgroundColor = ok_colour ;
                } else {
                    if ( document.order_form.elements[i].type !== 'hidden' ) {
                        break ;
                    }
                }
            }

            ignore_onblur = false ;

            return ;

        }

        // -------------------------------------------------------------------

//alert( total_amount ) ;

        // -------------------------------------------------------------------

        document.order_form.elements[ 'scAMOUNT' ].value = total_amount ;

        // -------------------------------------------------------------------

        document.order_form.submit() ;

        // -------------------------------------------------------------------

    }

    // -----------------------------------------------------------------------

    setTimeout( 'document.order_form.elements[0].focus()' , 1000 ) ;
        //  Focus the first element in the form...

    // -----------------------------------------------------------------------


