/*************************************************************************************************/
/* Name        : GoogleAdsense.js
/* Descirpiton : The javascript sample code provided by Google has been customized to meet Tesco requirements.
/*************************************************************************************************/
/*
* This function is optional. It is used to retrieve the value of Google Parameter - google_afs_query 
* using the query string parameter of the URL
*/
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"
        var left = screen.width;
        var newwindow = "'" + google_ads[i].url + "','GoogleAdWindow" + i + "','height=600,width=700,left=" + left + ",top=10,resizable=yes,scrollbars=yes,toolbar=yes,menubar=no,location=no,directories=no,status=yes'";
        
        adRendering =
        '<a class="ad_title" href="javascript:void(0);" onclick="javascript:window.open(' + newwindow + ');" >' +
        google_ads[i].line1 + '</a><br />' +
        '<span class="ad_text">' + google_ads[i].line2 +
        (isNarrow ? ' ' + google_ads[i].line3 : '') + '</span><br />' +
        '<a class="ad_url" href="javascript:void(0);" onclick="javascript:window.open(' + newwindow + ');" ><div>' + 
        google_ads[i].visible_url + '</div></a><br />'
        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_header narrow_header">Sponsored Links</div>' +
        '<div class="ad_box narrow_ads">' +
        narrowAds + '</div>';
    }
    
    if (wideAds != "") {
        if (document.getElementById("ZeroResults_SearchPage")) {
            document.getElementById("wide_ad_unit").innerHTML = '<hr class="googleAdsense_hr"/>' +
            '<div class="ad_header wide_header">Sponsored Links</div>' +
            '<div class="ad_box wide_ads">' +
            wideAds + '</div>';
        }
        else {
            document.getElementById("wide_ad_unit").innerHTML =
            '<div class="ad_header wide_header">Sponsored Links</div>' +
            '<div class="ad_box wide_ads">' +
            wideAds + '</div>';
        }
    }
}

/*
* This function is required. It assigns values to Google Adsense(GA) parameters and requests show_afs_ads.js file to Google.
* Path of show_afs_ads.js file is specified by 5th item ie JavaScriptURL in the commonParams array.
* This function will accept 3 parameters. 
    1st contains the Adsense query, 
    2nd is a concatinated string containing the values for Google Adsense Parameters common to all pages and      3rd is also a concatinated string but it conatins the values for page specific GA Parameters.
* The order of the parameter values in the concatinated string has to be strictly followed otherwise page will not display AFS ads.
* Order for commonParameters         - adTest, client, ie, oe, JavaScriptURL, adPage, adsafe
* Order for pageSpecificParameters   - ad, channel
*/

function googleadsense(query, commonParameters, pageSpecificParameters) {

    //Replacing &amp; with &
    query = (query.replace(/&amp;/g, "&"))
    var filterwords = /(tesco|tesco direct|direct|tescodirect)/ig;
    query = query.toLowerCase();
    query = query.replace(filterwords, " ");
    google_afs_query = query;
 
    //google_afs_query = GetParam('search');
    var commonParams = new Array();
    commonParams = commonParameters.split("|");

    var pageSpecificParams = new Array();
    pageSpecificParams = pageSpecificParameters.split("|");

    if (google_afs_query != null) {
        google_afs_adtest = commonParams[0]; // ** set parameter to off before launch to production
        google_afs_client = commonParams[1];  // substitute your client ID
        google_afs_ie = commonParams[2]; // select input encoding scheme
        google_afs_oe = commonParams[3]; // select output encoding scheme
        google_afs_ad = pageSpecificParams[0];
        google_afs_adPage = commonParams[5];
        google_afs_adsafe = commonParams[6]; // specify level for filtering non-family-safe ads 
        google_afs_channel = pageSpecificParams[1]; // enter your comma-separated channel IDs
        // 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('<script language="JavaScript" src="' + commonParams[4] + '"></script>');
    }

}

