
var url = '';

if ( $('data_individual_type_code') != undefined ) {
    $('data_individual_type_code').observe( 'change', function(event) {
        gather_url();
        new Ajax.Request(url, { asynchronous: 1, onSuccess: process, onFailure: error_set });
    });
}

if ( $('data_specialisation') != undefined ) {
    $('data_specialisation').observe( 'change', function(event) {
        gather_url();
        new Ajax.Request(url, { asynchronous: 1, onSuccess: process, onFailure: error_set });
    });
}

if ( $('data_towncode') != undefined ) {
    $('data_towncode').observe( 'change', function(event) {
        gather_url();
        new Ajax.Request(url, { asynchronous: 1, onSuccess: process, onFailure: error_set });
    });
}

if ( $('data_within_dist') != undefined ) {
    $('data_within_dist').observe( 'change', function(event) {
        gather_url();
        new Ajax.Request(url, { asynchronous: 1, onSuccess: process, onFailure: error_set });
    });
}

// Always refresh for business, just in case things got carried over

if ( '' == '1' )
{
    gather_url();
    new Ajax.Request(url, { asynchronous: 1, onSuccess: process, onFailure: error_set });
}


function error_set() {
    alert( "There seems to be a problem with the search. Please try changing your search criteria." );
}

function gather_url() {

    var base_url = 'http://www.lawyerlocator.co.uk/lawyer/lawyer_search/search_number/';

    // ajax loader
    var home_search_firm_counter = document.getElementById( 'home_search_firm_counter' );
    home_search_firm_counter.innerHTML = '<img src="http://www.lawyerlocator.co.uk/img/ajax-loader2.gif" /><span class="font_2" style="color: #000000;">&nbsp;Finding Lawyers...</a>';
    
    // skill type, e.g. lawyer or barrister
    var data_individual_type_code_obj = document.getElementById( 'data_individual_type_code' );
    
    if ( data_individual_type_code_obj != undefined ) {
        var data_individual_type_code = data_individual_type_code_obj.options[data_individual_type_code_obj.selectedIndex].value
        if ( data_individual_type_code != -1 ) {
            base_url += 'individual_type_code/' + data_individual_type_code + '/';
        }
    }

    // specialisation
    var data_specialisation_obj = document.getElementById( 'data_specialisation' );

    if ( data_specialisation_obj != undefined ) {
        var data_specialisation = data_specialisation_obj.options[data_specialisation_obj.selectedIndex].value
        if ( data_specialisation != -1 ) {
            base_url += 'specialisation/' + data_specialisation + '/';
        }
    }

    // postcode
    var data_postcode_obj = document.getElementById( 'data_postcode' );

    if ( data_postcode_obj != undefined ) {
        var data_postcode = data_postcode_obj.value;
        if ( data_postcode != '' ) {
            var data_postcode2 = data_postcode.split(" ");
            if ( data_postcode != '' && data_postcode != 'Postcode' ) {
                base_url += 'postcode/' + data_postcode2[0] + '/';
            }
        }
    }

    // towncode
    var data_towncode_obj = document.getElementById( 'data_towncode' );

    if ( data_towncode_obj != undefined ) {
        var data_towncode = data_towncode_obj.value
        if ( data_towncode != '' && data_towncode != 'Town' ) {
            base_url += 'towncode/' + escape( data_towncode ) + '/';
        }
    }

    // firmname
    var data_firmname_obj = document.getElementById( 'data_firmname' );

    if ( data_firmname_obj != undefined ) {
        var data_firmname = data_firmname_obj.value
        if ( data_firmname != '' ) {
            base_url += 'firm-name/' + escape( data_firmname ) + '/';
        }
    }

    // within distance
    var data_within_dist_obj = document.getElementById( 'data_within_dist' );

    if ( data_within_dist_obj != undefined ) {
        var data_within_dist = data_within_dist_obj.options[data_within_dist_obj.selectedIndex].value
        if ( data_within_dist != -1 ) {
            base_url += 'within-distance/' + data_within_dist + '/';
        }
    }

    // random number to prevent caching
    var randomnumber=Math.floor(Math.random()*10000);
    base_url += randomnumber;

    url = base_url;
}

function process(transport) {

    var response = transport.responseText;

    var response = response.replace( /<\/?[^>]+(>|$)/g, "" );

    var response = response.replace( /[\r\n]/mg, '' );

    var home_search_firm_counter = document.getElementById( 'home_search_firm_counter' );

    if ( !isNaN(response)&&parseInt(response) == response ) {
        // assume its int
        home_search_firm_counter.innerHTML = response;
    }
    else {
        // assume its text
        home_search_firm_counter.innerHTML = '<span class="font_2">'+response+'</span>';
    }
}

