function quickLinkOption(name,link) {
	var name = name;
	var value = link;
	var optionElement = document.createElement('option');
	
	optionElement.setAttribute('value',value);
	optionElement.appendChild(document.createTextNode(name));
	
	return optionElement;
}

function QuickLinksBuilder() {
  var quickLinksForm = document.createElement('form');
  quickLinksForm.setAttribute('id','quicklinks');
  quickLinksForm.setAttribute('action','#');
  var selectElement = document.createElement('select');
  selectElement.setAttribute('id','qlselect');
  
  var optionsArray = new Array();
  
  optionsArray.push(quickLinkOption("Quick Links","#"));
  optionsArray.push(quickLinkOption("-------------------","#"));  
  optionsArray.push(quickLinkOption("Introduction","introduction/index.shtml"));
  optionsArray.push(quickLinkOption("Instrumentation","instrumentation/index.shtml"));
  optionsArray.push(quickLinkOption("Facility access","facility_access/index.shtml"));
  optionsArray.push(quickLinkOption("Facility charges","facility_charges/index.shtml"));
  optionsArray.push(quickLinkOption("Courses and Training","courses_and_training/index.shtml"));
  optionsArray.push(quickLinkOption("Staff and students","staff_and_students/index.shtml"));
  optionsArray.push(quickLinkOption("Research projects","research/index.shtml"));
  optionsArray.push(quickLinkOption("Symposium","symposium/index.shtml"));
  
  for (var x = 0; x<optionsArray.length; x++) {
    selectElement.appendChild(optionsArray[x]);
  }
  
  quickLinksForm.appendChild(selectElement);
  
  var goButton = document.createElement('input');
  goButton.setAttribute('type','button');
  goButton.className = 'button';
  goButton.setAttribute('value','Go');
  
  quickLinksForm.appendChild(goButton);
  document.getElementById('head').insertBefore(quickLinksForm,document.getElementById('nav-global'));
}

function quickLinksHandler() {
  var selectElement = document.getElementById('qlselect');
  var optionValue = selectElement.options[selectElement.selectedIndex].value;
  window.location = optionValue;
}
  
function clearInput() {
  this.value = '';
}

function checkSearch() {
  if (this.query.value == "" || this.query.value == "Enter search terms") {
    alert("Please enter some search terms");
    return false;
  } else {
    return true;
  }
}

window.onload = function() {
  var searchForm = document.getElementById('search');
  var searchField = searchForm.query;
  
  if(!document.getElementById('quicklinks')){
    QuickLinksBuilder();
  }
  
  var quickLinksGoButton = document.getElementById('quicklinks').getElementsByTagName('input')[0];
  
  searchField.onfocus = clearInput;
  searchForm.onsubmit = checkSearch;
  quickLinksGoButton.onclick = quickLinksHandler;
}
