//------------------
// Title: search-jobs-home.js
// Description: This script is intended for the yacht homepage. It detects wether a certain radio button is checked
// and posts to different url's depending on the selected radio button. It also changes the name attribute of the input box 
// since the internal jobboard accepts "trefwoord" and the external jobboard uses "p_text". Also, the external jobboard does 
// not accept homemade form variables, only a predifined set of attributes. Posting other variables results in a error (400).
// Author: J.Smit
// Date: 16-12-2009
//------------------

// set up variables used througout the script:
// mwpHost = link to internal ADF Jobboard
// externalHost = link to external Jobboard
//var mwpHost = 'http://www.yacht.nl/vacatures?zoekterm=';
//var externalHost = 'http://vacatures.yacht.nl/wd/plsql/wd_portal.search_results?p_web_site_id=425&p_category_id=593&p_text=';

// function to load events when the document is available, we need this to use the getElementById function.
function addLoadEvent(func){
	var oldonload = window.onload;
	if (typeof window.onload != 'function')
	{
		window.onload = func;
	}
	else
	{
		window.onload = function(){
			oldonload();
			func();
		}
	}
}

// initialise listeners
function initialise(){
  var opdrachtForm = document.getElementById('search-job');
  opdrachtForm.onsubmit = function(){setQuery();}    
}

// sets the action attribute to also contain the key words from the input field.
function setQuery(){
  /* Set the variables and attributes */  
  var searchForm = $('#search-job');
  var currentAction = "";
  var searchInputOption = $('#search-job-buttons input[type="radio"]:checked').val();
  var searchInput = $('#search-job #search-field');
  var searchInputValue = searchInput.val();
  if (searchInputValue == defaultSearchText) {searchInputValue = '';}

  if (searchInputOption == 'vacature') {
    searchInput.attr('name', 'p_text');
    currentAction = externalHost;
  }
  else {
    searchInput.attr('name', 'trefwoord');
    currentAction = mwpHost;
  }
  currentAction += searchInputValue;

  searchForm.attr('action', currentAction);
  return true;
}
addLoadEvent(initialise);

