/*------------------------------------------------------------------------------
   Parklawn Elementary Website
   Created By:   Mike McKenna
   Description:: This adds correct class for anchor tags (<a>) to properly display link icons  
  ------------------------------------------------------------------------------*/
                              
function addClass() {
  var classAttribute = "";
  var thisHost  = window.location.hostname;            // get current page's domain (for external link checks)
  var anchors   = document.getElementsByTagName("a");  // get all anchor tags on page
  var emailChk  = new RegExp("^mailto", "i");          // email RegEx pattern
  var pdfChk    = new RegExp(".pdf", "i");             // pdf RegEx pattern

  thisHost = thisHost.replace("www.","").toLowerCase();  
 
  for (var i=0; i < anchors.length; i++) {
    var href = anchors[i].getAttribute("href").toLowerCase();     	  	  	
    if (emailChk.test(href)) {       
      classAttribute = "email";	
    } else if (pdfChk.test(href)) { 
      classAttribute = "pdf";	  		
    } else {
      var externalChk = (href.indexOf("http://") !== -1 && href.indexOf(thisHost) === -1) ? true : false;
      if (externalChk) {      
        classAttribute = "external";	
      }  
    }
   
    if (classAttribute !== "") {     
    //anchors[i].setAttribute("class", classAttribute);     // doesn't work in IE
    //anchors[i].setAttribute("className", classAttribute); // only works in IE
      anchors[i].className = classAttribute;                // cross-browser compliant
      if (classAttribute === "external") {
      	anchors[i].setAttribute("target", "_blank");
      }	
    } 
    classAttribute = "";  // reset 	
  } 
}