// JavaScript Document

/************  SEARCH SNIFFER CODE  *************/
/*	Created on 7/29/2009     					*/
/*	Created by Tom Dietrich  					*/
/************************************************/

/************************************************/
/*
 *  SET UP THE MATCHES
 *  this is set up as a complex array to ease with
 *  adding and subtracting possible search engines
 */
/* search engine matches */
var searchArray = new Array();
	// for Google:
	searchArray[0] = /^.*google\.[a-z\.]+\W.*[\?&]q=([^&]+).*$/;  // Google Search parameters
	// for Yahoo:
	searchArray[1] = /^.*yahoo\.[a-z\.]+\Wsearch.*[\?&]p=([^&]+).*$/;  // Yahoo Search parameters
	// for MSN:
	searchArray[2] = /^.*msn\.[a-z\.]+\W[a-z\.]+.*[\?&]q=([^&]+).*$/;  // MSN Search parameters
	// for Live:
	searchArray[3] = /^.*search\.live\.com\Wresults\.aspx.*[\?&]q=([^&]+).*$/;  // Live Search parameters
	// for Ask:
	searchArray[4] = /^.*ask\.com\Wweb.*[\?&]q=([^&]+).*$/  // Ask Search parameters
	// for Bing:
	searchArray[5] = /^.*bing\.[a-z\.]+\Wsearch.*[\?&]q=([^&]+).*$/  // Bing Search parameters
/* term matches */
var termArray = new Array();
	// for term "acupunture":
	termArray[0] = /^(.*)acupuncture(.*)$/;  // Reg Ex of the keyword or term
	termArray[1] = "http://www.leethk.com/acupuncture.html";  // Location to redirect
/************************************************/

/************************************************/
/*
 *  TRUE/FALSE - IS IT NATURAL SEARCH REFERRER
 *  return the name of the search engine
 *  or return a boolean = false
 */
function isNaturalSearch(REFERRER){
	for(i=0;i<=searchArray.length;i++){
		if(REFERRER.match(searchArray[i])){
			return true;
			break;
		}
	}
	return false;
}
/************************************************/

/************************************************/
/*
 *  REDIRECT
 *  run when the body loads add document.referrer
 *  as argument
 */
function redirectForSearch(REFERRER){
	if(isNaturalSearch(REFERRER)){
		for(i=0;i<=termArray.length-2;i=i+2){
			if(REFERRER.match(termArray[i])){
				document.location.href = termArray[i+1]
				return true;
				break;
			}
		}
	}
	return false;
}
/************************************************/