// Copyright (C) 2005, WebSurveyor Corporation
// All rights reserved.
// Author: David Alison

fx=50;
fy=50;
lw=175;
lh=200;
halign="left";
valign="top";
delayspeed=3;
surveyInNewWindow=false;
randomRate=2;  // 1 out of randomRate people will get the survey

NS6=false;
IE4=(document.all);
if (!IE4) {NS6=(document.getElementById);}
NS4=(document.layers);

function adjust() {
if ((NS4) || (NS6)) {
if (lastX==-1 || delayspeed==0)
{
lastX=window.pageXOffset + fx;
lastY=window.pageYOffset + fy;
}
else
{
var dx=Math.abs(window.pageXOffset+fx-lastX);
var dy=Math.abs(window.pageYOffset+fy-lastY);
var d=Math.sqrt(dx*dx+dy*dy);
var c=Math.round(d/10);
if (window.pageXOffset+fx>lastX) {lastX=lastX+delayspeed+c;}
if (window.pageXOffset+fx<lastX) {lastX=lastX-delayspeed-c;}
if (window.pageYOffset+fy>lastY) {lastY=lastY+delayspeed+c;}
if (window.pageYOffset+fy<lastY) {lastY=lastY-delayspeed-c;}
}
if (NS4){
document.layers['floatlayer'].pageX = lastX;
document.layers['floatlayer'].pageY = lastY;
}
if (NS6){
document.getElementById('floatlayer').style.left=lastX;
document.getElementById('floatlayer').style.top=lastY;
}
}
else if (IE4){
if (lastX==-1 || delayspeed==0)
{
lastX=document.body.scrollLeft + fx;
lastY=document.body.scrollTop + fy;
}
else
{
var dx=Math.abs(document.body.scrollLeft+fx-lastX);
var dy=Math.abs(document.body.scrollTop+fy-lastY);
var d=Math.sqrt(dx*dx+dy*dy);
var c=Math.round(d/10);
if (document.body.scrollLeft+fx>lastX) {lastX=lastX+delayspeed+c;}
if (document.body.scrollLeft+fx<lastX) {lastX=lastX-delayspeed-c;}
if (document.body.scrollTop+fy>lastY) {lastY=lastY+delayspeed+c;}
if (document.body.scrollTop+fy<lastY) {lastY=lastY-delayspeed-c;}
}
document.getElementById('floatlayer').style.posLeft = lastX;
document.getElementById('floatlayer').style.posTop = lastY;
}
setTimeout('adjust()',50);
}

function GetQueryStringFromRef()
{
	var parameterString = document.referrer.replace(/.*\?(.*)/, "$1");
	var parameterTokens = parameterString.split("&");
	var parameterList = new Array();
	
	for (i = 0; i < parameterTokens.length; i++)
	{
		var parameterName = parameterTokens[i].replace(/(.*)=.*/, "$1");
		var parameterValue = parameterTokens[i].replace(/.*=(.*)/, "$1");
	
		parameterList[parameterName] = parameterValue;
	}
	
//	return parameterList["q"];
	
	if (parameterList["q"] == undefined)
	{
		if (parameterList["p"] == undefined)
		{
			return "";
		}
		else
		{
			return parameterList["p"];
		}
	}
	else
	{
		return parameterList["q"];
	}
			
}

function hideSurvey()
{

	var expdate = new Date();
	var days = 30;  // NOTE: generate this based on user settings

	expdate.setTime(expdate.getTime() + days*24*60*60*1000);

	document.getElementById('floatlayer').style.display = "none";
	document.cookie = "WhyClicked=0; path=/; expires=" + expdate.toGMTString();;
}

function showSurvey()
{
	var sURL;
	var sQueryString;
	
	sURL = escape(document.location.href);
	
	sQueryString = GetQueryStringFromRef();
	
	if (surveyInNewWindow)
	{
		newWindow = window.open( "http://websurveyor.net/wsb.dll/2575/tu3.htm?wsb24=whoclicked.net/thanks.html&wsb51=" + escape(document.referrer) + "&wsb29=" + escape(sQueryString) );
		hideSurvey();
	}
	else
	{
		window.location.href = "http://websurveyor.net/wsb.dll/2575/tu3.htm?wsb24=" + sURL + "&wsb51=" + escape(document.referrer) + "&wsb29=" + escape(sQueryString);
		hideSurvey();
	}
}

