    var counter = 0;
    function province_changed ()
        {
            disable ( $('select#region') );
            $('select#region').val ( "" );

            if ( $('select#province' ).val () != "" )
            {
                var url = "/main/filter_regions";
                var params = 'province_id=' + $('select#province' ).val();

                $.ajax (
                    {
                        type: "POST",
                        url: url,
                        data: params,
                        complete: function ( xml, msg )
                        {
                        },
                        success: function ( data )
                        {
                            $('select#region').html ( data );
                            $('select#region').val ( "" );
                            enable ( $('select#region') );
                        }
                    }
                );
            }
        }

    function build_branches ( area_id, area_name, tel , counter )
        {
            return "\t\t\t\t\t<div id=\"branch_" + counter + "\" class=\"branch_row\" >\n\t\t\t\t\t\t<input type=\"hidden\" class=\"provider_area\" name=\"provider_area[]\" value=\"" + area_id + "\" />\n\t\t\t\t\t\t<input type=\"hidden\" class=\"area_name\" value=\"" + area_name + "\" />\n\t\t\t\t\t\t<input type=\"hidden\" class=\"provider_telephone\" name=\"provider_telephone[]\" value=\"" + tel + "\" />\n\t\t\t\t\t\t<div>" + counter + " - " + area_name + " - Tel: "+ tel + "</div><div id=\"remove_" + counter + "\" class=\"remove\" >[Remove]</div>\n\t\t\t\t\t</div>\n";
        }

    function bind_remove ( counter )
        {
            $('div#remove_'+counter).click( function ()
                {
                    var provider_area = new Array();
                    var area_name = new Array();
                    var provider_telephone = new Array();
                    var i = 0;
                    var k = 0;
                    $(this).parent().siblings().children().each ( function ()
                        {
                            if ( k == 3 ) k = 0;
                            if ( $(this).val() != '' )
                            {
                                if ( $(this).attr ('class') == 'provider_area' )
                                {
                                    provider_area [i] = $(this).val();
                                    k++;
                                }
                                if ( $(this).attr ('class') == 'area_name' )
                                {
                                    area_name [i] = $(this).val();
                                    k++;
                                }
                                if ( $(this).attr ('class') == 'provider_telephone' )
                                {
                                    provider_telephone [i] = $(this).val();
                                    k++;
                                }
                            }
                            if ( k == 3 ) i++;
                        });

                    $('div#list_branches').empty( function () { console.debug ( counter ) } );

                    for (j=0; j<=provider_area.length-1;j++)
                    {
                        $('div#list_branches').append ( build_branches ( provider_area[j], area_name[j], provider_telephone[j], j+1 ) );
                        bind_remove (j+1);
                    }
                });
        }


    function selection_changed ()
        {
            var age_count = $("#age_group .short_list tr.data_row").size ();

            var cat_count = $("#categories .short_list tr.data_row").size ();

            if ( age_count > 0 )
            {
                $(".buttons_table .button_wrapper.add_button").removeClass ( "disabled" );
                $(".buttons_table .button_wrapper.reset_button").removeClass ( "disabled" );
            } else {
                $(".buttons_table .button_wrapper.add_button").addClass ( "disabled" );
                $(".buttons_table .button_wrapper.reset_button").addClass ( "disabled" );
            }
            if ( cat_count > 0 )
            {
                $(".buttons_table .button_wrapper.add_button").removeClass ( "disabled" );
                $(".buttons_table .button_wrapper.reset_button").removeClass ( "disabled" );
            } else {
                $(".buttons_table .button_wrapper.add_button").addClass ( "disabled" );
                $(".buttons_table .button_wrapper.reset_button").addClass ( "disabled" );
            }
        }

    function validate()
        {
            $('div#errors').hide();
            $('p#ecompany').hide();
            $('p#eperson').hide();
            $('p#etel').hide();
            $('p#eintro').hide();

            var errors = '';

            if ( $('input#company').val() == '' )
            {
                errors += "Company\n";
                $('div#errors').show();
                $('p#ecompany').show();
            }

            if ( $('input#person').val() == '' )
            {
                errors += "Contact person\n";
                $('div#errors').show();
                $('p#eperson').show();
            }

            if ( $('input#tel').val() == '' )
            {
                errors += "Telephone number\n";
                $('div#errors').show();
                $('p#etel').show();
            }

            if ( $('textarea#introduction').val() == '' )
            {
                errors += "Introduction\n";
                $('div#errors').show();
                $('p#eintro').show();
            }

            if ( errors != '' )
            {
                alert ( "The following fields are required!\n" + errors );
                return false;
            }

            return true;
        }

    function form_post()
        {
            var url = "/directory/add_provider_post";
            var params = $('form#frm_add_provider').serialize()

            $.ajax (
                {
                    type: "POST",
                    url: url,
                    data: params,
                    complete: function ( xml, msg )
                    {
                    },
                    success: function ( data )
                    {
                        $('div#lists').remove ();
                        $('div#errors').remove ();
                        $('form#frm_add_provider').html (data);
                    }
                }
            );
            return false;
        }

    $(function() { $('input[@type=radio].star').rating(); });

    $(document).ready ( function ()
        {
            disable ( $('select#region') );

            $('select#province').change ( function () { province_changed(); } );

            $('input#add_branch').click ( function ()
                {
                    if ( $('div#list_branches').html() == '' ) counter = 0;

                    if ( $('select#region').val() != '' && $('select#region').val() != 'Area (optional) &raquo;' && $('select#region').val() != 'Area (optional) &nbsp;' )
                    {
                        if ( $('input#provider_tel').val() != '' )
                        {
                            var area_id = $('select#region').val();
                            var area_name = $('select#region option:selected').html();
                            var tel = $('input#provider_tel').val();
                            counter++;

                            $('div#list_branches').append ( build_branches ( area_id, area_name, tel , counter ) );

                            bind_remove ( counter );
                        }
                        else
                        {
                            alert ('Please capture a provider \ntelephone number in order \nto add a branch!');
                        }
                    }
                    else
                    {
                        alert ('Please select a region and capture a \nprovider telephone number in order \nto add a branch!');
                    }
                    return false;
                });

            $('input#provider_tel').focus ( function ()
                {
                    if ( $('input#provider_tel').val() == 'Telephone number &raquo;' || $('input#provider_tel').val() == 'Telephone number »')
                    {
                        $('input#provider_tel').val ('');
                    }
                }
                ).blur ( function ()
                    {
                        if ( $('input#provider_tel').val() == '' )
                        {
                            $('input#provider_tel').val('Telephone number »');
                        }
                    });

            $.multiselect ( "#age_group", "/main/age_groups", {
                onchange: function () {
                    selection_changed ();
                }
            } );

            $.multiselect ( "#categories", "/main/categories", {
                onchange: function () {
                    selection_changed ();
                }
            });

            $('form#frm_add_provider').submit ( function ()
                {
                    if ( validate () == true )
                    {
                        return true;
                    }
                    return false;
                });

        });
