﻿$(function() 
    {
        var inputs=$('input[type=text]');
        if (inputs.length>0 && inputs[0].className.indexOf('datepicker')==-1)
        {
            inputs[0].focus();
        }
        $('#noajax').remove();
        $('.datepicker').datepicker({ dateFormat: 'd M yy' });
      $("#textField")
        .keypress(NSuggest_Start)
        .blur(NSuggest_ClearDelayed)
        
        $('#searchform').submit(NSuggest_Clear);
        $('body').click(NSuggest_Clear);

    }
);
var NSuggestTimeout;
function NSuggest_Clear()
{
    $("#searchSuggest").hide(); clearTimeout(NSuggestTimeout); 
}
function NSuggest_ClearDelayed()
{
    setTimeout(NSuggest_Clear,300);
}
function NSuggest_Start()
{
    clearTimeout(NSuggestTimeout); 
    NSuggestTimeout=setTimeout("NSuggest_Get()",500);
}
function NSuggest_Get()
{
    $.getJSON("http://preview.ncbi.nlm.nih.gov/portal/utils/autocomp.fcgi?dict=pm_related_queries_2&callback=?&q=" + encodeURIComponent($("#textField").val()), NSuggest_CreateData);
}
function NSuggest_CreateData(q, data)
{
var ul = $("#searchSuggest");
ul.empty();
var count=0;
$.each(data, function(i, text){ if (text!='') { count++; ul.append($("<li/>").text(text).click( function() { $('#textField').val(text); $("#searchSuggest").hide() } )); } });
if (count>0)
    ul.slideDown();
}

function ResubmitForm( controlId1, controlValue1, controlId2, controlValue2 )
{
    document.getElementById(controlId1).value=controlValue1;
    if (controlId2!=null)
        document.getElementById(controlId2).value=controlValue2;
    document.getElementById("searchform").submit();
}

function ToggleTag( tagValue, element )
{
    var searchBox=document.getElementById("textField");
    var tagString="tag:"+tagValue;
    if (!element.checked)
        searchBox.value=searchBox.value.replace( tagString, " ");
    else
        searchBox.value=searchBox.value+" "+tagString;
    searchBox.value=searchBox.value.trim();
    document.getElementById("searchform").submit();
}

jQuery.fn.loadDialog = function( url ) {
  return this.each(function(){ $(this).load( url, function() { $(this).find('.dialog').each(function(){ eval($(this).attr('onajaxload'))}); } ); });
};

function addHistory( index )
{
    var searchBox=document.getElementById("textField");
    searchBox.value=searchBox.value+" #"+index;
}

