// 
//  Library Code
//
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_showHideLayers() { //v9.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) 
  with (document) if (getElementById && ((obj=getElementById(args[i]))!=null)) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
    obj.visibility=v; }
}

function MM_CheckFlashVersion(reqVerStr,msg){
  with(navigator){
    var isIE  = (appVersion.indexOf("MSIE") != -1 && userAgent.indexOf("Opera") == -1);
    var isWin = (appVersion.toLowerCase().indexOf("win") != -1);
    if (!isIE || !isWin){  
      var flashVer = -1;
      if (plugins && plugins.length > 0){
        var desc = plugins["Shockwave Flash"] ? plugins["Shockwave Flash"].description : "";
        desc = plugins["Shockwave Flash 2.0"] ? plugins["Shockwave Flash 2.0"].description : desc;
        if (desc == "") flashVer = -1;
        else{
          var descArr = desc.split(" ");
          var tempArrMajor = descArr[2].split(".");
          var verMajor = tempArrMajor[0];
          var tempArrMinor = (descArr[3] != "") ? descArr[3].split("r") : descArr[4].split("r");
          var verMinor = (tempArrMinor[1] > 0) ? tempArrMinor[1] : 0;
          flashVer =  parseFloat(verMajor + "." + verMinor);
        }
      }
      // WebTV has Flash Player 4 or lower -- too low for video
      else if (userAgent.toLowerCase().indexOf("webtv") != -1) flashVer = 4.0;

      var verArr = reqVerStr.split(",");
      var reqVer = parseFloat(verArr[0] + "." + verArr[2]);
  
      if (flashVer < reqVer){
        if (confirm(msg))
          window.location = "http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash";
      }
    }
  } 
}

function sendemail(recip, buckmain){
	var name = document.getElementById('name').value,
		email = document.getElementById('email').value,
		subject = document.getElementById('subject').value,
		emailbody = document.getElementById('emailbody').value;
	var receive = (document.getElementById('receive').checked) ? document.getElementById('receive').value : '';
		
	sendRequest('emailform2.php?embed=ajax', displaymessage, 
				'name=' + email + 
				'&email=' + email + 
				'&subject=' + subject + 
				'&emailbody=' + emailbody + 
				'&receive=' + receive + 
				'&username=' + recip + 
				'&buckmain=' + buckmain);
}

function openemailform(recip, buckmain){
	sendRequest('emailform2.php?embed=ajax&username=' + recip + '&buckmain=' + buckmain, displayemailform);
}

function displaymessage(response){
	var dest = document.getElementById('emailform');
//	dest.innerHTML = response.responseText;
	alert(response.responseText);
	nevermind();
}


function displayemailform(response) {
	var div = document.createElement('DIV');
	div.id = 'emailme';
	div.innerHTML = '<img src="/API/images/blank.gif" class="background" /><div id="emailform">' + response.responseText + '</div>';
	document.body.appendChild(div);
	document.getElementById('name').focus();
}

function nevermind(){
	var target = document.getElementById('emailme');
	target.parentNode.removeChild(target);
}

function sendRequest(url, callback, postData) {
	var req = createXMLHTTPObject();
	if (!req) return;
	var method = (postData) ? 'POST' : 'GET';
	req.open(method, url, true);
	req.setRequestHeader('User-Agent','XMLHTTP/1.0');
	if (postData)
		req.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
	req.onreadystatechange = function () {
		if (req.readyState != 4) return;
		if (req.status != 200 && req.status != 304) {
//			alert('HTTP error ' + req.status);
			return;
		}
		// 20080702 added check for callback
		if (callback){
			callback(req);
		}
	}
	if (req.readyState == 4) return;
	req.send(postData);
}

var XMLHttpFactories = [
	function () {return new XMLHttpRequest()},
	function () {return new ActiveXObject('Msxml2.XMLHTTP')},
	function () {return new ActiveXObject('Msxml3.XMLHTTP')},
	function () {return new ActiveXObject('Microsoft.XMLHTTP')}
];

function createXMLHTTPObject() {
	var xmlhttp = false;
	for (var i=0, l=XMLHttpFactories.length; i<l; i++) {
		try {
			xmlhttp = XMLHttpFactories[i]();
		} catch (e) {
			continue;
		}
		break;
	}
	return xmlhttp;
}

