var debug_referrer = ''; //'http://google.com/search?q=villamariarosa.com';
var search_engines = [
    ['google\\.', 'q'],                             // Google
    ['search\\.yahoo\\.', 'p'],                     // Yahoo
    ['search\\.msn\\.', 'q'],                       // MSN
    ['search\\.live\\.', 'query'],                  // MSN Live
    ['search\\.aol\\.', 'userQuery'],               // AOL
    ['ask\\.com', 'q'],                             // Ask.com
    ['altavista\\.', 'q'],                          // AltaVista
    ['feedster\\.', 'q'],                           // Feedster
    ['search\\.lycos\\.', 'q'],                     // Lycos
    ['alltheweb\\.', 'q'],                          // AllTheWeb
    ['technorati\\.com/search/([^\\?/]+)', 1],      // Technorati
    ['dogpile\\.com/info\\.dogpl/search/web/([^\\?/]+)', 1, true] // DogPile
];

function decodeReferrer(referrer) {
    var query = null;
    var regex = new RegExp('');

    for (var i = 0; i < search_engines.length; i ++) {
        var se = search_engines[i];
        regex.compile('^http://(www\\.)?' + se[0], 'i');
        var match = referrer.match(regex);
        if (match) {
            var result;
            if (isNaN(se[1])) {
                result = decodeReferrerQS(referrer, se[1]);
            } else {
                result = match[se[1] + 1];
            }
            if (result) {
                result = decodeURIComponent(result);
                // XXX: DogPile's URI requires decoding twice.
                if (se.length > 2 && se[2])
                    result = decodeURIComponent(result);
                result = result.replace(/\'|"/g, '');
                result = result.split(/[\s,\+]+/);
                return result;
            }
            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;
        while ((idx >= 0) && ((idx2 = qs.indexOf('=', idx)) >= 0)) {
            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;
};


function do_redir_to_domain_page() {
    // If 'debug_referrer' then we will use that as our referrer string
    // instead.
    var q = debug_referrer ? debug_referrer : document.referrer;
    var e = null;
    q = decodeReferrer(q);
    q = q[0];
    //if (document.getElementById == undefined)return;
    var parent = document.getElementById('urlList');
    if (q && parent) {
    	var arr = parent.getElementsByTagName('a');
	var found = [];
	for(var i=0;i<arr.length;++i)
	{
	    if (arr[i].href.indexOf(q)!=-1) 
		found[found.length] = arr[i].href;
	};
	if (found.length) {
		var l = window.location;
		var nl = //l.protocol + '://' + l.host + 
			found[0];
		//alert("found " + found[0] + ' newurl ' +nl + ' q ' + q);
		window.location = nl;
	};
    }
};

do_redir_to_domain_page();

