//Google ads generator (javascript)
//David Jamin  

//********** Global Variables **********//
var o_urlstring = document.URL;
o_urlstring = path(o_urlstring);
o_urlstring = replace(o_urlstring, "_");

/*
 * This function retrieves the search query from the URL.
 */

function GetParam(name) {
	var match = new RegExp(name + "=(.+?)[&]","i").exec(location.search);
    if (match==null) {
    	match = new RegExp(name + "=(.+)","i").exec(location.search);
    }

    if (match==null) {
    	return null;
    }

    match = match + "";
    result = match.split(",");
    return decodeURIComponent(result[1]);
}


/*
* 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 <i>must</i> be <b>google_afs_request_done</b>. If this
* function is not named correctly, your page will not display AFS ads.
*/

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 wideAds = "";   // wide ad unit html text
    var narrowAds = "";   // narrow ad unit html text

    for(i = 0; i < google_num_ads; i++) {
		var s = '<a class="googletitle" href="' + google_ads[i].url + '" ' +
                'onmouseout="window.status=\'\'" ' +
				'onmouseover="window.status=\'go to ' +
                google_ads[i].visible_url + '\'; return true">' +
'<div id="goog1">' + google_ads[i].line1 + '</a></div> - ' +
'<span class="goog2">' + google_ads[i].line2 + '</span>' +
'<a href="' + google_ads[i].url + '" ' +
                'onmouseout="window.status=\'\'" ' +
                'onmouseover="window.status=\'go to ' +
                google_ads[i].visible_url + '\'; return true">' +
'<span class="goog3">' + google_ads[i].visible_url + '</a></span>';

    	if (google_ads[i].type=="text/wide") {
        	// render a wide ad
			if (wideAds != ""){
				wideAds += '<br>' + s;
			}else{
				wideAds = s;
			}
		} else {
        	// render a narrow ad
			if (narrowAds != ""){
				narrowAds += '</td></tr>\n<tr><td>\n' + s;
			}else{
            	narrowAds = s;
			}
		}
	}

    if (narrowAds != "") {
		// Insert table before all the narrow ads.
		narrowAds = '<div id="googonesixty">\n' +
			'<table width="100%" border="0" cellspacing="0" cellpadding="0">\n<tr>\n<td>\n' +
            '<div id="fl_label"><a href="http://services.google.com/feedback/online_hws_feedback">Ads by Google</a></div></td>\n' +
        	'</tr>\n<tr>\n<td height="10" width="100%"></td>\n</tr>\n<tr>\n<td>\n' + narrowAds;

		//Close table after all the narrow Ads
        narrowAds += '</td>\n</tr>\n</table>\n</div>\n';
    }

    if (wideAds != "") {
		//Insert table before all the wide Ads.
		if ((o_urlstring.indexOf("public.findlaw") != -1) || 
			(o_urlstring.indexOf("public.ofc.findlaw") != -1)){
			wideAds = '<table border="0" cellpadding="2" cellspacing="0" width="100%">\n<tr>' +
			'<td bgcolor="#ffffff" height="1">\n' +
			'<table bgcolor="#ffffff" border="0" cellpadding="3" cellspacing="0" width="100%">\n'+
			'<tr><td align="left" bgcolor="#f3f3f3" height="20" valign="middle">\n' +
			'<hr size="1">\n'+
    		'<a style="text-decoration:none" ' +
        	'href="http://services.google.com/feedback/online_hws_feedback">' +
            '<span class="textsmall-lgray">Ads by Google</span></a></td>' + 
			'</tr>\n<tr>\n<td height="20" class="textsmall">\n' + wideAds;
			// Close table after all the wide Ads.
			wideAds += '<hr size="1"/>\n</td>\n</tr>\n</table>\n';
			wideAds += '</td>\n</tr>\n</table>\n';
		}else{
			wideAds = '<br clear="all" />' +
			'<table width="710" border="0" align="center" cellpadding="3" cellspacing="0" bgcolor="#ffffff">' +
    		'<tr>\n<td height="20" align="left" valign="middle"><hr size="1" />' +
    		'<div id="fl_label"><a href="http://services.google.com/feedback/online_hws_feedback">Ads by Google</a></div></td>' + 
			'</tr>\n<tr>\n<td height="20" class="textsmall">\n' + wideAds;
			// Close table after all the wide Ads.
			wideAds += '<hr size="1"/>\n</td>\n</tr>\n</table>\n';
		}
    }

    // Write HTML for wide and narrow ads to the proper <div> elements
    document.getElementById("wide_ad_unit").innerHTML = wideAds;
    document.getElementById("narrow_ad_unit").innerHTML = narrowAds;
}

// Helper Function: path
// strip out 'http://' and convert to lowercase
function path(urlstring) {
   urlstring = urlstring.toLowerCase();
   var start = urlstring.indexOf('//');
   if (start != -1) {
      start = start+2;
      //delete the "http://"
      urlstring=urlstring.substring(start, urlstring.length);
   }
   return urlstring;
}

// Helper Function: replace
// strip out t's from s (string)
function replace(s, t) {
    i = s.indexOf(t);
    r = "";
    if (i == -1) return s;
    r += s.substring(0,i) + '';
    if ( i + t.length < s.length)
        r += replace(s.substring(i+t.length, s.length), t, '');
    return r;
}

google_afs_query = GetParam('entry');
google_afs_ad = 'w3n5'; // specify the number of ads you are requesting
google_afs_hl = 'en'; // enter your interface language if not English
// google_afs_ie = 'utf8'; // select input encoding scheme
// google_afs_oe = 'utf8'; // select output encoding scheme

if ((o_urlstring.indexOf("public.findlaw") != -1) || 
	(o_urlstring.indexOf("public.ofc.findlaw") != -1)){
	google_afs_client = 'pub-6312685391644226';
	google_afs_channel = 'consumer';
}else{
	google_afs_client = 'pub-4773817401385873';
	google_afs_channel = 'professional';
}
