var xmlhttp;
var body;

function getXMLHTTP()
{
    var xmlhttp;
    try {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    catch(ex) { // either this is not IE, or it is a version of IE which does not support XMLHTTP
        try {
            xmlhttp = new XMLHttpRequest();
        }
        catch(ex) { // we can't use XMLHTTP because this browser is not compatible.
            xmlhttp = false;
        }
    }
    return xmlhttp;
}

function CountClick()
{
    if(body && xmlhttp) {
        var h1 = body.getElementsByTagName('h1');
        if(h1.length > 0) {
            xmlhttp.open('get', 'link/?org='+h1[0].id.substr(1), false);
            xmlhttp.send(null);
        }
    }
    return true; // continue processing
}

function hasParent(el, parentid)
{
    if(el) {
        if(el.id == parentid)
            return true;
        return hasParent(el.parentNode, parentid);
    }
    return false;
}

window.onload = function load() {
    if(xmlhttp = getXMLHTTP()) {
        body = document.getElementById('mainbody');
        if(body) {
            var anchors = body.getElementsByTagName('a');
            for(var i = 0; i < anchors.length; i++) {
                if( (anchors[i].className == 'link') || 
                    ( ((anchors[i].href.substr(0, 5) == 'http:') || (anchors[i].href.substr(0, 6) == 'https:')) 
                      && hasParent(anchors[i], 'description') ) ) {
                    anchors[i].onclick = CountClick;
                }
            }
        }
    }
    else {
//        alert('Not compatible');
    }
}
