/**
 * Search Engine Keyword Logger.
 *
 * This javascript can be used to record the keywords used to bring a visitor
 * to a page and then log the data either for aggregate analysis later or to
 * alter the visitor's user experience, perhaps by high-lighting the keyword on
 * the page
 *
 * Usage:
 *
 *   In HTML. Add the following line just before the </body> tag.
 *
 *   <script type="text/javascript" src="/mumps/se_keywords.js"></script>
 *
 *
 * @author Steve Mulligan
 * @version 0.1
 */

/**
 * Decode the referrer string and return a list of search keywords.
 */
function get_keywords() {
    referrer = decodeURIComponent(document.referrer);
    var keywords = null;

    if (referrer.match(/^http:\/\/(www\.)?alltheweb.*/i)) {
	// AllTheWeb
	if (referrer.match(/q=/))
	    keywords = referrer.replace(/^.*q=([^&]+)&?.*$/i, '$1');
    } else if (referrer.match(/^http:\/\/(www)?\.?google.*/i)) {
	// Google
	if (referrer.match(/q=/))
	    keywords = referrer.replace(/^.*q=([^&]+)&?.*$/i, '$1');
    } else if (referrer.match(/^http:\/\/search\.lycos.*/i)) {
	// Lycos
	if (referrer.match(/query=/))
	    keywords = referrer.replace(/^.*query=([^&]+)&?.*$/i, '$1');
    } else if (referrer.match(/^http:\/\/www\.bing.*/i)) {
	// Bing
	if (referrer.match(/q=/))
	    keywords = referrer.replace(/^.*q=([^&]+)&?.*$/i, '$1');
    } else if (referrer.match(/^http:\/\/search\.yahoo.*/i)) {
	// Yahoo
	if (referrer.match(/p=/))
	    keywords = referrer.replace(/^.*p=([^&]+)&?.*$/i, '$1');
    }

    if (keywords) {
		keywords = keywords.replace(/\'|"/, '');
		//keywords = keywords.replace(/[\s,\+\.]+/, ', ');
    }

    return keywords;
};

