// Copyright © 2002 by Apple Computer, Inc., All Rights Reserved.
//
// You may incorporate this Apple sample code into your own code
// without restriction. This Apple sample code has been provided "AS IS"
// and the responsibility for its operation is yours. You may redistribute
// this code, but you are not permitted to redistribute it as
// "Apple sample code" after having made changes.


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>
<head>
	<title>Mozilla Google Search</title>
  <style type="text/css">
    body {
      font-family:verdana, sans serif;
      font-size:14px;
    }
  </style>
  <script type="text/javascript">
    
  function callGoogle(method,params,callback){
    try {
      netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
    } catch (e) {
      alert(e);
      return false;
    }
    var soapCall = new SOAPCall();
    soapCall.transportURI = "http://api.google.com/search/beta2";
    soapCall.encode(0, method, "urn:GoogleSearch", 0, null, params.length, params);
    soapCall.asyncInvoke(
      function (response, soapcall, error)
       {
          var r = handleSOAPResponse(response,soapcall,error);
          callback(r);
        }
    );
  }
  
  function handleSOAPResponse (response,call,error)
  {
      if (error != 0)
      { 
          alert("Service failure");
          return false;
      } else 
      {
          var fault = response.fault; 
          if (fault != null) { 
              var code = fault.faultCode; 
              var msg = fault.faultString; 
              alert("SOAP Fault:\n\n" +
                  "Code: "  + code +
                  "\n\nMessage: " + msg
              );
              return false;
          } else 
          {
              return response;
          }
      }
  }
  
  function showResults(results)
  {
    if (!results)
    {
      return;
    }
    var params = results.getParameters(false,{});
    var matches = params[0].element.getElementsByTagName("item");
    var str = "";
    for (var i=1;i<matches.length;i++)
    {
      var URL, title, cache;
      var URLnode = matches[i].getElementsByTagName("URL").item(0).firstChild;
      var titleNode = matches[i].getElementsByTagName("title").item(0).firstChild;
      var cacheNode = matches[i].getElementsByTagName("cachedSize").item(0).firstChild;
    
      URL = URLnode.nodeValue;
      title = (titleNode==null)?URLnode.nodeValue:titleNode.nodeValue;
      cache = (cacheNode==null)?"":cacheNode.nodeValue;
    
      str += "<a href=\"" + URL + "\">" + title + "</a> " + cache + "<br/>";
    }
    document.getElementById("showLinks").innerHTML = str;
  }
  
  
  function doSearch()
  {
    var key = document.forms[0].elements["googleKey"].value;
    var query = document.forms[0].elements["queryTerms"].value;
  
    var p = new Array();
    // the search query
    p[0] = new SOAPParameter(key,"key");
    p[1] = new SOAPParameter(query,"q");
    p[2] = new SOAPParameter(0,"start");
    p[3] = new SOAPParameter(10,"maxResults");
    p[4] = new SOAPParameter(false,"filter");
    p[5] = new SOAPParameter("","restrict");
    p[6] = new SOAPParameter(false,"safeSearch");
    p[7] = new SOAPParameter("","lr");
    p[8] = new SOAPParameter("Latin-1","ie");
    p[9] = new SOAPParameter("Latin-1","oe");
    
    callGoogle("doGoogleSearch",p,showResults);
  }

    
  </script>
</head>

<body>
<h3>Mozilla Google Search</h3>
<form action="#" onsubmit="return false;">
<p>Enter a Google API Key (required):<br />
<input type="text" name="googleKey" value="Enter your google key here"></p>
<p>Enter your search terms:<br />
<input type="text" name="queryTerms"></p>
<p><input type="button" value="Search" onclick="doSearch();"></p>
</form>

<div id="showLinks">Search results will appear here.</div>

</body>
</html>