

var products = [

"Isis-Lite", "Isis-Lite", "&trade;",
"BathyPro", "BathyPro", "&trade;",
"DelphNav", "DelphNav", "&trade;",
"SB-Logger", "SB-Logger", "&trade;",
"GSF-Export", "GSF-Export", "&trade;",
"Isis Sonar", "IsisŪ Sonar","&trade;",
"TritonMap", "TritonMap","&trade;",
"Survey Planner", "Survey Planner","&trade;",
"SeaClass", "SeaClass","&trade;",
"PipeClass", "PipeClass","&trade;",
"ROVFlight", "ROVFlight","&trade;",
"A-B", "A-B","&trade;",
"DelphMap", "DelphMap","&trade;",
"3-DViz", "3-DViz","&trade;",
"SnippetPro", "SnippetPro","&trade;",
"MSTMosaic", "MSTMosaic","&trade;",
"HydroSuite", "HydroSuite","&trade;",
"HarborSuite", "HarborSuite","&trade;",
"SonarSuite", "SonarSuite","&trade;",
"SubbottomSuite", "SubbottomSuite","&trade;",
"Rugged Workstation", "Rugged Workstation","&trade;",
"Portable Workstation", "Portable Workstation","&trade;",
"Industrial Workstation", "Industrial Workstation","&trade;",
"Isis Bathy", "IsisŪ Bathy","&trade;",
"SB-Pick", "SB-Pick","&trade;",
"Advanced Filtering", "Advanced Filtering","&trade;",
"Swell Filtering", "Swell Filtering","&trade;",
"Replay Only", "Replay Only","&trade;",
"ASCII Reporting", "ASCII Reporting","&trade;",
"Printing", "Printing","&trade;",
"MosaicBasic", "MosaicBasic","&trade;",
"TargetPro", "TargetPro","&trade;",
"PipeTrack", "PipeTrack","&trade;",
"SS-MosaicRT", "SS-MosaicRT","&trade;",
"MBSS-Logger&trade;", "MBSS-Logger&trade;","&trade;",
"MB-Monitor", "MB-Monitor","&trade;",
"IntelliTrak", "IntelliTrak","&trade;",
"IntelliQC", "IntelliQC","&trade;",
"MosaicRT", "MosaicRT","&trade;",
"SBPick", "SBPick","&trade;",
"SnippetPro", "SnippetPro","&trade;",
"Sonar Suite", "Sonar Suite","&trade;",
"SS-Mosaic", "SS-Mosaic","&trade;",
"SS-Office", "SS-Office","&trade;",
"Subbottom Suite", "Subbottom Suite","&trade;",
"Survey Monitor", "Survey Monitor","&trade;",
"Survey Office", "Survey Office","&trade;",
"3DViz", "3DViz","&trade;",
"Triton A-B", "Triton A-B","&trade;",
"TritonNav", "TritonNav","&trade;",
"MB-DTM RT", "MB-DTM RT","&trade;",
"MB-Logger", "MB-Logger","&trade;",
"MB-Office", "MB-Office","&trade;",
"MB-Office", "MB-Office","&trade;",
"MBSS-Logger&trade;", "MBSS-Logger&trade;","&trade;",
"Mosaic Basic", "Mosaic Basic","&trade;",
"Harbor Suite", "Harbor Suite","&trade;",
"Hydro Suite", "Hydro Suite","&trade;",
"Isis Bathy", "IsisŪ Bathy","&trade;",
"Isis Sonar", "IsisŪ Sonar","&trade;",
"Isis Sonar Office", "Isis Sonar Office","&trade;",
"Isis-Lite", "IsisŪ-Lite","&trade;",
"BathyRT", "BathyRT","&trade;",
"Fusion Office", "Fusion Office","&trade;",
"MBSS-Logger&trade;", "MBSS-Logger&trade;","&trade;",
"SS Logger", "SS Logger","&trade;",
"SS-Logger&trade;", "SS-Logger&trade;", "&trade;",
"Isis", "Isis", "&trade;"
 ];

var trademarks = products;

var reg = ["Isis"];


