
var url = '';
var taxonomy_url = '';

if ( $('data_personal_business') != undefined ) {
    $('data_personal_business').observe( 'change', function(event) {
        // $( 'data_taxonomy_1' ).setStyle( { visibility: 'hidden' } );
        // $( 'data_taxonomy_2' ).setStyle( { visibility: 'hidden' } );
        $( 'data_taxonomy_3' ).setStyle( { visibility: 'hidden' } );
        var data_personal_business_obj = $( 'data_personal_business' );
        var data_taxonomy_1_obj = $( 'data_taxonomy_1' );
        var data_taxonomy_2_obj = $( 'data_taxonomy_2' );
        var data_taxonomy_3_obj = $( 'data_taxonomy_3' );
        data_taxonomy_1_obj.length = 1;
        data_taxonomy_2_obj.length = 1;
        data_taxonomy_3_obj.length = 1;
        data_taxonomy_1_obj.selectedIndex = 0;
        data_taxonomy_2_obj.selectedIndex = 0;
        data_taxonomy_3_obj.selectedIndex = 0;
        if ( data_personal_business_obj.value != '' ) {
            gather_taxonomy_url();
            new Ajax.Request( taxonomy_url, { method: 'get',
                                              asynchronous: 0,
                                              requestHeaders: {Accept: 'application/json'},
                                              onSuccess: taxonomy_personal_business,
                                              onFailure: error_set });
        }
        gather_url();
        new Ajax.Request( url, { asynchronous: 1, onSuccess: process, onFailure: error_set });
    });
}

if ( $('data_taxonomy_1') != undefined ) {
    $('data_taxonomy_1').observe( 'change', function(event) {
        // $( 'data_taxonomy_2' ).setStyle( { visibility: 'hidden' } );
        $( 'data_taxonomy_3' ).setStyle( { visibility: 'hidden' } );
        var data_taxonomy_1_obj = $( 'data_taxonomy_1' );
        var data_taxonomy_2_obj = $( 'data_taxonomy_2' );
        var data_taxonomy_3_obj = $( 'data_taxonomy_3' );
        data_taxonomy_2_obj.length = 1;
        data_taxonomy_3_obj.length = 1;
        data_taxonomy_2_obj.selectedIndex = 0;
        data_taxonomy_3_obj.selectedIndex = 0;
        if ( data_taxonomy_1_obj.value != '' ) {
            gather_taxonomy_url();
            new Ajax.Request( taxonomy_url, { method: 'get',
                                              asynchronous: 0,
                                              requestHeaders: {Accept: 'application/json'},
                                              onSuccess: taxonomy_1,
                                              onFailure: error_set });
        }
        gather_url();
        new Ajax.Request(url, { asynchronous: 1, onSuccess: process, onFailure: error_set });
    });
}

if ( $('data_taxonomy_2') != undefined ) {
    $('data_taxonomy_2').observe( 'change', function(event) {
        $( 'data_taxonomy_3' ).setStyle( { visibility: 'hidden' } );
        var data_taxonomy_2_obj = $( 'data_taxonomy_2' );
        var data_taxonomy_3_obj = $( 'data_taxonomy_3' );
        data_taxonomy_3_obj.length = 1;
        data_taxonomy_3_obj.selectedIndex = 0;
        if ( data_taxonomy_2_obj.value != '' ) {
            gather_taxonomy_url();
            new Ajax.Request( taxonomy_url, { method: 'get',
                                              asynchronous: 0,
                                              requestHeaders: {Accept: 'application/json'},
                                              onSuccess: taxonomy_2,
                                              onFailure: error_set });
        }
        gather_url();
        new Ajax.Request(url, { asynchronous: 1, onSuccess: process, onFailure: error_set });
    });
}

if ( $('data_taxonomy_3') != undefined ) {
    $('data_taxonomy_3').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 });
    });
}

function error_set() {
    alert( "There seems to be a problem with the search. Please try changing your search criteria." );
}

function gather_taxonomy_url() {

    var base_url = 'http://www.lawyerlocator.co.uk/lawyer/lawyer_search/quick_search_ajax/';

    var data_personal_business_obj = document.getElementById( 'data_personal_business' );
    var data_taxonomy_1_obj = document.getElementById( 'data_taxonomy_1' );
    var data_taxonomy_2_obj = document.getElementById( 'data_taxonomy_2' );
    var data_taxonomy_3_obj = document.getElementById( 'data_taxonomy_3' );

    if ( data_personal_business_obj != undefined ) {
        var data_personal_business = data_personal_business_obj.value;
        if      ( data_personal_business == 'P' ) {
            base_url += 'personal/1/';
        }
        else if ( data_personal_business == 'B' ) {
            base_url += 'business/1/';
        }
    }

    if ( data_taxonomy_1_obj != undefined || data_taxonomy_2_obj != undefined || data_taxonomy_3_obj != undefined ) {
        var data_taxonomy_1 = data_taxonomy_1_obj.options[data_taxonomy_1_obj.selectedIndex].value;
        var data_taxonomy_2 = data_taxonomy_2_obj.options[data_taxonomy_2_obj.selectedIndex].value;
        var data_taxonomy_3 = data_taxonomy_3_obj.options[data_taxonomy_3_obj.selectedIndex].value;
        if      ( data_taxonomy_3 != '' ) {
            base_url += 'taxonomy/' + data_taxonomy_3 + '/';
        }
        else if ( data_taxonomy_2 != '' ) {
            base_url += 'taxonomy/' + data_taxonomy_2 + '/';
        }
        else if ( data_taxonomy_1 != '' ) {
            base_url += 'taxonomy/' + data_taxonomy_1 + '/';
        }
    }

    var randomnumber=Math.floor(Math.random()*10000);
    base_url += randomnumber;

    taxonomy_url = base_url;
}

