/*
    guid: 496B2C76648C4FB6B90B5D0EF0120126
*/
/*use to attach an event to an element*/
function addEvent(obj, type, fn) {
  /* different functionality depending on user agent */
    if (obj.attachEvent) { // uses IE - bit more complicated for IE I'll spare the details...
        obj['e' + type + fn] = fn;
        obj[type + fn] = function() { obj['e'+type+fn](window.event); }
        obj.attachEvent('on' + type, obj[type + fn]); // do the actuall attach
    } else { // uses mozilla
        obj.addEventListener(type, fn, false);
    }
}
/*generally not called, but is usefull and should always be kept with the addEvent function*/
function removeEvent(obj, type, fn) {
  /* different functionality depending on user agent */
    if (obj.detachEvent) { // uses IE
        obj.detachEvent('on' + type, obj[type + fn]);
        obj[type + fn] = null; //kill the object being used for the event (release the resource)
    } else { // uses mozilla 
        obj.removeEventListener(type, fn, false);
    }
}
function clearTextBox(e){
  var srcElement;
 
  if(!e){e=window.event;}
  
  if(e.target){
    srcElement = e.target;
  }else{
    srcElement = e.srcElement;
  }
  if(srcElement){
    if((srcElement.origValue=='')||(srcElement.origValue==undefined)){
      srcElement.origValue = srcElement.value;
    }
    if((srcElement.origValue==srcElement.value)||(srcElement.origValue==undefined)){
      srcElement.value='';
    }
    addEvent(srcElement, "blur", restoreTextBox);
  }
  
}
function restoreTextBox(e){
  var srcElement;
  
  if(!e){e=window.event;}
  
  if(e.target){
    srcElement = e.target;
  }else{
    srcElement = e.srcElement;
  }
  if(srcElement.value==''){
    srcElement.value=srcElement.origValue;
  }
}
function addNonWindowEvents(){//must be qued with the window onload event for cross browser support.
  addEvent(document.getElementById('txtSearchQuery'), "focus", clearTextBox);
}
function insertAfter(target, bullet) {   
    target.nextSibling ?   
        target.parentNode.insertBefore(bullet, target.nextSibling)   
        : target.parentNode.appendChild(bullet);   
}   
function addPrintLinkURLs(){
  
  CSSNode = document.getElementById('printURLsCSS');
  CSSNode.parentNode.removeChild(CSSNode);
  
  for(i=0;i<document.getElementsByTagName('A').length;i++){
    var anchor = document.getElementsByTagName('A')[i];
      var newSpan = document.createElement('SPAN');
      var thisURL = anchor.getAttribute('href');
      //ensure a full url is used
      if((thisURL.substring(0, 5)=='http:')||(thisURL.substring(0, 5)=='file:')||(thisURL.substring(0, 7)=='mailto:')||(thisURL.substring(0, 4)=='ftp:')){
        //these are full URLs - do nothing
      }else{
        thisURLStart = window.location+'';
        thisURLStart = thisURLStart.substring(0, thisURLStart.lastIndexOf('/'));
        if(thisURL.substring(0,1)=='/'){
          thisURL = thisURLStart + thisURL;
        }else{
          thisURL = thisURLStart + '/' + thisURL;
        }
      }
      newSpan.appendChild(document.createTextNode(' [url: ' + thisURL + ']'));
      newSpan.className = 'printLink';
      insertAfter(anchor, newSpan);
      
  }
  
  
}
function addNewWindowLinks(){
  var thisHostname = location.hostname;
  for(i=0;i<document.getElementsByTagName('A').length;i++){
    var anchor = document.getElementsByTagName('A')[i];
    var linkHostname = anchor.hostname.substr(0, anchor.hostname.indexOf(':'));
    if(linkHostname==''){
      linkHostname=anchor.hostname;
    }
    //list conditions here...
    if(
      (anchor.getAttribute('rel')=='external')
      ||
      (anchor.getAttribute('rel')=='document')
      ||
      (
        (linkHostname!='')
        &&
        (linkHostname!=thisHostname)
        &&
        (anchor.protocol!='mailto')
      )
    ){
      //do the business
      anchor.target='_blank';
      if(anchor.title==''){
        anchor.title += ' (opens in a new window)';
      }else{
        anchor.title = 'opens in a new window';
      }
      if(anchor.getElementsByTagName('IMG').length<=0){
        var img = document.createElement('img');
        img.setAttribute('src', '/Style/Images/newWindowIcon.png');
        img.setAttribute('alt', '(opens in a new window)');
        anchor.appendChild(img);
      }
      
    }
  }
}
addEvent(window, "load", addNonWindowEvents);
//addEvent(window, "load", addPrintLinkURLs);
addEvent(window, "load", addNewWindowLinks);