var google_afs_adsafe = 'medium';
var googleAdNewWindow = '1';
var google_afs_ie = 'utf8';
var google_afs_oe = 'utf8';
var google_top = '';
var google_btm = '';

function google_afs_request_done(google_ads) {
  google_top = google_afs_da_build(google_ads, 'google_top', 0, google_afs_da_top_cnt);
  google_btm = google_afs_da_build(google_ads, 'google_btm', google_afs_da_top_cnt, google_afs_da_top_cnt+google_afs_da_btm_cnt);
  
  return;
}

function google_afs_da_build(google_ads, div_id, start_cnt, end_cnt) {
	  code = '<div class="'+div_id+'">';
	  code += '<p class="google_info"><a id="google_info" target="_blank" href="http://services.google.com/feedback/online_hws_feedback">'+google_ad_description+'</a></p>';
	  code += '<table class="google_table" width="100%" align="center" cellpadding="0" cellspacing="0">';
	  for(i = start_cnt; i < end_cnt; ++i) {
	    if (google_ads[i] && google_ads[i].line1 && google_ads[i].line2) {
	      line1 = google_ads[i].line1;
	      line2 = google_ads[i].line2;
	      if (google_ads[i].line3) {
	        line3 = google_ads[i].line3;
	      } else {
	        line3 = '';
	      }
	      line2 = enhance(line2, google_afs_da_query, '<b>', '</b>');
	      line3 = enhance(line3, google_afs_da_query, '<b>', '</b>');
	      code +=
	  '<tr>' +
	   '<td width=100% class="google_td">' +
	  '<a class="google_title_a" target="_blank" id=\'g_title_a'+i+'\' href="' + google_ads[i].url + '" onmouseover="this.style.color=\'orange\';return window.status = \''+google_ads[i].visible_url+'\';" onmouseout="this.style.color=\'#03c\';">' +
	    '<div class="google_title">' + line1 + '</div>'+
	  '</a>' +
	  '<div class="google_desc" onmouseover="getElementById(\'g_title_a'+i+'\').style.color=\'orange\';" onmouseout="getElementById(\'g_title_a'+i+'\').style.color=\'#03c\';">'+line2 + '&nbsp;' + line3 + '</div>'+
	  '<a class="google_url_a" target="_blank" id=\'g_desc_a'+i+'\' href="' + google_ads[i].url + '" onmouseover="return window.status = \''+google_ads[i].visible_url+'\';">' +
	    '<div class="google_url">' + google_ads[i].visible_url + '</div>' +
	  '</a>'+
	   '</td>' +
	  '</tr>';
	    } // if
	  } // for
	  code += '</table></div>';
	  return code;
}

function enhance(inputText, query, pre, post) {
  query += " ";
  buffer = query.match(/([^ ]*) /g);
  
  if (buffer) {
    for (var j = 0; j < buffer.length; ++j) {
      buffer[j] = buffer[j].replace(' ','');
      buffer[j] = buffer[j].replace('^','(');
      buffer[j] = buffer[j].replace('$',')');

      var patternKw = new RegExp(buffer[j],'ig');
      var matchFound = patternKw.exec(inputText);

      patternSubst= new RegExp(buffer[j],'ig');
      if(matchFound && matchFound[0].length>1) {
    	inputText = inputText.replace(patternSubst,pre+matchFound[0]+post);
      }
    }
  }
  return inputText;
}