var live_postcode = '';
var fetching = 0;
var current_timeout = 0;

function postcode_chk_1() {

    // reset fetching, user wants to try different keywords
    fetching = 0;

    live_postcode = document.getElementById( 'data_postcode' ).value;

    if ( ( live_postcode.length >= 2 ) || ( live_postcode == '' ) ) {
        clearTimeout ( current_timeout );
        //  leave 2 seconds before checking, assume it takes 2 seconds for user to type keyword(s)
        current_timeout = setTimeout( "postcode_chk_2()", 1500 );
    }
}

function postcode_chk_2() {

    var now_postcode = document.getElementById( 'data_postcode' ).value;

    /*
    if ( now_postcode == '' ) {
        return true;
    }
    */

    if ( live_postcode != now_postcode ) {
        clearTimeout ( current_timeout );
        // assume user is still typing, check again in 1 sec
        // how long we're going to pause between each key stroke e.g. 700 is 0.7 sec
        current_timeout = setTimeout( "postcode_chk_2()", 500 );
    }
    else {
        // once fetching, no need for other processes to do the same
        if ( fetching == 0 ) {

            // they are both the same, assume user has finished typing keyword(s)
            // lets go and search
            gather_url();
            new Ajax.Request(url, { method: 'get', onSuccess: process, onFailure: error_set });
            fetching = 1;
        }
    }
}

// lets do town

var live_towncode = '';

function towncode_chk_1() {

    // reset fetching, user wants to try different keywords
    fetching = 0;

    live_towncode = document.getElementById( 'data_towncode' ).value;

    if ( ( live_towncode.length >= 2 ) || ( live_towncode == '' ) ) {
        clearTimeout ( current_timeout );
        //  leave 2 seconds before checking, assume it takes 2 seconds for user to type keyword(s)
        current_timeout = setTimeout( "towncode_chk_2()", 1500 );
    }
}

function towncode_chk_2() {

    var now_towncode = document.getElementById( 'data_towncode' ).value;

    /*
    if ( now_towncode == '' ) {
        return true;
    }
    */

    if ( live_towncode != now_towncode ) {
        clearTimeout ( current_timeout );
        // assume user is still typing, check again in 1 sec
        // how long we're going to pause between each key stroke e.g. 700 is 0.7 sec
        current_timeout = setTimeout( "towncode_chk_2()", 500 );
    }
    else {

        // once fetching, no need for other processes to do the same
        if ( fetching == 0 ) {

            // they are both the same, assume user has finished typing keyword(s)
            // lets go and search
            new Ajax.Request(url, { method: 'get', onSuccess: process, onFailure: error_set });
            fetching = 1;
        }
    }
}

function towncode_chk_3() {

    // reset fetching, user wants to try different keywords
    fetching = 0;

    gather_url();
    new Ajax.Request(url, { method: 'get', onSuccess: process, onFailure: error_set });
    fetching = 1;
}

// lets do firmname

var live_firmname = '';

function firmname_chk_1() {

    // reset fetching, user wants to try different keywords
    fetching = 0;

    live_firmname = document.getElementById( 'data_firmname' ).value;

    if ( ( live_firmname.length >= 2 ) || ( live_towncode == '' ) ) {
        clearTimeout ( current_timeout );
        //  leave 2 seconds before checking, assume it takes 2 seconds for user to type keyword(s)
        current_timeout = setTimeout( "firmname_chk_2()", 1500 );
    }
}

function firmname_chk_2() {

    var now_firmname = document.getElementById( 'data_firmname' ).value;

    /*
    if ( now_firmname == '' ) {
        return true;
    }*/

    if ( live_firmname != now_firmname ) {
        clearTimeout ( current_timeout );
        // assume user is still typing, check again in 1 sec
        // how long we're going to pause between each key stroke e.g. 700 is 0.7 sec
        current_timeout = setTimeout( "firmname_chk_2()", 500 );
    }
    else {

        // once fetching, no need for other processes to do the same
        if ( fetching == 0 ) {

            // they are both the same, assume user has finished typing keyword(s)
            // lets go and search
            gather_url();
            new Ajax.Request(url, { method: 'get', onSuccess: process, onFailure: error_set });
            fetching = 1;
        }
    }
}

