//$Id: //IT/main/code/apps/web/static_web/deployment/common/javascript/devzone_enlink/enlinkment.js#25 $ //Make sure JQUERY or equivalent functions $() AND ready() have been loaded// -- if so add updateRSSFeedsForStats to the DOM ready() listener// -- if not, do nothing.var JQueryObj;//1. make sure $() is defined as a functionif (typeof($) == 'function') {	JQueryObj = $(document);	//2. make sure ready() is defined as a function	if (typeof(JQueryObj.ready) == 'function') {		//3. add the function doEnlinkment to the DOM ready() listener 		JQueryObj.ready(doEnlinkment);	}}//performs the enlinkmentfunction doEnlinkment() {	//validate presence of fpga or rio metadata 	if(validateMetadata()) {		var root_node = document.getElementById('pagearea');		if (root_node != null) {			var restrictions = ['a', 'h', 'img'];			var dictionary = getDictionary();			enlinkNode(root_node, dictionary, restrictions);			reportify(root_node);		}	}}function validateMetaNodeContent(meta_node, valid_content, loose) {	//assign default if loose was not passed	if (loose == null) {		loose = true;	}		var num = valid_content.length;	var valid = false;	for (var k=0; k<num; k++) {		//check if the language metatag has a valid language		if(loose) {			if(meta_node.getAttribute('content').toLowerCase().indexOf(valid_content[k].toLowerCase()) != -1) {				valid = true;				break; //exit for loop			}		}		else { //do strict string compare here (still all lower case but must match exactly - for language for now)			if(meta_node.getAttribute('content').toLowerCase() == valid_content[k].toLowerCase()) {				valid = true;				break; //exit for loop			}		}	}	return valid;}function validateSection(meta_node) {	var valid_content = ['DevZone'];	return validateMetaNodeContent(meta_node, valid_content);}function validateLanguage(meta_node) {	var valid_content = ['en'];	return validateMetaNodeContent(meta_node, valid_content, false); //pass false to loose so language must be EACTLY en}function validateDocType(meta_node) {	var valid_content = ['NI_Supported'];	return validateMetaNodeContent(meta_node, valid_content);	}function validateNigen10(meta_node) {	var rio_codes = [		'9607','9606','9608','9813','10045','7229','8110','8648','8647',		'5822','8086','8080','8078','9159','8932','6757','6783','4887',		'4886','7865','7864','5003','6932','7343','7044','8580','8242',		'8241','8240','9190','9189','4809','4808','4807','4806','4805',		'4801','4800','8252','8250','9384','7197','6292','6288','6287',		'6285','6283','9614','4909','8865','8849','5573','3351','5993',		'5988','5987','5986','4899','4898','4897','4892','4891','4890',		'4889','4812','4811','4810','9015','6289','4883','4881','8706',		'8705','8702','8891','6022','6905','8806','7814','5648','9457',		'8611','8677','9742','9792','9791','9790','9789','9788','9787',		'9786','9785','4914','4913','4912','4911','4910','8067','8066',		'9135','4908','4907','4906','4905','4904','4903','4902','4901',		'4900','5129','4858','7818','7817','7816','7815','6313','6312',		'9563','7342','6250','8231','8230','7180','7179','7178','8258',		'8257','8254','8253','9212','7225','5887','5886','5885','5884',		'6810','7994','6887','6209','6208','6948','6947','6937','6934',		'6933','3350','8251','6569','9812','5821','4893','4916','4796',		'2712','5572'	];	var r_series_codes = [		'9704','9703','8649','4574','4573','5003','6576','9702','3775',		'6646','4821','4820','6210','7284','4376','4375','5111','5110',		'5109','6742','6773','5195','5194','9323','9782','9781','6774',		'4377','5503','6191'	];	var fpga_code = '4398';	var valid_content = rio_codes.concat(r_series_codes, fpga_code);	return validateMetaNodeContent(meta_node, valid_content);	}function validateMetadata() {	var metatags = document.getElementsByTagName('meta');	var num = metatags.length;	var meta_node;	var meta_name;	var validation = new Array();	for (var k=0; k<num; k++) {		meta_node = metatags[k];		meta_name = meta_node.getAttribute('name');		if(meta_name != null) {			switch(meta_name.toLowerCase()) {				case 'nigen10':					validation[validation.length] = validateNigen10(meta_node);				break;    				case 'language':					validation[validation.length] = validateLanguage(meta_node);				break;								//ADD OTHER CASES HERE								default: ;			}		}	}		//make sure all required data validated	var num = validation.length;	var is_valid = true;	for (var k=0; k<num; k++) {		if(!validation[k]) {			is_valid = false;			break;		}	}	return is_valid;}function buildSplitRegExp(tag_names) {	var num = tag_names.length;	var regexp_str = '';	var tag_name;	for (var k=0; k<num; k++) {		tag_name = tag_names[k];		if (tag_name == 'b') {			alert('You cannot split on <b> tags currently - they are being used as dummy dom.');		}		else if(tag_name == 'h') {			regexp_str += '<' + tag_name + '[0-9][^>]*>.*?</' + tag_name + '[0-9]>|';		}		else if(tag_name == 'img') {			regexp_str += '<img[^>]*>|';		}		else {	 //catch rest of cases			regexp_str += '<' + tag_name + '[^>]*>.*?</' + tag_name + '>|';		}	}	regexp_str = regexp_str.substr(0, regexp_str.length-1); //remove trailing pipe char	regexp_str += '';	return new RegExp(regexp_str, 'gi');}function buildPaddingRegExp(tag_names) {	var num = tag_names.length;	var regexp_str = '(';	var tag_name;	for (var k=0; k<num; k++) {		tag_name = tag_names[k];		if (tag_name == 'b') {			alert('You cannot split on <b> tags currently - they are being used as dummy dom.');		}		else if(tag_name == 'h') {			regexp_str += tag_name + '[0-9]>|';		}		else if(tag_name == 'img') {			regexp_str += '<img[^>]*>|';		}		else {	 //catch rest of cases			regexp_str += tag_name + '>|';		}	}	regexp_str = regexp_str.substr(0, regexp_str.length-1); //remove trailing pipe char	regexp_str += ')';	return new RegExp(regexp_str, 'gi');}function prepareDom(dom_html, restrictions) {	//dummy dom to pad html for stable parsing.	var dummy_dom = '<b> </b>';		//pad with dummy_dom to ensure we don't start with a match.	var htmlString = dummy_dom+dom_html;		//remove/replace html number code for &nbsp;	htmlString = htmlString.replace(new RegExp('&#160;', 'gi'), '&nbsp;');			//ensure algorithm interleave synchronization - pad with a dead tag after restricted tags	htmlString = htmlString.replace(buildPaddingRegExp(restrictions), "$1"+dummy_dom);		//alert(htmlString);	return htmlString;}//transforms text to links in input_string for all matches to the regular expression regexp, using replace_string as the link textfunction enlink(input_string, regexp, replace_string) {	return input_string.replace(regexp, replace_string);}//enlinks the node root_node using the dictionary passed, ignoring existing links (anchor tags)function enlinkNode(root_node, dictionary, restrictions) {	//prepare the dom for enlinkment parsing	var htmlString = prepareDom(root_node.innerHTML, restrictions);			//define the anchor tag finding regular expression	var split_reg_exp = buildSplitRegExp(restrictions);	//create an array of the strings corresponding to the anchors' html	var anchor_strings = htmlString.match(split_reg_exp);	var num_anchors = 0;	if(anchor_strings != null) {		num_anchors = anchor_strings.length;	}	else {		anchor_strings = new Array();	}	//get array of strings (html) by splitting at anchor tags (anchor html not included in any of the elements)	var no_anchor_strings = htmlString.split(split_reg_exp);	var num_no_anchor_strings = 0;	if(no_anchor_strings != null) {		num_no_anchor_strings = no_anchor_strings.length;	}	else {		no_anchor_strings = new Array();	}		//enlink each string in the array (now that anchor html has been removed)	for(var m=0; m<num_no_anchor_strings; m++) {		//enlink the current string with all the dictionary elements		for (var k=0; k<dictionary.length; k++) {			no_anchor_strings[m] = enlink(no_anchor_strings[m], new RegExp(dictionary[k][0], dictionary[k][3]), '<a class="enlinkment" href="' + dictionary[k][1] + '">'+dictionary[k][2] + '</a>'); 		}	}		//make anchor_strings and no_anchor_strings the same length to facilitate interleaving the arrays below	var diff = num_no_anchor_strings - num_anchors;	if(diff > 0) {		for(var k=0; k<diff; k++) {			anchor_strings[anchor_strings.length] = ''; //append null strings to the array to equalize length		}	}	if (diff < 0){		diff = 0 - diff; //make diff positive		for(var k=0; k<diff; k++) {			no_anchor_strings[no_anchor_strings.length] = ''; //append null strings to the array to equalize length		}	}		//get the number of items now in both arrays	var num_items = no_anchor_strings.length;	//now merge the anchor tags and the enlinked text back into a single html string	temp = ''; //we reuse the temp variable defined above here	for(var m=0; m<num_items; m++) {		//NOTE: we can safely start with no_anchor tags here because we automatically inject an empty		temp += no_anchor_strings[m] + anchor_strings[m];	}			//reassign the node html we just enlinked	root_node.innerHTML = temp;}//creates and returns the dictionaryfunction getDictionary() {		//STEP 1: create the dictionary data	//----------------------------------		//elements: [product number, model page id]	var crio_numbers = [	['9012', '203347'],	['9014', '203500'],	['9002', '14160'],['9004', '14161'], ['9072', '203963'], 	['9074', '203964'], ['9101', '14156'], ['9102', '14157'], ['9103', '14158'], ['9104', '14159'], 	['9151', '14163'], ['9205', '202571'], ['9219', '203423'], ['9201', '14588'], ['9215', '14166'], 	['9219', '203423'], ['9239', '203420'], ['9206', '202572'], ['9221', '14590'], ['9229', '203422'], 	['9211', '14165'], ['9217', '202713'], ['9233', '14591'], ['9237', '202632'], ['9263', '14170'], 	['9265', '201848'], ['9401', '201849'], ['9421', '14172'], ['9422', '202714'], ['9423', '14173'], 	['9425', '14592'], ['9435', '14174'], ['9411', '14171'], ['9472', '14175'], ['9474', '14176'], 	['9476', '201851'], ['9477', '202481'], ['9481', '14177'], ['9485', '202822'], ['9505', '202711'], 	['9852', '202743'], ['9853', '201972'], ['9802', '204328'], ['9870', '204259'], ['9871', '204260'], 	['9951', '14548']	];	var num_crios = crio_numbers.length;		//elements: [product number, model page id]	var pci_rseries_numbers = [	['7830', '202006'],	['7831', '13861'],	['7833', '202007'],['7811', '202011'], ['7813', '202012']	];	var num_pci_rseries = pci_rseries_numbers.length;		//elements: [product number, model page id]	var pxi_rseries_numbers = [	['7830', '202008'],	['7831', '11873'],	['7833', '202009'],['7811', '13862'], ['7813', '202013'],	['7842', '205129'], ['7841', '205128'], ['7852', '205131'], ['7851', '205130']	];	var num_pxi_rseries = pxi_rseries_numbers.length;				//STEP 2: create the dictionary from the data above	//-------------------------------------------------		var dictionary = new Array();	var offset = 0;		//add crio products to dictionary	offset = dictionary.length;	for (var k=0; k<num_crios; k++) {		dictionary[k+offset] = ["(NI|cRIO|RIO|CompactRIO|NI\\scRIO|NI&nbsp;cRIO)(\\s|&nbsp;)*(\\-)\?(\\s)*"+crio_numbers[k][0], 'http://sine.ni.com/nips/cds/view/p/lang/en/nid/'+crio_numbers[k][1], 'NI '+crio_numbers[k][0], 'gi'];	}		//add the pci r-series products to the dictionary	offset = dictionary.length;	for (var k=0; k<num_pci_rseries; k++) {		dictionary[k+offset] = ["(NI(\\s|&nbsp;)*)\?PCI(\\s)*(\\-)\?(\\s)*"+pci_rseries_numbers[k][0]+"R\?", 'http://sine.ni.com/nips/cds/view/p/lang/en/nid/'+pci_rseries_numbers[k][1], 'NI PCI-'+pci_rseries_numbers[k][0]+'R', 'gi'];	}		//add the pxi r-series products to the dictionary	offset = dictionary.length;	for (var k=0; k<num_pxi_rseries; k++) {		dictionary[k+offset] = ["(NI(\\s|&nbsp;)*)\?PXI(\\s)*(\\-)\?(\\s)*"+pxi_rseries_numbers[k][0]+"R\?", 'http://sine.ni.com/nips/cds/view/p/lang/en/nid/'+pxi_rseries_numbers[k][1], 'NI PXI-'+pxi_rseries_numbers[k][0]+'R', 'gi'];	}		//add fpga family page to dictionary	dictionary[dictionary.length] = ["(LabVIEW|LV)(\\s|&nbsp;)*(FPGA)", 'http://www.ni.com/fpga', 'LabVIEW FPGA', 'i'];		//add crio family page to dictionary	dictionary[dictionary.length] = ["CompactRIO", 'http://www.ni.com/compactrio', 'CompactRIO', 'i'];	return dictionary;}function reportify(root_node) {	//get the the enlinkment anchors	var anchors = getElementsByClass(root_node, 'a', 'enlinkment');	var num_anchors = anchors.length;	//if the WebTrends reporting function is defined, assign the enlinkment onclick events accordingly	if (typeof(dcsMultiTrack) == 'function') {		for (var k=0; k<num_anchors; k++) {			anchors[k].id = 'enlink'+k; //give the enlink a unique id on the page			anchors[k].onclick = function()	{				dcsMultiTrack('DCSext.Enlinkment',window.location.href+'__'+this.innerHTML+'__'+this.id); //WebTrends call					}		}	}			//inject the number of enlinkment links created into a hidden field at the end of the document	injectNumEnlinksHidden(num_anchors);	}function injectNumEnlinksHidden(num_anchors) {	var num_enlinks_element = document.createElement('input'); //create an input element	num_enlinks_element.setAttribute('type', 'hidden'); //make it hidden	num_enlinks_element.setAttribute('name', 'enlink_count'); //name it	num_enlinks_element.setAttribute('value', num_anchors+''); //inject the number of enlinkments into it's value	var the_body = document.getElementsByTagName('body')[0]; //get a reference to the body	the_body.appendChild(num_enlinks_element); //add the new num_enlinks_element to the body}function getElementsByClass(root_node, tag_name, class_name) {	var elementArray = new Array();	var matchedArray = new Array();		elementArray = root_node.getElementsByTagName(tag_name);		for (var i=0; i < elementArray.length; i++) {		var pattern = new RegExp("(^| )" + class_name + "( |$)");				if (pattern.test(elementArray[i].className))		{			matchedArray[matchedArray.length] = elementArray[i];		}	}	return matchedArray;}
