    $(function(){
      $('.rate_container h2').corner('5px');

      $('div#advanced_search_link a').click(function() {
        $('div#advanced_search').slideToggle();
        $('div#advanced_search_link').css({'visibility' : 'hidden'});
        return false;
      });

      $('.county_group').click(function() {
        $(this).parent().parent().find('.list_towns').slideToggle();
        return false;
      })
      
      $('#button-user').click(function() {
        $('#form-taxi').fadeOut('slow',function() {
          $('#button-taxi').removeClass('button-active');
          $('#button-user').addClass('button-active');
          $('#form-user').fadeIn();
        });
        return false;
      });

      $('#button-taxi').click(function() {
        $('#form-user').fadeOut('slow', function() {
          $('#button-user').removeClass('button-active');
          $('#button-taxi').addClass('button-active');
          $('#form-taxi').fadeIn();
        });
        return false;
      });
      
      
      $('#dropdown_county').change(function(){
        $('#list_towns').load( 'edit-locations.html', 'ajaxCounty='+$(this).val())
      });
      
      $(".tooltip-form :input").tooltip({ 
          position: "center right", 
          offset: [-2, 10], 
          effect: "fade", 
          opacity: 0.7, 
          tip: '.tooltip' 
      });
      
      $.facebox.settings.opacity = 0.5;
      
      $('a[rel*=facebox]').facebox();

      $("#check_all_locations").click(function() {
        var checked_status = this.checked;
        $("input[name=postcodes\[\]]").each(function() {
          this.checked = checked_status;
        });
      });

      $('.county_group').click(function() {
        $(this).parent().parent().find('.list_towns').slideToggle();
        return false;
      })
      
      $('.rate_container h2').css({'cursor' : 'pointer'});
      $('.rate_container div:first').css({'display' : 'block'});
      
      $('.rate_container h2').click(function() {
        $(this).parent().find('.form-standard-rate').slideToggle();
      });
      
      $(".sf-menu").superfish({ 
            delay:       1000,                            // one second delay on mouseout 
            animation:   {opacity:'show',height:'show'},  // fade-in and slide-down animation 
            speed:       'normal',                          // faster animation speed 
            autoArrows:  false,                           // disable generation of arrow mark-up 
            dropShadows: true                            // disable drop shadows 
      }); 
      
      
      $('#homepage_feature_image').innerfade({
            animationtype: 'fade',
            speed: 750,
            timeout: 6000,
            type: 'sequence',
            containerheight: '250px'
      }); 

    });


    function update_anti_social() {
      return false;
      switch(document.getElementById('anti_social_selector').value) {
        case '0':
        case '1':
          document.getElementById('anti_social_start').disabled = true;
          document.getElementById('anti_social_end').disabled = true;
          document.getElementById('anti_social_rate_start').disabled = true;
          document.getElementById('anti_social_rate_per_mile').disabled = true;
          break;
        default:
          document.getElementById('anti_social_start').disabled = false;
          document.getElementById('anti_social_end').disabled = false;
          document.getElementById('anti_social_rate_start').disabled = false;
          document.getElementById('anti_social_rate_per_mile').disabled = false;
      }
    }
    
    function removeFormField(vehicle_id, id) {
      $('#'+vehicle_id+'_'+id).slideUp();
      id = id.substring(3);
      $.post('edit-customised-rates.php', 'ajaxRemove='+id);
    }

    function removeSearch(id) {
      $('#row'+id).remove();
      $.post('saved-searches.php', 'ajaxRemove='+id);
    }

    function removeLocation(id) {
      $(id).slideUp();
      id = id.substring(5);
      $.post('edit-locations.php', 'ajaxRemove='+id);
    }

    function limitCharacters(el,limit) {
      if (el.value.length > limit) {
        el.value = el.value.substring(0,limit);
      }
    }
    
    
    function addFormField(vehicle_id) {
      var id = document.getElementById(vehicle_id+"_id").value;
      
      var gridContent = '<tr class="grid-row-data" id="'+vehicle_id+'_row' + id + '">'
      + ' <th> ' + id + ' </th>'
      + ' <td><input type="text" size="40" name="rate_from['+vehicle_id+'][]" id="rate_from' + id + '" class="town_postcode_autocomplete" title="Enter the first location - the pickup point" /></td>'
      + ' <td><input type="text" size="40" name="rate_to['+vehicle_id+'][]" id="rate_to' + id + '" class="town_postcode_autocomplete" title="Enter the second location - the destination" /></td>'
      + ' <td><input type="text" size="5" name="rate_standard['+vehicle_id+'][]" id="rate_standard' + id + '" value="0.00" title="Enter the standard price for this fare. This should be the price for the whole fare including starting rate and rate per mile" /></td>'
      + ' <td><input type="text" size="5" name="rate_anti_social['+vehicle_id+'][]" id="rate_anti_social' + id + '" value="0.00" title="Enter the anti-social price for this fare. This should be the price for the whole fare including starting rate and rate per mile. If not available, enter 0 here." /></td>'
      + ' <td><input type="checkbox" name="rate_both_ways['+vehicle_id+'][]" id="rate_both_ways' + id + '" value="1" title="Should the fare be the same if passenger is travelling from the second location to the first location?" /></td>'
      + ' <td><a href="#" onClick="removeFormField('+vehicle_id+',\'row' + id + '\'); return false;"><img src="images/delete.png" /></a></td>'
      + '</tr>';

      $('#vehicle_'+vehicle_id+' tbody').append(gridContent);

      
      setAutocompleteTowns('.town_postcode_autocomplete');

      $("#" + vehicle_id + "_row" + id + " :input[type=text]:first").focus();
      
      id = (id-1) + 2;
      document.getElementById(vehicle_id+'_id').value = id;
      return false;

    }





    (function($)
    {
      $.fn.blink = function(options)
      {
        var defaults = { delay:1000 };
        var options = $.extend(defaults, options);

        return this.each(function()
        {
          var obj = $(this);
          setInterval(function()
          {
            if($(obj).css("visibility") == "visible")
            {
              $(obj).css('visibility','hidden');
            }
            else
            {
              $(obj).css('visibility','visible');
            }
          }, options.delay);
        });
      }
    }(jQuery))