function fixIEQuotes(){
	var objQuotes = document.getElementsByTagName('q');
	var strOpen, strClose;

	for (var i=0; i<objQuotes.length; i++){
		if (isNested(objQuotes[i])){
			// Double-quotes
			strOpen = document.createTextNode('\u2018');
			strClose = document.createTextNode('\u2019');
		} else {
			// Single-quotes
			strOpen = document.createTextNode('\u201c');
			strClose = document.createTextNode('\u201d');
		}
		// Insert quotation marks around quote
		objQuotes[i].parentNode.insertBefore(strOpen, objQuotes[i]);
		objQuotes[i].appendChild(strClose);
	}
	objQuotes = null;
	
	function isNested(objElement){
		var objParent = objElement;
		do {// Check if nested quote
			objParent = objParent.parentNode;
			if (objParent.tagName && objParent.tagName.toLowerCase() == 'q'){
				return true;
			}
		} while (objParent.parentNode);
		return false;
	}
}

var APIGROUP = {}
APIGROUP.stopwords = {
	// {word: '', href: '/'}, [, newtab: true]
	words: [
// Company Names
		{word: 'Alliance Fire Protection', href: '/profiles/Alliance+Fire+Protection%2C+Inc.'},
		{word: [ 'Anco Products, Inc', 'Anco Products', 'Anco' ], href: '/profiles/Anco+Products%2C+Inc.'},
		{word: [ 'APi Construction Company', 'APi Construction Co', 'APi Construction' ], href: '/profiles/APi+Construction+Company'},
		{word: ['APi Distribution', 'APi Distribution, Inc.'], href: '/profiles/APi+Distribution%2C+Inc.'},
		{word: [ 'APi Electric', 'APi Electric Company'], href: '/profiles/APi+Electric+Company'},
		{word: 'APi Facilities Management', href: '/profiles/APi+Facilities+Management'},
		{word: [ 'APi National Service Group', 'APi NSG', 'NSG' ], href: '/profiles/APi+National+Service+Group'},
		{word: ['API Supply', 'APi Supply'], href: '/profiles/API+Supply%2C+Inc.'},
		{word: 'APi Systems Group', href: '/profiles/APi+Systems+Group%2C+Inc.'},
		{word: 'APi Systems Integrators', href: '/profiles/APi+Systems+Integrators'},
		{word: [ 'Classic Industrial Services, Inc', 'Classic Industrial Services' ], href: '/profiles/Classic+Industrial+Services%2C+Inc.'},
		{word: 'Communication Systems', href: '/profiles/APi+Systems+Integrators'},
		{word: [ 'Davis-Ulmer Sprinkler Company', 'Davis-Ulmer Sprinkler Co', 'Davis-Ulmer' ],  href: '/profiles/Davis-Ulmer+Sprinkler+Company'},
		{word: [ 'Delta Fire Systems, Inc', 'Delta Fire Systems', 'Delta Fire' ], href: '/profiles/Delta+Fire+Systems%2C+Inc.'},
		{word: [ 'Doody Mechanical, Inc', 'Doody Mechanical' ], href: '/profiles/Doody+Mechanical%2C+Inc.'},
		{word: 'Garage Door Store', href: '/profiles/Garage+Door+Store'},
		{word: [ 'Grunau Company, Inc', 'Grunau Company', 'Grunau Co', 'Grunau' ], href: '/profiles/Grunau+Company%2C+Inc.'},
		{word: [ 'Industrial Contractors, Inc', 'Industrial Contractors', 'ICI' ], href: '/profiles/Industrial+Contractors%2C+Inc.'},
		{word: 'Industrial Fabricators', href: '/profiles/Industrial+Fabricators%2C+Inc.'},
		{word: [ 'International Fire Protection', 'IFP'], href: '/profiles/International+Fire+Protection%2C+Inc.'},
		{word: [ 'The Jamar Company', 'Jamar' ], href: '/profiles/Jamar+Company'},
		{word: [ 'LeJeune Steel Company', 'LeJeune Steel Co' ], href: '/profiles/LeJeune+Steel+Company'},
		{word: [ 'Northern Fire \& Communication', 'Northern Fire and Communication' ], href: '/profiles/APi+Systems+Integrators'},
		{word: [ 'NYCO, Inc', 'NYCO' ], href: '/profiles/NYCO%2C+Inc.'},
		{word: 'Rich Fire Protection', href: '/profiles/Rich+Fire+Protection+Company%2C+Inc.'},
		{word: 'Security Fire Protection', href: '/profiles/Security+Fire+Protection+Company%2C+Inc.'},
		{word: [ 'Tessier\'s, Inc', 'Tessier\'s' ], href: '/profiles/Tessier%27s%2C+Inc.'},
		{word: [ 'Twin City Garage Door Company', 'TCGD' ], href: '/profiles/Twin+City+Garage+Door+Company'},
		{word: [ 'United States Fire Protection, Inc', 'United States Fire Protection', 'U.S. Fire Protection', 'USFP' ], href: '/profiles/United+States+Fire+Protection%2C+Inc.'},
		{word: 'VFP Fire Systems', href: '/profiles/VFP+Fire+Systems%2C+Inc.'},
		{word: [ 'Viking Automatic Sprinkler', 'Viking Sprinkler','Viking' ], href: '/profiles/Viking+Automatic+Sprinkler+Company'},
		{word: [ 'Vipond Fire Protection Ltd', 'Vipond Fire Protection' ], href: '/profiles/Vipond+Fire+Protection'},
		{word: 'Vipond Systems Group', href: '/profiles/Vipond+Systems+Group'},
		{word: [ 'Western States Fire Protection Co', 'Western States Fire Protection', 'WSFP' ], href: '/profiles/Western+States+Fire+Protection%2C+Inc.'},
// People
		{word: [ 'Russell Becker', 'Russ Becker' ], href: '/API/resources/bios/api_group_inc-russell_becker.pdf', newtab: true},
		{word: 'Lee R. Anderson', href: '/API/resources/bios/api_group_inc-lee_r_anderson_sr.pdf', newtab: true},
// global	
		{word: 'LEED related projects', href: '/leed_projects.php', nocase: true},
		{word: 'LEED Engineering', href: '/leed_professionals.php', nocase: true},
		{word: 'LEED construction', href: '/alternative_energy.php', nocase: true},
		{word: 'award-winning', href: '/awards.php'},
		{word: 'awards', href: '/awards.php'},
		{word: 'outstanding reputation', href: '/awards.php'},
		{word: 'internal networking', href: '/meet_our_executives.php'},
		{word: 'solid financial foundation', href: '/financial_strength.php'},
		{word: 'top-notch training', href: '/leadership_development.php'},
		{word: 'APi Group headquarters', href: '/api_headquarters.php'},
		{word: [ 'our commitment to building a safer environment', 'our commitment' ], href: '/our_commitment.php'},
		{word: [ 'our employees uniting as a team', 'teamwork' ], href: '/united_approach.php'},
		{word: 'work and achieve', href: '/careers.php'},
		{word: '1926', href: '/timeline.php'},
		{word: 'construction-related', href: '/construction.php'},
//		{word: 'garage door', href: '/companies.php'},
		{word: 'fire suppression system', href: '/life_safety.php'},
// companies
		{word: 'family of independent companies', href: '/united_approach.php'},
		{word: [ 'APi family of companies', 'APi family', 'family of companies' ], href: '/united_approach.php'},
		{word: [ 'group of companies', 'APi Group companies', 'Group companies' ], href: '/our_companies.php'},
		{word: 'welcome companies', href: '/new_business_acquisitions.php'},
		{word: [ 'strategic acquisitions', 'acquisitions' ], href: '/new_business_acquisitions.php'},
		{word: [ 'independently managed companies', 'our companies', 'companies' ], href: '/our_companies.php', nocase: true},
		{word: [ 'industrial and specialty construction', 'specialty construction' ], href: '/construction.php'},
		{word: [ 'fire protection company', 'fire protection' ], href: '/fire_protection_profiles.php'},
		{word: 'life safety', href: '/life_safety.php'},
// Restricted words
		{word: 'Textrafine', href: 'http://www.ancoproductsinc.com/insulation/textrafine.html', newtab: true, external: true},
	],
	newstopwords: [],
	wholewords: true,
	limit: true,
	stoptags: /^A$|H1|H2|H3|H4|BLOCKQUOTE/i
};
function linkify(element){
	if (!element) { return; }
	var completed = false;
	var SW = APIGROUP.stopwords;
	SW.words = SW.words.concat(SW.newstopwords);

	function wordArray(string, obj) {
		var words = obj.word;
		if (words.constructor == Array){
			for (var i=0, l=words.length; i<l; i++){
				if ( string.match( buildRegExp( obj.nocase, words[i] ))) {
					return words[i];
				}
			}
		} else {
			return string.match( buildRegExp( obj.nocase, words ) ) ? words : false ;
		}
	}

	function buildRegExp( nocase, pattern ){
		var fix = SW.wholewords ? '\\b' : '' ;
		var flag = nocase ? 'i' : '' ;
		return new RegExp(fix + pattern + fix, flag);
	}

	function restrictedContainer(elem){
		return SW.stoptags.test(elem.tagName);
	}	

	function innerHighlight(node, obj){
		var skip = 0;
		if (SW.limit && completed){ 
			return skip; 
		}
		if (!obj){return}
		var word = obj.word;
		var pattern = SW.wholewords ? buildRegExp(obj.nocase, word) : false;
		if (node.nodeType == 3) {
			pattern = wordArray(node.data, obj);
			if (!pattern){
				return skip;
			}
			var start = pattern.toString();
			var pos = obj.nocase ? 
					node.data.toLowerCase().indexOf(start.toLowerCase()) : 
					node.data.indexOf(start) ;
			if (pos >= 0) {
				if ( restrictedContainer(node.parentNode) ){
					return;
				}
				completed = true;
				var spannode = document.createElement('a');
				spannode.href = obj.href;
				spannode.target = obj.newtab ? '_blank' : '' ;
				var middlebit = node.splitText( pos );
				var endbit = middlebit.splitText( start.length );
				spannode.appendChild( middlebit.cloneNode(true) );
				middlebit.parentNode.replaceChild(spannode, middlebit);
				skip = 1;
			}
		} else if (node.nodeType == 1 && node.childNodes && !/(script|style|^a$)/i.test(node.tagName)) {
			if ( restrictedContainer(node.tagName) ){
				return;
			}
			for (var i=0, l=node.childNodes.length; i<l; i++) {
				i += innerHighlight(node.childNodes[i], obj);
			}
		}
		return skip;
	}

	for (var i=0,l=SW.words.length; i<l; i++){
		completed = false;
		innerHighlight(element, SW.words[i]);
	}
}

