openDesktopDownload = function()
  {
    var scrollTop = (window.scrollY ? window.scrollY : (document.documentElement.scrollTop ? document.documentElement.scrollTop : 0));
    document.getElementById('DesktopDownload').style.top = (scrollTop + 100) + 'px';
    document.getElementById('DesktopDownload').style.display='block';
    document.getElementById('DesktopDownloadSearch').focus();
    return false;
  }
  
  function KeymanDesktopSearch(txt)
  {
    var e = document.getElementById('DesktopDownloadSearch');
    e.value = txt;
    e.focus();
    beforeSearch(null);
    return false;
  }
  
  function prepSearch(e)
  {
    if(e.value == 'Language, Keyboard or Country')
    {
      e.value = '';
      e.style.color = 'black';
    }
  }
  
  var lastSelectedKeyboard = null;

  function clickKeyboardPopup(event)
  {
    event.cancelBubble = true;
    if(event.stopPropagation) event.stopPropagation();
    return true;
  }
  
  function selectKeyboard(KeyboardID)
  {
    var e = document.getElementById('keyboard_'+KeyboardID);
    if(e)
    {
      if(e == lastSelectedKeyboard)
      {
        e.className = 'Keyboard';
        document.getElementById('DesktopDownloadResults').className = '';
        lastSelectedKeyboard = null;
      }
      else 
      {
        if(lastSelectedKeyboard) lastSelectedKeyboard.className = 'Keyboard';
        document.getElementById('DesktopDownloadResults').className = 'KeyboardSelected';
        e.className = 'Keyboard Selected';
        lastSelectedKeyboard = e;
        e = document.getElementById('keyboard_'+KeyboardID+'_email');
        if(e) e.focus();
      }
    }
    else
    {
      document.getElementById('DesktopDownloadResults').className = '';
      if(lastSelectedKeyboard) lastSelectedKeyboard.className = 'Keyboard';
      lastSelectedKeyboard = null;
    }
  }
  
  function resetSelectedKeyboard()
  {
    if(lastSelectedKeyboard) lastSelectedKeyboard.className = 'Keyboard';
    document.getElementById('DesktopDownloadResults').className = '';
    lastSelectedKeyboard = null;
  }
  
  var xmlHttpReq = false, ks_cache = new Array(), lastValue = null;
  
  function xmlhttpPost(url, query, render) {
    // Mozilla/Safari
    if(!xmlHttpReq)
    {
      if (window.XMLHttpRequest) {
        xmlHttpReq = new XMLHttpRequest();
      }
      // IE
      else if (window.ActiveXObject) {
        xmlHttpReq = new ActiveXObject('Microsoft.XMLHTTP');
      }
    }
    xmlHttpReq.open('POST', url, true);
    xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xmlHttpReq.onreadystatechange = function()
    {
      if(xmlHttpReq.readyState == 4)
      {
        render(xmlHttpReq, url, query);
      }
    }
    xmlHttpReq.send(query);
  }

  function keySearch(event)
  {
    /*var e = document.getElementById('DesktopDownloadSearch');
    if(e.value == lastValue) return false;
    
    if(e.value.length < 3)
    {
      if(lastValue == '') return false;
    }*/

    return beforeSearch(event, true);
  }
  
  function beforeSearch(event, isKey)
  {
    var e = document.getElementById('DesktopDownloadSearch');
    
    var v = e.value;
    if(isKey && v.length < 3) v = '';
    if(v == lastValue) return false;
    lastValue = v;
    
    if(xmlHttpReq) xmlHttpReq.abort();
    xmlHttpReq = null;
    e.className = 'searching';
    // AJAX search
    xmlhttpPost('/keyman/downloads/keyboards/ajax.php', 'Search='+encodeURIComponent(v), 
      function(x, u, q)
      {
        resetSelectedKeyboard();
        document.getElementById('DesktopDownloadResults').innerHTML = x.responseText;
        e.className = '';
        xmlHttpReq = null;
      }
    );
    return false;
  }
  
  function SelectAndShowDownloadForm(event, e, isKeyboard, title, id, id2)
  {
    if(event.cancelBubble) event.cancelBubble();
    if(event.stopPropagation) event.stopPropagation();
    resetSelectedKeyboard();
    selectKeyboard(id);
    return ShowDownloadForm(e, isKeyboard, title, id, id2);
  }
  
  function cancelSearch()
  {
    document.getElementById('DesktopDownload').style.display = 'none';
    return false;
  }

  function SubmitDownloadForm(id)
  {
    var e = document.getElementById(id);
    var filter  = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    if (e && filter.test(e.value))
    {
      return true;
    }
    else
    {
      alert('Please enter a valid email address');
      e.focus();
    }
    return false;
  }
  
