/******************************************************************************
save this file as astrack.js and place 
<script src="/astrack.js" type="text/javascript"></script>
at the end of your page code (after the last AdSense ad unit)

You must already have the UrchinTracker installed to use this.
 
Modified with permission from Jim Rotherford's Adsense Pepper
(http://www.digitalmediaminute.com/) 

© SeoBook.com. For updates see http://www.seobook.com/archives/001370.shtml 
You are allowed to use this but you should keep this copyright notice here

******************************************************************************/

function asConvert()
{
    // ASSUMING: LP_PATH IS POPULATED
    // get only keyword portion of URL. NOTE THIS ASSUMES THE KEYWORD IS THE LAST PORTION OF THE URL.
    var cur_kw1=document.location.toString().split(LP_PATH)[1];
    // trim off trailing / if any.
    var cur_kw2 = cur_kw1.split('/')[0];
    // trim off trailing ? and query string if any.
    var cur_kw = cur_kw2.split('?')[0];

    var targetDoc = "/as_convert.php";
    var targetParm = "kw="+cur_kw;
    
    var xmlHttpReq;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        xmlHttpReq = new XMLHttpRequest();
    }
    //IE
    else if (window.ActiveXObject) {
        xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlHttpReq.open('POST', targetDoc, true);
    xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xmlHttpReq.send(targetParm);
}

function as_click () {
    asConvert();
    urchinTracker ('/asclick');
}

function adsense_init () {

    if (document.all) {  //ie

        var el = document.getElementsByTagName("iframe");
    
        for(var i = 0; i < el.length; i++) {
            if(el[i].src.indexOf('googlesyndication.com') > -1) {

                el[i].onfocus =  as_click;
            }
        }
    
    } else {   // firefox
    
        window.addEventListener('beforeunload', doPageExit, false);
        window.addEventListener('mousemove', getMouse, true);
    
    }
        
}

//for firefox
var px;
var py;

function getMouse(e) {
    px=e.pageX;
    py=e.clientY;
}

function findY(obj) {
    var y = 0;
    while (obj) {
        y += obj.offsetTop;
        obj = obj.offsetParent;
    }
    return(y);
}

function findX(obj) {
    var x = 0;
    while (obj) {
        x += obj.offsetLeft;
        obj = obj.offsetParent;
    }
    return(x);
}

function doPageExit(e) {

    ad = document.getElementsByTagName("iframe");
    for (i=0; i<ad.length; i++) {
        var adLeft = findX(ad[i]);
        var adTop = findY(ad[i]);
        var inFrameX = (px > (adLeft - 10) && px < (parseInt(adLeft) + parseInt(ad[i].width) + 15));
        var inFrameY = (py > (adTop - 10) && py < (parseInt(adTop) + parseInt(ad[i].height) + 10));
        
        if (inFrameY && inFrameX) {
            asConvert();
            urchinTracker('/asclick');
        
        }
    }

}

//end for firefox

