﻿// to use: add to <a href='' className = "newWin w500 h300 scroll resize toolbar location">blah</a>

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

function openNewWindow(e) {
  var features = '';
  var start;
  var end;
  var width;
  var height;
  var thisurl = this.href;
  width = parseInt(getDimensionFrom(this, 'w'))>0 ? getDimensionFrom(this, 'w') : '';
  height = parseInt(getDimensionFrom(this, 'h'))>0 ? getDimensionFrom(this, 'h') : '';
  if (height.length>0 || width.length>0) {
    features += height.length>0 ? 'height='+height+',' : '';
    features += width.length>0 ? 'width='+width+',' : '';
    features += (thisurl.indexOf("help.aspx") > 0) ? 'scrollbars=yes,' : '';
  }
  features += getFeatures(this);
   
  if (!e) var e = window.event;
  if (features.length>0) {
    if (features.substr(features.length-1,1) == ",")
      features = features.substr(0, features.length-1);
    window.open(this.href, '_new', features);
  } else {
    window.open(this.href);
  }
  return false;
}

function getDimensionFrom(obj, attrib) {
  if (obj.className.indexOf(" "+attrib) != -1) {
    start = obj.className.indexOf(" "+attrib) + 1
    end = obj.className.indexOf(" ", start);
    end = (end == -1) ? obj.className.length - start : end - start;
    return obj.className.substr(start+1, end-1);
  } else {
    return "";
  }
}

function getFeatures(obj) {
  var features = "";
  if (obj.className.indexOf(" scrollNo") != -1) { features += "scrollbars=no,"; } else { features += "scrollbars=yes,"; }
  if (obj.className.indexOf(" locationNo") != -1) { features += "location=no,"; } else { features += "location=yes,"; }
  if (obj.className.indexOf(" historyNo") != -1) { features += "history=no,"; } else { features += "history=yes,"; }
  if (obj.className.indexOf(" toolbarNo") != -1) { features += "toolbar=no,"; } else { features += "toolbar=yes,"; }
  if (obj.className.indexOf(" menuNo") != -1) { features += "menubar=no,"; } else { features += "menubar=yes,"; }
  if (obj.className.indexOf(" resizeNo") != -1) { features += "resizable=no"; } else { features += "resizable=yes"; }
  return features;
}


function GetAnchors() {
  var elements = new Array('a', 'area');
  for (var j=0; j < elements.length; j++) {
    var x = document.getElementsByTagName(elements[j]);
	  for (var i=0;i<x.length;i++) {
	    if (x[i].className.indexOf('newWin') != -1) {
			  x[i].onkeypress = openNewWindow;
			  x[i].onclick = openNewWindow;

			  if (x[i].title != "") {
			     // use the existing title    
			  } else {
			     x[i].setAttribute("title", "New Window");
			  }			  
			  
		  } else if (x[i].className.indexOf('popup') != -1) {
			  x[i].onkeypress = openNewWindow;
			  x[i].onclick = openNewWindow;
			  x[i].setAttribute("title", "Pop-up Window");
		 } else if (x[i].className.indexOf('word') != -1) {
			  x[i].onkeypress = openNewWindow;
			  x[i].onclick = openNewWindow;
			  x[i].setAttribute("title", "Word Document");
		} else if (x[i].className.indexOf('ppt') != -1) {
			  x[i].onkeypress = openNewWindow;
			  x[i].onclick = openNewWindow;
			  x[i].setAttribute("title", "PowerPoint Document");
		  } else if (x[i].className.indexOf('pdf') != -1) {
			  x[i].onkeypress = openNewWindow;
			  x[i].onclick = openNewWindow;
			  x[i].setAttribute("title", "Adobe PDF Document");
		  } else if (x[i].className.indexOf('printPage') != -1) {
			  x[i].onkeypress = printPage;
			  x[i].onclick = printPage;
			  x[i].setAttribute("title", "Print Page");
		  } else if (x[i].className.indexOf('external') != -1) {
		    x[i].onkeypress = openNewWindow;
			  x[i].onclick = openNewWindow;
			  x[i].setAttribute("title", "External link");
		  } else if (x[i].className.indexOf('internal') != -1) {
			  x[i].setAttribute("title", "Internal link");
		  }
	  }    }

}

function printPage() {
window.print();
}

addLoadEvent(GetAnchors);

    



