
      /*
       * 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;
          }

          var wideAds = "";   // wide ad unit html text
          var narrowAds = "";   // narrow ad unit html text

          var ad_id = Math.uuid();
          var narrowAdCount = 0;
          for(i = 0; i < google_num_ads; i++)
          {
              if (google_ads[i].type=="text/wide")
              {
                  // render a wide ad
                  wideAds+='<li><h5 class="ad_title"><a onmouseover="javascript:window.status=\'' +
                          google_ads[i].url + '\';return true;" ' +
                          'onmouseout="javascript:window.status=\'\';return true;" ' +
                          'href="' + google_ads[i].url + '">' +

                          '<span class="ad_line1">' + google_ads[i].line1 + '</span></a>' +
                          
                          '<span class="ad_text text">' + google_ads[i].line2 + '</span>' +
                          
                          '<a style="text-decoration:none" onmouseover="javascript:window.status=\'' +
                          google_ads[i].url + '\';return true;" ' +
                          'onmouseout="javascript:window.status=\'\';return true;" ' +
                          'href="' + google_ads[i].url + '">' +
                          
                          '<span class="ad_url site">' + google_ads[i].visible_url + '</span></a></h5></li>';
              }

              else
              {
                  // render a narrow ad
                  narrowAds+='<li><h5 class="ad_title"><a onmouseover="javascript:window.status=\'' +
                          google_ads[i].url + '\';return true;" ' +
                          'onmouseout="javascript:window.status=\'\';return true;" ' +
                          'onclick="javascript:logAdClick(\'' + ad_id + '\',' + narrowAdCount + ');return true;" ' +
                          'href="' + google_ads[i].url + '">' +

                          '<span class="ad_line1">' + google_ads[i].line1 + '</span></a></h5>' +

                          '<span class="ad_text">' + google_ads[i].line2 + '</span><br/>' +

                          '<span class="ad_text">' + google_ads[i].line3 + '</span><br/>' +

                          '<a style="text-decoration:none" onmouseover="javascript:window.status=\'' +
                          google_ads[i].url + '\';return true;" ' +
                          'onmouseout="javascript:window.status=\'\';return true;" ' +
                          'onclick="javascript:logAdClick(\'' + ad_id + '\',' + narrowAdCount + ');return true;" ' +
                          'href="' + google_ads[i].url + '">' +

                          '<span class="ad_url site">' + google_ads[i].visible_url + '</span></a></h5></li>';
                  narrowAdCount++;
              }
          }

          if (narrowAds != "")
          {
              narrowAds = '<h4><a href="http://services.google.com/feedback/online_hws_feedback">Ads by Google</a></h4>' +
							'<ul class="_3">' + narrowAds + '</ul>';
          }

          if (wideAds != "")
          {
              wideAds = '<h4><a href="http://services.google.com/feedback/online_hws_feedback">Ads by Google</a></h4>' +
							'<ul class="_3">' + wideAds + '</ul>';
          }

          // Write HTML for wide and narrow ads to the proper <div> elements
	  var adblocks = 
	  	[
		  //{ "name": "horizontal_ad_block_1", "type": "wide" },
		  //{ "name": "horizontal_ad_block_2", "type": "wide" },
		  { "name": "vertical_ad_block", "type": "narrow" }
		];

          
          var params = "ad_id="+ encodeURIComponent(ad_id) + "&num_displayed=" + encodeURIComponent(narrowAdCount) + "&note=" + encodeURIComponent(location.href);
          $.ajax({
              url: "/php-logger/log_google_afs_display.php",
              data: params,
              type: "POST",
              complete: function(){}
          });
          	  
          // once document is fully loaded, place ads in appropriate div element(s)
          $(document).ready(function() {
              for ( var i=0; i<adblocks.length ; ) {
                  adblock = adblocks[i++];
                  if ( document.getElementById(adblock.name) ) {
                      switch ( adblock.type ) {
                          case 'wide':
                              document.getElementById(adblock.name).innerHTML = wideAds;
                              break;
                          case 'narrow':
                              document.getElementById(adblock.name).innerHTML = narrowAds;
                              break;
                      }
                  }
              }
          });
      }

      function logAdClick(ad_id, rank) {
          var params = "ad_id="+ encodeURIComponent(ad_id) + "&rank=" + encodeURIComponent(rank);
          $.ajax({
              url: "php-logger/log_google_afs_click.php",
              data: params,
              type: "POST",
              complete: function(){}
          });
      }

