/************************************************
BEGIN: Save/Retrieve Search Terms And Populate Search Form
************************************************/
var saveUtility = null;
var isSaveUtilityLoaded = false;

/**
 * Saves the users search input.  This should be done onsubmit of the search form.  Pass in the value
 * the user entered into the search input field.
 */
function saveSearchInput(searchValue)
{
    try
    {
       var searchTermsTmp = document.forms.NavPartSearchForm.searchTerms.value;
       if(isSaveUtilityLoaded && searchTermsTmp != null && searchTermsTmp != "")
       {
           saveUtility.searchTerms = searchTermsTmp;
           saveUtility.save(1);  // how long to save the value (in days)
       }
   }
   catch(ex)
   {
   }
}

/**
 * Initializes the save utility object.  This function should be called on document.onload (or window.onload).
 * Calls saveUtilityLoaded when/if the save utility is successfully loaded.
 */
function initializeSaveUtility()
{
   try
   {
       saveUtility = new PObject("partSearchForm", {searchTerms:"Keyword or Part#"}, saveUtilityLoaded);
    }
    catch(ex)
    {
        // allow to continue.  Ignore exception.
    }
}

/**
 * Signifies that the save utility is loaded and ready to be used for reading stored values and for saving values.
 * This function populates the search form in the header of the website for each page.  Called by the
 * initializeSaveUtility() function.
 */
function saveUtilityLoaded()
{
    try
    {
        isSaveUtilityLoaded = true;
        var objFormSearchField = document.forms["NavPartSearchForm"].searchTerms;
        populateSearchForm(objFormSearchField);
    }
    catch(ex)
    {
        // ignore exception.
    }
}

/**
 * Populates the search form's search terms based on the saved value in the save utility. Called by saveUtilityLoaded function.
 */
function populateSearchForm(objSearchField)
{
    try
    {
        if(isSaveUtilityLoaded && objSearchField != null && objSearchField.value !== undefined)
            objSearchField.value = retrieveSavedSearchInput();
    }
    catch(ex)
    {
        // ignore
    }
}

/**
 * Retrieves the saved search input from the save utility.
 */
function retrieveSavedSearchInput()
{
    try
    {
        if(isSaveUtilityLoaded)
        {
            return saveUtility.searchTerms;
        }
    }
    catch(ex)
    {
        // ignore, return default value at end of function.
    }

    return "Keyword or Part #";
}

try
{
    Handler.add(window, "load", initializeSaveUtility);
}
catch(ex)
{
    // ignore, attempt to force the initializeSaveUtility to load:
    try
    {
        initializeSaveUtility();
    }
    catch(ex)
    {
        // ignore.
    }
}

/************************************************
END: Save/Retrieve Search Terms And Populate Search Form
************************************************/

