var visible = 'view';

function toggleForm()
{
    if (visible == 'view')
    {
        $('#view').fadeOut(1000);
        $('#form').fadeIn(1000);
        
        visible = 'form';
    }
    else
    {
        $('#view').fadeIn(1000);
        $('#form').fadeOut(1000);
        
        visible = 'view';
    }
}

// Auto-fire actions after document is loaded
$(document).ready(function ()
{
    try
    {
        // Handle by event
        switch (PARAMS[2])
        {
            case 'fishingtournament':
                
                //
                // Private functions
                //
                function getTotal()
                {
                    var data = {};
                    
                    // T's
                    data.tshirts = $('#tshirt_small').val() + ',' +
                                   $('#tshirt_medium').val() + ',' +
                                   $('#tshirt_large').val() + ',' +
                                   $('#tshirt_xlarge').val();
                                   
                    // Tournament type
                    data.tournamentType = $('input[name=tournament_type]:checked').val();
                    if ($('input[name=tournament_type]:checked').val() == 'offshore')
                    {
                        data.offshoreTournamentType = $('input[name=offshore_tournament_type]:checked').val();
                    }
                    else
                    {
                        data.inshoreTournamentAgeGroup = $('input[name=inshore_age_group]:checked').val();
                    }
                    
                    $.post(CURRENT_URL + 'event/calculate/fishingtournament/', data, function (response)
                    {   
                        $('#total_amount').html('$' + response);
                    });
                }
                
                //
                // Bind events
                //
                
                // Tournament type
                $('input[name=tournament_type]').click(function ()
                {
                    if (this.value == 'offshore')
                    {
                        $('#inshore_options').hide();
                        $('#offshore_options').show();
                    }
                    else
                    {
                        $('#inshore_options').show();
                        $('#offshore_options').hide();
                    }
                });
                
                // Total ammount calculation
                $('input[class=price]').click(getTotal);
                $('select[class=price]').change(getTotal);
                $('input[id="first_name"]').change(getTotal); // sometimes the total does not start correctly, Added first name change to recalculate again
                
                // Form submit
                $('#form form').submit(function()
                {
                    var fields = {};
                    $(this).find('input[type!=submit][type!=reset],select').each(function (element)
                    {
                        if (fields[this.name] == undefined)
                        {
                            switch (this.type)
                            {
                                case 'text':
                                case 'password':
                                case 'select-one':
                                    fields[this.name] = this.value;
                                    break;
                                
                                case 'radio':
                                    fields[this.name] = $('input[name=' + this.name + ']:checked').val();
                                    break;
                            }
                        }
                        
                    });
                    
                    // Validate form
                    for (var fieldName in fields)
                    {
                        $('input[name=' + fieldName + ']').css('border-color', ''); // Set normal border
                        var fieldValue = fields[fieldName];
                        var error = false;
                        
                        switch (fieldName)
                        {
                            
                            // Length
                            case 'first_name':
                            case 'last_name':
                            case 'address':
                            case 'city':
                            case 'state':
                            case 'zip':
                            //case 'boat_name':
                            //case 'boat_length':
                            //case 'boat_registration_number':
                            //case 'port_of_call':
                            //case 'where_docked':
                            //case 'captains_full_name':
                            //case 'captains_phone':
                            //case 'captains_email':
                            //case 'angler_1':
                                
                                if (fieldValue.length < 2)
                                {
                                    $('#form input[name=' + fieldName + ']').css('border-color', 'red');
                                    error = true;
                                }
                                
                                break;
                            
                            case 'email':
                                
                                if (fieldValue.length < 5)
                                {
                                    $('#form input[name=' + fieldName + ']').css('border-color', 'red');
                                    error = true;
                                }
                                break;
                        }
                    }
                    
                    if (!error)
                    {
                        $.post(CURRENT_URL + 'event/save/fishingtournament/', fields, function (response)
                        {   
                            if (response.error)
                            {
                                try
                                {
                                    for (var fieldName in response.errorFields)
                                    {
                                        $('#form input[name=' + fieldName + ']').css('border-color', 'red');
                                    }
                                }
                                catch (e)
                                {
                                    // Do nothing
                                }
                            }
                            else
                            {
                                redirect(response.redirect_url);
                            }
                        }, 'json');
                    }
                    return false;
                });
                
                $('input[id=offshore_tournament]').click();
                $('input[name=offshore_tournament_type]:first').click();
                break;
        }
    }
    catch (e)
    {
        alert('Error: ' + e);
    }
});
