// set debugMode to true to check

var debugMode=false;
var testurl = "http://www.google.co.uk/search?hl=en&q=na+no&btnG=Google+Search&meta=";
// var testurl = "http://www.onestoptechshop.com/poo/shit/arse/";

function JSQueryString(par) {
var sTemp 
var sPar
var sFin, iStart, iEnd
	sTemp = new String(window.location)
	sPar = new String(par + "=")
	iStart = sTemp.indexOf(sPar)
	if (iStart>0) {
		iFin = sTemp.indexOf("&",iStart)
		if (iFin==-1) {
			//Se non c'è fine allora è l'ultimo
			iFin = sTemp.length
		}
		sFin = sTemp.substring(iStart + sPar.length,iFin)
	} else {
		sFin = ""
	}
	return sFin
}
function urldecode(str) {
    var ret = str
    ret = ret.replace(/\+/g, '%20');
    ret = decodeURIComponent(ret);
    ret = ret.toString();
    return ret;
}
function urlencode( str ) {
    var ret = str;
    ret = ret.toString();
    ret = encodeURIComponent(ret);
    ret = ret.replace(/%20/g, '+');
    return ret;
}
function parseUri (str) {
	var	o   = parseUri.options,
		m   = o.parser[o.strictMode ? "strict" : "loose"].exec(str),
		uri = {},
		i   = 14;
	while (i--) uri[o.key[i]] = m[i] || "";
	uri[o.q.name] = {};
	uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) {
		if ($1) uri[o.q.name][$1] = $2;
	});
	return uri;
};
parseUri.options = {
	strictMode: false,
	key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],
	q:   {
		name:   "queryKey",
		parser: /(?:^|&)([^&=]*)=?([^&]*)/g
	},
	parser: {
		strict: /^(?:([^:\/?#]+) {)?(?:\/\/((?:(([^:@]*) {?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
		loose:  /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+) {)?(?:\/\/)?((?:(([^:@]*) {?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
	}
};
// {{{ parse_str
function parse_str(str, array){
    var glue1 = '=';
    var glue2 = '&';

    var array2 = str.split(glue2);
    var array3 = [];
    for(var x=0; x<array2.length; x++){
        var tmp = array2[x].split(glue1);
        array3[unescape(tmp[0])] = unescape(tmp[1]).replace(/[+]/g, ' ');
    }

    if(array){
        array = array3;
    } else{
        return array3;
    }
}// }}}
parseUri.options.strictMode = true;

// *** TESTING ***
// normal code
var seo_referrer = new parseUri(document.referrer)
// testing code
//var seo_referrer = new parseUri(testurl)
// *** choose one of the two above

var seo_query = new Array()
seo_query = parse_str(seo_referrer.query)
if (debugMode) {
	document.writeln("You should see the parameters sent to the search site: " + seo_referrer.query)
	document.writeln("</br>")
}
var refdom = (seo_referrer.host + seo_referrer.path).toLowerCase()
if (refdom.substr(0,7)=="http://") {
	refdom = refdom.substr(7,99)
}
if (debugMode) {
	document.writeln("You should see the search engine with the path, without protocol (example: www.google.com/search): " + refdom)
	document.writeln("</br>")
}
var lookfor
var p
if (refdom == "www.google.com/search") {
	lookfor = "www.google.com/search"; p = "q";
	refdomshort = "google";
} else if (refdom == "www.google.co.uk/search" ) {
    lookfor = "www.google.co.uk/search"; p = "q";
	refdomshort = "google";
} else if (refdom == "search.msn.co.uk/results.aspx" ) {
    lookfor = "search.msn.co.uk/results.aspx"; p = "q";
	refdomshort = "msn";
} else if (refdom == "search.msn.com/results.asp" ) {
    lookfor = "search.msn.com/results.asp"; p = "q";
	refdomshort = "msn";
} else if (refdom == "search.yahoo.com/search" ) {
    lookfor = "search.yahoo.com/search"; p = "p";
	refdomshort = "yahoo";
} else if (refdom == "search.yahoo.com/search/dir" ) {
    lookfor = "search.yahoo.com/search/dir"; p = "p";
	refdomshort = "yahoo";
} else if (refdom == "uk.search.yahoo.com/search" ) {
    lookfor = "uk.search.yahoo.com/search"; p = "p";
	refdomshort = "yahoo";
} else if (refdom == "uk.search.yahoo.com/search/dir" ) {
    lookfor = "uk.search.yahoo.com/search/dir"; p = "p";
	refdomshort = "yahoo";
} else if (refdom.substr(0,23) == "www.onestoptechshop.com" ) {
	refdomshort = "onestop";
} else if (refdom.substr(0,16) == "www.facebook.com" ) {
	refdomshort = "facebook";
} else { refdom='x'}

if (debugMode) {
	document.writeln("domain+path: " + lookfor)
	document.writeln("</br>")
	document.writeln("query: " + p)
	document.writeln("</br>")
}
var epi
var epi2
s = seo_query[p];

if (s){
	epi2 = urlencode(seo_query[p]);
}


if (lookfor){
	epi = urlencode(lookfor);
}

if (debugMode) {
	document.writeln("epi (engine): " + epi)
	document.writeln("</br>")
	document.writeln("epi2 (keyword): " + epi2)
	document.writeln("</br>")
}


// ASP
// ASP
// ASP

var ref = querySt("ref");
var fire = false;
//alert ('refdom=' + refdom);
//alert ('ref=' + ref);
//alert ('refdomshort=' + refdomshort);
if (refdom=='x'){
	// WE HAVE NO REFERRING DOMAIN - ALWAYS FIRE
	fire = true;
//alert ('1');
} else {
	if (refdomshort == "onestop"){
		fire = false;
//alert ('1a');
	} else {
		// REFERRING DOMAIN (SEARCH ENGINE)
		if ( (refdomshort == "facebook") || ((ref=='goo') && (refdomshort == "google")) || ((ref=='msn')  && (refdomshort == "msn")) || ((ref=='ypi') && (refdomshort == "yahoo")) || ((ref=='yah') && (refdomshort == "yahoo"))){
			fire = false;
//alert ('2');
		} else {
			fire = true;
//alert ('3');
		}		
	}
}


if (fire){
	// Print tracking image
	document.write("<img style=\"display:none\" src='http://clk.tradedoubler.com/click?p=76912&a=1552567&g=0&epi=" + epi + "&epi2=" + epi2 + "'>");
	testurl = "<img src='http://clk.tradedoubler.com/click?p=76912&a=1552567&g=0&epi=" + epi + "&epi2=" + epi2 + "'>";
	//alert(testurl);
}
function querySt(ji) {
	hu = window.location.search.substring(1);
	gy = hu.split("&");
	for (i=0;i<gy.length;i++) {
		ft = gy[i].split("=");
		if (ft[0] == ji) {
			return ft[1];
		}
	}
}

// ASP
// ASP
// ASP




