  /*
   * This function is used to retrieve the value in the search box on the page
   */
  function GetParam(name) {
    var match = new RegExp('[\?&]'+name + "=([^&]+)","i").exec(location.search);
    if (match==null) return null;
    else return decodeURIComponent(match[1]).replace(/\+/g,' ');
  }

  /*
   * 
   */
  var ad_test = true;
  function toggle_ad_test(adtest) {
      if(adtest == 'on') { ad_test = true; }
      else { ad_test = false; }
  }
 
  /*
   * 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 must be google_afs_request_done. If this
   * function is not named correctly, your page will not display AFS ads.
   */
  
  var afs_ads_wide = new Array();
  var afs_ads_narrow = new Array();
  var ad_modules = new Array();
 
  function google_afs_request_done(google_ads) {
 
    var wideAds = "";   //wide ad unit html text
    var narrowAds = "";   //narrow ad unit html text
 
    for(i = 0; i < google_ads.length; i++){
      var isNarrow = google_ads[i].type=="text/narrow"
      adRendering =
        '<a class="ad_title" href="' + google_ads[i].url + '" >' +
        google_ads[i].line1 + '</a><br>' +
        '<span class="ad_text">' + google_ads[i].line2 + '<br>' +
        (isNarrow ? google_ads[i].line3 + '<br>' : '') +
        '<a class="ad_url" href="' + google_ads[i].url + '" >' +
        google_ads[i].visible_url + '</a><br><br>';

      if(isNarrow) {
          afs_ads_narrow.push(adRendering);
      } else {
          afs_ads_wide.push(adRendering);
      }
    }

    var module_div;
    var html;
    var id, type, count;

    for(var ctr = 0; ctr < ad_modules.length; ctr++) {
        id = ad_modules[ctr].id;
        type = ad_modules[ctr].type;
        count = ad_modules[ctr].count;

        html = '';

        for(var i = 0; i < count; i++) {
            if(type == 'wide') {
                if(afs_ads_wide.length > 0) {
                    html += afs_ads_wide.shift();
                }
            } else {
                if(afs_ads_narrow.length > 0) {
                    html += afs_ads_narrow.shift();
                }
            }
        }

        if(!ad_modules[ctr].embed) {
            module_div = document.getElementById('afs_ad_unit_'+id);
        } else {
            module_div = document.getElementById('end_of_paid_listings');
            if(!module_div) {
                module_div = document.getElementById('afs_ad_unit_'+id);
            }
        }

        var content_div = 'ad-search'; 
        if(ad_test) { content_div = 'ad-search-test'; }

        if(module_div && html != '') {
            module_div.innerHTML =
              '<div class="ad_box '+type+'_ads '+content_div+'">' +
              '<div class="ad_header '+type+'_header">Sponsored Links</div>' +
              html + '</div>';
        }
    }
  }
  
  function ads_push(id, type, count, embed_in_results) {
      ad_modules.push({'id': id, 'type': type, 'count': count, 'embed': embed_in_results});
  }

    var google_num_ads = 0; 
    var google_last_ad_type = '';

    function google_ad_request_done(google_ads) { 
        /*
         * This function is required and is used to display 
         * the ads that are returned from the JavaScript 
         * request. You should modify the document.write 
         * commands so that the HTML they write out fits
         * with your desired ad layout.
         */ 
         
         var s;

        /* 
         * Verify that there are actually ads to display. 
         */
        if (google_ads.length == 0) return;

        google_num_ads += google_ads.length; 
        google_last_ad_type = google_ads[0].type;
        /* 
         * If an image or Flash ad is returned, display that ad. 
         * If a rich media ad is returned, display that as "as is."
         * Otherwise, build a string containing all of the ads 
         * then use a document.write() command to print that 
         */
        
        var ads_list = new Array();
        var ads_list_wide = new Array();
        var ads_list_img = new Array();

        if (google_ads[0].type == "image") { 
            ads_list_img.push('<a href="' + google_ads[0].url +
                    '" target="_top" title="go to ' + google_ads[0].visible_url +
                    '"><img border="0" src="' + google_ads[0].image_url +
                    '"width="' + google_ads[0].image_width + '"height="' + google_ads[0].image_height +
                    '"></a>'); 
        } else if (google_ads[0].type == "flash") { 
            ads_list.push('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"' + 
                    ' codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0"' + 
                    ' WIDTH="' + google_ad.image_width +
                    '" HEIGHT="' + google_ad.image_height + '">' +
                    '<PARAM NAME="movie" VALUE="' + google_ad.image_url + '">' +
                    '<PARAM NAME="quality" VALUE="high">' +
                    '<PARAM NAME="AllowScriptAccess" VALUE="never">' +
                    '<EMBED src="' + google_ad.image_url + '" WIDTH="' + google_ad.image_width + 
                    '" HEIGHT="' + google_ad.image_height + '" TYPE="application/x-shockwave-flash"' + ' AllowScriptAccess="never" ' +
                    ' PLUGINSPAGE="http://www.macromedia.com/go/ getflashplayer"></EMBED></OBJECT>');
        } else if (google_ads[0].type == "html") { 
            ads_list.push(google_ads[0].snippet);
        } else if (google_ads[0].type == "text") {
            for(var i=0; i < google_ads.length; i++) {
                ads_list.push('<a class="ad_title" href="' + google_ads[i].url + '" >' + 
                google_ads[i].line1 + '</a><br /><span class="ad_text">' +
                google_ads[i].line2 + '<br />' +
                google_ads[i].line3 + '</span><br />' +
                '<a class="ad_url" href="' + google_ads[i].url + '" >' + 
                google_ads[i].visible_url + '</a><br><br>');
               
                ads_list_wide.push('<a class="ad_title" href="' + google_ads[i].url + '" >' + 
                google_ads[i].line1 + '</a><br /><span class="ad_text">' +
                google_ads[i].line2 + ' ' + google_ads[i].line3 + '</span><br />' +
                '<a class="ad_url" href="' + google_ads[i].url + '" >' + 
                google_ads[i].visible_url + '</a><br><br>');
            } 
        }
        

        var module_div;
        var html;
        var id, type, count;

        for(var ctr = 0; ctr < ad_modules.length; ctr++) {
            id = ad_modules[ctr].id;
            count = ad_modules[ctr].count;
            type = ad_modules[ctr].type;

            html = '';

            for(var i = 0; i < count; i++) {
                if(type == 'wide' || type == 'narrow') {
                    if(ads_list.length > 0) {
                        if(type == 'wide') {
                            html += ads_list_wide.shift();
                            ads_list.shift();
                        } else {
                            ads_list_wide.shift();
                            html += ads_list.shift();
                        }
                    }
                } else if (ads_list_img.length > 0) {
                    html += ads_list_img.shift();
                }
            }

            if(html != '') {
                if(!ad_modules[ctr].embed) {
                    module_div = document.getElementById('afs_ad_unit_'+id);
                } else {
                    module_div = document.getElementById('end_of_paid_listings');
                    if(!module_div) {
                        module_div = document.getElementById('afs_ad_unit_'+id);
                    }
                }

                var content_div = 'ad-content'; 
                if(ad_test) { content_div = 'ad-content-test'; }

                if(module_div && html != '') {
                    module_div.innerHTML =
                      '<div class="ad_box '+type+'_ads '+content_div+'">' +
                      '<div class="ad_header '+type+'_header"><a class="ad_attribution" href="'+ google_info.feedback_url+'">Sponsored Links</a></div>' +
                      html + '</div>';
                }
            }
        }
        return;
            
    }
  


  var google_afs_query;
  function afs_search(q, spec, client, channels, page, adtest, link) {
      toggle_ad_test(adtest);

      // ** Populate search box with query
      // document.getElementById('search_box').value = google_afs_query;
      google_afs_query = q;
      google_afs_ad = spec; // specify the number of ads you are requesting
      google_afs_client = client; // Type your client_id between the quotes. Please verify spelling.
      google_afs_channel = channels; // enter your comma-separated channel IDs

      google_afs_qry_lnk = link; // Value of the link text clicked by the user to trigger this request.      

      google_afs_oe = 'utf8'; // select output encoding scheme
      google_afs_adsafe = 'high'; // specify level for filtering non-family-safe ads
      google_afs_adtest = adtest; // ** set parameter to off before launch to production
      // google_afs_hl = 'en'; // enter your interface language if not English

      var page_num = parseInt(page);
      if(page_num > 0) { google_afs_adpage = page_num + 1; }
          
 
      /*
       * The JavaScript returned from the following page uses
       * the parameter values assigned above to populate an array
       * of ad objects. Once that array has been populated,
       * the JavaScript will call the google_afs_request_done
       * function to display the ads.
       */
      document.write('<script language="JavaScript" src="http://www.google.com/afsonline/show_afs_ads.js"></script>');
  }

  function afc_search(count, client, channels, page, adtest, adtype) {
      toggle_ad_test(adtest);

      // The following parameters should be modified appropriately
      google_ad_client = client; // substitute your client_id
      google_ad_channel = channels; // substitute your channel 
      google_ad_output = 'js'; // leave this value as js 
      google_max_num_ads = count; // specify the number of maximum ads 
      google_ad_type = adtype; // type of ads to display 
      google_safe = 'high'; // specify the ad safety

      // The following parameter is required if you are displaying image, Flash or rich media ads.
      google_image_size = '300x250'; // the size of the ad block

      var page_num = parseInt(page);
      if(page_num > 0) {
          google_skip = page_num * count;
      }

      // The following parameters are options you can specify as necessary
      // google_encoding = 'utf8'; // specify the output language of the ads
      // google_language = 'en'; // override the default page language
      // The following parameter should be used only during testing
      google_adtest = adtest; // remove this line on launch day

      document.write('<script language="JavaScript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>');
  }


