 // This document contains article-specific javascript functions and utilities

function emailarticle(aid) 
{
	strFeatures = "width=500,height=375,scrollbars=no,center=yes,border=thin,help=no,status=no";
	strTitle = window.open ("/custom/email_article.asp?aid=" + aid, "emailarticle", strFeatures);
	return false;
}

function hide_empty_section(){
var sections = new Array("summary", "prereq", "related", "content", "rules", "internal", "issues", "requirements", "additional", "answer", "form", "objectives", "callid", "callflow", "assessment"); //array of div id's for sections
var sectionCounter;

 sectionCounter = 0
 for(i=0;i<sections.length;i++)
 {
	if(document.getElementById(sections[i])) {  
		var section = document.getElementById(sections[i]);
		var contents = section.innerHTML;		
		
		if (contents.search("stepxstep") != -1){//if section has step content add show / hide to section header
			var headers = section.getElementsByTagName("h2");
			if (headers.length > 0){
				var headerContent = headers[0].innerHTML;
				headers[0].innerHTML = "<span style = 'float:right; font-size: 12px; font-weight:normal;'><a href = 'javascript:show_hide_all_steps();'>show / hide all steps</a></span>" + headerContent;
			}
		}//end if section has step content
		
		contents = contents.replace(/&nbsp;*/img, "");//strip out html spaces
		contents = contents.replace(/<script>.*<\/script>/img, "");//strip out scripts
		contents = contents.replace(/<h[1-6]>[^<>]*<\/h[1-6]>/img, "");//strip out headers
		contents = contents.replace(/<a[^<>]*>[^<>]*<\/a>/img, "");//strip out links (anchors)
		contents = contents.replace(/\[\[[^\]]*\]\]/img, "");//strip out smart tags
		contents = contents.replace(/[\f\n\r\t\v\s]*/img, "");//strip out whitespace
	
		if (contents.length < 1){	
			section.style.display = "none"; // HIDE SECTION
			var link_id = sections[i] + "_link";
			if (document.getElementById(link_id)){
				var link = document.getElementById(link_id);
				link.style.display = "none"; // HIDE LINK
			}//end if
		} else {
			sectionCounter = sectionCounter + 1;		
			//alert(sections[i] + " contents = '" + contents + "'")
		}//end if
	} else {
			var link_id = sections[i] + "_link";
			if (document.getElementById(link_id)){
				var link = document.getElementById(link_id);
				link.style.display = "none"; // HIDE LINK
				//alert (link_id);
			}			
	}
 }//end for
//  	alert("section counter = '" + sectionCounter + "'"); 
 
 if (document.getElementById("nav")){
	 var nav = document.getElementById("nav")
	 var nav_contents = nav.innerHTML;
	 
	 nav.innerHTML = nav_contents.replace("&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;", "&nbsp;&nbsp;|&nbsp;&nbsp;")
	 if (sectionCounter < 2){
			nav.style.display = "none"; 
			Nifty("div#title","big bottom");
	 }
 }
	if (sectionCounter < 2){ //if there isn't more than one section, hide section titles

		for(i=0;i<sections.length;i++)
 		{
			if(document.getElementById(sections[i])) {  
				section = document.getElementById(sections[i]);
				contents = section.innerHTML;
				eval("contents = contents.replace(/<h2>" + sections[i] + "<.h2>/img, \"<div style = 'clear:both;'></div>\");");
				section.innerHTML = contents;
			}
		}
	}

}//end function hide_empty_section


