 function clipToRect(el, t, r, b, l)
  {
    el.style.clip = "rect(" + t + "px, " + r + "px, " + b + "px, " + l + "px)";
  }
  
  var isIE = ! (document["all"] === undefined);
  if (isIE)
  {
    var EXTENDED_HEIGHT = 75;
    var CONTRACTED_HEIGHT = 50;
  }
  else
  {
    var EXTENDED_HEIGHT = 73;
    var CONTRACTED_HEIGHT = 48;
  }  

  function _doContract(el, currentHeight, targetHeight)
  {
      if (currentHeight > targetHeight)
      {
          el.style.height = currentHeight + 'px';
          currentHeight -= 5;
          window.setTimeout(function() { _doContract(el,currentHeight, targetHeight); }, 0);
      }
      else
      {
          el.style.height = CONTRACTED_HEIGHT;
          el.className = "contracted";
          isAnimating = false;
      }
  }

  function _doExpand(el, currentHeight, targetHeight)
  {
      if (currentHeight < targetHeight)
      {
          el.className = "extended";      
          el.style.height = currentHeight + 'px';
          currentHeight += 5;
          window.setTimeout(function() { _doExpand(el,currentHeight, targetHeight); }, 0);
      }
      else
      {
          el.style.height = EXTENDED_HEIGHT;
          isAnimating = false;
      }
  }


  function getCookie(name)
  {
    var re = new RegExp(name + "=([^;]+)");
    var value = re.exec(document.cookie);
    return (value != null) ? unescape(value[1]) : null;
  }

  var today = new Date();
  var expiry = new Date(today.getTime() + 28 * 24 * 60 * 60 * 1000); // plus 28 days

  function setCookie(name, value)
  {
    document.cookie = name + "=" + escape(value) + '; expires=' + expiry.toGMTString();
  }
  
  function contract()
  {
    var form = document.getElementById("form");
    setCookie("SearchForm", "Simple");
    _doContract(form, EXTENDED_HEIGHT, CONTRACTED_HEIGHT);
  }
  
  function expand()
  {
    var form = document.getElementById("form");
    setCookie("SearchForm", "Advanced");
    _doExpand(form, CONTRACTED_HEIGHT, EXTENDED_HEIGHT);
  }

  var isShowingAdvanced = false;
  var isAnimating = false;
  function toggleAdvancedSearch(el)
  {
    if (isAnimating)
      return;
          
    if (isShowingAdvanced)
    {
      el.innerHTML = "Advanced Search";    
      contract();
    }
    else
    {
      el.innerHTML = "Simple Search";    
      expand();
    }
    isShowingAdvanced = !isShowingAdvanced;
  }
  
  if (getCookie("SearchForm") == "Advanced")
  {
    function doToggleAdvancedSearch()
    {
      toggleAdvancedSearch(document.getElementById('advanced'));
    }
    
    if (! isIE)
      window.addEventListener("load", doToggleAdvancedSearch, false);
    else
      window.attachEvent("onload", doToggleAdvancedSearch);
  }
  
  function setResultsPerPage()
  {
    var n = getCookie("ResultsPerPage");
    var resultsPage = document.getElementById('resultsPerPage');
    for (var i = 0; i < resultsPage.options.length; i++)
    {
      var thisOption = resultsPage.options[i];
      if (thisOption.value == "" + n)
      {
        resultsPage.selectedIndex = i;
        return;
      }
    }
  }
  
  function doSearch()
  {
    var resultsPage = document.getElementById('resultsPerPage');
    setCookie("ResultsPerPage",
              resultsPage.options[resultsPage.selectedIndex].value);
    document.forms["searchform"].submit();  
  }
  
  if (getCookie("ResultsPerPage"))
  {
    if (! isIE)
      window.addEventListener("load", setResultsPerPage, false);
    else
      window.attachEvent("onload", setResultsPerPage);
  }
  
  var NORMAL_FORM_COLOR = '#d9e2e9';
  var HILITE_FORM_COLOR = '#AEC8E1';
  
  function fadeToColor(el, color)
  {
    if (isIE)
    {
      el.style.filter = "blendTrans(duration=0.2)"
      el.filters.blendTrans.Apply()              
    }
  
    el.style.backgroundColor = color;
  
    if (isIE)
      el.filters.blendTrans.Play();  
  }
  
  function selectCategory(el, name)
  {
    var categorySelectBox = document.getElementById('category');
    var regionSelectBox = document.getElementById('region');
    var theForm = document.getElementById('form');
    for (var i = 0; i < categorySelectBox.options.length; i++)
    {
      var thisOption = categorySelectBox.options[i];
      if (thisOption.value == name)
      {
        categorySelectBox.selectedIndex = i;
        regionSelectBox.focus();

        fadeToColor(theForm, HILITE_FORM_COLOR);
        
        window.setTimeout(function() { fadeToColor(theForm, NORMAL_FORM_COLOR); }, 750);
        break;
      }
    }
  }
  
  function _doResizeLarge(el, currentHeight, targetHeight)
  {
    isResizing = true;
    if (currentHeight > targetHeight)
    {
      isResizing = false;
      el.className = 'large';
      el.style.height = targetHeight + 'px';
      resizeWindow();
    }
    else
    {
      el.style.height = currentHeight + 'px';
      window.setTimeout(function() { _doResizeLarge(el, currentHeight + 3, targetHeight); }, 10);
    }
  }

  function _doResizeSmall(el, currentHeight, targetHeight)
  {
    isResizing = true;
    if (currentHeight < targetHeight)
    {
      isResizing = false;
      el.className = 'small';
      el.style.height = targetHeight + 'px';
      resizeWindow();
    }
    else
    {
      el.style.height = currentHeight + 'px';
      window.setTimeout(function() { _doResizeSmall(el, currentHeight - 3, targetHeight); }, 10);
    }
  }
  
  function resizeToLarge(el, animate)
  {
    if (animate)
    {
      el.style.height = '100px';
      _doResizeLarge(el, 100, 196);
    }
    else
    {
      el.style.height = '196px';
    }
  }
  
  function resizeToSmall(el, animate)
  {
    if (animate)
    {
      el.style.height = '196px';
      _doResizeSmall(el, 196, 100);
    }
    else
    {
      el.style.height = '100px';
    }
  }
  
  var windowHeight = 0;
  var shouldDoResize = false;
  var isResizing = false;
  function resizeWindow()
  {
    if (isIE)
    {
      newWindowHeight = document.body.clientHeight;
    }
    else
    {
      newWindowHeight = window.outerHeight;
    }

    if (windowHeight == newWindowHeight || isResizing)
    {
      return;
    }

    var theLogo = document.getElementById('logo');
    
    animate = 0;
    if (windowHeight != 0)
      animate = 1;


    if (newWindowHeight > 490 && (windowHeight < 490 || !animate))
    {
      resizeToLarge(theLogo, animate);
    }
    else if (newWindowHeight < 490 && (windowHeight > 490 || !animate))
    {
      resizeToSmall(theLogo, animate);
    }
    windowHeight = newWindowHeight;
  }