  /* derived from http://www.google.com/support/forum/p/customsearch/thread?tid=6599a46db93d8c10&hl=en by tkp 041910*/
  function findByClass(tagName, className) {
    var r = new RegExp('\\b' + className + '\\b');
    var elements = document.getElementsByTagName(tagName);
    for (var i = 0;  i < elements.length;  i++) {
      var e = elements[i];
      if (e.className && r.test(e.className)) {
        return e;
      }
    }
    return null;
  }

  function searchSubmitted() {
    var e;
    if ((e = findByClass('input', 'gsc-input')) != null) {
      if (e.value === '') {
        searchCleared();
      }
    }
  }

  function searchComplete() {
    document.getElementById('cse-results')
      .style.display = 'block';
    document.getElementById('maincontent')
      .style.display = 'none';
    document.title = 'NCAS - Search Results';
    /* the getposition function is used here to make the height of the search results and sidebar equivalent for both IE and others*/
    if (/MSIE (\d+\.\d+);/.test(navigator.userAgent))
    	{
    	 get_position('cse-results','z','sidebar');
    	 /*document.getElementById('sidebar').style.height='800px';*/
    	}
    else
    	{
    	 document.getElementById('sidebar').style.height='800px';
    	 get_position('cse-results','z','sidebar');
    	 
    	 }
    /*document.getElementById('roundr').style.display='none';*/
  }

  function searchCleared() {
    document.getElementById('maincontent')
      .style.display = 'block';
    document.getElementById('cse-results')
      .style.display = 'none';
  }

  function init() {

    var cseId = '015578417650167555690:xpeu2n3bf6o';

    var customSearchControl =
      new google.search.CustomSearchControl(cseId);

    /*what is below displays up to 10 results per page, which is too many.... 
    customSearchControl.setResultSetSize(
      google.search.Search.FILTERED_CSE_RESULTSET);*/
    customSearchControl.setResultSetSize(
      google.search.Search.SMALL_RESULTSET);
    /* 8 results would be obtained by using google.search.Search.LARGE_RESULTSET */

    customSearchControl.setSearchCompleteCallback(
      null, searchComplete);

    var options = new google.search.DrawOptions();
    options.setSearchFormRoot('cse-search-form');
	options.setInput(document.getElementById('query_input'));
    customSearchControl.draw('cse-results', options);

    var e;

    if ((e = findByClass('form', 'gsc-search-box')) != null) {
      if (e.addEventListener) {
        e.addEventListener(
          'submit', searchSubmitted, false);
      } else if (e.attachEvent) {
        e.attachEvent(
          'onsubmit', searchSubmitted);
      }
    }

    if ((e = findByClass('input', 'gsc-search-button')) != null) {
      if (e.addEventListener) {
        e.addEventListener(
          'click', searchSubmitted, false);
      } else if (e.attachEvent) {
        e.attachEvent(
          'onclick', searchSubmitted);
      }
    }

    if ((e = findByClass('div', 'gsc-clear-button')) != null) {
      if (e.addEventListener) {
          e.addEventListener(
            'click', searchCleared, false);
      } else if (e.attachEvent) {
        e.attachEvent(
          'onclick', searchCleared);
      }
    }
  }

  google.load('search', '1');
  google.setOnLoadCallback(init, true); 

