/* This function scan HTTP_REFERER and extract query string.
if query string exist, return decoded string. otherwise return ''.
*/
function get_query_from_str(r) {
  if (r.length) {
    patterns = ["q=","query=","p=","MT=","search="]
    found = 0;
    while (patterns.length>0 && !found) {
      pat = patterns.shift();
      i = r.indexOf(pat);
      if (i>=0) {
        r = r.substring(i+pat.length);
        found = 1;
      }
    }
    if (found) {
      i = r.indexOf("&")
      if (i>=0) {
        r = r.substring(0,i)
      }
      re = /[+]/g;
      r = r.replace(re, ' ');
      try {
        r = decodeURIComponent(r);
      } catch(e) {
        // r = unescape(r);
        r = "";
      }
      re = /[%]2B/g;
      r = r.replace(re, '+');
      return r;
    }
  }
  return '';
}
function get_ref_query() {
  r = document.referrer;
  if (r.length) {
    r = get_query_from_str(r);
    if (r) return r;
  }
  r = location.search;
  if (r.length) {
    r = get_query_from_str(r);
    if (r) return r;
  }
  return '';
}


