(function($){
    samo.freight_monitor = function() {
        var _ROOT_URL = samo.ROUTES.freight_monitor.url,$module_container = $('#freight_monitor'),_controls = $('select,input', $module_container),DOLOAD = (parseInt($.getParameter('DOLOAD',true)) == 1);
        samo.cache_controls($module_container);
        $('.CHECKIN',$module_container).datePicker().mask(Date.mask);
        $('.PLUS_MINUS,.NIGHTS_FROM',$module_container).SpinButton();
        if (DOLOAD) {
            getFreights();
        }
        $('.STATEINC',$module_container).bind('change',function() {
            var $disabled = (0 == $(this).val());
            $('.TOURINC',$module_container).attr('disabled',$disabled);
            if (!$disabled) {
                $.getScript(getParams('STATEINC'));
            }
        });
        $('.TOWNFROMINC',$module_container).bind('change',function() {
            if (0 != $(this).val()) {
                $.getScript(getParams('TOWNFROMINC'));
            }
        });
        $('input,select',$module_container).bind('change',function() {
            $('.resultset',$module_container).empty();
        });

        $('.load',$module_container).bind('click',getFreights);


        function getParams(action) {
            var useGET = arguments[1] || false;
            var result = {};
            result.samo_action = action;
            _controls.each(function(){
                var name = this.name || $(this).attr('name');
                var value = $.controlValue(this,useGET);
                if (name && value && value !== 0)
                    result[name] = value;
            });
            return _ROOT_URL + $.param(result);

        }

        function getFreights() {
            var is_ok = true;
            $.each(['TOWNFROMINC','STATEINC','TOURINC'],function() {
                if (0 == $('.' + this,$module_container).val()) {
                    alert(samo.LANG['NOT_SET_' + this]);
                    $('.' + this,$module_container).get(0).focus();
                    is_ok = false;
                }
            });
            if (is_ok) $.getScript(getParams('FREIGHTS'));
        }

    }
    $('document').ready(samo.freight_monitor);
})(samo.jQuery);