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

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

function google_ad_request_done(google_ads) {
    /*
     * This function is required and is used to display
     * the ads that are returned from the JavaScript
     * request. You should modify the document.write
     * commands so that the HTML they write out fits
     * with your desired ad layout.
     */
    var s = '';
    var i;

    /*
     * Verify that there are actually ads to display.
     */
    if (google_ads.length == 0) {
      return;
    }

	google_set = 1;

	/*
	* If an image or flash ad is returned, display that ad.
	* Otherwise, build a string containing all of the ads and
	* then use a document.write() command to print that string.
	*/

	if (google_ads[0].type == "flash") {
		//We do not use flash ad so the code in Google document is not used.
	} else if (google_ads[0].type == "image") {
		//We do not use image ad so the code in Google document is not used.
	} else if (google_ads[0].type == "html") {
		//We do not use html ad so the code in Google document is not used.
	} else {
		if ((o_urlstring.indexOf("vendor") != -1) || (o_urlstring.indexOf("marketcenter") != -1) || (o_urlstring.indexOf("firms-1") != -1) || (o_urlstring.indexOf("news.lp") != -1)){
			s +='<div id="fl_container">';
      	}else if (o_urlstring.indexOf("writ") != -1) {
			s +='<div id="fl_container">';
	  	}else{
			s +='<div id="fl_container">';
	  	}

    	s += '<div id="fl_segment2">';

      	if (google_ads.length == 1) {
        	/*
         	* Partners should adjust text sizes
         	* so ads occupy the majority of ad space.
         	*/
        	s +=  '<a href="' + google_ads[0].url + '" ' +
                  'onmouseout="window.status=\'\'" ' +
                  'onmouseover="window.status=\'go to ' +
                  google_ads[0].visible_url + '\'; return true>" ' +
'<div id="goog1">' + google_ads[0].line1 + '</a></div>' + 
'<span class="goog2">' + google_ads[0].line2 + '&nbsp;' + google_ads[0].line3 + '</span>' +
'<a href="' + google_ads[0].url + '" ' +
                  'onmouseout="window.status=\'\'" ' +
                  'onmouseover="window.status=\'go to ' +
                  google_ads[0].visible_url + '\'; return true>" ' +
'<span class="goog3">' + google_ads[i].visible_url + '</a></span>';

      } else if (google_ads.length > 1){
        	/*
         	* For text ads, append each ad to the string.
         	*/
        	for(i=0; i < google_ads.length; ++i) {
		  		if (i > 0){
      				if ((o_urlstring.indexOf("vendor") != -1) || (o_urlstring.indexOf("marketcenter") != -1) || (o_urlstring.indexOf("firms-1") != -1) || (o_urlstring.indexOf("news.lp") != -1)){
						s += '<table height="3px"><tr height="3px"><td height="3px"></td></tr></table>';
					}else{
						s += '<table height="3px" width="350"><tr height="3px"><td height="3px"></td></tr></table>';
					}
		  		}
          	s +=   '<a 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 + '&nbsp;' + google_ads[i].line3 + '</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 (o_urlstring.indexOf("boards.answers") != -1){
				if (google_ads[0].bidtype == "CPC"){
					google_adnum = google_adnum + google_ads.length;
				}
			}
      	}
	  	s += '</div>' +
	 		'<div id="fl_label"><a href=\"'+
			google_info.feedback_url + '\">Ads by Google</a></div>'+ '</div>';
	} 
    document.write(s);
    return;
  }

// 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 Channel ID: 
//LP: pub-4773817401385873  
//Consumer: pub-6312685391644226  

    google_ad_output = 'js';
	google_kw_type = 'broad';
	google_language = 'en';
    google_safe = 'high';
	google_ad_type = 'text';
	google_feedback = 'on';

	if ((o_urlstring.indexOf("news.") != -1) || (o_urlstring.indexOf("infirmation.") != -1) || (o_urlstring.indexOf("vendor") != -1) || (o_urlstring.indexOf("marketcenter") != -1) || (o_urlstring.indexOf("stu.") != -1) || (o_urlstring.indexOf("boards.") != -1) || (o_urlstring.indexOf("practice.") != -1)){
		if ((o_urlstring.indexOf("news.findlaw") != -1) || (o_urlstring.indexOf("boards.answers") != -1)){
			//Consumer Channel
			google_ad_client = 'pub-6312685391644226';
		}else{
			//LP Channel
			google_ad_client = 'pub-4773817401385873';
		}
		if (o_urlstring.indexOf("boards.answers") != -1){
			google_kw = 'VOIP';
			google_ad_channel = '';
			if (google_adnum == 0){
     			google_max_num_ads = '3';
				google_skip = 0;
			}else{
     			google_max_num_ads = '2';
				google_skip = google_adnum;
			}
		}else{
     		google_max_num_ads = '5';

			var num=Math.floor(Math.random()*6)
			if (num == 5){
				//google_kw = 'Online Trading';
				google_ad_channel = 'channel5';
			}else if (num == 1){
				//google_kw = 'Online Trading';
				google_ad_channel = 'channel1';
			}else if (num == 3){
				//google_kw = 'VOIP';
				google_ad_channel = 'channel3';
			}else if (num == 4){
				//google_kw = 'Online Trading';
				google_ad_channel = 'channel4';
			}else{ 
				//google_kw = 'Online Trading';
				google_ad_channel = 'channel2';
			}
		}
		//temp_google_kw = google_kw;
	 }else{
		google_ad_client = 'pub-4773817401385873';
     	google_max_num_ads = '3';
		if (o_urlstring.indexOf("forms.") != -1){
			google_ad_channel = 'forms';
		}else if (o_urlstring.indexOf("11stategov") != -1){
			google_ad_channel = 'jurisdictions';
		}else if ((o_urlstring.indexOf("casecode") != -1) || (o_urlstring.indexOf("caselaw") != -1)){
			google_ad_channel = 'cases_codes';
		}else if (o_urlstring.indexOf("01topics") != -1){
			google_ad_channel = 'practice_areas';
		}
	}

