function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            if (oldonload) {
                oldonload();
            }
            func();
        }
    }
}

addLoadEvent(function() {
        var len = document.links.length;
        var oldlinks = new Array(len);
        for (i = 0; i < len; i++){
                // Following line should be changed to match path of php file:
                var preface = "http://www.mediathatmattersfest.org/lib/clicktracking.php?";
                var url = escape(document.links[i].href);
                var url_text = document.links[i].innerHTML;

              // Check URL text for tags, and remove them:
               if (url_text.match("<em>")){
                     url_text = url_text.replace("<em>", "");
                     url_text = url_text.replace("</em>", "");
               }
               if (url_text.match("<EM>")){
                     url_text = url_text.replace("<EM>", "");
                     url_text = url_text.replace("</EM>", "");
               }
               if (url_text.match("<strong>")){
                     url_text = url_text.replace("<strong>", "");
                     url_text = url_text.replace("</strong>", "");
               }
               if (url_text.match("<STRONG>")){
                     url_text = url_text.replace("<STRONG>", "");
                     url_text = url_text.replace("</STRONG>", "");
               }
             // Checking if link is a Take Action Link: 
                var take_action = "no";
                
               var ultags=document.getElementsByTagName("UL").length;
               for (j = 0; j < ultags; j++){
                    if (document.getElementsByTagName("UL")[j]){
                        if (document.getElementsByTagName("UL")[j].className=="actionlinks"){
				innerHTMLs=document.getElementsByTagName("UL")[j].innerHTML;
				if (innerHTMLs.match(url_text) ){
					take_action="yes";
				}
		    	}
                    }
                }

                // Check if innterHTML is an image 
                if ( url_text.match("<IMG") ) {
                      take_action="no";
                      preface = preface + "&takeaction=" + take_action + "&site=" + url + "&url=" + url;
                 }
                else if (url_text.match("<img") ) {
                      take_action="no";
                      preface = preface + "&takeaction=" + take_action + "&site=" + url + "&url=" + url;
                }
                else if (url_text == "") {
                      take_action="no";
                      preface = preface + "&takeaction=" + take_action + "&site=" + url + "&url=" + url;
                }
                else if (url_text.substr(0,3) == "<img"){
                      take_action="no";
                      preface = preface + "&takeaction=" + take_action + "&site=" + url + "&url=" + url;
                }
                else {
                      preface = preface + "&takeaction=" + take_action + "&site=" + escape(url_text) + "&url=" +  url;
                }
                
                // Check if URL: starts with http://, doesn't include mtmf.org, doesn't include mailto or php script
                if (document.links[i].href.match("http://")){
                   if (!document.links[i].href.match("mediathatmattersfest.org")){
                        if (!document.links[i].href.match("mailto:")){
                              if (!document.links[i].href.match("clicktracking.php")){
                                     document.links[i].href = preface;
                              }
                        }
                   }
               }
        }
})
