
var url = '';

// Setup the onchange functions for the 7 skills we have

for ( var id=1; id<=7; id++ ) {
    if ( $('skills'+id) != undefined ) {
        $('skills'+id).observe( 'change', function(event) {
            gather_url();
            new Ajax.Request(url, { asynchronous: 1, onSuccess: process, onFailure: error_set });
        });
    }
}

if ( $('area') != undefined ) {
    $('area').observe( 'change', function(event) {
        gather_url();
        new Ajax.Request(url, { asynchronous: 1, onSuccess: process, onFailure: error_set });
    });
}

if ( $('salary') != undefined ) {
    $('salary').observe( 'change', function(event) {
        gather_url();
        new Ajax.Request(url, { asynchronous: 1, onSuccess: process, onFailure: error_set });
    });
}

if ( $('employers') != undefined ) {
    $('employers').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 again later." );

    var home_search_button_box = document.getElementById( 'home_search_button_box' );
    home_search_button_box.innerHTML = '<input id="home_search_button" type="submit" name="home_search_button" value="  View Jobs  " />';
}

function gather_url() {

    var base_url = 'http://www.totalrewardsjobs.co.uk/jobs/job_search_number/';

    // Ajax loader

    var home_search_button_box = document.getElementById( 'home_search_button_box' );
    home_search_button_box.innerHTML = '<img src="http://www.totalrewardsjobs.co.uk/img/ajax-loader.gif" />';

    // Loop through 7 skills

    for ( var id=1; id<=7; id++ ) {
        var skill_obj = document.getElementById( 'skills'+id );
        if ( skill_obj != undefined ) {
            var skill = skill_obj.options[skill_obj.selectedIndex].value
            if ( skill != -1 ) {
                base_url += 'skills'+id+'/'+skill+'/';
            }
        }
    }

    var employers_obj = document.getElementById( 'employers' );

    if ( employers_obj != undefined ) {
        var employers = employers_obj.options[employers_obj.selectedIndex].value
        if ( employers != -1 ) {
            base_url += 'employers/' + employers + '/';
        }
    }

    // area
    var area_obj = document.getElementById( 'area' );

    if ( area_obj != undefined ) {
        var area = area_obj.options[area_obj.selectedIndex].value
        if ( area != -1 ) {
            base_url += 'area/' + area + '/';
        }
    }

    // salary
    var salary_obj = document.getElementById( 'salary' );

    if ( salary_obj != undefined ) {
        var salary = salary_obj.options[salary_obj.selectedIndex].value
        if ( salary != -1 ) {
            base_url += 'salary/' + salary + '/';
        }
    }

    // keywords
    var keywords_obj = document.getElementById( 'search_box_keywords' );

    if ( keywords_obj != undefined ) {
        var keywords = keywords_obj.value
        if ( keywords != '' ) {
            base_url += 'keywords/' + escape( keywords ) + '/';
        }
    }

    // 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 jobs_val = 'View Jobs';

    var response = response.replace( /<\/?[^>]+(>|$)/g, "" );

    var response = response.replace( /[\r\n]/mg, '' );

    if ( !isNaN(response)&&parseInt(response) == response ) {
        // its a number
        jobs_val = 'View ' + response + ' Jobs';
    }

    var home_search_button_box = document.getElementById( 'home_search_button_box' );
    home_search_button_box.innerHTML = '<button type="submit"><span class="round_button"><span> ' + jobs_val + ' </span></span></button>';

    return true;
}

var live_keyword = '';
var fetching = 0;

function keyword_chk_1() {

    // reset fetching, user wants to try different keywords
    fetching = 0;

    live_keyword = document.getElementById( 'search_box_keywords' ).value;

    if ( live_keyword.length >= 2 ) {
        //  leave 2 seconds before checking, assume it takes 2 seconds for user to type keyword(s)
        setTimeout( "keyword_chk_2()", 1500 );
    }
}

function keyword_chk_2() {

    var now_keyword = document.getElementById( 'search_box_keywords' ).value;

    if ( now_keyword == '' ) {
        return true;
    }

    if ( live_keyword != now_keyword ) {
        // 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
        setTimeout( "keyword_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;
        }
    }
}

