// initialize global varibles
var typeAheadFocus = 'false';
var typeAheadActiveOption = 0;
var activeOptionId = '';
var typeAheadDirectSelection = 'no';
var newQueryPending = 'no';
var typeAheadTextInputLength = 0;
var lastTypeAheadTextInput = "";
var lastTypeAheadTextInputLength = 0;
var typeAheadTextInputInitialFocus = 'no';
var formSubmissionInProgress = 'no';
var typeAheadInstruction = '';
var searchStringPrompt = 'Please enter some keywords';


$(document).ready(function(){

	// if simple search autocomplete cookie exists then toggle the checkbox to reflect this
	try{
		if(readCookie('simpleSearchAutoCompleteCookie')){
			document.simpleSearchFrm.typeAheadToggle.checked = true;
		}
	}
	catch (e){
	
	}

	if( $('#typeAheadTextInput') ){
	
		typeAheadInstruction = $('#typeAheadTextInput').val();
	
	}
    
    // listen to see if the type ahead input gains focus and set flag and input value accordingly
    $('#typeAheadTextInput').focus(function () {
        
        if(typeAheadTextInputInitialFocus == 'no'){
            document.simpleSearchFrm.simpleSearchString.value = '';
            typeAheadTextInputInitialFocus = 'yes'
        } 
        typeAheadFocus = 'true';
         
    });   
    
    // listen to see if the type ahead input drops focus and set flag accordingly
    $('#typeAheadTextInput').blur(function () {
         typeAheadFocus = 'false';
    });
     
	// listen to see if user is clicking outside of dropdown and input - and if so close the drop down
    $(document).mousedown(function (e) {
    
    	if( $('#typeAheadListBox') ){
         
          var mouseXPos = e.pageX
          var mouseYPos = e.pageY;

          var typeAheadInputPos = findPos(document.getElementById('typeAheadTextInputWrapper'));
          var typeAheadInputWidth = $('#typeAheadTextInputWrapper').width();
          var typeAheadInputHeight = $('#typeAheadTextInputWrapper').height();
          var typeAheadListBoxPos = findPos(document.getElementById('typeAheadListBox'));
          var typeAheadListBoxWidth = $('#typeAheadListBox').width();
          var typeAheadListBoxHeight = $('#typeAheadListBox').height();
        
          if( (mouseXPos > typeAheadInputPos[0]) && (mouseXPos < (typeAheadInputPos[0] + typeAheadInputWidth)) && (mouseYPos > typeAheadInputPos[1]) && (mouseYPos < (typeAheadInputPos[1] + typeAheadInputHeight))){
               
          }
          else{
               if( (mouseXPos > typeAheadListBoxPos[0]) && (mouseXPos < (typeAheadListBoxPos[0] + typeAheadListBoxWidth)) && (mouseYPos > typeAheadListBoxPos[1]) && (mouseYPos < (typeAheadListBoxPos[1] + typeAheadListBoxHeight))){
                    
               }
               else{
                    $('#typeAheadListBox').remove();
                    typeAheadActiveOption = 0;
               }
          }
          
        }
        
    }); 
    
    // listen for keyup
    $(document).keyup(function(event){
        
        if( document.simpleSearchFrm.typeAheadToggle.checked == false){           
        
            // check to see if the type ahead input has focus 
            if(typeAheadFocus == 'true'){
                
                var unicode=event.keyCode? event.keyCode : event.charCode;          
                
                // check to see which key was pressed
                
                // if arrow navigation keys have been pressed set active option and classes accordingly
                if(unicode == 38){
                    typeAheadDirectSelection = 'yes';
                    $('#typeAheadList a').removeClass('typeAheadActive');
                    if(typeAheadActiveOption >= 1){
                        typeAheadActiveOption = typeAheadActiveOption - 1;
                        activeOptionId = 'typeAheadListOption' + typeAheadActiveOption;
                        $('#' + activeOptionId).addClass('typeAheadActive');
                    }
                    if(typeAheadActiveOption < 1){
                        typeAheadDirectSelection = 'no';
                    }
                }
                else if(unicode == 40){
                    typeAheadDirectSelection = 'yes';
                    var typeAheadOptionsLength = $('#typeAheadList a').length;
                    $('#typeAheadList a').removeClass('typeAheadActive');
                    if(typeAheadActiveOption < typeAheadOptionsLength){
                        typeAheadActiveOption = typeAheadActiveOption + 1;
                        activeOptionId = 'typeAheadListOption' + typeAheadActiveOption;
                        $('#' + activeOptionId).addClass('typeAheadActive');
                    }
                    else if(typeAheadActiveOption >= typeAheadOptionsLength){
                        typeAheadActiveOption = 0;
                    }
                }
                // if return key pressed relocate to href value of active option
                else if((unicode == 13)){
        
                    if(typeAheadDirectSelection == 'yes'){
                    	var newPageUrl = document.location.href = $('#'+activeOptionId).attr('href');
                    	$('#typeAheadListBox').remove();           
                      	document.location.href = newPageUrl;
                    }
    
                    return false;
                }
             	else if((unicode == 27)){
             
                 	if( $('#typeAheadListBox') ){
                 	
                 		$('#typeAheadListBox').remove();
                 		
                 		lastTypeAheadTextInputLength = 0;
                 	
                 	}
                 
                 } 
                // conditionally make a type ahead query pending check
                else{         
                
                    // determine length of current query character length
                    typeAheadTextInputLength = $('#typeAheadTextInput').val().length;
                    
                    // if current query character length is 3 or more then proceed
                    if(typeAheadTextInputLength >=3){           
                        
                        if($('#typeAheadTextInput').val() == lastTypeAheadTextInput){
                            
                            queryPendingCheck();
                             
                       }
                        else{
                      
                            setTimeout("queryPendingCheck()", 1000)
                            
                        }
                        
                    }
                    
                 	else{
                 
	                 	if( $('#typeAheadListBox') ){
	                 	
	                 		$('#typeAheadListBox').remove();
	                 		
	                 		lastTypeAheadTextInputLength = 0;
	                 	
	                 	}
	                 
	                 }   
                
                }
                
            }
            
        }
        
    });   
    
});

