﻿var MAX_ADS = 40; 
var MAX_CAT = 6; 
var MAX_REL = 40; 

var ads = '<dt class="dmHeading">Sponsored Listings</dt>'; 
var popularCategories = '<dt>Popular Categories</dt>'; 
var relatedSearches = '<dt>Related Searches</dt>'; 
var debugResult = '';
var stoken = '';
var debug = false;

//sp: sub page?
//ad: adult
//bn: banned
//nr: needs review
//kw: keywords
//dn: domain

//set correct client ID
var c = 'ca-dp-meshdigital02_js';
if (ad){ c = 'ca-dp-meshdigital02_adult_js'; }

var google_afd_request = {
    client: c,
    domain_name: location.host,
    num_radlinks: 14
}

//get any potential search terms
var q = getParam('q');
var t = getParam('token');

if (sp){
    //sub-page
    google_afd_request.q = q;
    google_afd_request.token = t;    
} 
else {
    //base page
    google_afd_request.s = dn;
    if (kw.length > 0){
        google_afd_request.kw = kw;
        //encodeURIComponent(kw);
        google_afd_request.hl = 'en';
    }
}

if (debug){
    /* Debugging request */ 
    document.write(parseResult(google_afd_request));
}

function google_afd_ad_request_done(google_afd_response) {
    
    if (debug){
        debugResult = parseResult(google_afd_response);
        document.write(debugResult);
    }
    if (google_afd_response.error_code){
        if (debug){
            alert("ERROR_CODE:" + google_afd_response.error_code);        
        }
    }
        
    if (!google_afd_response.faillisted) {
         
        //set search token
        if (google_afd_response.search_token){
            stoken = google_afd_response.search_token;
        }
         
        // Format ads.
        if (google_afd_response.ads && google_afd_response.ads.length > 0) {   
            var google_ads = google_afd_response.ads;
            var total_ads = google_ads.length;
            if (total_ads > MAX_ADS){total_ads = MAX_ADS};
            for (var i = 0; i < total_ads; i++) {
                ads += '<dd><dl><dt><a href="' + google_ads[i].url + '"' +
                        ' title="">' + google_ads[i].line1 + '</a></dt>' + 
                        '<dd>' + google_ads[i].line2;                    
                if (google_ads[i].line3 != undefined && google_ads[i].line3 != '') {
                    ads += '</dd><dd>' + google_ads[i].line3;
                }
                ads += '</dd><dd class="dmUrl"><a href="' + google_ads[i].url + '"' +
                        ' title="">' + google_ads[i].visible_url + '</a></dd></dl></dd>';
            }
        }

        
        // Format popular categories.
        if (google_afd_response.categories && google_afd_response.categories.length > 0) {   
            var google_categories = google_afd_response.categories;
            var total_cat = google_categories.length;
            if (total_cat > MAX_CAT){total_cat = MAX_CAT};
            for (var i = 0; i < total_cat; i++) {
                popularCategories += '<dd';
                if (i == 0){
                    popularCategories += ' class="dmFirstCat"';
                } else if (i == (total_cat-1)){
                    popularCategories += ' class="dmLastCat"';
                }
                popularCategories += '><dl>';
                popularCategories += '<dt><a href="/?' + google_afd_response.request.s +
                                    '&q=' + encodeURIComponent(google_categories[i].term) +
                                    '&token=' + google_categories[i].token + '">' +
                                    google_categories[i].term + '</a></dt>';
                                    
                if (google_categories[i].subcategories && google_categories[i].subcategories.length > 0) {
                    for (var j = 0; j < google_categories[i].subcategories.length; j++) {     
                      popularCategories += '<dd><a href="/?' +
                                        google_afd_response.request.s +
                                        '&q=' + encodeURIComponent(google_categories[i].subcategories[j].term) +
                                        '&token=' + google_categories[i].subcategories[j].token + '">' +
                                        google_categories[i].subcategories[j].term + '</a></dd>';          
                    }
                }
                popularCategories += '</dl></dd>';
            }
        }
            
        
        // Format related searches.
        if (google_afd_response.link_units && google_afd_response.link_units.length > 0) { 
            var google_link_units = google_afd_response.link_units;
            var total_rel = google_link_units.length;
            if (total_rel > MAX_REL){total_rel = MAX_REL};
            for (var i = 0; i < total_rel; i++) {
                relatedSearches += '<dd><a href="/?domain_name=' +
                google_afd_response.request.s + 
                '&q=' + encodeURIComponent(google_link_units[i].term) +
                '&token=' + google_link_units[i].token + '">' +
                google_link_units[i].term + '</a></dd>';
            }
        }
    } 
    processNotify(google_afd_response);
}    

function processNotify(resp){
    
    var res_nr = false;
    if (resp.needsreview){res_nr = (resp.needsreview === 'true');}
    var res_ad = false;
    if (resp.adult){res_ad = (resp.adult === 'true');}
    var res_bn = false;
    if (resp.faillisted){res_bn = resp.faillisted;}
    
    /* banned */
    if (res_bn){
        if (!bn){
            //notify banned
            sendnotify('BAND');
        }
        window.location = "http://www.google.com/";
    }
    /* adult */
    else if (res_ad && !ad){
        //notify adult, as we arent aware
        sendnotify('ADLT');
    }
    /* under review */
    else if (res_nr && !nr){
        //notify under review
        sendnotify('REVW');
    }    
    /* errors */
    else if (resp.error_code){
        //notify error
        sendnotify('ERRC');
    }
}

