window.onload = function() {
  var elm = document.body;
  var f2afLink = new RegExp('VR_F2AF_LINK');
  var hostedLink = new RegExp('VR_HOSTED_LINK');
  var surveyLink = new RegExp('VR_SURVEY_LINK');
  var unsubLink = new RegExp('VR_UNSUB_LINK');
  var validURL = new RegExp('^(http(s)?://)|(ftp://)|(mailto:)');
  var relativeURL = new RegExp('^http(s)?://' + document.location.hostname);
  var poweredByVR = new RegExp('\\/landing\\/\\?mm/');
  var URLHash = new RegExp('(#[\\w\\-]+)$');
  
	
  $(elm).select('a').each(function(link){
    if(f2afLink.test(link.href) == true){
      link.target = '';
      link.href = 'javascript:alert("Forward to a friend links will be activated after your email is sent.");';
    }
    else if(hostedLink.test(link.href) == true){
      link.target = '';
      link.href = 'javascript:alert("Hosted version links will be activated after your email is sent.");';
    }
    else if(surveyLink.test(link.href) == true){
      link.target = '';
      link.href = 'javascript:alert("Survey links will be activated after your email is sent.");';
    }
    else if(unsubLink.test(link.href) == true){
      link.target = '';
      link.href = 'javascript:alert("Unsubscribe links will be activated after your email is sent.");';
    }
    else if(poweredByVR.test(link.href) == true){
      link.target = '_blank';
    }
    else if(validURL.test(link.href) == true && relativeURL.test(link.href) == false){
      link.target = '_blank';
    }
    else{
      var hash = URLHash.exec(link.href);
      if(hash == null){
        link.target = '';
        link.href = 'javascript:alert("Relative URLs will not work in emails, please make sure all links start with http:// or https://");';
      }
      else{
        hash = hash[0].replace('#', '');
        link.href = 'javascript:window.scroll(0,document.getElementsByName("'+ hash + '")[0].offsetTop);';
      }
    }
  });
};