/*
function linkify(element){
	if (!element) {
		return;
	}
	APIGROUP.stopwords.words = APIGROUP.stopwords.words.concat(APIGROUP.stopwords.newstopwords);
	var completed = '';
	
	function innerHighlight(node, index){
		if (APIGROUP.stopwords.limit && completed){ 
			return skip; 
		}
		var obj = APIGROUP.stopwords.words[index],
			pattern = obj.word,
			newtab = obj.newtab,
			skip = 0;
			if (APIGROUP.stopwords.wholewords){
				var pattern2 = obj.nocase ? new RegExp('\\b' + pattern + '\\b','i') : new RegExp('\\b' + pattern + '\\b');
			}
		if (node.nodeType == 3) {
			var start = pattern2 ? node.data.match(pattern2) : pattern ;
			var pos = node.data.indexOf(start);
			if (pos >= 0) {
				completed = true;
				var stoptags = APIGROUP.stopwords.stoptags;
				for (var i=0, l=stoptags.length; i<l; i++){
					if (node.parentNode.tagName == stoptags[i]){
						return;
					}
				}
				var spannode = document.createElement('a');
				spannode.href = (!obj.external) ? '/' + obj.href : obj.href ;
				spannode.target = obj.newtab ? '_blank' : '' ;
				var middlebit = node.splitText(pos);
					endbit = middlebit.splitText(pattern.length);
					middleclone = middlebit.cloneNode(true);
				spannode.appendChild(middleclone);
				middlebit.parentNode.replaceChild(spannode, middlebit);
				skip = 1;
			}
		} else if (node.nodeType == 1 && node.childNodes && !/(script|style)/i.test(node.tagName)) {
			for (var i = 0; i < node.childNodes.length; ++i) {
				if (pattern2){
					var goo = node.childNodes[i].innerHTML;
					if (goo && !goo.match(pattern2) ){
						continue;								
					}
				}
				i += innerHighlight(node.childNodes[i], index);
			}
		}
		return skip;
	}

	for (var i=0,l=APIGROUP.stopwords.words.length; i<l; i++){
		completed = false;
		innerHighlight(element, i);
	}
}
*/