
/*
* This function is required. It processes the google_ads JavaScript object,
* which contains AFS ads relevant to the user's search query. The name of
* this function <i>must</i> be <b>google_afs_request_done</b>. If this
* function is not named correctly, your page will not display AFS ads.
*/
function google_afs_request_done(google_ads)
{
  //Verify that there are actually ads to display.
  var google_num_ads = google_ads.length;
  if (google_num_ads <= 0)
  {
      return;
  }
  
  //declare html string
  var adsHtml = '<h2><a href="http://services.google.com/feedback/online_hws_feedback">' + googleTitle + '</a></h2>';  

	//loop through returned ads, adding to html
  for(i = 0; i < google_num_ads; i++)
  {	
		adsHtml+='<h3><a onmouseover="javascript:window.status=\'' + google_ads[i].url + '\';return true;" onmouseout="javascript:window.status=\'\';return true;" href="' + google_ads[i].url + '">' + google_ads[i].line1 + '</a></h3>';
		
		var middleLine = '<p>' + google_ads[i].line2 + ' ';
		if(google_ads[i].type = "text/narrow" && google_ads[i].line3 != null) { middleLine += google_ads[i].line3; }
		middleLine += '</p>';
		adsHtml += middleLine;
		
		//make sure the displayed url is not too long for the design
		var abbreviatedVisibleUrl = AbbreviateString(google_ads[i].visible_url, urlMaxChars);
		 					
		adsHtml+='<p class="resultURL"><a onmouseover="javascript:window.status=\'' + google_ads[i].url + '\';return true;" onmouseout="javascript:window.status=\'\';return true;" href="' + google_ads[i].url + '">' + abbreviatedVisibleUrl + '</a></p>';
  }
	
  //Write HTML to the targetElementId
  document.getElementById(targetElementId).innerHTML = adsHtml;
}


//This function retrieves the search query from the URL
//NOTE: the Google implementation of this function FAILS if q=xxx is *not* the last part of the QS
//	so am replacing it with this more robust version
function GetParam(name)
{
  var qs = new queryString()
  return (qs.get(name));
}


/* 
 * http://adamv.com/dev/javascript/querystring
 * Client-side access to querystring name=value pairs
 * Version 1.2.3
 * 22 Jun 2005
 * Adam Vandenberg
*/
function QueryString(qs) 
{ 
	// optionally pass a querystring to parse
	this.params = new Object();
	this.get=QueryString_get;
	
	if (qs == null)
		qs=location.search.substring(1,location.search.length);

	if (qs.length == 0) return;

	// Turn <plus> back to <space>
	// See: http://www.w3.org/TR/REC-html40/interact/forms.html#h-17.13.4.1
	qs = qs.replace(/\+/g, ' ');
	var args = qs.split('&'); // parse out name/value pairs separated via &
	
	// split out each name=value pair
	for (var i=0;i<args.length;i++) 
	{
		var value;
		var pair = args[i].split('=');
		var name = unescape(pair[0]);

		if (pair.length == 2)
			value = unescape(pair[1]);
		else
			value = name;
					
		this.params[name] = value;
	}
}

function QueryString_get(key, default_) 
{
	// This silly looking line changes UNDEFINED to NULL
	if (default_ == null) default_ = null;
	
	var value=this.params[key];
	if (value==null) value=default_;
	
	return value;
}

/*
 * used to control the # of characters in the displayed urls
 * which, if too wide, can break the page
 * If the string is too long it'll chop it and add a ...
 */
function AbbreviateString(string, maxLength)
{
	if(string.length <= maxLength)
	{
		return string;
	}
	else
	{
		return string.substr(0, (maxLength-3)) + "...";		
	}	
}