//Event tag handler for form events

//Declare constants
var SCROLLED = false;

//Handle pair call for special parameter to the NetInsight event tag call
function handlePair(key, value, command) {
	if (command == null) {
		//Do nothing
	} else {
		if (command == 'A')
			ntptAddPair(key, value);
		if (command == 'D')
			ntptDropPair(key, value);
	}
	return true;
}
 
//Handle form event for NetInsight
function ntptEvent(eventText, key, value, command){
	handlePair(key,value,command);
	return ntptEventTag(eventText);    
}

//Handle Link event for NetInsight
function ntptLink(linkObj,querymod,time){
    return ntptLinkTag(linkObj,querymod,time);    
}

//Handle Submit event for NetInsight
function ntptSubmit(formObj,querymod, time, key, value, command){ 
    handlePair(key,value,command);  
	return ntptSubmitTag(formObj,querymod, time);   
}

//Handle scroll event for NetInsight
function ntptScrollEvent(eventText, key, value, command){
    if (SCROLLED) {
        //do nothing.  Suppress event
    } else {
        ntptEvent(eventText, key, value, command);
        SCROLLED = true;
    }
    return true;	
}

function ntptFireWt(nSec) { var date = new Date();var curDate;do{curDate = new Date();}while((curDate.getTime() - date.getTime()) < (nSec*1000)); }


//AUTOMATIC LINK TRACKING CODE STARTS HERE

var DOWNLOAD_TYPES = "pdf,doc,xls,zip";  //Specify all the download extensions here
var pageURL = document.location.href;
var protLess = pageURL.substring(pageURL.indexOf("//")+2);
var pageDomain = protLess.substring(0, protLess.indexOf("/"));
var ONSITE_DOMAINS = pageDomain + ",www." + pageDomain;  //List all onsite domains.


function isDownload(linkURL) {
   var linkArray = linkURL.split("/");
   var linkURI = linkArray[linkArray.length-1];
   var linkFile = "";
   var linkExt = "";
   var fileChunks = [];

   if (linkURI.indexOf("?") > 0) {
      linkFile = linkURI.split("?")[0];
   }
   else if (linkURI.indexOf("?") == 0) { return false; }
   else {linkFile = linkURI}

   if (linkFile.indexOf(".") > 0) {
      fileChunks = linkFile.split(".");
      linkExt = fileChunks[fileChunks.length-1];
      var dTypes = DOWNLOAD_TYPES.split(",");
      for(var h=0; h<dTypes.length; h++) {
        if (linkExt.toLowerCase() == dTypes[h]) { 
             return true;
        }
     }
        return false;
   }
   else { return false; }
}



function getLinkTargetType(linkURL) {
   var protLess = linkURL.substring(linkURL.indexOf("//")+2);
   var linkDomain = protLess.substring(0, protLess.indexOf("/"));

   if (linkURL.indexOf("javascript:") == 0) { return "Unknown"; }
   if(ONSITE_DOMAINS.indexOf(",") < 0) {  
      if(linkDomain == ONSITE_DOMAINS) { return "Internal"; }  
      else { return "External"; }
   }
   else { 
      var doms = ONSITE_DOMAINS.split(",");
      for (var i=0; i<doms.length; i++) {
        if (linkDomain == doms[i]) { return "Internal"; }
      }
      return "External";
   }
}


function customLinkTracker() {
   var linkObj = this;
   var linkURL = linkObj.href;
   var linkTitle = linkObj.innerHTML.replace(/^[\s\t]+/,"").replace(/\n/, "").replace(/\s{2}/g," ").replace(/amp;/g,"");
   var linkType = "";
   var linkTargetType = getLinkTargetType(linkURL);
   var linkTarget =  linkObj.target;
   var linkParams = "ev=siteLinks&linkurl=" + encodeURIComponent(linkURL);  

   if (linkTitle.indexOf("<img") == 0 || linkTitle.indexOf("<IMG") == 0) {
      linkTitle = linkObj.getElementsByTagName('img')[0].getAttribute('alt');
   } 

   if (isDownload(linkURL)) { linkType = "Download"; }
   
   else if (linkObj.id.indexOf("nav") == 0) { linkType = "Top Nav"; }
   else if ((linkObj.parentNode.parentNode.parentNode.className != null) && (linkObj.parentNode.parentNode.parentNode.className == "OverlayContainer") ) { linkType = "Common Questions"; } 
   else if ((linkObj.parentNode.parentNode.parentNode.className != null) && (linkObj.parentNode.parentNode.parentNode.className.indexOf("Menu") == 0)) { linkType = "Sub Nav"; }
   else if ((linkObj.parentNode.parentNode.parentNode.className != null) && (linkObj.parentNode.parentNode.parentNode.className == "T2Nav")) { linkType = "Left Nav"; }
   else if ((linkObj.parentNode.parentNode.parentNode.parentNode != null) && (linkObj.parentNode.parentNode.parentNode.parentNode.className == "ContentFeature") && (linkObj.parentNode.parentNode.parentNode.parentNode.getElementsByTagName('h2')[0].innerHTML == "COMMON QUESTIONS")) { linkType = "Common Questions"; } 
   else if ((linkObj.parentNode.parentNode.parentNode.className != null) && (linkObj.parentNode.parentNode.parentNode.parentNode.className == "Footer")) { linkType = "Footer"; }
   else if ((linkObj.parentNode.parentNode.parentNode.parentNode.className != null) && (linkObj.parentNode.parentNode.parentNode.parentNode.className == "OverlayContainer") ) { linkType = "Common Questions"; } 
   else if ((linkObj.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.className != null) && (linkObj.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.className == "OverlayContainer") ) { linkType = "Common Questions";  } 
   else if ((linkObj.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode!= null)&&(linkObj.parentNode.className == "GlobalNav"||linkObj.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.className == "Header" )) { linkType = "Header"; }
   else if ((linkObj.parentNode.parentNode.parentNode.className != null) && (linkObj.parentNode.parentNode.parentNode.className == "TabBox")) { linkType = "Sub Tab"; }  
   else if ((linkObj.getAttribute('onclick') !== null) && (linkObj.getAttributeNode('onclick').nodeValue.indexOf("switchtabs(") == 0)) { linkType = "Sub Tab"; }
   else if (linkURL.indexOf("javascript:") == 0) { linkType = "JavaScript"; }
   else { linkType = "Content"; }

   if (linkTargetType == "External") { if(linkObj.target) { linkTarget = linkObj.target; } }
   else if (linkTargetType == "Unknown" && linkType == "Sub Tab") { linkTargetType = "Internal"; }
   if (linkTarget == "") { linkTarget = "_self"; }
   
   ntptEventTag(linkParams + "&link=" + encodeURIComponent(linkTitle.replace(/<[^>]*>/g,"")) + "&linktype=" + encodeURIComponent(linkType) + "&linktargettype=" +encodeURIComponent(linkTargetType) + "&target=" + encodeURIComponent(linkTarget));
   //alert(linkParams + "&link=" + encodeURIComponent(linkTitle.replace(/<[^>]*>/g,"")) + "&linktype=" + encodeURIComponent(linkType) + "&linktargettype=" +encodeURIComponent(linkTargetType) + "&target=" + encodeURIComponent(linkTarget));
}




var links = document.links; 
for(var i=0; i<links.length; i++) {
   var linkObj = links[i];
   var linkURL = linkObj.href;
   if ((linkURL != location.href) && (linkURL.indexOf(location.href + "#") != 0)) {
      linkObj.onmousedown = customLinkTracker;
   }
}