function gather_url( no_random ) {

    var base_url = 'http://www.lawyerlocator.co.uk/lawyer/lawyer_search/taxonomy_search_number/';

    // ajax loader

    var home_search_firm_counter = document.getElementById( 'quick-search-counter' );
    home_search_firm_counter.innerHTML = '<img src="http://www.lawyerlocator.co.uk/img/ajax-loader2.gif" /><span class="quick-search-wait">&nbsp;Finding&nbsp;Lawyers...</a>';
    
    // taxonomy

    var data_taxonomy_1_obj = document.getElementById( 'data_taxonomy_1' );
    var data_taxonomy_2_obj = document.getElementById( 'data_taxonomy_2' );
    var data_taxonomy_3_obj = document.getElementById( 'data_taxonomy_3' );

    if ( data_taxonomy_1_obj != undefined || data_taxonomy_2_obj != undefined || data_taxonomy_3_obj != undefined ) {
        var data_taxonomy_1 = data_taxonomy_1_obj.options[data_taxonomy_1_obj.selectedIndex].value;
        var data_taxonomy_2 = data_taxonomy_2_obj.options[data_taxonomy_2_obj.selectedIndex].value;
        var data_taxonomy_3 = data_taxonomy_3_obj.options[data_taxonomy_3_obj.selectedIndex].value;
        if      ( data_taxonomy_3 != '' ) {
            base_url += 'taxonomy/' + data_taxonomy_3 + '/';
        }
        else if ( data_taxonomy_2 != '' ) {
            base_url += 'taxonomy/' + data_taxonomy_2 + '/';
        }
        else if ( data_taxonomy_1 != '' ) {
            base_url += 'taxonomy/' + data_taxonomy_1 + '/';
        }
    }

    // postcode

    var data_postcode_obj = document.getElementById( 'data_postcode' );

    if ( data_postcode_obj != undefined ) {
        var data_postcode = data_postcode_obj.value;
        if ( data_postcode != '' && data_postcode != 'Postcode' ) {
            var data_postcode2 = data_postcode.split(' ');
            if ( data_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( convert_international( 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

    if ( !no_random ) {
        var randomnumber=Math.floor(Math.random()*10000);
        base_url += randomnumber;
    }

    url = base_url;
}

function process(transport) {

    var response = transport.responseText;

    var home_search_firm_counter = document.getElementById( 'quick-search-counter' );

    // Just drop it straight in

    home_search_firm_counter.innerHTML = response;
}

function taxonomy_personal_business(transport) {

    var response = transport.responseText;
    var data_taxonomy_1_obj = $( 'data_taxonomy_1' );

    var items = response.evalJSON(true);
    var display_taxonomy_1 = false;

    items.taxonomy.each ( function ( item, index ) {
        display_taxonomy_1 = true;
        var opt = document.createElement('option');
        opt.text = item.Name;
        opt.value = item.CategoryOfWorkID;
        data_taxonomy_1_obj.options.add(opt);
    });

    if ( display_taxonomy_1 ) {
        $( 'data_taxonomy_1' ).setStyle( { visibility: 'visible' } );
    }
}

function taxonomy_1(transport) {

    var response = transport.responseText;
    var data_taxonomy_2_obj = $( 'data_taxonomy_2' );

    var items = response.evalJSON(true);
    var display_taxonomy_2 = false;

    items.taxonomy.each ( function ( item, index ) {
        display_taxonomy_2 = true;
        var opt = document.createElement('option');
        opt.text = item.Name;
        opt.value = item.CategoryOfWorkID;
        data_taxonomy_2_obj.options.add(opt);
    });

    if ( display_taxonomy_2 ) {
        $( 'data_taxonomy_2' ).setStyle( { visibility: 'visible' } );
    }
}

function taxonomy_2(transport) {

    var response = transport.responseText;
    var data_taxonomy_3_obj = $( 'data_taxonomy_3' );

    var items = response.evalJSON(true);
    var display_taxonomy_3 = false;

    items.taxonomy.each ( function ( item, index ) {
        display_taxonomy_3 = true;
        var opt = document.createElement('option');
        opt.text = item.Name;
        opt.value = item.CategoryOfWorkID;
        data_taxonomy_3_obj.options.add(opt);
    });

    if ( display_taxonomy_3 ) {
        $( 'data_taxonomy_3' ).setStyle( { visibility: 'visible' } );
    }
}

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 ( 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 ( 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 ( 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;
        }
    }
}

