/*
   This file is part of Civiscrape, a prototype job listings website
   produced for the Cabinet Office by The Dextrous Web Ltd.

   Author: Harry Metcalfe (harry@thedextrousweb.com)

   The content of this file is Crown Copyright
*/

var terms = document.getElementById('terms');
var loc = document.getElementById('location');

addEvent(terms, 'focus', termsText);
addEvent(terms, 'blur', termsText);

addEvent(loc, 'focus', locText);
addEvent(loc, 'blur', locText);

addEvent(window, 'load', winLoad);

var ac = new Ajax.Autocompleter('location', 'suggest', 'suggest.php', {minChars:0,frequency: 0.1, updateElement: update});

function update(li)
{
   loc.value = li.innerHTML.substr(0, li.innerHTML.indexOf(','));
}

function termsText()
{
   process(terms, 'all vacancies');
}

function locText()
{
   process(loc, 'anywhere');
}

function winLoad()
{
   initialise(terms, 'all vacancies');
   initialise(loc, 'anywhere');
}

function process(el, text)
{
   if(el.value == '')
   {
      el.value = text;
      el.style.color = '#DDDDDD';
   }
   else if(el.value == text)
   {
      el.value = '';
      el.style.color = '#000000';
   }
   else
   {
      el.style.color = '#000000';
   }
}

function initialise(el, text)
{
   if(el.value == '' || el.value == text)
   {
      el.style.color = '#DDDDDD';
   }
   else
   {
      el.style.color = '#000000';
   }
}