(function($){
    samo.hotel_stopsale = function() {
        var _ROOT_URL = samo.ROUTES.hotel_stopsale.url,$module_container = $('#hotel_stopsale'),_controls = $('select,input', $module_container), DOLOAD = (parseInt($.getParameter('DOLOAD',true)) == 1);
        samo.cache_controls($module_container);
        $('select,input',$module_container).bind('change',function(){
            $('.resultset',$module_container).empty();
        });
        $('.TOWNFROMINC',$module_container).bind('change',function(){
            $('.STATEINC').find('option').remove();
            $('.TOWNTO').find('option').remove();
            if (this.value != 0) {
                $.getScript(getParams('TOWNFROMINC'),true);
            }
        });
        $('.STATEINC',$module_container).bind('change',function(){
            $('.TOWNTO').find('option').remove();
            if (this.value != 0) {
                $.getScript(getParams('STATEINC'),true);
            }
        });
        samo.loadBtn.bind('click',function(){
            if (validateFields()) {
                $.getScript(getParams('HOTELLIST'),true);
            }
        });
        $('.resultset',$module_container).listen('click','.link',function(){
            var hotel = $(this).attr('samo:HOTELINC');
            $(this).parent().parent().attr('id','hotel_' + hotel);
            var result = {HOTELINC: hotel, STATEINC: $('.STATEINC',$module_container).val(), samo_action: 'STOPSALES'};
            $.getScript(_ROOT_URL + $.param(result));
        });
        function validateFields() {
            if (!$.controlValue('STATEINC',false,$module_container)) {
                alert(samo.LANG.NOT_SET_STATEINC);
                return false;
            }
            return true;
        }
        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);

        }
    }
    $(document).ready(samo.hotel_stopsale);
})(samo.jQuery);
