function processAnchors() {
    var anchors = window.document.links; //getElementsByTagName("a");

    if(!anchors)
        return true;

    for(var index = 0; index < anchors.length; index++) { // loop through all anchor elements
        var anchor = anchors[index];

        if(!anchor.title && !anchor.onmouseover)        
            if(anchor.hostname == window.document.location.host) {
                if(/\.[a-z]+$/i.test(anchor.href))
                    anchor.title = "Download: " + anchor.innerText; 
                else {                                                            
                    //anchor.onresponse = gotPageDescription;
                    //anchor.onmouseover = fetchPageDescription;
                }                    
            }
               
        if(/\.(?:doc|xls|ppt|pdf|txt|rtf)$/.test(anchor.href)) { // download
            if(anchor.onclick instanceof Function)
                continue; // custom onclick script assigned

            anchor.onclick = openNewWindow;
            anchor.className = anchor.href.substr(anchor.href.length - 3, 3);
        }
        else       
            if(anchor.hostname == this.document.location.host) {
                if(anchor.onclick instanceof Function)
                    continue; // custom onclick script assigned
                
                anchor.onclick = openEditMode // internal link
            }                
            else {
                if(!anchor.className)                    
                    anchor.className = /^mailto:/.test(anchor.href) ? "msg" : "external";

                if(!(anchor.onclick instanceof Function))
                    anchor.onclick = openNewWindow; // external link

                if(anchor.href.match(/^mailto:(.+)$/)) {
                    anchor.title = "Send an e-mail to " + anchor.innerText + " (" + RegExp.lastParen + ")";
                    continue;
                }
                
                if(anchor.onmouseover instanceof Function)
                    continue;
                
                //anchor.onresponse = gotPageTitle;
                //anchor.onmouseover = fetchPageTitle;                                
            }    
    }
};

function gotPageTitle(request) {
    this.onmouseover = null;
    
    if(request.status != 200) // could not fetch XML
        return;
        
    var contentType = request.getResponseHeader("Content-Type");
    var contentLength = request.getResponseHeader("Content-Length");
    
    var titlePattern = /<title>(.+)<\/title>/;
    var matches = request.responseText.match(titlePattern);
    
    this.title = matches instanceof Array
        ? matches[1]
        : this.innerText;
};

function fetchPageTitle() {     
    var succeded = sendAJAXRequest(this, "GET", this.href, "");

    if(!succeded)
	this.onmouseover = null;
};

function gotPageDescription(request) {
    this.onmouseover = null;
        
    if(request.status != 200) // could not fetch XML
        return;
       
    var contentType = request.getResponseHeader("Content-Type");
    var contentLength = request.getResponseHeader("Content-Length");
                    
    this.title = /text\/plain/.test(contentType)
        ? request.responseText || this.innerText
        : this.innerText;
};

function fetchPageDescription() {
    var succeded = sendAJAXRequest(this, "GET", this.href + "?mode=Describe", "");
    
    if(!succeded)
	this.onmouseover = null;	
};

function openEditMode() {    
    if(!window.event || !window.event.ctrlKey)
        return true

    window.document.location.href = /\?/.test(this.href)
        ? this.href + "&mode=Edit"
        : this.href + "?mode=Edit";

    return false;
};

function openNewWindow() {
    window.open(this.href);

    return false;
};

function sendAJAXRequest(client, method, location, body) {
    try {
        var request = typeof ActiveXObject == "function"
            ? new ActiveXObject("Microsoft.XMLHTTP")
            : new XMLHttpRequest();
    
        request.onreadystatechange = function() {
            switch(request.readyState) {
                case 4 : client.onresponse(request); break;
            }
        };
    
        request.open(method, location, true);
        request.send(body);

        return request;
    }
    catch(error) {
        return null;
    }
};
