/*  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,' ');
}

/*
 * 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.
 */
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 = '<div class="googleAFSchunk"><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 />' : '') + '</span><a class="ad_url" href="' + google_ads[i].url + '" >' + google_ads[i].visible_url + '</a></div>';
        isNarrow ? narrowAds += adRendering : wideAds += adRendering;
    }
    /* Write HTML for wide and narrow ads to the proper <div > elements */
    if (narrowAds != "") {
        document.getElementById("narrow_ad_unit").innerHTML = '<div class="ad_box narrow_ads">' +
        '<div class="ad_header narrow_header">Sponsored Links </div>' + narrowAds + '</div>';
    }
    if (wideAds != "") {
        document.getElementById("wide_ad_unit").innerHTML = '<div class="ad_box wide_ads">' + 
        '<div class="ad_header wide_header">Sponsored Links</div>' + wideAds + '</div>';
    }
}
google_afs_query = GetParam('searchString');

/* check to see if the site uses a different param for searching */
if (google_afs_query == null) { google_afs_query = GetParam('searchText'); }

/* check to see if the site uses a different param for searching */
if (google_afs_query == null) { google_afs_query = GetParam('searchTerm'); }

if (google_afs_query != null) {
    /** Populate search box with query */
   /* document.getElementById('nvbsrchtxt').value = google_afs_query;*/
    google_afs_ad = googleAFSNumberResults ; /* specify the number of ads you are requesting*/
    google_afs_client = 'meredith'; /* Type your client_id between the quotes. Please verify spelling. */
    google_afs_channel = googleAFSChannelName ; /* enter your comma-separated channel IDs*/
    google_afs_adsafe = 'high'; /* specify level for filtering non - family - safe ads*/
    /* google_afs_adtest = 'off'; ** set parameter to off before launch to production*/
    /* google_afs_hl = 'en'; / *  enter your interface language if not English*/
    /*
     * 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( google_afs_client);*/
}
