     var minViewLeft = 1000000;
     var minViewTop = 1000000;
     var maxViewRight = 0;
     var maxViewBottom = 0;
     var alreadyLogged = false;
     var idElements = new Array();
     var pageStartMilliseconds = (new Date()).getTime();
 
 function getCurrentViewLeft()
     {
     var scroll_x,scroll_y;
     if (self.pageYOffset) // all except Explorer
     {
	scroll_x = self.pageXOffset;
	scroll_y = self.pageYOffset;
     }
     else if (document.documentElement && document.documentElement.scrollTop)
	// Explorer 6 Strict
     {
	scroll_x = document.documentElement.scrollLeft;
	scroll_y = document.documentElement.scrollTop;
     }
     else if (document.body) // all other Explorers
     {
	scroll_x = document.body.scrollLeft;
	scroll_y = document.body.scrollTop;
     }
     
     return scroll_x;
     }
 
 function getCurrentViewTop()
     {
     var scroll_x,scroll_y;
     if (self.pageYOffset) // all except Explorer
     {
	scroll_x = self.pageXOffset;
	scroll_y = self.pageYOffset;
     }
     else if (document.documentElement && document.documentElement.scrollTop)
	// Explorer 6 Strict
     {
	scroll_x = document.documentElement.scrollLeft;
	scroll_y = document.documentElement.scrollTop;
     }
     else if (document.body) // all other Explorers
     {
	scroll_x = document.body.scrollLeft;
	scroll_y = document.body.scrollTop;
     }
     return scroll_y;
     }
 
 function getCurrentViewRight()
     {
     //
     // Currently displayed coordinates
     //
     var x,y;
     if (self.innerHeight) // all except Explorer
     {
	x = self.innerWidth;
	y = self.innerHeight;
     }
     else if (document.documentElement && document.documentElement.clientHeight)
	// Explorer 6 Strict Mode
     {
	x = document.documentElement.clientWidth;
	y = document.documentElement.clientHeight;
     }
     else if (document.body) // other Explorers
     {
	x = document.body.clientWidth;
	y = document.body.clientHeight;
     }
     
     return x + getCurrentViewLeft();
     }

 
 function getCurrentViewBottom()
     {
     //
     // Currently displayed coordinates
     //
     var x,y;
     if (self.innerHeight) // all except Explorer
     {
	x = self.innerWidth;
	y = self.innerHeight;
     }
     else if (document.documentElement && document.documentElement.clientHeight)
	// Explorer 6 Strict Mode
     {
	x = document.documentElement.clientWidth;
	y = document.documentElement.clientHeight;
     }
     else if (document.body) // other Explorers
     {
	x = document.body.clientWidth;
	y = document.body.clientHeight;
     }
     
     return y + getCurrentViewTop();
     }

 function getPageWidth()
     {
     //
     // Find the page size
     //
     
     var x;
     var test1 = document.body.scrollHeight;
     var test2 = document.body.offsetHeight
     if (test1 > test2) // all but Explorer Mac
     {
	x = document.body.scrollWidth;
     }
     else // Explorer Mac;
     //would also work in Explorer 6 Strict, Mozilla and Safari
        {
	x = document.body.offsetWidth;
	}
     return x;
     }
 
 function getPageHeight()
     {
     //
     // Find the page size
     //
     
     var y;
     var test1 = document.body.scrollHeight;
     var test2 = document.body.offsetHeight
     if (test1 > test2) // all but Explorer Mac
     {
	y = document.body.scrollHeight;
     }
     else // Explorer Mac;
     //would also work in Explorer 6 Strict, Mozilla and Safari
        {
	y = document.body.offsetHeight;
	}
     return y;
     }
 
     

function phoneHome()
     {
     // Here's the biggest hack ever:
	 var image1 = new Image(); 
	 image1.src = "/add_viewlog.html?page_width=" + escape(getPageWidth())
	 		 + "&page_height="+ escape(getPageHeight()) + "&view_top="+ escape(minViewTop)
	 		 + "&view_left="+ escape(minViewLeft) + "&view_bottom="+ escape(maxViewBottom)
	 		 + "&view_right="+ escape(maxViewRight) + "&viewedElements=" + escape(getViewedElementsString())
	 		 + "&url=" + escape(getUrl()).replace(/[+]/g, "%2B") + "&timespent=" + escape(getTimeOnPage())
	 		 + "&referrer=" + escape(getReferrer()).replace(/[+]/g, "%2B") + "&connectiontype=" + escape(getConnectionType())
	 		 + "&language=" + escape(getUserLanguage()) + "&cookies=" + escape(getCookieEnabled())
	 		 + "&page_title=" + escape(document.title).replace(/[+]/g, "%2B");
	 alreadyLogged = true;
     }

