﻿$(document).ready(function () {
    $("#course_search_keywords").keypress(function () {
        if (event.keyCode == 13) {
            //alert("course_search_keywords got key pressed " + event.keyCode);
            return BRITSearch();
        }
    });

    $("#txtSearchAll").keypress(function () {
        if (event.keyCode == 13) {
            //alert("txtSearchAll got key pressed " + event.keyCode);
            return BRITSearch();
        }
    });

    //onFocus="javascript:if(this.value=='COURSE SEARCH'){this.value='';}" onBlur="javascript:if(this.value==''){this.value='COURSE SEARCH';}" -->
    $("#course_search_keywords").focus(function () {
        if ($("#course_search_keywords").val() == 'COURSE SEARCH') {
            $("#course_search_keywords").val('');
            $("#txtSearchAll").val("Search this site...");
        }
    });

    $("#course_search_keywords").blur(function () {
        if ($("#course_search_keywords").val() == '') {
            $("#course_search_keywords").val('COURSE SEARCH');
        }
    });

    $("#txtSearchAll").focus(function () {
        if ($("#txtSearchAll").val() == 'Search this site...') {
            $("#txtSearchAll").val('');
            $("#course_search_keywords").val('COURSE SEARCH');
        }
    });

    $("#txtSearchAll").blur(function () {
        if ($("#txtSearchAll").val() == '') {
            $("#txtSearchAll").val('Search this site...');
        }
    });

});

/*
function SearchAllSite() {
//alert("spFormOnSubmitCalled: " + spFormOnSubmitCalled);
var keyWords = $("#txtSearchAll").val();
var searchPageUrl = "/Search/Results.aspx?k=" + keyWords;
if(keyWords.length > 0){
alert("calling SearchAllSite");
//_spFormOnSubmitCalled = true;
window.location = searchPageUrl;
}
}*/

function BRITSearch() {
    var courseKeyWords = $("#course_search_keywords").val();
    var keyWords = $("#txtSearchAll").val();

    if (courseKeyWords.length > 0 && courseKeyWords != "COURSE SEARCH") {
        //_spFormOnSubmitWrapper() sets a variable to true 
        //when the form is submitted for the first time. Reloading the page resets the variable to false. 
        //But using Ajax the page is not reloaded and the variable is always true after doing the first Ajax call 
        //which causes the second Ajax call to fail.
        //http://andreasglaser.net/post/2009/08/04/SharePoint-2007-Ajax-and-onsubmit3dreturn-_spFormOnSubmitWrapper()3b.aspx
        //DO NOT remove below statement
        _spFormOnSubmitCalled = true;

        var searchPageUrl = "/Search/CourseResults.aspx?k=" + courseKeyWords + "&s=Courses";
        window.location = searchPageUrl;
    }
    else
        if (keyWords.length > 0 && keyWords != "Search this site...") {
            _spFormOnSubmitCalled = true;

            //var searchPageUrl = "/Search/Results.aspx?k=" + keyWords;
            var searchPageUrl = "/Search/SiteSearchResults.aspx?k=" + keyWords;            
            window.location = searchPageUrl;
        }

        return false;
}  

