﻿function checkLinks ()
{
	ReplaceLinks();
}

function ReplaceLinks ()
{

    var links = document.getElementsByTagName("A");
    
    var LinkExclusionsList;
    var AnyLinkExclusions = false;
    var ExcludeLink = false;
    var TargetOption;   
    
    try
    {
        TargetOption = TargetOptionID;
    }
    catch (err)
    {
        TargetOption = 0;
    }
       
    if (LinkExclusions != "")
    {
        AnyLinkExclusions = true;
        
        // split LinkExclusions into an array by ,
        LinkExclusionsList = LinkExclusions.split(",");
        
        // if a link doesn't have a query string on it, then we need to make sure a trailing / is on the end
        for (i = 0; i < LinkExclusionsList.length; i++)
        {
            if (LinkExclusionsList[i].indexOf("?") == -1)
            {
                if (LinkExclusionsList[i].substr(LinkExclusionsList[i].length-1,1) != "/")
                {
                    LinkExclusionsList[i] += "/";
                }
            }
        }   
    }
	
	for (i = 0; i < links.length; i++)
	{
		// only change fully qualified links (otherwise links won't work)
		if(links[i].href.indexOf('http://') != -1 || links[i].href.indexOf('https://') != -1)
		{
		    // don't change javascript links
		    if (links[i].href.indexOf('javascript') != -1)
		    {
		        // do nothing
		    }
		    else
		    {
		        ExcludeLink = false;
    		    
		        // any exclusions?
		        if (AnyLinkExclusions)
		        {
		            // is this link in the link exclusions list? if it is, do not replace it
		            for (j = 0; j < LinkExclusionsList.length; j++)
		            {
		                // if the base url of this link is in the exclusions list, do not replace it
		                if (links[i].toString().indexOf(LinkExclusionsList[j].toString()) != -1)
		                {
		                    ExcludeLink = true;
		                    break;
		                }
		            }
		        }
    		    
		        if (!ExcludeLink)
		        {
		            // make sure this link isn't one of our blocked links
		            if (links[i].href.indexOf("linkmoney.com") != -1)
		            {
		                // skip it
		            }
		            else if (links[i].href.indexOf("linkmoney.com/link") != -1)
		            {
		                // skip it
		            }
		            else
		            {
		                
		                XBrowserAddHandlerClick(links[i], 'http://www.linkmoney.com/Link.aspx?id=' + uid + '&orig_url=' + encodeURIComponent(links[i].href));

		                // TargetOptionID
		                // 0 = do nothing
		                // 1 = open in same window
		                // 2 = open in new window
		                //if (TargetOption == 1) { links[i].target = '_top'; }
		                //else if (TargetOption == 2) { links[i].target = '_blank'; }
        		        
		                if (TargetOption == 2) { links[i].target = '_blank'; }
		                else { links[i].target = '_top'; }
		            }
		        }
		    }
        }
	}
}

function XBrowserAddHandlerClick(target, link)
{

  if ( target.addEventListener ) {
    target.addEventListener("mousedown", function(e){target.href=link;}, false);
  } else if ( target.attachEvent ) {
    target.attachEvent("onmousedown", function(e){target.href=link;});
  } else {
    var originalHandler = target["onmousedown"];
    if ( originalHandler ) {
      target["onmousedown"] = function(e){originalHandler(e);target.href=link;};
    } else {
      target["onmousedown"] = function(e){target.href=link;};
    }
  }
}

if(window.onload == null) 
   window.onload = checkLinks; 
else 
   var timeout = window.setTimeout("checkLinks (); clearTimeout(timeout )", 5000);

  
