function getScript(url) {
  var scripttag = document.createElement("script");
  scripttag.setAttribute("type","text/javascript");
  scripttag.setAttribute("src",url);
  document.getElementsByTagName("head")[0].appendChild(scripttag);
}

function searchYahoo(query) {
  var url = 'http://query.yahooapis.com/v1/public/yql?q=select%20title,abstract,url%20from%20search.news%20where%20';
  url+= 'query%3D"'+escape(query)+'"';
  url+= '&format=json';
  url+= '&diagnostics=true';
  url+= '&callback=parseResponse';
  //document.write(url);
  getScript(url);
}

function parseResponse(data) {
// empty the div
  var results = document.getElementById("results");
  while (results.hasChildNodes()) {
    results.removeChild(results.lastChild);
  }
// loop through the search results
  for (var i=0; i<data.query.results.result.length; i++) {
    var title = data.query.results.result[i].title;
    var summary = data.query.results.result[i].abstract;
    var url = data.query.results.result[i].url;
// create the headline link
    var header = document.createElement("h2");
    var link = document.createElement("a");
    link.setAttribute("href",url);
    var text = document.createTextNode(title);
    link.appendChild(text);
    header.appendChild(link);
// create the summary paragraph
    var para = document.createElement("p");
    var paratext = document.createTextNode(summary);
    para.appendChild(paratext);
// insert the markup
    results.appendChild(header);
    results.appendChild(para);
  }
}

//Sample API calls to Yahoo search
//http://search.yahooapis.com/NewsSearchService/V1/newsSearch?appid=YahooDemo&query=madonna&results=2&language=en
//http://search.yahooapis.com/WebSearchService/V1/webSearch?appid=YahooDemo&query=persimmon&results=2&output=json
//http://api.search.yahoo.com/WebSearchService/V1/webSearch?appid=YahooDemo&query=persimmon&results=2&output=json
//http://local.yahooapis.com/LocalSearchService/V2/localSearch?appid=YahooDemo&query=pizza&zip=94306&results=2