function hitRandom()
{
	randomNumber = Math.floor(Math.random()*(randomRate)) + 1;
	return randomNumber == 1;
}

function hitFromSearch()
{
	// if the document.referrer contains google, yahoo or msn it came from search
	return ((document.referrer.indexOf("www.google.") > -1) || (document.referrer.indexOf("search.msn.") > -1) || (document.referrer.indexOf("search.yahoo.") > -1) || (document.referrer.indexOf("google.com") > -1) );
}

function define()
{
if ((NS4) || (NS6))
{
if (halign=="left") {fx=ifx};
if (halign=="right") {fx=window.innerWidth-ifx-lw-20};
if (halign=="center") {fx=Math.round((window.innerWidth-20)/2)-Math.round(lw/2)};
if (valign=="top") {fy=ify};
if (valign=="bottom") {fy=window.innerHeight-ify-lh};
if (valign=="center") {fy=Math.round((window.innerHeight-20)/2)-Math.round(lh/2)};
}
if (IE4)
{
if (halign=="left") {fx=ifx};
if (halign=="right") {fx=document.body.offsetWidth-ifx-lw-20}
if (halign=="center") {fx=Math.round((document.body.offsetWidth-20)/2)-Math.round(lw/2)}
if (valign=="top") {fy=ify};
if (valign=="bottom") {fy=document.body.offsetHeight-ify-lh}
if (valign=="center") {fy=Math.round((document.body.offsetHeight-20)/2)-Math.round(lh/2)}
}
}
if (document.URL.indexOf("survey=1") != -1 | (hitRandom() & hitFromSearch()) )
{
if (NS4) {document.write('<LAYER NAME="floatlayer" LEFT="'+fx+'" TOP="'+fy+'">');}
if ((IE4) || (NS6)) {document.write('<div id="floatlayer" style="position:absolute; left:'+fx+'; top:'+fy+';">');}
if (document.cookie.indexOf("WhyClicked=0") != -1)
{
	document.getElementById('floatlayer').style.display = "none";
}

document.write( '<table width="300" border="1" cellspacing="0" cellpadding="0">' );
document.write( '<tr><td><table width="300" border="0" cellspacing="0" cellpadding="2" bgcolor="#C0C0C0"><tr>' );
document.write( '<td align="left" bgcolor="#000080" background="http://whoclicked.net/images/g_caption.jpg">' );
document.write( '<table width="100%" border="0" cellspacing="0" cellpadding="1"><tr>' );
document.write( '<td width="100%" align="left"><font face="Tahoma,Arial,Helvetica" size="2" color="#FFFFFF"><strong>Survey Invitation</strong></font></td>' );
document.write( '<td align="right"><a href="#" onclick="hideSurvey();"><img src="http://whoclicked.net/images/close_btn.gif" alt="Close this invitation" border="0"></a></td>' );
document.write( '</tr></table></td></tr><tr><td height="100%"><table border="0" cellpadding="3"><tr>' );
document.write( '<td><font face="Verdana,Helvetica" size="2" color="#000000">We are conducting research on why people come to our site from Search Engines. You have been randomly selected to take a brief, anonymous survey. <br><br>Will you take the survey now?<br></font></td>' );
document.write( '</tr></table></td></tr><tr><td height="100%"><table width="100%" border="0" cellpadding="3"><tr>' );
document.write( '<td align="center"><input type="button" value="  Yes  " width="40" onclick="showSurvey();" style="font-weight: bold;">&nbsp;&nbsp;<input type="button" value="    No    " width="40" onclick="hideSurvey();" style="font-weight: normal;"></td>' );
document.write( '</tr></table></td></tr></table></td></tr></table>' );

if (NS4)
{
document.write('</LAYER>');
}
if ((IE4) || (NS6))
{
document.write('</DIV>');
}
ifx=fx;
ify=fy;
define();
window.onresize=define;
lastX=-1;
lastY=-1;
adjust();
}