function wily_computeFrameName(name)
{
	if (window.top != window.self) {
		if ((window.name) && (window.name.length > 0)) {
			name=name+"_"+wily_crc32_hex(window.name);
		} else {
			name=name+"_"+wily_crc32_hex(window.self.document.URL);
		}
	}
	return name;
}

function wily_createCookie(name,value,days)
{
	wily_createCookie0(wily_computeFrameName(name),value,days);
}

function wily_createCookie0(name,value,days)
{
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function wily_readCookie(name)
{
	return wily_readCookie0(wily_computeFrameName(name));
}

function wily_readCookie0(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function wily_eraseCookie(name)
{
	wily_eraseCookie0(wily_computeFrameName(name));
}

function wily_eraseCookie0(name)
{
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(name) == 0) {
			cn = c.split('=')[0];
			wily_createCookie0(cn,0,-1);
		}
	}
}

function wily_eraseTopWilyCookies()
{
	wily_eraseCookie0("wily_unloadTime");
	wily_eraseCookie0("wily_clickTime");
	wily_eraseCookie0("wily_altClickTime");
	wily_eraseCookie0("wily_clickLink");
}

function wily_URLencode(sStr)
{
	if (window.encodeURIComponent) {
		return encodeURIComponent(sStr).replace(/\'/g, '%27');
	} else {
		return escape(sStr).replace(/\+/g, '%2C');
	}
}

function wily_now()
{
	return new Date().getTime();
}

function wily_eraseStaleClick()
{
	var altClickTime = wily_readCookie0("wily_altClickTime");
	wily_eraseCookie0("wily_altClickTime");
}

function wily_record_clickTime(event)
{
	if (!event) var event = window.event;
	var targ;
	if (event.target) targ = event.target;
	else if (event.srcElement) targ = event.srcElement;
	if (targ.nodeType == 3) targ = targ.parentNode; // for safari
	if (targ.href) {
		wily_createCookie0("wily_clickTime", wily_now());
		wily_createCookie0("wily_clickLink", wily_URLencode(targ.href));
	} else {
		wily_createCookie0("wily_altClickTime", wily_now());
		window.setTimeout('wily_eraseStaleClick();', 500);
	}
	wily_eraseCookie("wily_unloadTime");
}

function wily_onclick(event)
{
	if (!event) var event = window.event;
	var targ;
	if (event.target) targ = event.target;
	else if (event.srcElement) targ = event.srcElement;
	if (targ.nodeType == 3) targ = targ.parentNode; // for safari

	wily_createCookie0("wily_clickTime", wily_now());
	wily_eraseCookie("wily_unloadTime");

	if (targ.href) {
		wily_createCookie0("wily_clickLink", wily_URLencode(targ.href));
	} else if (targ.action) {
		wily_createCookie0("wily_clickLink", wily_URLencode(targ.action));
	} else {
		wily_createCookie0("wily_clickLink", "undetermined");
	}
}

function wily_attachEventToLinks()
{
	for (i = 0; i < document.links.length; i++)
	{
		var link = document.links[i];
		if (link.addEventListener) {
			link.addEventListener('click',wily_onclick,true)
		} else if (document.attachEvent) {
			link.attachEvent('onclick',wily_onclick)
		}
	}
}

function wily_attachEventToForms()
{
	for (i = 0; i < document.forms.length; i++)
	{
		var link = document.forms[i];
		if (link.addEventListener) {
			link.addEventListener('submit',wily_onclick,true);
		} else if (document.attachEvent) {
			link.attachEvent('onsubmit',wily_onclick);
		}
	}
}

function wily_attachEventToPopups()
{
	if (window.createPopup) {
		var prevFunc = window.createPopup;
		window.createPopup = function (vArgs) {
			var result = prevFunc(vArgs);
			if (document.attachEvent) {
				result.document.attachEvent('onclick', wily_record_clickTime);
			}
			return result;
		}
	}
}

function wily_attachEventToDocument()
{
	if (window.wily_record_clickTime) {
		if (document.addEventListener) {
			document.addEventListener('click',wily_record_clickTime,true)
			document.addEventListener('submit',wily_record_clickTime,true);
		} else if (document.attachEvent) {
			document.attachEvent('onclick',wily_record_clickTime)
			document.attachEvent('onsubmit',wily_record_clickTime);
		}
	}
}

function wily_window_imageSentinel(browserDoneUrl)
{
	wilyImage = new Image();
	wilyImage.src = browserDoneUrl;
}

function wily_window_onunload()
{
	try {
		wily_createCookie("wily_unloadTime", wily_now());
		if (window.top != window.self) {
			var loadCounter = 0;
			if (window.top.wily_loadCounter) {
				loadCounter = window.top.wily_loadCounter;
			}
			loadCounter++;
			window.top.wily_loadCounter = loadCounter;
		}
	} catch (e) {
	}
}

function wily_attachEventToWindow(earlyFlag) {
	if (earlyFlag) {
        var wily_prev_onload = (window.onload) ? window.onload : function () {};
		window.onload = function () {
			wily_window_onload();
			return wily_prev_onload();
		};
    } else {
		if (window.addEventListener) {
			window.addEventListener('load',wily_window_onload,false);
		} else if (window.attachEvent) {
			window.attachEvent('onload',wily_window_onload);
		}
	}
	if (window.addEventListener) {
		window.addEventListener('unload',wily_window_onunload,false);
	} else if (window.attachEvent) {
		window.attachEvent('onunload',wily_window_onunload);
	}
}

function wily_attachEventToFrameset(earlyFlag) {
	if (window.top != window.self) {
		var fsTags = top.document.getElementsByTagName("frameset");
		if (fsTags.length > 0) {
			if (window.wily_record_clickTime) {
				if (top.addEventListener) {
					top.addEventListener('load',wily_eraseTopWilyCookies,false);
				} else if (document.attachEvent) {
					top.attachEvent('onload',wily_eraseTopWilyCookies);
				}
			}
		}
	}
}

function wily_crossFrameDomainFixer(wily_functionCall, funcArg, docDomain)
{
//	alert (document.domain + " : " + document.URL );
	try
	{
		//var framesetDomain = window.top.document.domain;
		return wily_functionCall(funcArg);
	}
	catch ( e)
	{
		// the domain is not the same, so we must try to trim from left.
		if ( wily_trimDocDomain() )
		{
			// domain was trimmed.. try if parent's domain can be accesed,
			// call the function again
			return wily_crossFrameDomainFixer(wily_functionCall, funcArg, docDomain);
		}
		else
		{
			// revert back to the original domain for the document.
			// Document not on the same second-level domain as frameset
			document.domain = docDomain;
			return (function (x) {});
		}
	}
}

/**
Will trim the document.domain based on the following rules
   - if the domain ends with any with 2 alphabets after the last dot, then
	 it	 will attempt to trim till the second last dot.
   - if it ends with 3 or more alphabets, then it wil trim till the last dot.<b>
Return : true if it was able to trim the domain, false if not.
*/
function wily_trimDocDomain()
{
	var retVal = true;
	var docDomain  = document.domain;
	var firstDot = docDomain.indexOf('.');
	var lastDot = docDomain.lastIndexOf('.');
	if ( firstDot < lastDot )
	{
		var endsWith = docDomain.slice(lastDot+1).length;
		if ( endsWith == 2 )   // is a country, so domain must have 2 dots
		{
		   var secondLastDot = docDomain.lastIndexOf('.', (lastDot-1));
		   if ( firstDot < secondLastDot )
		   {
			 document.domain = docDomain.slice(firstDot+1);
		   }
		   else
		   {
			 retVal = false;
		   }
		}
		else
		{
		  document.domain = docDomain.slice(firstDot+1);
		}
	}
	else
	{
	  retVal = false;
	}
	return retVal;;
}
