
$(function () {
    $('.watermark:text').each(function () {
        var title = $(this).attr('title');
        if ($(this).val()=='')
            $(this).val(title);
    });
    $('.watermark:text').focusin(function () {
        if ($(this).val() == $(this).attr('title')) {
            $(this).val('');
            $(this).data('color',$(this).css('color'));
            $(this).css('color', '#404040');
        }
    });
    $('.watermark:text').focusout(function () {
        var val = $(this).val();
        if ($.trim(val) == '') {
            var title = $(this).attr('title');
            $(this).val(title);
            if ($(this).data('color'))
            	$(this).css('color', $(this).data('color'));
        }
    });
});

$(function () {
    $("#searchbox [id$='SearchText']").autocomplete({
        source: function (request, response, callback) {
            var SearchString = $("#searchbox [id$='SearchText']").val();
            $.ajax({ url: "http://ww2.medway.gov.uk/WebServices/Google_WebService/GoogleSearch.asmx/GetlgslText?prefixText=" + JSON.stringify(SearchString) + "&format=jsonp&jsoncallback=?",
                dataType: "jsonp",
                json: "jsoncallback",
                success: function (json) {
                    response($.map(json.d, function (item) {
                        return {
                            data: item,
                            value: item,
                            result: item
                        }
                    }))
                },
                error: function (xhr, msg) {
                    //alert(xhr.responseText + " " + msg);
                }
            });
        },
        minLength: 1,
        select: function (event, ui) {

            document.location = "http://www.medway.gov.uk/search?q=" + encodeURIComponent(ui.item.value) + "&btnG=Google+Search&site=All&client=Medway_frontend&output=xml_no_dtd&proxystylesheet=Medway_frontend";
        }
    });
});

$(function () {
    $("#q").autocomplete({
        source: function (request, response, callback) {
            var SearchString = $("#q").val();
            $.ajax({ url: "http://ww2.medway.gov.uk/WebServices/Google_WebService/GoogleSearch.asmx/GetlgslText?prefixText=" + JSON.stringify(SearchString) + "&format=jsonp&jsoncallback=?",
                dataType: "jsonp",
                json: "jsoncallback",
                success: function (json) {
                    response($.map(json.d, function (item) {
                        return {
                            data: item,
                            value: item,
                            result: item
                        }
                    }))
                },
                error: function (xhr, msg) {
                    //alert(xhr.responseText + " " + msg);
                }
            });
        },
        minLength: 1,
        select: function (event, ui) {

            document.location = "http://www.medway.gov.uk/search?q=" + encodeURIComponent(ui.item.value) + "&btnG=Google+Search&site=All&client=Medway_frontend&output=xml_no_dtd&proxystylesheet=Medway_frontend";
        }
    });
});

// The 3 functions below have been created to get the search facility working in the Lagan eForms Medwa header
// Not sure if this doSearch function is required anymore
function doSearch()
{
	document.location = "http://www.medway.gov.uk/search?q=" + encodeURIComponent($("#SearchText").value) + "&btnG=Google+Search&site=All&client=Medway_frontend&output=xml_no_dtd&proxystylesheet=Medway_frontend";
	return false;
}

$(function() {
    $("#searchbox [id$='ibtnSearch']").click(function() {

	window.location.href = "http://www.medway.gov.uk/search?q=" + encodeURIComponent($("#SearchText").val()) + "&btnG=Google+Search&site=All&client=Medway_frontend&output=xml_no_dtd&proxystylesheet=Medway_frontend";
        return false;
      }
    );
});

$(function() {
    $("#searchbox [id$='SearchText']").keypress(function(event) {
        var keycode = (event.keyCode ? event.keyCode : event.which);
        if (keycode == '13') {

            window.location.href = "http://www.medway.gov.uk/search?q=" + encodeURIComponent($("#SearchText").val()) + "&btnG=Google+Search&site=All&client=Medway_frontend&output=xml_no_dtd&proxystylesheet=Medway_frontend";
            return false;
        }
      }
    );
});
// The 3 functions above have been created to get the search facility working in the Lagan eForms Medwa header
