//Code for identifying WAM links and tagging so they show up in Google Analytics

function sendHit() {
   
   var theURL = window.location.pathname;
   theURL += '/downloads/';
   theURL += this.innerHTML;
   theURL.toString(); 
   if (theURL.indexOf("aspx")>0) {      
      theURL = theURL.replace(/\.aspx/,"");
   }   
   try {
      pageTracker._trackPageview(theURL); 
   }
   catch(err) {}
       //comment out last line once ready
       //return false;
}

function sendExtHit() {
   var theURL = window.location.pathname;
	 theURL += '/outboundlink/';
	 theURL += this.innerHTML;
	 theURL.toString();
	    if (theURL.indexOf("aspx")>0) {      
      theURL = theURL.replace(/\.aspx/,"");
   }   
	 try {
      pageTracker._trackPageview(theURL); 
   }
   catch(err) {}
}

function findLinks() { 
   findExtLinks();
   var link = document.links;
   var regex = /idoc\.ashx\?docId/i;   
	 //Test regular expression for on NAOcopy
	 //var regex2 = /^http:\/\/cmscore\/naocopy/i;
	 var regex2 = /^http:\/\/www\.nao\.org\.uk/i;
   for(var i=0;i<link.length;i++){
      if (link[i].href.match(regex) && link[i].href.match(regex2)) {
      link[i].onclick = sendHit;
      }
   }
}

function findExtLinks() {
    var link = document.links;
		//Test regular expression for on NAOdev
	  //var regex = /^http:\/\/cmscore2\/naodev/i;
		var regex = /^http:\/\/www\.nao\.org\.uk/i;
		var regex2 = /^http:/i;
	  for(var i=0;i<link.length;i++){
      if (!(link[i].href.match(regex)) && link[i].parentNode.id!='share' && link[i].href.match(regex2)) {
      link[i].onclick = sendExtHit;
      }
   }
}

function getElementByClass(theClass) {
				 var allHTMLTags = new Array();
				 try{
    				 //Create Array of All HTML div Tags
    				 var allHTMLTags=document.getElementsByTagName('div');
    
    				 //Loop through all tags using a for loop
    				 for (i=0; i<allHTMLTags.length; i++) {
    
    				 		 //Get all tags with the specified class name.
    						 if (allHTMLTags[i].className==theClass) {
    
    						 //Grab content
    						 thisSector = allHTMLTags[i].innerHTML;
    						 thisSector = thisSector.toLowerCase();
    						 //strip tags
    						 var start = thisSector.indexOf('\<p\>') + 3;
    						 var end = thisSector.indexOf('\<\/p\>');
    						 //This is the bit we want:
    						 thisSector = thisSector.substring(start,end);
								 return thisSector;
    						 }
    				}
				}
				catch(err){}
}