// only make a type ahead query if the user there are now more characters in the input than when the last query was made and there is no form submission currently in progress
function queryPendingCheck(){
 
    if(($('#typeAheadTextInput').val() != lastTypeAheadTextInput) && formSubmissionInProgress == 'no'){
        typeAheadAjaxRequest(); 
    }
      
}

// make the type ahead ajax request
function typeAheadAjaxRequest(){
  
  var dynamicTypeAheadRequest;
  
  try{
    // Firefox, Opera 8.0+, Safari
      dynamicTypeAheadRequest=new XMLHttpRequest();
    }
  catch (e){
    // Internet Explorer
    try{
        dynamicTypeAheadRequest=new ActiveXObject('Msxml2.XMLHTTP');
      }
    catch (e){
      try{
          dynamicTypeAheadRequest=new ActiveXObject('Microsoft.XMLHTTP');
        }
      catch (e){
          alert('Your browser does not support AJAX!');
        }
      }
    }
    
    dynamicTypeAheadRequest.onreadystatechange=function(){
    
    	// if there is a successful response
        if(dynamicTypeAheadRequest.readyState==4){
        
        var typeAheadJsonResponse = '';
        
        // set a variable for the JSON object which is returned    
        typeAheadJsonResponse =  eval('(' + dynamicTypeAheadRequest.responseText + ')');
        
        // check to see that the response has results
        if( (typeof(typeAheadJsonResponse.typeAheadResponse.books) != 'undefined') || (typeof(typeAheadJsonResponse.typeAheadResponse.otherFormats) != 'undefined') || (typeof(typeAheadJsonResponse.typeAheadResponse.authors) != 'undefined') || (typeof(typeAheadJsonResponse.typeAheadResponse.extras) != 'undefined') ){
	        
	        // dynamically build the HTML elements
	        typeAheadListBoxContent = document.createElement('div');
	        typeAheadListBoxContent.setAttribute( 'id', 'typeAheadListBoxContent' );
	        
	        
	        typeAheadMainHeading = document.createElement('h3');
	        typeAheadMainHeadingLogo = document.createElement('img');
	        typeAheadMainHeadingLogo.setAttribute( 'src', '/waterstonesweb/graphics/simple_search/heading_logo.png' );
	        typeAheadMainHeadingText = document.createElement('span');
	        typeAheadMainHeadingText.appendChild(document.createTextNode('search suggestions'));
	        
	        typeAheadMainHeading.appendChild(typeAheadMainHeadingLogo);
	        typeAheadMainHeading.appendChild(typeAheadMainHeadingText);
	        
	        typeAheadSelectList = document.createElement('ul');
	        typeAheadSelectList.setAttribute( 'id', 'typeAheadList' );
	        
	        // check to see if specific types of content have been returned in the JSON object and build the specific HTML for those      
	        
	        var optionCount = 1;
	        
	        // if books exist loop over the number returned building a drop down entry for each
	        if(typeAheadJsonResponse.typeAheadResponse.books){
	        
	        	var booksLength = typeAheadJsonResponse.typeAheadResponse.books.length;
	        
		        typeAheadSelectOptionsHeading = document.createElement('li');
		        typeAheadSelectOptionsHeading.className = 'optionsTitle';
		        typeAheadSelectOptionsHeading.appendChild(document.createTextNode('Books'));
		        typeAheadSelectList.appendChild(typeAheadSelectOptionsHeading);
		        for (i=0;i<booksLength;i++){
		            typeAheadSelectOption = document.createElement('li');
		            typeAheadSelectOptionLink = document.createElement('a');
		            typeAheadSelectOptionLink.className = 'typeAheadListOption clearfix';
		            typeAheadSelectOptionLink.setAttribute( 'id', ('typeAheadListOption' + optionCount) );
		            typeAheadSelectOptionLink.setAttribute( 'href', typeAheadJsonResponse.typeAheadResponse.books[i].url );
		            typeAheadSelectOptionLinkIcon = document.createElement('img');
		            typeAheadSelectOptionLinkIcon.setAttribute( 'src', '/waterstonesweb/graphics/simple_search/format_icons/format_books.png' );
		            typeAheadSelectOptionLink.appendChild(typeAheadSelectOptionLinkIcon);
		            typeAheadSelectOptionLinkContent = document.createElement('span');
		            typeAheadSelectOptionLinkContentTitle = document.createElement('span');
		            
					var productTitle = typeAheadJsonResponse.typeAheadResponse.books[i].title;	
					var titleReplaced = stringHighlight(productTitle);
		           	titleReplaced = '<em>' + titleReplaced + '</em>';
		            $(typeAheadSelectOptionLinkContent).append(titleReplaced);
		            
		            var productAuthor = typeAheadJsonResponse.typeAheadResponse.books[i].author;
		            var productAuthorTemp = typeAheadJsonResponse.typeAheadResponse.books[i].author.toLowerCase();
					var authorReplaced = stringHighlight(productAuthor);
		           	authorReplaced = ' by ' + authorReplaced;
		            $(typeAheadSelectOptionLinkContent).append(authorReplaced);
		            
					typeAheadSelectOptionLinkContent.appendChild(document.createTextNode(' (' + typeAheadJsonResponse.typeAheadResponse.books[i].format + ')'));
		            typeAheadSelectOptionLink.appendChild(typeAheadSelectOptionLinkContent);
		            typeAheadSelectOption.appendChild(typeAheadSelectOptionLink);
		            typeAheadSelectList.appendChild(typeAheadSelectOption);
		            
		            optionCount = optionCount + 1;
		            
		        }
		    
		    }
		    
		    // if other formats exist loop over the number returned building a drop down entry for each
	        if(typeAheadJsonResponse.typeAheadResponse.otherFormats){
	        
	        	var otherFormatsLength = typeAheadJsonResponse.typeAheadResponse.otherFormats.length;
	        
		        typeAheadSelectOptionsHeading = document.createElement('li');
		        typeAheadSelectOptionsHeading.className = 'optionsTitle';
		        typeAheadSelectOptionsHeading.appendChild(document.createTextNode('Other formats'));
		        typeAheadSelectList.appendChild(typeAheadSelectOptionsHeading);
		        for (i=0;i<otherFormatsLength;i++){
		            typeAheadSelectOption = document.createElement('li');
		            typeAheadSelectOptionLink = document.createElement('a');
		            typeAheadSelectOptionLink.className = 'typeAheadListOption clearfix';
		            typeAheadSelectOptionLink.setAttribute( 'id', ('typeAheadListOption' + optionCount) );
		            typeAheadSelectOptionLink.setAttribute( 'href', typeAheadJsonResponse.typeAheadResponse.otherFormats[i].url );
		            
		            typeAheadSelectOptionLinkIcon = document.createElement('img');
		            var imageFileName = "";
		            
		            // Determine which other format icon to use
		            switch (typeAheadJsonResponse.typeAheadResponse.otherFormats[i].format)
					{
					case 'otherFormats':
					  	typeAheadSelectOptionLinkIcon.setAttribute( 'src', '/waterstonesweb/graphics/simple_search/format_icons/other_formats_generic.png' );
					  	break;
					case 'audio':
						typeAheadSelectOptionLinkIcon.setAttribute( 'src', '/waterstonesweb/graphics/simple_search/format_icons/audio.png' );           
					  	break;
					case 'dvdVideo':
						typeAheadSelectOptionLinkIcon.setAttribute( 'src', '/waterstonesweb/graphics/simple_search/format_icons/dvd_video.png' );
					  	break;
					case 'games':
						typeAheadSelectOptionLinkIcon.setAttribute( 'src', '/waterstonesweb/graphics/simple_search/format_icons/games.png' );
					  	break;
					default:
						typeAheadSelectOptionLinkIcon.setAttribute( 'src', '/waterstonesweb/graphics/simple_search/format_icons/book.png' );
					}
		            
		            typeAheadSelectOptionLink.appendChild(typeAheadSelectOptionLinkIcon);
		            typeAheadSelectOptionLinkContent = document.createElement('span');
		            
		            var productTitle = typeAheadJsonResponse.typeAheadResponse.otherFormats[i].title;	
					var titleReplaced = stringHighlight(productTitle);
		           	titleReplaced = '<em>' + titleReplaced + '</em>';
		            $(typeAheadSelectOptionLinkContent).append(titleReplaced);
		            
		            var productAuthor = typeAheadJsonResponse.typeAheadResponse.otherFormats[i].author;
		            var productAuthorTemp = typeAheadJsonResponse.typeAheadResponse.otherFormats[i].author.toLowerCase();
					var authorReplaced = stringHighlight(productAuthor);
		           	authorReplaced = ' by ' + authorReplaced;
		            $(typeAheadSelectOptionLinkContent).append(authorReplaced);	            
		            
		            typeAheadSelectOptionLinkContent.appendChild(document.createTextNode(' (' + typeAheadJsonResponse.typeAheadResponse.otherFormats[i].format + ')'));
		            typeAheadSelectOptionLink.appendChild(typeAheadSelectOptionLinkContent);
		            typeAheadSelectOption.appendChild(typeAheadSelectOptionLink);
		            typeAheadSelectList.appendChild(typeAheadSelectOption);
		            
		            optionCount = optionCount + 1;           
		            
		        }
		        
		    }
	
	        // if authors exist loop over the number returned building a drop down entry for each	    
	        if(typeAheadJsonResponse.typeAheadResponse.authors){
	        
	        	var authorsLength = typeAheadJsonResponse.typeAheadResponse.authors.length;
	         
		        typeAheadSelectOptionsHeading = document.createElement('li');
		        typeAheadSelectOptionsHeading.className = 'optionsTitle';
		        typeAheadSelectOptionsHeading.appendChild(document.createTextNode('Authors'));
		        typeAheadSelectList.appendChild(typeAheadSelectOptionsHeading);
		        for (i=0;i<authorsLength;i++){
		            typeAheadSelectOption = document.createElement('li');
		            typeAheadSelectOptionLink = document.createElement('a');
		            typeAheadSelectOptionLink.className = 'typeAheadListOption clearfix';
		            typeAheadSelectOptionLink.setAttribute( 'id', ('typeAheadListOption' + optionCount) );
		            typeAheadSelectOptionLink.setAttribute( 'href', typeAheadJsonResponse.typeAheadResponse.authors[i].url );
		            typeAheadSelectOptionLinkIcon = document.createElement('img');
		            typeAheadSelectOptionLinkIcon.setAttribute( 'src', '/waterstonesweb/graphics/simple_search/format_icons/format_authors.png' );
		            typeAheadSelectOptionLink.appendChild(typeAheadSelectOptionLinkIcon);
		            typeAheadSelectOptionLinkContent = document.createElement('span');
		            
		            var productAuthor = typeAheadJsonResponse.typeAheadResponse.authors[i].author;	
					var authorReplaced = stringHighlight(productAuthor);
		           	authorReplaced = '<em>' + authorReplaced + '</em>';
		            $(typeAheadSelectOptionLinkContent).append(authorReplaced);
		            
					var productTitle = typeAheadJsonResponse.typeAheadResponse.authors[i].title;	
					var titleReplaced = stringHighlight(productTitle);
		           	titleReplaced = ' (' + titleReplaced + ')';
		            $(typeAheadSelectOptionLinkContent).append(titleReplaced);            
		            
		            typeAheadSelectOptionLink.appendChild(typeAheadSelectOptionLinkContent);
		            typeAheadSelectOption.appendChild(typeAheadSelectOptionLink);
		            typeAheadSelectList.appendChild(typeAheadSelectOption);
		            
		            optionCount = optionCount + 1;           
		            
		        }
		        
		    }
	
	        // if extras exist loop over the number returned building a drop down entry for each	    
	        if(typeAheadJsonResponse.typeAheadResponse.extras){
	        
	        	var extrasLength = typeAheadJsonResponse.typeAheadResponse.extras.length;
		        
		        typeAheadSelectOptionsHeading = document.createElement('li');
		        typeAheadSelectOptionsHeading.className = 'optionsTitle';
		        typeAheadSelectOptionsHeading.appendChild(document.createTextNode('Extras'));
		        typeAheadSelectList.appendChild(typeAheadSelectOptionsHeading);     
		        for (i=0;i<extrasLength;i++){
		            typeAheadSelectOption = document.createElement('li');  
		            typeAheadSelectOptionLink = document.createElement('a');
		            typeAheadSelectOptionLink.className = 'typeAheadListOption clearfix'; 
		            typeAheadSelectOptionLink.setAttribute( 'id', ('typeAheadListOption' + optionCount) );
		            typeAheadSelectOptionLink.setAttribute( 'href', typeAheadJsonResponse.typeAheadResponse.extras[i].url );
		            typeAheadSelectOptionLinkIcon = document.createElement('img');
		            typeAheadSelectOptionLinkIcon.setAttribute( 'src', '/waterstonesweb/graphics/simple_search/format_icons/format_extras.png' );
		            typeAheadSelectOptionLink.appendChild(typeAheadSelectOptionLinkIcon);
		            typeAheadSelectOptionLinkContent = document.createElement('span');
		            
		            var extraTitle = typeAheadJsonResponse.typeAheadResponse.extras[i].title;
		            if (extraTitle != null){
		            	var titleReplaced = stringHighlight(extraTitle);
			           	titleReplaced = '<em>' + titleReplaced + '</em>';
			            $(typeAheadSelectOptionLinkContent).append(titleReplaced);
		            }						            
		            
		            typeAheadSelectOptionLinkContent.appendChild(document.createTextNode(' ' + typeAheadJsonResponse.typeAheadResponse.extras[i].description));
		            typeAheadSelectOptionLink.appendChild(typeAheadSelectOptionLinkContent);
		            typeAheadSelectOption.appendChild(typeAheadSelectOptionLink);
		            typeAheadSelectList.appendChild(typeAheadSelectOption);
		            
		            optionCount = optionCount + 1;
		            
		        }
		        
		    }
	        
	        optionCount = optionCount - 1;
	        
	 		// remove any existing loading
	     	if($('#typeAheadLoading')){
	     		$('#typeAheadLoading').remove();
	     	}
	     	
	     	// remove any existing list
		  	if($('#typeAheadList')){
		  		$('#typeAheadListBoxContent').remove();
		  	}
	 		
	 		// append the new select box HTML element to the body tag in the DOM
	 		typeAheadListBoxContent.appendChild(typeAheadMainHeading);
	 		typeAheadListBoxContent.appendChild(typeAheadSelectList);
	 		
	        $('#typeAheadListBottom').before(typeAheadListBoxContent);
	
	        // bind listener behaviour to the links within the new select box HTML element
	        $('#typeAheadList a').mouseover(function () {
	        	 $('#typeAheadList a').removeClass('typeAheadActive');
	             $(this).addClass('typeAheadActive');
	             typeAheadDirectSelection = 'no';
	             typeAheadActiveOption = eval($(this).attr('id').split('typeAheadListOption')[1]);
	        });
	        $('#typeAheadList a').mouseout(function () {
	             $(this).removeClass('typeAheadActive');
	        });
	        
	     lastTypeAheadTextInputLength = typeAheadTextInputLength;
  
	     }
	     // if no results are returned remove the type ahead list box
	     else{     
	     
		  	// remove any existing type ahead list box
		  	if($('#typeAheadListBox')){
		  		$('#typeAheadListBox').remove();
		  	}
		  	
	    } 
     }
  }
    
  // set the actual ajax query url and parameters and make the request
  if(dynamicTypeAheadRequest){
  
  	// remove any existing type ahead list box
  	if($('#typeAheadListBox')){
  		$('#typeAheadListBox').remove();
  	}
  
  	// build initial type ahead list box structure
    typeAheadSelectBox = document.createElement('div');
    typeAheadSelectBox.setAttribute( 'id', 'typeAheadListBox' );
    typeAheadSelectBox.className = 'typeAheadListBox';
    
    // build a loading element and append to the list box
	typeAheadLoading = document.createElement('div');
    typeAheadLoading.setAttribute( 'id', 'typeAheadLoading' );    
    
	typeAheadLoading.appendChild(document.createTextNode('Loading suggestions '));
	
	typeAheadLoadingProgress = document.createElement('img');
	typeAheadLoadingProgress.setAttribute( 'id', 'loadingProgress' );
    typeAheadLoadingProgress.setAttribute( 'src', '/waterstonesweb/graphics/simple_search/loading_progress.gif' );
    
    typeAheadLoading.appendChild(typeAheadLoadingProgress);
	            
    typeAheadSelectListBottom = document.createElement('span');
    typeAheadSelectListBottom.setAttribute( 'id', 'typeAheadListBottom' );
    
    typeAheadSelectBox.appendChild(typeAheadLoading);
    typeAheadSelectBox.appendChild(typeAheadSelectListBottom);
        
	// append the new select box HTML element to the body tag in the DOM
    $('body').append(typeAheadSelectBox);
        
    // position it based on the text input parent position
    var typeAheadInputPos = findPos(document.getElementById('typeAheadTextInput'));
    var typeAheadInputHeight = $('#typeAheadTextInput').height();      
        
    $('#typeAheadListBox').css({ position: "absolute", top: (typeAheadInputPos[1]+typeAheadInputHeight+12), left: typeAheadInputPos[0] });
   
  	lastTypeAheadTextInput = document.simpleSearchFrm.simpleSearchString.value;
   
    dynamicTypeAheadRequest.open('GET', '/waterstonesweb/searchByKeyword.do?simpleSearchString='+document.simpleSearchFrm.simpleSearchString.value, true);
    //dynamicTypeAheadRequest.open('GET', '/waterstonesweb/scripts/type_ahead_response.txt', true);
    dynamicTypeAheadRequest.send(null);
  }
 
}

