;ET.QUICKBEDS = 
{
    $form: null,
    
    fieldMappings: 
    {
        Region: 'Destination'
    },

    init: function()
    {
        this.$form = $('#qb_quickBedsForm');
        this.setupForm();
        this.setupValidation();
    },
    
    setupForm: function()
    {
        $qbSelect = ET.QUICKBEDS.$form.find('#qb_region');
        $.get('/quickbeds/query/regionlist.aspx?q=Quickbeds', function(data)
        {
           $(data).find('Quickbeds QR').each(function()
            {
               var $qbOptgroup = $qbSelect.append('<optgroup label="' + $(this).attr('sRegion') + '">');
               $(this).children().each(function()
               {
                   $qbOptgroup.append('<option value="' +  $(this).attr('lSubRegionId') + '">' + $(this).attr('sSubRegion') + '</option>');
               });
            });
        });
        
        ET.QUICKBEDS.$form.find('#qb_CheckIn').val(ET.DATETIME.convertUSFormat(ET.DATETIME.weekFromToday()));
        
        // When number of children in drop down selected, send value of selected item to displayChildLists to display child ages select elements
        ET.QUICKBEDS.$form.find('#qb_childNum').bind('change', function()
        {
            ET.QUICKBEDS.displayChildLists($(this).val());
        });
        
        // If on stage, change form action to be stage version
        if(ET.getEnv() == 'staging')
        {
            this.$form.attr('action', 'http://hotels.stg.escapetravel.com.au/SearchLink.aspx');
        };
        
        ET.FORMS.replaceSubmitButton(ET.QUICKBEDS.$form.find('.submitButton'));
    },   
    
    setChildAgesValue: function()
    {
        // Create ages string based on children's ages
        var numChildAges = ET.QUICKBEDS.$form.find('#qb_childNum').val();
        var childrenAges = '';
        ET.QUICKBEDS.$form.find('#qb_childAgesContainer select :lt('+ numChildAges +')').each(function()
        {
            childrenAges += $(this).val() + ',';
        });
        $('#qb_Ages').val(childrenAges.substring(0, childrenAges.length-1));
    },
    
    displayChildLists: function(selectedNumber) 
    {
        if(selectedNumber == 0)
        {
            ET.QUICKBEDS.$form.find('#qb_childAgesContainer').hide();
            return;
        }
        
        var numChildSelects = ET.QUICKBEDS.$form.find('#qb_childAgesContainer select').length;
        var count = numChildSelects;
        while(count >= 1)
        {
            if(count > selectedNumber)
            {
                ET.QUICKBEDS.$form.find('#qb_' + 'child' + count  + 'AgeList').hide();
            }
            else
            {
                ET.QUICKBEDS.$form.find('#qb_' + 'child' + count  + 'AgeList').show();
            }
            count--;
        }
        ET.QUICKBEDS.$form.find('#qb_childAgesContainer').show();
    },
    
    setupValidation: function()
    {
        jQuery.validator.addMethod('qb_childAges', function(value, element, params) 
        {
            var result = true;
            var numChildAges = ET.QUICKBEDS.$form.find('#qb_childNum').val();
            ET.QUICKBEDS.$form.find('#qb_childAgesContainer select:lt('+ numChildAges +')').each(function()
            {
                if($(this).val() == -1)
                {
                    result = false;
                    return false;
                }
            });
            
            return result;
        }, jQuery.format('Not all child ages have been specified. Please provide missing childs age'));
        
        // Once validation passes, set destination to match in intDestinations object
        this.$form.validate(
        {
            rules: 
            {
                Region: 'required',
                Ages: 'qb_childAges'
            },
            
            invalidHandler: function(e, validator) 
            {
               ET.FORMS.invalidForm(e, validator, ET.QUICKBEDS.fieldMappings);
            },

            submitHandler: function(form)
            {
                ET.QUICKBEDS.setChildAgesValue();
                ET.QUICKBEDS.$form.trigger('replaceSubmit');
                form.submit();
            }
        }); 
    }
};
ET.QUICKBEDS.init();
