//(c) Richard Mag for Siddhaika.org, 2010
//to be used with downloadcounter.php

var fileFormats=new Array(".pdf", ".rtf", ".doc")

function anchorOnClick() {
  if (!document.getElementsByTagName){
    return;
  }

  var anchors = document.getElementsByTagName("a");
  for (var i=0; i<anchors.length; i++) {
    var anchor = anchors[i];
    var hrefStr = anchor.href
//    hrefStr=hrefStr.slice(10) //for test purposes, to remove "file:///d:"
    for(var x=0; x<fileFormats.length; x++){
      if(hrefStr.length > fileFormats[x].length && hrefStr.lastIndexOf(fileFormats[x]) == hrefStr.length - fileFormats[x].length){
        var httpStr = "http://";
        var documentStr = "www.siddhaika.org" + hrefStr;

        if(hrefStr.substr(0, httpStr.length) == httpStr){  //if href starts with "http://" then remove "http://" from hrefStr
          documentStr = hrefStr.slice(httpStr.length)
        }

        anchor.onclickHrefStr="window.open('/downloadcounter.php?url=" + documentStr + "')"

        anchor.onclick = function(e){
          var targ;
          if (!e) e = window.event;
          if (e.target){ //FF, Opera
            targ = e.target;
          }
          else if (e.srcElement){ //IE
            targ = e.srcElement;
          }
          if (targ.nodeType == 3){ //Safari
            targ = targ.parentNode;
          }

          eval(targ.onclickHrefStr);
          return false;
        };

        //update onClick for all IMG children
        for (var childIndex=0; childIndex < anchor.childNodes.length;
childIndex++) {
          if (anchor.childNodes[childIndex].tagName == "IMG") {
            //add onclick of its parent's onclickHrefStr
            anchor.childNodes[childIndex].onclick = function(e){
              var targ;
              if (!e) e = window.event;
              if (e.target){ //FF, Opera
                targ = e.target;
              }
              else if (e.srcElement){ //IE
                targ = e.srcElement;
              }
              if (targ.nodeType == 3){ //Safari
                targ = targ.parentNode;
              }
              eval(targ.parentNode.onclickHrefStr);

              return false;
            };
          }
        }
      }
    }
  }
}

//window.onload = anchorOnClick; //via onloads.js