// only carry out an actual search if the text input has characters in it (which are not the prompt) and there is not currently an active drop down selection
function validateSimpleSearchTypeAhead(){

	searchStringPrompt = 'Please enter some keywords';
        
    if(typeAheadDirectSelection == 'yes'){
        return false;
    }
    
    if( (document.simpleSearchFrm.simpleSearchString.value.length <= 0) || ( $('#typeAheadTextInput').val() == typeAheadInstruction ) || ( $('#typeAheadTextInput').val() == searchStringPrompt ) ){
        document.simpleSearchFrm.simpleSearchString.value = searchStringPrompt;
        return false;
    }
    else{
    	formSubmissionInProgress = 'yes';
    	
    	// set cookie to disable simple search autocomplete / delete cookie conditionally
  		if( document.simpleSearchFrm.typeAheadToggle.checked == true){
  			createCookie('simpleSearchAutoCompleteCookie','off');
  		}
  		else{
  			eraseCookie('simpleSearchAutoCompleteCookie');
  		}  	 	
        return true;
    }
    
}
// determine the recurrsive position of a DOM element
function findPos(obj) {
  var curleft = curtop = 0;
  try{
	  if (obj.offsetParent) {
	    curleft = obj.offsetLeft
	    curtop = obj.offsetTop
	    while (obj = obj.offsetParent) {
	      curleft += obj.offsetLeft
	      curtop += obj.offsetTop
	    }
	  }
	}
	catch(err){
	}
  return [curleft,curtop];
}

