
var win = window;    // window to search.
var n   = 0;
var browser=navigator.appName;   // google's Chrome doesn't seem to recognize window.execScript


var debug = false;  // Set to display "alerts".

function findInPage(str) {

  if (debug) {alert ('FindInPage='+str);}

  var txt, i, found;

  if (str == "")
    return false;
 

  // Find next occurance of the given string on the page, wrap around to the
  // start of the page if necessary.


 
  if ((window.execScript) && (browser != 'Netscape')) {


    txt = win.document.body.createTextRange();

    // Find the nth match from the top of the page.

    for (i = 0; i <= n && (found = txt.findText(str)) != false; i++) {
      txt.moveStart("character", 1);
      txt.moveEnd("textedit");
    }

    // If found, mark it and scroll it into view.
    if (found) {
      if (debug) {alert ('Search found='+str);}
      txt.moveStart("character", -1);
      txt.findText(str);
      txt.select();
      txt.scrollIntoView();
      n++;
      window.scrollBy(0,50);
      return true
    }

    // Otherwise, start over at the top of the page and find first match.

    else {
      if (n > 0) {
        n = 0;
        findInPage(str);
      }

      // Not found anywhere, give message.
      else
        var coll = document.getElementById('p');
        coll.scrollIntoView(true);
        return false;
    }
  } else {
    // Look for match starting at the current point. If not found, rewind
    // back to the first match.


    if (!win.find(str))
      while(win.find(str, false, true))
        n++;
    else {
      n++;
      if (debug) {alert ('firefox found');}
      window.scrollBy(0,50);
      }
    // If not found in either direction, give message.
    if (n == 0)
       var coll = document.getElementById('p');
       coll.scrollIntoView(true);
       return false;
  }
  return true;
}



// Pass the referrer string to this function. 
// The additional content is stripped out, certain keywords
// are stripped out to, and search is made on the page.
 
function SearchString(referrer)
{
var iPos = 0;
var found = false;

if (debug) {alert ('referrer='+referrer);}
// alert(referrer);
  if (!referrer) {
    return false;
  }
  
  var queryPrefix
// See if referrer is yahoo or AOL
//  alert (referrer.indexOf("yahoo"),0);
  if (referrer.indexOf("yahoo")>0) {
     queryPrefix="p=";}
     else {
        if (referrer.indexOf("aol.")>0) {
               queryPrefix="query=";}
         else {
     queryPrefix="q=";}
          }
 
  var startPos = referrer.toLowerCase().indexOf(queryPrefix);

  if (debug) {alert ('queryPrefix='+queryPrefix+' '+startPos);}

  if ((startPos < 0) || (startPos + queryPrefix.length == referrer.length)) {
    return false;
  }
  

  var endPos = referrer.indexOf("&", startPos);
  if (endPos < 0) {
    endPos = referrer.length;
  }

  var queryString = referrer.substring(startPos + queryPrefix.length, endPos);

  // fix the space characters
  queryString = queryString.replace(/%20/gi, " ");
  queryString = queryString.replace(/\+/gi, " ");

  // remove the quotes (if you're really creative, you could search for the
  // terms within the quotes as phrases, and everything else as single terms)
  queryString = queryString.replace(/%22/gi, "");
  queryString = queryString.replace(/\"/gi, "");
  queryString = queryString.toLowerCase();

  // Get an array of words in query string
  var queryStringWord = queryString.split(" ");
 
  //Remove certain words from the search criteria
  var keyWords = ['cottage', 'holiday', 'cottages', 'holidays', 'the', 'self', 'catering', 'villa', 'villas','in','like','james','claude','butler','hoseasons','cheap'];
  for (var i=0; i<keyWords.length; i++) {
       // Now look at each word in queryString, and remove word if it is in the list
       for (var i2=0; i2<queryStringWord.length; i2++) {
          // If keyword has been found, remove it
          if (queryStringWord[i2]==keyWords[i]) {
              if (debug) {alert ('Found keyWord='+keyWords[i]);}
              queryStringWord.splice(i2,1);
           } 
       }
  }


  // Look for entire search srtring
  if (debug) {alert ('first Page Search='+queryStringWord.join(" "));}
  found=findInPage(queryStringWord.join(" "));

  if(!found) {
    //We haven't found that string, so look for the first two words
    if (queryStringWord.length>1) {
       if (debug) {alert ('Two word page search='+queryStringWord[0]+' '+queryStringWord[1]+ '  Position='+iPos);}
       found=findInPage(queryStringWord[0]+' '+queryStringWord[1]);
    }
  }

  if(!found) {
    //We haven't found the first two words, so look loop round, looking for word we can find
    var i=0;
    while (i<queryStringWord.length && !found) {
       if (debug) {alert ('word array page search='+queryStringWord[i]);}
       found=findInPage(queryStringWord[i]);
       i++;
    }
  }
}