function getCookieEnabled()
	{
	try {
		document.body.addBehavior("#default#clientCaps");
		return document.body.cookieEnabled;
	}
	catch(e)
		{
		return "unsupported";
		}
	}   

function getUserLanguage()
	{
	try {
		document.body.addBehavior("#default#clientCaps");
		return document.body.userLanguage;
	}
	catch(e)
		{
		return "unsupported";
		}
	}   

function getConnectionType()
	{
	try {
		document.body.addBehavior("#default#clientCaps");
		return document.body.connectionType;
	}
	catch(e)
		{
		return "unsupported";
		}
	}   

function getReferrer()
	{
		return document.referrer;
	}

function getUrl()
	{
		return location.href;
	}

function getTimeOnPage()
	{
		return (new Date()).getTime() - pageStartMilliseconds;
	}

function recordViewedArea()
     {
     if (getCurrentViewTop() < minViewTop)
         minViewTop = getCurrentViewTop();
     if (getCurrentViewLeft() < minViewLeft)
         minViewLeft = getCurrentViewLeft();
     if (getCurrentViewBottom() > maxViewBottom)
         maxViewBottom = getCurrentViewBottom();
     if (getCurrentViewRight() > maxViewRight)
         maxViewRight = getCurrentViewRight();
     }

function leavingPage()
    {
    if (alreadyLogged == false)
    	recordViewedElements();
        recordViewedArea();	// If we're on a mozilla-based browser, the onScroll events might not have been working, so take a final update first
        if (alreadyLogged == false)
            phoneHome();
    }

//                              ATTRIBUTES                              //

function has_attr(element, attribute) {
    // Mozilla
    if (element.hasAttribute) return element.hasAttribute(attribute);
    
    // IE. Notice we filter out non-string attributes.
    return (element.getAttribute(attribute) != "");
}


function findIdElements()
    {
    var allElements = document.getElementsByTagName("*");	// This work in at least IE 6 and Firefox
    for (i = 0; i < allElements.length; i++)
        {
        if (has_attr(allElements[i], "ID"))
            {
            allElements[i].viewed = false;
            idElements.unshift(allElements[i]);
            }
        }
    }

function recordViewedElements()
    {
    for (i = 0; i < idElements.length; i++)
        {
        var thisElement = idElements[i];
        if (thisElement.viewed == false &&
        	thisElement.offsetTop >= minViewTop &&
        	thisElement.offsetLeft >= minViewLeft &&
        	(thisElement.offsetLeft + thisElement.offsetWidth) <= maxViewRight &&
        	(thisElement.offsetTop + thisElement.offsetHeight) <= maxViewBottom)
            {
            thisElement.viewed = true;
            //alert("element with ID " + thisElement.getAttribute("ID") + " has been entirely viewed");
            }
        
        }
    }

function getViewedElementsString()
    {
    var retString = "";
    for (i = 0; i < idElements.length; i++)
        {
        if (idElements[i].viewed)
            {
            retString += idElements[i].getAttribute("ID") + ",";
            }
        }
    return retString;
    }


function loadContent(file)
  {
  var scriptTag = document.getElementById('loadScript');
  var head = document.getElementsByTagName('head').item(0)
  //if(scriptTag) head.removeChild(scriptTag);
    
  script = document.createElement('script');
  script.src = file;
  script.type = 'text/javascript';
  script.id = 'loadScript';
  
  head.appendChild(script)
  }	  


function showStats()
    {
    loadContent("js_viewlog.html?page_height=" + getPageHeight());
    }


function onLoadHandler()
    {
    findIdElements();
    recordViewedArea();
    recordViewedElements();
    }

function onScrollHandler()
    {
    recordViewedArea();
    recordViewedElements();
    }
    
window.onscroll = onScrollHandler;
window.onload = onLoadHandler;
window.onbeforeunload = leavingPage;
window.onunload = leavingPage;
