﻿/**************************************************
    Search Entry
**************************************************/

// When the user clicks to enter a value to search for this function
// is called to remove the default text if it is in the text box.
function SearchEntryText_OnFocus(element, parentID)  {
    var data = SearchEntry_GetData(parentID);
    if (!data)  {
        return null;
    }


    if (element.value.replace(/^\s+|\s+$/g,"") == data.defaultSearchText)  {
        element.value="";
    }
}


// If the user exits the search text box this function is called and puts the
// default text back into the search text box if nothing else was entered.
function SearchEntryText_OnBlur(element, parentID)  {

    var data = SearchEntry_GetData(parentID);
    if (!data)  {
        return null;
    }


    if (element.value.replace(/^\s+|\s+$/g,"") == "")  {
        element.value=data.defaultSearchText;
    }
}


// Is called whenever a key press event occurs in the search text box
// in order to catch an enter key click.  We want to have the enter key
// press act as if the search button was clicked.
function SearchEntryText_EnterKeyPress(e, buttonID, parentID)  { 

    var evt = e ? e : window.event;
    if (evt.keyCode == 13)  { 
        SearchEntryButton_OnClick(document.getElementById(buttonID), parentID);
        return false; 
    } 
    
}


// Function captures the search entry button click and checks to see if the user has
// entered a search string.  If there is a search string the user is redirected to
// the baird search page through the go.aspx page, otherwise an error message is displayed.
function SearchEntryButton_OnClick(element, parentID)  {

    var data = SearchEntry_GetData(parentID);
    if (!data)  {
        return null;
    }


    var searchText = document.getElementById(data.searchTextID).value.replace(/^\s+|\s+$/g,"");
    if (searchText=="" || searchText==data.defaultSearchText)  {
        alert("Please enter a word or phrase and search again.");
    }  else  {
        window.location.href=data.searchResultsUrl + "?query=" + escape(searchText);
    }
}


// The Search Entry control builds a script object at the bottom of the 
// containing page with the data needed for the client side scripting.  
// This function retrieves and builds that object.
function SearchEntry_GetData(parentID) {
    try {
        data = eval(parentID + "_Data");
    }
    catch(e) {}
    return data;
}
