
var url = ''; 

if ( $('skills1') != undefined ) { $('skills1').observe( 'change', function(event){ gather_url(); new Ajax.Request(url, { asynchronous: 1, onSuccess: process, onFailure: error_set }); }); }
if ( $('skills2') != undefined ) { $('skills2').observe( 'change', function(event){ gather_url(); new Ajax.Request(url, { asynchronous: 1, onSuccess: process, onFailure: error_set }); }); }
if ( $('skills3') != undefined ) { $('skills3').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 }); }); }

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://jobs.pensionsworld.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://jobs.pensionsworld.co.uk/img/ajax-loader.gif" />';

    // skills 1
    var skills1_obj = document.getElementById( 'skills1' );
    
    if ( skills1_obj != undefined ) {
        var skills1 = skills1_obj.options[skills1_obj.selectedIndex].value
        if ( skills1 != -1 ) {
            base_url += 'skills1/' + skills1 + '/';
        } 
    }
    
    // skills 2
    var skills2_obj = document.getElementById( 'skills2' );
    
    if ( skills2_obj != undefined ) {
        var skills2 = skills2_obj.options[skills2_obj.selectedIndex].value
        if ( skills2 != -1 ) {
            base_url += 'skills2/' + skills2 + '/';
        } 
    }
    
    // skills 3
    var skills3_obj = document.getElementById( 'skills3' );
    
    if ( skills3_obj != undefined ) {
        var skills3 = skills3_obj.options[skills3_obj.selectedIndex].value
        if ( skills3 != -1 ) {
            base_url += 'skills3/' + skills3 + '/';
        } 
    }
    
    // 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;
        }
    }
}