  var timer;
  var i = 0;
  var pagenum = 1;
  var searchcount = 0;

  function prevpage()
  {
    pagenum--;
    xmlreqGET("searchagain.php?out=Y&js=on&page="+pagenum);
  }
  
  function nextpage()
  {
    pagenum++;
    xmlreqGET("searchagain.php?out=Y&js=on&page="+pagenum);
  }
        
  function dosearch(str, sheet, download)
  {    
    // Add something to track that a goal was achieved in Google website
    // optimizer.
    try {
      var gwoTracker=_gat._getTracker("UA-10525256-4");
      gwoTracker._trackPageview("/3962593487/goal");
    }catch(err){}

    clearTimeout(timer);
    document.smsearch.searchstr.value = str;
        
    if (str.length > 0)
    {
      if (sheet)
      {
        str += "&sheet=on";
      }
      
      if (download)
      {
        str += "&download=on";
      }
      
      pagenum = 1;
      searchcount++;
      xmlreqGET("search.php?output=yes&trace=Y&searchstr="+str);
      document.getElementById("results").innerHTML = "<h3>Searching.</h3>";
      timer = setTimeout("waiting()",1000);
    }
  }
  
  function waiting()
  {
    if (i == 0)
    {
      document.getElementById("results").innerHTML = "<h3>Searching.</h3>";
      i++;
      timer = setTimeout("waiting()",1000);
    }
    else if (i == 1)
    {
      document.getElementById("results").innerHTML = "<h3>Searching..</h3>";
      i++;
      timer = setTimeout("waiting()",1000);
    }
    else if (i == 2)
    {
      document.getElementById("results").innerHTML = "<h3>Searching...</h3>";
      i = 0;
      timer = setTimeout("waiting()",1000);
    }
  }

  function recordClick(e)
  {
    if (typeof e == 'undefined')
      var e = window.event;

    var source;
    if (typeof e.target != 'undefined') 
    {
      source = e.target;
    } else if (typeof e.srcElement != 'undefined') {
      source = e.srcElement;
    } else {
      return true;
    }

    if (source.nodeType == 3)
      source = source.parentNode;
		
    var id, target, url, label;
    
    if( source.tagName == "IMG" )
    {
      if( source.parentNode.tagName == "A" )
      {
        id = source.parentNode.getAttribute('id');
	      target = source.parentNode.getAttribute('href');
      }
      
      label = source.getAttribute("alt");
  	}
    else
    {
      id = source.getAttribute('id');
  	  target = source.getAttribute('href');
  	  label = source.childNodes[0].nodeValue;
  	}
   	url = document.location.href;
	
	  apiurl = "http://www.myliszt.com/recordclick.php?linkid=" + id + "&label=" + label + "&target=" + escape(target) + "&url=" + escape(url);
	  xmlreqGET(apiurl);
  }

  function handle_response(response)
  {
    // This function is used for search results primarily, but also
    // handles the responses from the recordclick.php script.
    if (response == "Click recorded")
    {
      return true;
    }
    
    var results  = response.split("\n");
    var newhtml  = "";
    var anymatches = 0;
    
    // If this particular search result has been superceded by a later search
    // then ignore it. Note that there is a non-zero risk that searches may
    // not return in the right order.
    searchcount--;
    if (searchcount > 0)
    {
      return;
    }
    
    // Cancel the timer
    clearTimeout(timer);
    
    var totalresults = results[0];
    var startresults = (20*pagenum)-19;
    var endresults   = Math.min((20*pagenum),totalresults);
    newhtml += "Showing results " + startresults + "-" + endresults;
    newhtml += " of " + totalresults + ".\n";
    
    for (var i in results)
    {
      if (results[i] != null && results[i].length > 10)
      {
        var fields = results[i].split("\t");
        var rank   = fields[0];
        var site   = fields[1];
        var link   = fields[2]; 
        var title  = fields[3];
        var desc   = fields[4];
        var price  = fields[5];
        var avail  = fields[6];

        if (desc != null && title != null && link != null && price != null && avail != null && site != null)
        {        
          anymatches = 1;
          newhtml += "\n<div id='result'>";
          newhtml += "<table><tr><td width=600>";
          newhtml += "<A href='"+link;
          newhtml += "' onClick=\"javascript: pageTracker._trackPageview('/links/";
          newhtml += site + "-" +i+ "-" + escape(title) + "');\" id=link_"+i+">"+title+"</a><P>";
          
          if (desc.length > 300)
          {
            desc = desc.substring(0,300) + "...";
          }

          newhtml += desc+"</P>";
          newhtml += "<DIV ID='resultfoot'><i>Provided by "+site+". "+avail+"</i></div>";
          newhtml += "</td>\n<td width=100 valign=top>";
          newhtml += "<DIV ID='resultprice'><p align=center>$" + price + "<BR>";
          newhtml += "<div class=\"clearbutton\"> ";
          newhtml += "<a onClick=\"javascript: pageTracker._trackPageview('/links/" +site+ "-" +i+ "-" + escape(title) + "-button');\"";
          newhtml += " class=\"GlobalOrangeButton\" href=\""+link+"\"><span>More...</span></a></div></p></div>";
          newhtml += "</td></tr></table>";
          newhtml += "</DIV>";          
        }
      }  
    }
    
    newhtml += "<TABLE><TR><TD align=LEFT>";
    if (pagenum > 1)
    {
      newhtml += "<A HREF='#' onclick='prevpage();'>Previous</a>";
    }

    newhtml += "</TD>";
    
    newhtml += "<TD align=right>";
    
    if ((pagenum*20) < totalresults)
    {
      newhtml += "<a href='#' onclick='nextpage();'>Next</a>";
    }
    
    newhtml += "</TD></TR></table>";
    
    if (anymatches == 1)
    {
      document.getElementById("results").innerHTML = newhtml;
      document.smsearch.searchstr.blur();
    }
    else
    {
      document.getElementById("results").innerHTML = "No matches found. Please make sure at least one box is checked, or try a simpler search.";
    }
  }

  function handle_error()
  {
    // Let's ignore any errors, since they seem to be appearing inappropriately.
    // alert("There was an error accessing the search results!");
    return true;
  }

  function checkEmail(str)
  {
    var success = true;
    
    if (str == "myemail@address.com")
    {
      success = false;
    }
    else
    {
      success = (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
    }
    
    if (!success)
    {
      alert("Please enter a valid email address.");
    }
    
    return success;
  }

