var googleQuery = "";
function processReferrer(/*ref*/) {
  var ref          = document.referrer;
  var regexGoogle  = /^[a-zA-Z0-9:\/\/]*\.google\.[a-zA-Z.]+/;
  var arrayMatches = ref.match(regexGoogle);
  if (arrayMatches != null) {
    if (arrayMatches.length > 0) {
      var regexQuery = "q=([^&]*)";
      arrayMatches   = ref.match(regexQuery);
      if (arrayMatches != null) {
        if (arrayMatches.length > 0) {
          googleQuery = parseQueryGoogleReferrer(arrayMatches[0]);
        }
      }
    }
  }
  return googleQuery;
}

function parseQueryGoogleReferrer(query) {
  query = query.replace("q=", "");
  query = query.replace(/^\s+|\s+$/g, " ");
  query = query.replace("?", "");
  query = query.replace(/\+/g, " ");

  // stopwords - super harcodeado
  var stopwords = new Array (8) ;
  stopwords[0]  = 'site:';
  stopwords[1]  = 'link:';
  stopwords[2]  = 'inurl:';
  stopwords[3]  = 'www';
  stopwords[4]  = '.com';
  stopwords[5]  = 'anuncios';
  stopwords[6]  = 'mundo';
  stopwords[7]  = 'anuncio';
  stopwords[8]  = 'inurl%3A';
  stopwords[9]  = 'link%3A';
  stopwords[10] = 'site%3A';

  for (i=0;i<stopwords.length;i++) {
    query = eval("query.replace(/" + stopwords[i] + "/g, \"\");");
  }
  return query;
}
