// Alan Marcero, 800response.com
// June, 2007
//

function decodeReferrer() {
	var search_engines = [
		['google\\.', 'q'],                             // Google
		['search\\.yahoo\\.', 'p'],                     // Yahoo
		['search\\.msn\\.', 'q'],                       // MSN
		['search\\.live\\.', 'q'],                  	// MSN Live
		['search\\.aol\\.', 'userQuery'],               // AOL
		['ask\\.com', 'q'],                             // Ask.com
		['altavista\\.', 'q'],                          // AltaVista
		['search\\.lycos\\.', 'query'],                 // Lycos
		['alltheweb\\.', 'q'],                          // AllTheWeb
		['dogpile\\.com/info\\.dogpl/search/web/([^\\?/]+)', 1, true] // DogPile
	];
	var referrer = document.referrer;
	
//  uncomment for debugging/testing - sets the referrer to a google search
//	var referrer = "http://www.google.com/search?q=800+response+marketing+llc&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a";

	var query = null;
    var regex = new RegExp('');

    for (var i = 0; i < search_engines.length; i ++) {
        var se = search_engines[i];
        var match = referrer.match('^http://(www\\.)?' + se[0], 'i');
        if (match) {
            var result;
            if (isNaN(se[1])) 
                result = decodeReferrerQS(referrer, se[1]);
            else 
                result = match[se[1] + 1];
				
            if (result) {
                result = decodeURIComponent(result);
				
                // DogPile's URI requires decoding twice.
                if (se.length > 2 && se[2])
                    result = decodeURIComponent(result);
					
                result = result.replace(/\'|"/g, '');
                result = result.split(/[\s,\+\.]+/);
				
				//formulate the cookie
				var cookie = "keywords=";
				for(var i = 0; i != result.length; i++)
					cookie += " " + escape(result[i]);
					
				//write the cookie
				document.cookie = cookie;

//  uncomment for debugging/testing -- prints the cookie contents to an alert
//				if (document.cookie.indexOf('keywords') != -1) 
//					alert("Got Cookies!\n\n" + document.cookie); 
//				else 
//					alert("No Cookies!"); 
            }
            break;
        }
		
    }
    return null;
}

function decodeReferrerQS(referrer, match) {
    var idx = referrer.indexOf('?');
    var idx2;
    if (idx >= 0) {
        var qs = new String(referrer.substring(idx + 1));
        idx  = 0;
        idx2 = 0;
        for (var i = 0;
			 // gets the index for substring or stops if it searches all of referrer
			 (idx >= 0) && ((idx2 = qs.indexOf('=', idx)) >= 0) && (i != referrer.length);
			 i++) {
            var key, val;
            key = qs.substring(idx, idx2);
            idx = qs.indexOf('&', idx2) + 1;
            if (key == match) {
                if (idx <= 0) 
                    return qs.substring(idx2+1);
                else
                    return qs.substring(idx2+1, idx - 1);
            }
        }
    }
    return null;
}

if (window.addEventListener) window.addEventListener("load",  decodeReferrer, false);
else if (window.attachEvent) window.attachEvent("onload", decodeReferrer);