 if(isIE())
 {
 	window.attachEvent('onload', initTracking);
 }
 else if(isStandard())
 {
 	window.addEventListener('load', initTracking, false);
 }
 else if(typeof document.addEventListener != 'undefined')
 {
 	//opera 7
 	document.addEventListener('load', initTracking, false);
 }

var mouseOverFrame = false;
var frameHasFocus;
var mouseX;
var mouseY;
//this determines what frames to look for:
var iframeSrc = "googlesyndication.com";


function initTracking()
{
	var frameEl = document.getElementsByTagName("iframe");
	
	for(var i = 0; i < frameEl.length; i++) 
	{
		if(frameEl[i].src.indexOf(iframeSrc) > -1) 
		{
			if (isIE())
			{
				
				window.attachEvent('onbeforeunload', checkForClick);
				frameEl[i].attachEvent('onfocus', recordFrameFocus);
				frameEl[i].attachEvent('onblur', recordFrameBlur);
				frameEl[i].attachEvent('onmouseover', recordMouseIn);
				frameEl[i].attachEvent('onmouseout', recordMouseOut);
			}
		  else 
		  {
				//must be standard
				window.addEventListener('beforeunload', checkForClick, false);
				frameEl[i].addEventListener('mouseover', recordMouseIn, false);
				frameEl[i].addEventListener('mouseout', recordMouseOut, false);
			}

		}
	}
}

function recordFrameFocus()
{
	//alert("recordFrameFocus");
	frameHasFocus = true;
}

function recordFrameBlur()
{
	//alert("recordFrameBlur");
	frameHasFocus = false;
}
function recordMouseIn(event)
{
	frameWithMouse = event.target || event.srcElement
	mouseOverFrame = true;
}

var frameWithMouse = null;

function recordMouseOut()
{
	frameWithMouse = null;
	mouseOverFrame = false;
}

function checkForClick(event)
{		
	var clickCandidate = false;
	if (isIE())
	{
		//in IE we can at first check, if we are in the frame by testing for mouseOverFrame and frameHasFocus	
		if (mouseOverFrame && frameHasFocus)
		{
			//we do not have to check for event.target.name as with alt + -> frameHasFocus becomes false
			//here we can be already quite sure that we are in the frame
			//now we determine the position of the mouse inside the frame
			mouseX = event.clientX + document.body.scrollLeft;
			mouseY = event.clientY + document.body.scrollTop;
			
			//"Google Anzeigen" link needs not to be excluded, as this link opens a new window instead of leaving the page
			clickCandidate = true;
			
		}	
	}
	else if (isStandard())
	{
		//in Netscape/Mozilla we have only mouseOverFrame for checking, if it is a candidate
		//as mousetracking stops at the frame border. So we basically know, where he entered the frame
		//is it possible to exclude keyevents
		//eventType is always beforeUnload
		if (mouseOverFrame)
		{
			//when doing right click on iframe and going forward or back we do not end up here
			//when clicking on the link, event.target.tagName is undefined
			//when using browserback via alt + <- , it is HTML.
			if (! event.target.tagName)
			{
				clickCandidate = true;		
			}
		}		
	}
	//the times of status window access are almost over IE7 and Firefox 2 don't allow it anymore, so it 
	//is no use trying to use it.
	
	if (clickCandidate)
	{
		recordClick();
	}
  
}

function statusWritable()
{
	window.status = "Hallo";
}

function isIE()
{
	return typeof window.attachEvent!="undefined";
}

function isStandard()
{
	return typeof window.addEventListener!="undefined";
}

function recordClick()
{
	var reportId = document.getElementById("reportIdField").value;

	var time = new Date();
	var timeStamp = time.getTime()

	//http://<server>/<app>/app?rep_id=1&service=adtrack
	var trackerUrl = basePath + "/app?service=adtrack" + "&rep_id=" + reportId + "&timeStamp=" + timeStamp;		
  	
 	var req = createXMLHttpRequest();
  	if (req)
  	{
	  	req.onreadystatechange = function()
	  	{
			if (req.readyState == 4)
	    	{
	       		var xmlDoc = req.responseXML;
	   		}
		}
		req.open("GET",trackerUrl,true);

	  req.send(null);
	}
}
    
function createXMLHttpRequest() 
{
	var xhttp;
	if (window.ActiveXObject) 
	{
		try 
		{
			// IE 6 and higher
			xhttp = new ActiveXObject("MSXML2.XMLHTTP");
       	} 
       	catch (e) 
       	{
       		try 
       		{
       			// IE 5
       			xhttp = new ActiveXObject("Microsoft.XMLHTTP");
           	} 
           	catch (e) 
           	{
           		xhttp=false;
           	}
    	}
    }
    else if (window.XMLHttpRequest) 
    {
    	try 
    	{
        	// Mozilla, Opera, Safari ...
            xhttp = new XMLHttpRequest();
        } 
        catch (e) 
        {
            xhttp=false;
        }
    }
	return xhttp;
}  	
