//------------------
// 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(){
  if (document.getElementById('vacature') != null){
    document.onload = setMenuImage();
    var radioVacature = document.getElementById('vacature');
    var radioOpdracht = document.getElementById('opdracht');
    var opdrachtForm = document.getElementById('search-job');
    radioVacature.onchange = function(){setForm('vacature');}
    radioOpdracht.onchange = function(){setForm('opdracht');}
    opdrachtForm.onsubmit = function(){setQuery();}    
  }
  else{
    document.onload = setMenuImage();    
  }
}
// sets the form input's name and action attributes to represent the right url's and such.
function setForm(variant){
  var opdrachtForm = document.getElementById('search-job');
  var searchInput = document.getElementById('search-query');
  if (variant == 'vacature'){
    searchInput.name = 'p_text';
    opdrachtForm.action = externalHost;
  }
  else{
    searchInput.name = 'trefwoord';
    opdrachtForm.action = mwpHost;
  }
}
// sets the action attribute to also contain the key words from the input field.
function setQuery(){
  var opdrachtForm = document.getElementById('search-job');
  var searchInput = document.getElementById('search-query');
  currentAction = opdrachtForm.action;
  newAction = currentAction + searchInput.value;
  opdrachtForm.action = newAction;
  return true;  
}
addLoadEvent(initialise);