// bold the matching search term, upper case or lower case, in the passed string and return this new string
function stringHighlight(newString){

	var newStringTemp = newString.toLowerCase();

	var searchTerm = document.simpleSearchFrm.simpleSearchString.value.toLowerCase();
	var searchTermLength = document.simpleSearchFrm.simpleSearchString.value.length;
	
	var indexTemp = 0;
	var indexInstance = 0;
	var indexCount = 0;
	
	var indexInstance = newStringTemp.indexOf(searchTerm,indexTemp);
	
	if( indexInstance != -1 ){
		
		while ( indexInstance != -1 ){
		
			var indexInstance = newStringTemp.indexOf(searchTerm,indexTemp);
			
			if( indexInstance != -1 ){
			
				//the increments used below are specifically for the length of characters in the tags used to wrap the search terms and will need to change if the tag is changed
				newString = newString.substr(0,indexInstance) + '<strong>' + newString.substr(indexInstance);
				newStringTemp = newStringTemp.substr(0,indexInstance) + '<strong>' + newStringTemp.substr(indexInstance);
				indexInstance = indexInstance + 8;
				newString = newString.substr(0,indexInstance+searchTermLength) + '</strong>' + newString.substr(indexInstance+searchTermLength);
				newStringTemp = newStringTemp.substr(0,indexInstance+searchTermLength) + '</strong>' + newStringTemp.substr(indexInstance+searchTermLength);
		
				indexTemp = indexInstance+searchTermLength+9;
				
			}
		}
		
	}
	
	return(newString);
	
}
