// This JS file manages the quicklinks boxes on the Surrey homepage
// It allows them to auto submit without clicking the Go buttons
// It also prevents the form from being submitted if the dropdowns have not been selected
// It uses JQuery to determine when the DOM is ready

// Declare global variables
var selectors, submitters, quicklinksForm, dropdownPopulated

// Check whether DOM is ready and then apply event handlers
$(document).ready(function(){
  selectors = document.getElementsByTagName('select');
  submitters = document.getElementsByTagName('input');
  quicklinksForm = document.getElementById('quicklinks_form');
  dropdownPopulated = false;

  quicklinksForm.onsubmit = function(){return checkQuicklinks()};
  if(quicklinksForm.captureEvents) quicklinksForm.captureEvents(Event.SUBMIT);

  for(i=0; i<selectors.length; i++){
    if(selectors[i].className == 'quicklinks_select'){
      selectors[i].onchange = doQuicklink;
  	  if(selectors[i].captureEvents) selectors[i].captureEvents(Event.ONCHANGE);
   	  }
    }
  
  for(i=0; i<submitters.length; i++){
    if(submitters[i].className == 'quicklinks_submit'){
      submitters[i].onclick = checkDropdown;
	  if(submitters[i].captureEvents) submitters[i].captureEvents(Event.CLICK);
	  }
    }
  });
 
// Submit the form when the dropdown changes
function doQuicklink(){
  // First reset the other quicklinks boxes
  for(i=0; i<selectors.length; i++){
    if(this.id != selectors[i].id){
	  selectors[i].selectedIndex = 0;
	  }
	}
  // Now submit the form
  if(this.selectedIndex != 0)this.form.submit();
  }
  

function checkQuicklinks(){
  // Check that the appropriate select box is populated
  if(dropdownPopulated) return true;
  else return false;
  }
  

function checkDropdown(){
  dropdownPopulated = false;
  var dropdownID = this.id.replace('_submit', '');
  if(document.getElementById(dropdownID).selectedIndex > 0) dropdownPopulated = true;
  else dropdownPopulated = false;
  }						   



