﻿function searchBoxClear() {	
	var searchTextbox = document.getElementById('ctl00_ContentPlaceHolderContent_SearchTextBox1');
	if (searchTextbox.value == "enter search") {
		searchTextbox.value = "";
		searchTextbox.className = "";
	}
	showRecentSearches();
}
function searchBoxReplace() {
	var searchTextbox = document.getElementById('ctl00_ContentPlaceHolderContent_SearchTextBox1');
	if (searchTextbox.value == "") {
		searchTextbox.value = "enter search";
		searchTextbox.className = "textbox_watermark";
	}
}

function saveSearch() {
    //define the list size
    var listSize = 10;
    
    //define the cookie expiration
    var now = new Date;
    var expDate = new Date(now.getTime() + (365*24*60*60*1000));
                
    //get the search term
    var qs = new Querystring();
    var searchTerm = unescape(qs.get('usterms','enter search'));
    
    //initialize the array
    var searchArray = init_array();
    var size;            
                
    //if the cookie exists, get the existing value and convert it into an array
    if (get_cookie('RecentSearches') != null) {
        get_array('RecentSearches',searchArray);
    }
    
    //check the rendered output of the page to see if any results were returned. If none, then set the search to a null.
    if (searchTerm != 'enter search') {
        var searchResults = document.getElementById('ctl00_ContentPlaceHolderContent_UltimateSearchOutput1');
        if (searchResults.innerHTML.indexOf('did not match any documents') > -1) {
            searchTerm = 'enter search';
        }
    }
        
    //the actual saving part        
    if (searchTerm != 'enter search') { //this term never gets saved. This is representatrive of a null search.
        size = next_entry(searchArray);                
        searchArray[size] = searchTerm; //add the current search to the end of the array                
        set_array('RecentSearches', searchArray, expDate);                                                
        
        for (var i=1; i < size; i++) { //delete any older duplicates
            if (searchArray[i] == searchTerm) {
                del_entry('RecentSearches', searchArray, i, expDate);
            }
        }                
        
        if (size > listSize) { //trim list down to listSize                  
            del_entry('RecentSearches', searchArray, 1, expDate);                                        
        }               
    } //end if
    
} //end function

function showRecentSearches() {
    //clear the current popup
    var searchPopup = document.getElementById('recentSearchPopup');
    searchPopup.innerHTML = '';
    
    //initialize the array
    var searchArray = init_array();
    
    //if the cookie exists, get the existing value and convert it into an array
    if (get_cookie('RecentSearches') != null) {            
        get_array('RecentSearches',searchArray);
        
        //add a header
        var searchPopupTitle = document.createElement('p');
        searchPopupTitle.innerHTML = 'Recent Searches';
        searchPopup.appendChild(searchPopupTitle);
        
        //add the closebox
        var closeBox = document.createElement('img');
        var closeLink = document.createElement('a');            
        closeBox.src = 'http://www.keylaboratory.com/images/closebox.gif';
        closeBox.alt = 'close search list';
        closeLink.href = 'javascript:hideRecentSearches();';
        closeLink.title = 'close search list';
        closeLink.appendChild(closeBox);
        searchPopup.appendChild(closeLink);
        
        //add a list
        var searchList = document.createElement('ul');
        searchPopup.appendChild(searchList);
        
        //populate the list, newest searches first                       
        for (var i=(next_entry(searchArray)-1); i > 0 ; i--) {
            var listItem = document.createElement('li');
            var linkItem = document.createElement('a');
            linkItem.innerHTML = (searchArray[i]);
            linkItem.href = '/resources/site/advanced_search.aspx?usterms=' + searchArray[i];
            linkItem.title = 'search again for "' + searchArray[i] + '"';
            listItem.appendChild(linkItem);
            searchList.appendChild(listItem);
        } //end for
        
        //actually show the damn thing
        if (searchPopup.filters)
            searchPopup.filters[0].apply();            
        searchPopup.style.display = 'block';
        if (searchPopup.filters)
            searchPopup.filters[0].play(0.2);
               
    } //end if
    placeRecentSearches(); 
} //end function
    
function hideRecentSearches() {
    var searchPopup = document.getElementById('recentSearchPopup');        
    searchPopup.style.display = 'none';
} //end function

function placeRecentSearches() {
    var searchPopup = document.getElementById('recentSearchPopup');
    var searchTextbox = document.getElementById('ctl00_ContentPlaceHolderContent_SearchTextBox1');
    var theForm = document.getElementById('aspnetForm');
    var searchTextboxX = findPosX(searchTextbox);
    var searchTextboxY = findPosY(searchTextbox);
    var formX = findPosX(theForm);
    var formY = findPosY(theForm);
    var searchPopupX = searchTextboxX - formX;
    var searchPopupY = searchTextboxY + 19;        
    searchPopup.style.left = searchPopupX + 'px';
    searchPopup.style.top = searchPopupY + 'px';    
} //end function