String.prototype.unescapeHtml = function () {
    var temp = document.createElement("div");
    temp.innerHTML = this;
    var result = temp.childNodes[0].nodeValue;
    temp.removeChild(temp.firstChild)
    return result;
}

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 wideAds1 = "";   // wide ad unit 1 html text
    var wideAds2 = "";   // wide ad unit 2 html text
    var wideAdsArray = new Array();
    var narrowAds = "";   // narrow ad unit html text
    var narrowAds_header = ""; // header (contains annonces google and feedback link) for narrow ad unit

    var j = 0;
    var k = 0;
    for(i = 0; i < google_num_ads; i++)
    {
        if (google_ads[i].type == "text/wide")
        {
            // render a wide ad
            wideAdsArray[j] = '<div class="google_wide_unit_content" >'+
    			'<div style="width: 100%;"><a class=\"title\" href="' +
    			google_ads[i].url + '" onmouseout="window.status=\'\'" onmouseover="window.status=\'afficher la page ' +
    			google_ads[i].visible_url.unescapeHtml() + '\';return true" target="_blank"><b>' +
    			google_ads[i].line1.unescapeHtml() + '</b></a></div><div style="width: 100%; margin-left: 3px;">' +
    			google_ads[i].line2.unescapeHtml() + '</div><div style="width: 100%px;"><a href="' +
    			google_ads[i].url + '" onmouseout="window.status=\'\'" onmouseover="window.status=\'afficher la page ' +
    			google_ads[i].visible_url.unescapeHtml() + '\';return true" target="_blank">' +
    			google_ads[i].visible_url.unescapeHtml() + '</a></div></div><div class="clear"></div>';

            j++;
        }
        else
        {
            // render a narrow ad
            if (k == 0)
                txt_class = 'class=\"google_announce_narrow_last\"';
            else
                txt_class = 'class=\"google_announce_narrow\"';
            narrowAds += '<div ' + txt_class + '><a class=\"title\" href="' +
            			google_ads[i].url + '" onmouseout="window.status=\'\'" onmouseover="window.status=\'afficher la page ' +
            			google_ads[i].visible_url.unescapeHtml() + '\';return true" target="_blank">' +
            			google_ads[i].line1.unescapeHtml() + '</a><p class=\"description\">' +
            			google_ads[i].line2.unescapeHtml() + '</p><p class=\"lien\"><a href="' +
            			google_ads[i].url + '" onmouseout="window.status=\'\'" onmouseover="window.status=\'afficher la page ' +
            			google_ads[i].visible_url.unescapeHtml() + '\';return true" target="_blank">' +
            			google_ads[i].visible_url.unescapeHtml() + '</a></p></div>';
            k = 1;
        }
    }

    if (typeof(wide_split_no) == 'undefined') {
        wide_split_no = 2;
    }
    
    //split wide announces in 2 
    for (i = 0; i < wide_split_no; i++) {
        wideAds1 += wideAdsArray[i];
    }

    for (i = wide_split_no; i < wideAdsArray.length; i++) {
        wideAds2 += wideAdsArray[i];
    }


    // Write HTML for wide and narrow ads to the proper <div> elements
    if (wideAds1 != "") {
        wideAds1 = '<div class="google_wide_unit_header">' + 
            '<a href=\"http://services.google.com/feedback/online_hws_feedback\" ' + 
            'style="color:000000">' + txt_google_ads + '</a></div>' + wideAds1;

        if (document.getElementById('wide_ad_unit1') !== null)
            document.getElementById('wide_ad_unit1').innerHTML = wideAds1;
    } else {
        if (document.getElementById('wide_ad_div1') !== null)
            document.getElementById('wide_ad_div1').style.display = 'none';
    }

    if (wideAds2 != "") {
        wideAds2 = '<div class="google_wide_unit_header">' + 
            '<a href=\"http://services.google.com/feedback/online_hws_feedback\" ' + 
            'style="color:000000">' + txt_google_ads + '</a></div>' + wideAds2;

        if (document.getElementById('wide_ad_unit2') !== null)
            document.getElementById('wide_ad_unit2').innerHTML = wideAds2;
    } else {
        if (document.getElementById('wide_ad_div2') !== null)
            document.getElementById('wide_ad_div2').style.display = 'none';
    }

    if (narrowAds != "") {
        narrowAds_header = '<div class="google_narrow_unit_header"><a style="text-decoration:none" ' +
                    'href="http://services.google.com/feedback/online_hws_feedback"><span><h4>' + txt_google_ads + '</h4></span></a></div>';

        if (document.getElementById('narrow_ad_unit') !== null)
            document.getElementById('narrow_ad_unit').innerHTML = narrowAds_header + narrowAds;
    } else {
        if (document.getElementById('narrow_ad_div') !== null)
            document.getElementById('narrow_ad_div').style.display = 'none';
    }
}