//If the text in text node matches a product name, then we do our magic
//and replace the matched name with the replacement text
//finally, an entity is added if one is specified
function productMatch(n, prod, repl, entity) {
	
	if (n.parentNode==null) return; //can't do much with this
	
	var regex = prod;
	var rexp = new RegExp(regex, "gim");
	var rstring = n.data;

	var prevStart = 0;
	var posttext, mat;
	var temp = document.createDocumentFragment();
	while((mat=rexp.exec(rstring))!=null){ //Individual match
	  var pretext = rstring.substr(prevStart, mat.index);
	  posttext = rstring.substr(rexp.lastIndex);
	  var span = document.createElement("span");
	  span.appendChild(document.createTextNode(repl)); // matched product name
	  span.className = "productTM";
	  prevStart = rexp.lastIndex;
	  if (pretext.length>0) { //add text before the match
		 temp.appendChild(document.createTextNode(pretext));  
	  }
	  temp.appendChild(span);
	}
	temp.appendChild(document.createTextNode(posttext));
	if (entity!=null){
		if ((posttext!=null) && (posttext.indexOf(entity))==-1) {
			
		var espan = document.createElement("span");
	    espan.appendChild(document.createTextNode(entity)); // matched product name
	    espan.className = "entity";
		
		var par = n.parentNode;
		
		if (par.nextSibling == null) { //tack it onto the end of the parent, if the parent is a solo node, such as an anchor element
			par.appendChild(espan);
		} else {
			
			temp.appendChild(espan);
			//At this point, we really should split the anchor element and construct
			//a new node set with the following elements:
			//<anchor> -> pretext ->productname-><end anchor><entity><anchor duplicated>posttext<end anchor>
			// but I'm feeling lazy
		}
		
		}
	}
	n.parentNode.replaceChild(temp, n); 
	
	/*
	if (putelement.parentNode != null) {
		var temp2 = document.createDocumentFragment();
		temp2.appendChildNode(putelement);
	}
	// An entity such as TM or Reg needs special handling
	if (entity!=null) {
		var putelement = n; //The element that will precede the entity
		var par = n.parentNode;
		//Entities should fall outside of the anchor tag, if wrapping
		if ((par!=null) && (par.tagName=="A" || par.tagName=="a")) {
			putelement = par;
			
		if (putelement.parentNode != null) {
			putelement.replaceChild(temp2, 
		}temp2.appendChild(par);
			temp2.appendChild(document.createTextNode(entity));
			n.parentNode.replaceChild(temp, n); 
		} else	{
			par.append
		}
		temp.appendChild(document.createTextNode(entity));
	}

	
	//We need to replace the existing single node of text with a new span node that specifies the 
	//style for the product name and contains the original node as a child node
	*/

}


//For each text node, test whether it contains any of the product, TM or Reg strings
function processText(n) {
	var contents = n.data;
	var parent = n.parent;
	// Don't process already marked items
	if  ((parent!=null) && (parent.tagName=="span") && (parent.getAttribute("className")=="productTM"))  {
								alert("span productTM");
	} else {
		
		for (var i=0; i<products.length; i=i+3) {
			var prod = products[i];
			var regex = prod;
			if (contents.match(regex)) productMatch(n, prod, products[i+1], products[i+2]);
		}
	}
}


		

function processNode(n) {
	
	if (n==null) return;
	
	if (n.nodeType == 3) { //Text node
		processText(n);	
	} 
	else {
		var kids = n.childNodes;
		var numkids = kids.length;
		if (numkids>0) {
			for (var i=0; i<numkids; i++) {
				var n2 = kids[i];
				// Don't reprocess something already defined as a product span
				if (n2!=null)  { //element node
						processNode(n2);
				}
			}

		}
	}
}



function p2() {
 
 	var allelements;
	processNode(document.body);
	//if (document.all != null) { allelements = document.all; }
	//else {allelements = document.getElementsByTagName("*");}

	//if (allelements == null) alert("No Elements Found");
	//for (var i=0; i<allelements.length; i++) {
	//	 processNode(allelements[i]);
	//}
}
