;SF.QUICKBEDS = 
{
    $form: null,
    
    fieldMappings: 
    {
        Region: 'Destination'
    },

    init: function()
    {
        this.$form = $('#qb_quickBedsForm');
        this.setupForm();
        this.setupValidation();
    },
    
    setupForm: function()
    {
    
        $('#ac_lastMinuteRadio').attr('checked', true).bind('click', function(e) {
            $('#cdp_calypsoForm').hide();
            SF.QUICKBEDS.$form.show();
        });

        
        $('#ac_yearRoundRadio').bind('click', function(e) {
            //$('#cdp_calypsoForm').show();
            //SF.QUICKBEDS.$form.hide();
            window.open('http://www.octopustravel.com/au/Enter.jsp?siteid=studentau', '_blank');
        });
            
        
        $qbSelect = SF.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>');
               });
            });
        });
        
        SF.QUICKBEDS.$form.find('#qb_CheckIn').val(SF.DATETIME.convertUSFormat(SF.DATETIME.weekFromToday()));
        
        // When number of children in drop down selected, send value of selected item to displayChildLists to display child ages select elements
        SF.QUICKBEDS.$form.find('#qb_childNum').bind('change', function()
        {
            SF.QUICKBEDS.displayChildLists($(this).val());
        });
        
    },   
    
    setChildAgesValue: function()
    {
        // Create ages string based on children's ages
        var numChildAges = SF.QUICKBEDS.$form.find('#qb_childNum').val();
        var childrenAges = '';
        SF.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)
        {
            SF.QUICKBEDS.$form.find('#qb_childAgesContainer').hide();
            return;
        }
        
        var numChildSelects = SF.QUICKBEDS.$form.find('#qb_childAgesContainer select').length;
        var count = numChildSelects;
        while(count >= 1)
        {
            if(count > selectedNumber)
            {
                SF.QUICKBEDS.$form.find('#qb_' + 'child' + count  + 'AgeList').hide();
            }
            else
            {
                SF.QUICKBEDS.$form.find('#qb_' + 'child' + count  + 'AgeList').show();
            }
            count--;
        }
        SF.QUICKBEDS.$form.find('#qb_childAgesContainer').show();
    },
    
    setupValidation: function()
    {
        jQuery.validator.addMethod('qb_childAges', function(value, element, params) 
        {
            var result = true;
            var numChildAges = SF.QUICKBEDS.$form.find('#qb_childNum').val();
            SF.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'
            },
            
            highlight: function(element, errorClass) {
                 $(element).css('background-color', 'LemonChiffon')
              },
                        
            invalidHandler: function(form, validator) {
                var strError ='';
                var errorMessage = '';
                var intErrors = validator.numberOfInvalids();   
                
                for (var formError in validator.errorMap) {
                    strError += SF.QUICKBEDS.fieldMappings[formError] + ': ' + validator.errorMap[formError] + '\n'                               
                }
                if (strError != '') {
                    alert(strError);
                };
                        
            },

            submitHandler: function(form)
            {
                SF.QUICKBEDS.setChildAgesValue();
                form.submit();
            }
        }); 
    }
};
SF.QUICKBEDS.init();
