/*
	UPDATE LOG:
	09-08-2009 	JJLANG	-	MODIFIED createpossibles FUNCTION TO NOT ATTEMPT TO OFFER VALID 
							LINK
*/

/************************************************
	purpose:	run functions to modify page
	note: custom from main.js to not include breadcrumbs
*************************************************/
window.onload = function() {
	// make left menu context sensitive
	leftMenu();
	
	// browser detection
	BrowserDetect.init();
	
	// build top menu drop downs
	if (BrowserDetect.browser != 'Netscape'){
		topMenu();
	};
	
	// If browser is Netscape need to make style fixes
	if (BrowserDetect.browser == 'Netscape'){
		Netscapeadjustments()
	};
	
	// highlight image of section currently on
	highlightSection();
	
	// load son of suckerfish to get menus to work in IE
	if(window.attachEvent) {
		window.attachEvent("onload",sfHover);
	}
	
	// ensure all external links are correct
	checkLinks();
}



// JavaScript Document
/*
Modifications:
2/02/2009	Added a redirect for hap and snac. Remove 3/01/2009.
*/
function Homepage(){
// in real bits, urls get returned to our script like this:
// res://shdocvw.dll/http_404.htm#http://www.DocURL.com/bar.htm 

	//For testing use DocURL = "res://shdocvw.dll/http_404.htm#https://www.microsoft.com/bar.htm"
	DocURL = document.URL;
		
	//this is where the http or https will be, as found by searching for :// but skipping the res://
	protocolIndex=DocURL.indexOf("://",4);
	
	//this finds the ending slash for the domain server 
	serverIndex=DocURL.indexOf("/",protocolIndex + 3);

		//for the href, we need a valid URL to the domain. We search for the # symbol to find the begining 
	//of the true URL, and add 1 to skip it - this is the BeginURL value. We use serverIndex as the end marker.
	//urlresult=DocURL.substring(protocolIndex - 4,serverIndex);
	BeginURL=DocURL.indexOf("#",1) + 1;
	
	urlresult=DocURL.substring(BeginURL,serverIndex);
				
	//for display, we need to skip after http://, and go to the next slash
	displayresult=DocURL.substring(protocolIndex + 3 ,serverIndex);

	InsertElementAnchor(urlresult, displayresult);
}

