<table xmlns="http://query.yahooapis.com/v1/schema/table.xsd">
  <meta>
        <description>Gets a list of SEC S-1 filings (those filed when a company wants to make an IPO).</description>
        <sampleQuery>select entry from usgs.filings</sampleQuery>
        <sampleQuery>select entry from usgs.filings where entry.title like "cloud computing"</sampleQuery>
  </meta>
  <bindings>
    <select itemPath="" produces="XML">
		<urls>
			<url><![CDATA[http://sec.gov/cgi-bin/browse-edgar?action=getcurrent&type=s-1&company=&dateb=&owner=include&start=0&count=40&output=atom]]></url>
		</urls>
		<execute><![CDATA[
			default xml namespace = "http://www.w3.org/2005/Atom";

			var xml = request.get().response;  // Call the url defined above
			var entries = <entries/>; // Prep the output object

			y.log("Called SEC Web site and about to iterate over results.");

			for each(var entry in xml.entry)
			{
				// Get rid of all the other filings except for S-1s
				if (entry.category.@term.toString() === "S-1")
				{
					// Add the filing to the output
					y.log("Adding S-1 filing: " + entry.title);

					var link = entry.link.@href;
					y.log("Link to filing's plain text version: " + link);

					y.log("Getting the filing from the SEC's Web site");

//                    var filingDocuments = y.rest(link).get().response;
//					var summary = filingDocuments.replace(/\n/g, " ").replace(/.*(div id="formDiv".*)div id="filerDiv".*/, "<$1");

//					y.log("Got the filing from the SEC Web site");
//                    y.log("Summary = " + summary);

					y.log("Adding entry to collection of filings");
					var newEntry = <entry>
                        <link rel="alternate" type="text/plain" href={link}/>
                        <title>{entry.title.toString().replace(/ \(.*/, "")}</title>
                        <update>{entry.updated.text()}</update>
<!--                        <summary>{summary}</summary>-->
                    </entry>;

                    entries.* += newEntry;
				}
			}

			response.object = entries;
		]]></execute>
    </select>
  </bindings>
</table>

