
function tag_url_and_redirect(e)
{
    if (!e)
    {
        var e=window.event;
    }

    //Get the event target element
    var targ;
    if (e.target)
    {
        targ=e.target;
    }
    else if (e.srcElement)
    {
        targ=e.srcElement;
    }
    if (targ.nodeType==3) // defeat Safari bug
    {
        targ = targ.parentNode;
    }

    //Get the HREF associcated with the event target element
    var url_clicked;
    if (targ.tagName == 'IMG')
    {
        url_clicked = targ.parentNode.href;
    }
    else
    {
        url_clicked = targ.href;
    }

    //Tag the HREF
    var tagged_url;
    tagged_url = tag_url(url_clicked);
    //alert("Tagged target URL: " + tagged_url);
    //return true;

    //Set the HREF associcated with the event target element with the newly
    //tagged HREF
    if (targ.tagName == 'IMG')
    {
        targ.parentNode.href = tagged_url;
    }
    else
    {
        targ.href = tagged_url;
    }

    //Since the HREF associcated with the event target element has already
    //been set to the newly tagged HREF above, we can just let the browser
    //go to that HREF. So, here we can return true.
    return true;

    //window.location(tagged_url);
    //return false;
}


function tag_url(url)
{
    //Check if the url has been tagged before. If yes, return it immediately.
    if (url.lastIndexOf('utm_nmg=tagged') != -1)
    {
        return url;
    }

    //Find out how the URL is structured, so we know how to append parameters
    var tagged_url;
    var host, divider, path;
    var e=/(http:\/)?\/?([^:\/\s]+)(\/)?(.*$)/;

    if (url.match(e))
    {
        host    = RegExp.$2;
        divider = RegExp.$3;
        path    = RegExp.$4;

        if (1 > path.length)
        {
            if (1 > divider.length)
            {
                tagged_url = url + "/?";
            }
            else
            {
                tagged_url = url + "?";
            }
        }
        else
        {
            if (path.lastIndexOf('?') != -1)
            {
                return url;
                //tagged_url = url + "&";
            }
            else
            {
                tagged_url = url + "?";
            }
        }
    }

    else
    {
        return url;
    }

    //When we reach here, we just append the parameters to tagged_url
    var z        = _uGC(document.cookie, '__utmz=', ';');
    var source   = _uGC(z,'utmcsr=', '|');
    var medium   = _uGC(z,'utmcmd=', '|');
    var campaign = _uGC(z,'utmccn=', '|');
    var context  = _uGC(z,'utmctr=', '|');
    tagged_url = tagged_url + "utm_source=" + source;
    tagged_url = tagged_url + "&utm_medium=" + medium;
    tagged_url = tagged_url + "&utm_term=" + context;
    tagged_url = tagged_url + "&utm_campaign=" + campaign;
    tagged_url = tagged_url + "&utm_nmg=tagged";

    return tagged_url;
}

/*
_uGC - needed for new version of Google Analytics (ga.js instead of urchin.js
Not sure what this does - it's from Google
*/
function _uGC(l,n,s) {
 if (!l || l=="" || !n || n=="" || !s || s=="") return "-";
 var i,i2,i3,c="-";
 i=l.indexOf(n);
 i3=n.indexOf("=")+1;
 if (i > -1) {
  i2=l.indexOf(s,i); if (i2 < 0) { i2=l.length; }
  c=l.substring((i+i3),i2);
 }
 return c;
}

