var DEF_VAL   = "search here..."; // Default Value
var isSafari = (navigator.userAgent.indexOf("AppleWebKit") !=-1); // Detecting not only Safari but WebKit-based browsers
var _searchFieldFirstFocus = true;

run();

function run()
{
	var oldOnload = window.onload; // window.onload will be overwritten, so save the old value
	if (typeof(window.onload) != "function") {
		window.onload = init;
	} else {
		window.onload = function() {
			oldOnload();
			init();
		}
	}
}

function init() 
{
	if (!document.getElementById) return;
	var theSearchField = document.getElementById('globalSearchField'); // Enter search box's id attribute here
	if (isSafari) {
        // Changing type to 'search' from 'text'
        // and inserting 'autosave,' 'results,' 'placeholder' values for Safari and WebKit-based browsers
		theSearchField.setAttribute('type', 'search');
		theSearchField.setAttribute('autosave', 'saved.data');
		theSearchField.setAttribute('results', '5');
		theSearchField.setAttribute('placeholder', DEF_VAL);
	}
}

// only ever clears the first time a user sets focus to the field.
// subsequent edits will keep the value.
function ClearSearchFieldOnFirstFocus(element) 
{
   if (_searchFieldFirstFocus)
   {
	   element.value = "";
	   element.style.color = "black";
	   _searchFieldFirstFocus = false;
	}
}


//allow the user to hit enter to submit the search
function SubmitOnEnterKey(e)
{
   var keynum = -1;
   
   if(window.event) // IE
   {
      keynum = e.keyCode
   }
   else if(e.which) // Netscape/Firefox/Opera
   {
      keynum = e.which
   }

   if (keynum == 13)/* || (event.keyCode==40 && event.srcElement.type!='select-one')) && (event.srcElement.type != 'textarea') && (event.srcElement.type != 'button') && (event.srcElement.type != 'submit')) {event.keyCode=9; return true;};*/
   	OnSearchButtonClick();
   	
   return true;
}
//Determines if there is a form called 'searchForm' on the current page and updates the form them submits.
//... if no form found, the page is redirected to search page with minimal search options
function OnSearchButtonClick()
{
   var theSearchField = document.getElementById('globalSearchField'); 
   
   ClearSearchFieldOnFirstFocus(theSearchField);
   
   if (document.getElementById('searchForm'))  //searchForm must contain a hidden searchField (or text field)
   {
      //hook into the searchForm and update the search field
      var searchForm = document.getElementById('searchForm');
      searchForm.searchField.value = theSearchField.value;
      
      //submit the form
      searchForm.submit();
   }
   else
   {
      document.location = "search.asp?haveSearched=y&searchField=" + theSearchField.value;
   }
}