function convert_steps(){

	var step_table, step_tables, x, i, step_tbody, step_tbodies, 
	step_rows, step_row, step_cells, step_cell, nodes, tag, title, prefix, suffix, content;	
	step_tables = document.getElementsBySelector("table.stepxstep");
	for (i = 0; i < step_tables.length; i++) {
		
		step_table = step_tables[i];	
		step_tbodies = step_table.getElementsByTagName("tbody");
		//alert ("hello" + step_tbodies.length);
		if (step_tbodies.length > 0){
			rowCounter = 0;
			nodes = step_tbodies[0].childNodes;
			for (x = 0; x < nodes.length; x++){
				if ((nodes[x].tagName == "TR")||(nodes[x].tagName == "tr")){
					rowCounter++;
					subnodes = nodes[x].childNodes;
					for (y = 0; y < subnodes.length; y++){
						if ((subnodes[y].tagName == "TD")||(subnodes[y].tagName == "td")){
							//alert(subnodes[y].tagName);
							cell = subnodes[y];
							cell_headers = cell.getElementsByTagName("h3");
							if (cell_headers.length > 0){
								title = cell_headers[0].innerHTML;
								cell.removeChild(cell_headers[0]);
							} else {
								title = "Step " + rowCounter;
							}
							prefix = "<div class = 'step' id = 'step" + rowCounter + "'>" + rowCounter + ".&nbsp;&nbsp;<a href = \"javascript: show_hide_steps('step" + rowCounter + "');\" >" + title + "</a></div>\n<div class = 'step_content' id = 'step" + rowCounter + "_content'>\n";
							suffix = "</div>\n";
							content = cell.innerHTML;
							cell.innerHTML = prefix + content + suffix;

						}
					}//end for each potential cell
				}			
			}//end for each potential row 
			
		}// had tbody

	}// end for each step table

}// end function


function template_run_functions(template_type){

switch (template_type) {
	case "general":
		Nifty("div#title","big top");
		Nifty("div#nav","big bottom");
		break;
	case "techincal_bulletin":
		Nifty("div#title","big top");
		Nifty("div#nav","big bottom");
		break;
	case "faq":
		Nifty("div#template_faq div#title","big");
		break;
	case "news":
		Nifty("div#title","big");
		break;	
	case "simple":
		Nifty("div#title","big");
		break;	
	default:
		Nifty("div#title","big top");
		Nifty("div#nav","big bottom");	
		break;
}

//everything after var location doesn't work in other sites
hide_empty_section();	
convert_steps();
show_hide_steps("");
add_targets();


if (top.location.href.match("display")){//make backward compatible

if (document.getElementById("article")){
	var article = document.getElementById("article");
	var contents = article.innerHTML;	
	var href;
	var x = 0;
	contents = contents.replace("[[file]]", "/pf/12/webfiles/");//add paths to webfiles
	contents = contents.replace(/\[\[[^\]]*\]\]/img, "");//strip out smart tags
	article.innerHTML = contents;

	var images = article.getElementsByTagName("img"), src;
	for (x = 0; x < images.length; x++){
		src = images[x].src.toString();
		src = src.replace("http://199.239.30.197/kb/article/custom/tiny_mce/plugins/imagelibrary/image_library/", "/custom/image_library/");//make sure images are referenced correctly
		src = src.replace("http://kbadmin.wildblue.net/custom/tiny_mce/plugins/imagelibrary/image_library/", "/custom/image_library/");//make sure images are referenced correctly
		src = src.replace("http://help.wildblue.net/display/2n/kb/tiny_mce/plugins/imagelibrary/image_library/", "/custom/image_library/");//make sure images are referenced correctly
		src = src.replace("http://help.servicecenter.wildblue.net/display/2n/kb/tiny_mce/plugins/imagelibrary/image_library/", "/custom/image_library/");//make sure images are referenced correctly
		src = src.replace(/^tiny_mce\/plugins\/imagelibrary\/image_library\//img, "/custom/image_library/");//make sure images are referenced correctly
		images[x].src = src;		
	}//end for
	
	var links = article.getElementsByTagName("a"), article_id, href;
	var url = String(document.location);
	var portal;
	//alert(url)
	while (window.history.previous){
		alert(window.history.previous);
	}
	for (x = 0; x < links.length; x++){
		href = links[x].getAttribute('href');
		if (href.search("article_link:") != -1){
			article_id = href.substring(href.lastIndexOf(":") + 1, href.length);
			href = "http://help.wildblue.net/article.asp?article=" + article_id + "&p=14";
			links[x].setAttribute('href', href);
			links[x].target = "_top";			
			//document.write ("new href = " + links[x].getAttribute('href') + "<br>");
		}	
	}//end for	
	
	if (document.getElementById("modified")){
		var modified = document.getElementById("modified");
		modified.style.display = 'none';
	}
}	// end if article div
} else {//new interface



}
}//end function