function HtmlEncode(text)
{
    return text.replace(/&/g, '&amp').replace(/'/g, '&quot;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
}

function TagAttrib(name, value)
{
    return ' '+name+'="'+HtmlEncode(value)+'"';
}

function PrintTag(tagName, needCloseTag, attrib, inner){
    document.write( '<' + tagName + attrib + '>' + HtmlEncode(inner) );
    if (needCloseTag) document.write( '</' + tagName +'>' );
}

function URI(href)
{
    IEVer = window.navigator.appVersion;
    IEVer = IEVer.substr( IEVer.indexOf('MSIE') + 5, 3 );

    return (IEVer.charAt(1)=='.' && IEVer >= '5.5') ?
        encodeURI(href) :
        escape(href).replace(/%3A/g, ':').replace(/%3B/g, ';');
}

function InsertElementAnchor(href, text)
{
    PrintTag('A', true, TagAttrib('HREF', URI(href)), text);
}

function createpossibles() {
	  	var amatch = 'none';
		
		var matchlink = [
		'http://www.nsa.gov/selinux/index.cfm',
		'http://www.nsa.gov/selinux/',
		'http://www.nsa.gov/selinux',
		'http://www.nsa.gov/selinux/index.shtml',
		'http://www.nsa.gov/selinux/news.cfm',
		'http://www.nsa.gov/selinux/info/faq.cfm',
		'http://www.nsa.gov/selinux/info/docs.cfm',
		'http://www.nsa.gov/selinux/info/developer.cfm',
		'http://www.nsa.gov/selinux/info/list.cfm',
		'http://www.nsa.gov/selinux/info/contrib.cfm',
		'http://www.nsa.gov/selinux/info/related.cfm',
		'http://www.nsa.gov/snac/',
		'http://www.nsa.gov/snac',
		'http://www.nsa.gov/snac/index.cfm',
		'http://www.nsa.gov/snac/index.shtml',
		'http://www.nsa.gov/hap/',
		'http://www.nsa.gov/hap',
		'http://www.nsa.gov/hap/index.cfm',
		'http://www.nsa.gov/hap/index.shtml',
		'http://www.nsa.gov/ia/events/index.cfm'
		];
		var alternatelink = [
		'http://www.nsa.gov/research/selinux/index.shtml',
		'http://www.nsa.gov/research/selinux/index.shtml',
		'http://www.nsa.gov/research/selinux/index.shtml',
		'http://www.nsa.gov/research/selinux/index.shtml',
		'http://www.nsa.gov/research/selinux/news.shtml',
		'http://www.nsa.gov/research/selinux/faqs.shtml',
		'http://www.nsa.gov/research/selinux/docs.shtml',
		'http://www.nsa.gov/research/selinux/developer.shtml',
		'http://www.nsa.gov/research/selinux/list.shtml',
		'http://www.nsa.gov/research/selinux/contrib.shtml',
		'http://www.nsa.gov/research/selinux/related.shtml',
		'http://www.nsa.gov/ia/guidance/security_configuration_guides/index.shtml',
		'http://www.nsa.gov/ia/guidance/security_configuration_guides/index.shtml',
		'http://www.nsa.gov/ia/guidance/security_configuration_guides/index.shtml',
		'http://www.nsa.gov/ia/guidance/security_configuration_guides/index.shtml',
		'http://www.nsa.gov/ia/programs/h_a_p/index.shtml',
		'http://www.nsa.gov/ia/programs/h_a_p/index.shtml',
		'http://www.nsa.gov/ia/programs/h_a_p/index.shtml',
		'http://www.nsa.gov/ia/programs/h_a_p/index.shtml',
		'http://www.nsa.gov/ia/events/index.shtml'
		];
		
	for (var i=0; i<matchlink.length; i++) {
		if (matchlink[i] == document.URL) {
			if (matchlink[i].indexOf('hap') != -1 || matchlink[i].indexOf('snac') != -1) 
			{
				window.location.href=alternatelink[i];
			}
		amatch = alternatelink[i];
		break;
		}
	}
	 if (amatch != 'none') {
	  	document.write('<LI>');
	  	document.write('The page you have entered has moved. It can now be found at: <BR> <a href="' + amatch + '">' + amatch + '</a>');
	  	document.write('</LI>');
			
	 }
};

function placehomepagelink() {
	  if (!((window.navigator.userAgent.indexOf("MSIE") > 0) && (window.navigator.appVersion.charAt(0) == "2")))
	  { 
	  	Homepage();
	  }	
};

/************************************************
	function: 	clientSideInclude
	parameters:	id, url
	purpose:	pull in any page
*************************************************/
function clientSideInclude(id, url) {
  var req = false;
  // For Safari, Firefox, and other non-MS browsers
  if (window.XMLHttpRequest) {
    try {
      req = new XMLHttpRequest();
    } catch (e) {
      req = false;
    }
  } else if (window.ActiveXObject) {
    // For Internet Explorer on Windows
    try {
      req = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        req = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e) {
        req = false;
      }
    }
  }
 var element = document.getElementById(id);
 if (!element) {
  alert("Bad id " + id +
   "passed to clientSideInclude." +
   "You need a div or span element " +
   "with this id in your page.");
  return;
 }
  if (req) {
    // Synchronous request, wait till we have it all
    req.open('GET', url, false);
    req.send(null);
    element.innerHTML = req.responseText;
  } else {
    element.innerHTML =
   "Sorry, your browser does not support " +
      "XMLHTTPRequest objects. This page requires " +
      "Internet Explorer 5 or better for Windows, " +
      "or Firefox for any system, or Safari. Other " +
      "compatible browsers may also exist.";
  }
}
