//
// ajax include with target div
//
function ajaxinclude(url,target)
	{
    // native XMLHttpRequest object
    document.getElementById(target).innerHTML = 'sending...';
    if (window.XMLHttpRequest)
		{
        req = new XMLHttpRequest();
        req.onreadystatechange = function() {ajaxincludeDone(target);};
        req.open("GET", url, false);
        req.send(null);
    // IE/Windows ActiveX version
    	}
	else if (window.ActiveXObject)
		{
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req)
			{
            req.onreadystatechange = function() {ajaxincludeDone(target);};
            req.open("GET", url, false);
            req.send();
	        }
	    }
	}

function ajaxincludeDone(target)
	{
    // only if req is "loaded"
    if (req.readyState == 4)
		{
        // only if "OK"
        if (req.status == 200)
			{
            results = req.responseText;
            document.getElementById(target).innerHTML = results;
        	}
		else
			{
            document.getElementById(target).innerHTML="ajaxinclude error:\n" +
                req.statusText;
        	}
	    }
	}

//
// get all elements that use specified class name
//
document.getElementsByClassName = function(cl)
	{
	var retnode = [];
	var myclass = new RegExp('\\b'+cl+'\\b');
	var elem = this.getElementsByTagName('*');
	for (var i = 0; i < elem.length; i++)
		{
		var classes = elem[i].className;
		if (myclass.test(classes)) retnode.push(elem[i]);
		}
	return retnode;
	}

//
// show topic and question if id's are passed in using URL parameters
//
function getParameter(name) // parse URL parameters
	{
	var url = window.location.href;
	var paramsStart = url.indexOf("?");
	if(paramsStart != -1)
		{
		var paramString = url.substr(paramsStart + 1);
		var tokenStart = paramString.indexOf(name);
		if(tokenStart != -1)
			{
			paramToEnd = paramString.substr(tokenStart + name.length + 1);
			var delimiterPos = paramToEnd.indexOf("&");
			if(delimiterPos == -1)
				{
				return paramToEnd;
				}
			else
		 		{
				return paramToEnd.substr(0, delimiterPos);
				}
			}
		}
	}

function initializeTopic(tvalue, qvalue) // expand layers for topic and question bassed on URL parameters
{
     // alert("initializeTopic for " + tvalue +" and "+qvalue);
	var topic=tvalue; // get topic ID from URL param
      var question=qvalue; // get question ID from URL param
	
      if(topic==undefined || topic ==null){ return;}
	document.topicForm.topic.value = topic;
	if(question == undefined || question ==null)         // expand layers for topic only
	{        
	    toggleTopic(topic,'topic');		
	}
	else                                                 //  expand layers for topic and question
	{   //alert("expand layers for topic and question");
	    toggleTopic(topic,'topic');
          toggleLayer('sav_a_' + qvalue,'sav_q_' + qvalue);
	}
}


//
// toggle visibility of all elements using specified class
//
function toggleTopic(whichLayer,searchClass)
	{
	var display = document.getElementsByClassName(searchClass);
  	for (i = 0; i < display.length; i++)
		{
		display[i].style.display = "none";
		}

	if (document.getElementById)
		{
// this is the way the standards work
		var topic = document.getElementById(whichLayer).style;
		}
	else if (document.all)
		{
// this is the way old msie versions work
		var topic = document.all[whichLayer].style;
		}
	else if (document.layers)
		{
// this is the way nn4 works
		var topic = document.layers[whichLayer].style;
		}
	topic.display = "block";
	}

//
// toggle properties of element that uses specified id name
//
function toggleLayer(whichLayer,whichLink)
	{
	if (document.getElementById)
		{
// this is the way the standards work
		var answer = document.getElementById(whichLayer).style;
		var question = document.getElementById(whichLink).style;
		answer.display = answer.display? "":"block";
		question.fontWeight = question.fontWeight? "":"bold";
		question.backgroundImage = question.backgroundImage? "":"url('/ppdocs/us/cns/images/minusbottom.gif')";
		}
	else if (document.all)
		{
// this is the way old msie versions work
		var answer = document.all[whichLayer].style;
		var question = document.all[whichLink].style;
		answer.display = answer.display? "":"block";
		question.fontWeight = question.fontWeight? "":"bold";
		question.backgroundImage = question.backgroundImage? "":"url('/ppdocs/us/cns/images/minusbottom.gif')";
		}
	else if (document.layers)
		{
// this is the way nn4 works
		var answer = document.layers[whichLayer].style;
		var question = document.layers[whichLink].style;
		answer.display = answer.display? "":"block";
		question.fontWeight = question.fontWeight? "":"bold";
		question.backgroundImage = question.backgroundImage? "":"url('/ppdocs/us/cns/images/minusbottom.gif')";
		}
	}

//
// show all elements using specified class
//
function showAnswers()
	{
	var answer = document.getElementsByClassName('answer');
	var question = document.getElementsByClassName('question');
	for (i = 0; i < answer.length; i++)
		{
		answer[i].style.display = "block";
		question[i].style.fontWeight = "bold";
		question[i].style.backgroundImage = "url('/ppdocs/us/cns/images/minusbottom.gif')";
		}
	}

//
// hide all elements using specified class
//
function hideAnswers()
	{
	var answer = document.getElementsByClassName('answer');
	var question = document.getElementsByClassName('question');
	for (i = 0; i < answer.length; i++)
		{
		answer[i].style.display = "";
		question[i].style.fontWeight = "";
		question[i].style.backgroundImage = "";
		}
	}

//
// hide all elements using specified class
//
function toggleUnderline(showLayer, hideLayer)
	{
	if (document.getElementById)
		{
// this is the way the standards work
		var showLayer = document.getElementById(showLayer).style;
		var hideLayer = document.getElementById(hideLayer).style;
		}
	else if (document.all)
		{
// this is the way old msie versions work
		var showLayer = document.all[showLayer].style;
		var hideLayer = document.all[hideLayer].style;
		}
	else if (document.layers)
		{
// this is the way nn4 works
		var showLayer = document.layers[showLayer].style;
		var hideLayer = document.layers[hideLayer].style;
		}
	showLayer.textDecoration = "none";
	hideLayer.textDecoration = "";
	}
