
function replaceQueryString(url,param,value) {
    var re = new RegExp("([?|&])" + param + "=.*?(&|$)","i");
    if (url.match(re)) {
        return url.replace(re,'$1' + param + "=" + value + '$2');
    }
    else {
        return url + '&' + param + "=" + value;
    }
}

$(function() {
    $('a.formToggle').click(function(){
        var $this = $(this);
        $this.parents('form').hide();
        $(this.hash.replace("#","."),$this.parents('.tab')).show();
        return false;
    });
    $("input:text",'.tab form')
        .each(function(){
            if(this.value === '' && this.title !== '') {
                this.value = this.title;
            }
        })
        .click(function(){
            if(this.value === this.title) {
                this.value = '';
            }
        })
        .blur(function(){
            if(this.value === '') {
                this.value = this.title;
            }
        })
        .bind('validate',function() {
            if(this.value === this.title) {
                this.value = '';
            }
        });
    $('form', '.tab').submit(function() {
        $('input:text', $(this)).trigger('validate');
    });
    
    $('.page_selector','#results').change(function() {
        location.href = replaceQueryString(location.href,'page',$(this).val());
    });
    
    $('.displayFaster').click(function() {
        var checked = $(this).attr('checked');
        var otherCheck = $('.mapSearch');
        if (otherCheck.attr('checked') && checked) {
            $('.mapSearch').attr('checked',null);
        }
        $('.displayFaster').attr('checked',checked);
    });
    $('.mapSearch').click(function() {
        var checked = $(this).attr('checked');
        var otherCheck = $('.displayFaster');
        if (otherCheck.attr('checked') && checked) {
            otherCheck.attr('checked',null);
        }
        $('.mapSearch').attr('checked',checked);
    });
    
    $('#search-by-map').click(function() {
        $("input[name*='loc']").val('y');
        $('#search-button').click();
    });

    $('.tab-link').click(function(e) {
        e.preventDefault();
        var award = $(this).attr('id').substring(4);
        $("input[name='award_id']").val(award);
        $("input[name='inspection_type']").val($("#tab_"+award+"-main > form > input[name='inspection_type']").val());
        $("#search-button").click();
    });
    
    $('.remove-area').click(function() {
        $(this).children().attr('src', '/images/ajax-loader.gif');
        $("input[name*='town_id[]']").remove();
        $('#search-button').click();
    });
    $('.remove-county').click(function(e) {
        $(this).children().attr('src', '/images/ajax-loader.gif');
        $("input[name*='town_id[]']").val($("input[name*='town_id[]']").val().split('-')[0]);
        $('#search-button').click();
    });
    $('.remove-town').click(function(e) {
        $(this).children().attr('src', '/images/ajax-loader.gif');
        $("input[name*='town_id[]'][value*='" + $(this).attr('id').split('-')[2] + "']").remove();
        $('#search-button').click();
    });
    $('.remove-council').click(function() {
        $(this).children().attr('src', '/images/ajax-loader.gif');
        $("input[name*='client_id']").val('');
        $('#search-button').click();
    });
});
 

