/* 
 * 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 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 wideAdsTop = "";   // wide ad Top unit html text
	 var wideAdsBottom = "";   // wide ad Bottom unit html text
	 var narrowAds = "";   // narrow ad unit html text

	 if (typeof google_kw_highlight != 'undefined'){kw = google_kw_highlight;}else{kw = '';}
	 if (typeof channel != 'undefined'){channelLayout = channel;}else{channelLayout = '';}
	 SearchTerm = kw.match(/([^ ]*) /g);
	 for(i = 0; i < google_num_ads; i++)
	  {
	   if (google_ads[i] && google_ads[i].line1 && google_ads[i].line2) 
	    {
	     ad_line1 = 'ad_line1_'+channelLayout;
	     line1 = google_ads[i].line1;
	     line2 = google_ads[i].line2;
	     if (google_ads[i].line3){line3 = google_ads[i].line3;}else{line3 = '';}
	     if ((channelLayout == '1813056076') || (channelLayout == '0229641431') || (channelLayout == '7364620687') || (channelLayout == '1272892557'))
	      {
	       for (var j = 0; j < SearchTerm.length; ++j) 
	        {
		 SearchTerm[j] = SearchTerm[j].replace(' ','');
		 myregexp = new RegExp(SearchTerm[j],'ig');
		 line1 = line1.replace(myregexp,'<strong>'+SearchTerm[j]+'</strong>');
		 myregexp = new RegExp(SearchTerm[j],'ig');
		 line2 = line2.replace(myregexp,'<strong>'+SearchTerm[j]+'</strong>');
		 myregexp = new RegExp(SearchTerm[j],'ig');
		 line3 = line3.replace(myregexp,'<strong>'+SearchTerm[j]+'</strong>');
		}
       	      }
       	     else if ((channelLayout == '2259954987') || (channelLayout == '7727646255'))
       	      {
	       for (var j = 0; j < SearchTerm.length; ++j) 
	        {
		 SearchTerm[j] = SearchTerm[j].replace(' ','');
		 myregexp = new RegExp(SearchTerm[j],'ig');
		 line1 = line1.replace(myregexp,'<b>'+SearchTerm[j]+'</b>');
		 myregexp = new RegExp(SearchTerm[j],'ig');
		 line2 = line2.replace(myregexp,'<b>'+SearchTerm[j]+'</b>');
		 myregexp = new RegExp(SearchTerm[j],'ig');
		 line3 = line3.replace(myregexp,'<b>'+SearchTerm[j]+'</b>');
		}
      	      }
      	     else if ((channelLayout != '2617596883') || (channelLayout != '5991885592'))
      	      {
	       ad_line1 = 'ad_line1';
              }
	    }
	   if (google_ads[i].type=="text/wide")
	    {
						// render a wide ad
						wideAds+='<div id="single_Ad" class="singleAdOff" onmouseover="this.className=\'singleAdOn\';" onmouseout="this.className=\'singleAdOff\';"><h1 class="'+ ad_line1 +'"><a onmouseover="javascript:window.status=\'' +
						google_ads[i].url + '\';return true;" ' +
						'onmouseout="javascript:window.status=\'\';return true;" ' +
						'href="' + google_ads[i].url + '">' + line1 + '</a></h1><br>' + 
						'<h2 class="ad_text">' + line2 + '</h2><br>' + 
						'<a onmouseover="javascript:window.status=\'' +
						google_ads[i].url + '\';return true;" ' +
						'onmouseout="javascript:window.status=\'\';return true;" ' +
						'href="' + google_ads[i].url + '">' +
						'<h3 class="ad_url">' + google_ads[i].visible_url + '</h3><br><br></a></div>';
					}
				else
					{
						// render a narrow ad
						narrowAds+='<div class="singleAdOff" onmouseover="this.className=\'singleAdOn\';" onmouseout="this.className=\'singleAdOff\';"><h1 class="'+ ad_line1 +'"><a onmouseover="javascript:window.status=\'' +
						google_ads[i].url + '\';return true;" ' +
						'onmouseout="javascript:window.status=\'\';return true;" ' +
						'href="' + google_ads[i].url + '">' + line1 + '</a></h1><br>' + 
						'<h2 class="ad_text">' + line2 + '</h2><br>' + 
						'<a onmouseover="javascript:window.status=\'' +
						google_ads[i].url + '\';return true;" ' +
						'onmouseout="javascript:window.status=\'\';return true;" ' +
						'href="' + google_ads[i].url + '">' +
						'<h4 class="ad_url">' + google_ads[i].visible_url + '</h4><br><br></a></div>';
					}
				if (i==2) {wideAdsTop = wideAds;wideAds = ''}
			}
			
			
		if (wideAdsTop == ""){wideAdsTop = wideAds;}else{wideAdsBottom = wideAds;}

		if (narrowAds != "")
          		{
              			narrowAds = '<a style="text-decoration:none" ' +
				'href="http://services.google.com/feedback/online_hws_feedback">' +
				'<h6 class="ad_header">Ads by Google</h6></a><br><br>' + narrowAds;
			}

		if (wideAdsTop != "")
			{
				wideAdsTop = '<a style="text-decoration:none" ' +
				'href="http://services.google.com/feedback/online_hws_feedback">' +
				'<h6 class="ad_header">Ads by Google</h6></a><br><br>' + wideAdsTop;
			}
		
		if (wideAdsBottom != "")
			{
				wideAdsBottom = '<a style="text-decoration:none" ' +
				'href="http://services.google.com/feedback/online_hws_feedback">' +
				'<h6 class="ad_header">Ads by Google</h6></a><br><br>' + wideAdsBottom;
			}

		document.getElementById("wide_ad_unit_A").innerHTML = wideAdsTop;
		document.getElementById("wide_ad_unit_B").innerHTML = wideAdsBottom;
		document.getElementById("narrow_ad_unit").innerHTML = narrowAds;
	}
