/**********************************************************************
	Javascript functions for Pane Help
	Author: Chris Sanders (csand), 1999-2003
	
	Note: Many functions are designed for PROC, PH_MEGA, TROU topics
	which have subtopic_choice radio buttons and/or supertopic_choice
	radio buttons.
	
	Note: These function are designed to work for IE4+ and Nav6 browsers.
	
	Note: Cannot use less-than or greater-than symbols
	in the javascript or the XML parser will crash.
**********************************************************************/

// Global variables
var g_strSupertopicChoiceContext = ""
var g_strSubtopicChoiceContext = ""
var g_strCurrentContext = ""
var g_arrContextElements
var g_arrContextRelatedElements
var g_arrArrowImages = new Array('arrowblueright.gif','arrowbluedown.gif')

var g_arrV4

var isIE4 = false
var isNav4 = false
var isNav6 = false

//Create the getV4 function, and then run it.
function getV4()
{
	if ( typeof parent.v4 == 'string' )
	{
		var pv4 = parent.v4;
		g_arrV4 = pv4.split(',');
	}
	else
	{
		g_arrV4 = new Array('');
	}
}
getV4(); //execute the function during page load

//Conditional content test
function ccTest( strValue )
{
	return ( v4Contains(strValue) || userAgentContains(strValue) );
}

function v4Contains( strValue )
{
	if ( typeof strValue == 'string' )
	{
		if ( strValue != "" )
		{
			//Handle possible multiple values
			var arrValues;
			arrValues = strValue.split(',');
			
			for( i = 0; i < g_arrV4.length; i++ )
			{
				for( j = 0; j < arrValues.length; j++ )
				{
					if ( g_arrV4[i] == arrValues[j] ) return true;
				}
			}
		}
	}	
	return false;
}

function userAgentContains( strValue )
{
	if ( typeof strValue == 'string' )
	{
		if ( strValue != "" )
		{
			//Handle possible multiple values
			var arrValues;
			arrValues = strValue.split(',');
			
			for( j = 0; j < arrValues.length; j++ )
			{
				if ( navigator.userAgent.indexOf( arrValues[j] ) != -1) return true;
			}
		}
	}	
	return false;
}

function onPageLoad( strTemplate )
{
	//Even though the page is supposed to be fully loaded before the onLoad event is triggered,
	//Nav6 will return false from the existsForm function unless there is a slight delay before
	//querying for the existence of the form.
	if (isIE4)
	{
		startPage(strTemplate)
	}
	else if (isNav6) 
	{
		var strNext = "startPage('" + strTemplate + "')"
		setTimeout(strNext,0) //Even though the delay is set to 0, this works?!
	}
}

function startPage( strTemplate )
{
	if ((strTemplate == 'PROC') || (strTemplate == 'TROU') || (strTemplate == 'PH_MEGA'))
	{
		if ( existsForm("SubtopicChoiceForm") || existsForm("SupertopicChoiceForm") )
		{
			// set the classNames of the elements that have context
			// or are related ancestors of the context elements
			if ( existsForm("SupertopicChoiceForm") )
			{
				g_arrContextElements = new Array('SUBTOPIC_CHOICE','INSTRUCTIONS','MORE_INFO','LINK')
				g_arrContextRelatedElements = new Array('SUBTOPIC_CHOICES','LINKS')
			}
			else
			{
				g_arrContextElements = new Array('INSTRUCTIONS','MORE_INFO','LINK')
				g_arrContextRelatedElements = new Array('LINKS')
			}
			hideContextContent() //hide any elements that have context
			resetSupertopicChoiceForm() //make sure the buttons are unchecked and the context is nothing
			resetSubtopicChoiceForm() //make sure the buttons are unchecked and the context is nothing
		}
		else
		{
			showContextContent()
		}
	}	
}
function resetSupertopicChoiceForm()
{
 	if ( existsForm("SupertopicChoiceForm") )
 	{
	 	//uncheck all SupertopicChoice radio elements
 		var oRadioGroup = document.forms["SupertopicChoiceForm"].SupertopicChoice
 		for (var i = 0; i < oRadioGroup.length; i++)
 		{
 			oRadioGroup[i].checked = false
 		}
 		//set the supertopic_choice context to nothing
 		setSupertopicChoiceContext("")
 	}
}
function resetSubtopicChoiceForm()
{
 	if ( existsForm("SubtopicChoiceForm") )
 	{
	 	//uncheck all SubtopicChoice radio elements
 		var oRadioGroup = document.forms["SubtopicChoiceForm"].SubtopicChoice
 		for (var i = 0; i < oRadioGroup.length; i++)
 		{
 			oRadioGroup[i].checked = false
 		}
 		//set the subtopic_choice context to nothing
 		setSubtopicChoiceContext("")
 	}
}

function setCurrentContext()
{
	g_strCurrentContext = getSupertopicChoiceContext() + getSubtopicChoiceContext()
}

function setSupertopicChoiceContext( strContext )
{
	g_strSupertopicChoiceContext = strContext
}

function getSupertopicChoiceContext()
{
	return g_strSupertopicChoiceContext
}

function setSubtopicChoiceContext( strContext )
{
	g_strSubtopicChoiceContext = strContext
}

function getSubtopicChoiceContext()
{
	return g_strSubtopicChoiceContext
}

function hasTheContext( strElementContext, strCurrentContext )
{
	// Does this element have the current context

	var arrElementContext = strElementContext.split(",")
	
	for (var i = 0; i < arrElementContext.length; i++)
	{
		if ( (arrElementContext[i] == g_strCurrentContext) || (arrElementContext[i] == g_strSupertopicChoiceContext) )
		{
			return true
		}
	}
	return false
}
function getElementContext( oElement )
{
	//The context is contained in the id attribute
	var strContext = ( oElement.id ) ? oElement.id : ""
	return strContext
}
function clickedSupertopicChoice( strContext )
{
	setSupertopicChoiceContext( strContext ) // remember the context
	hideContextContent()  // first, hide all context content
	resetSubtopicChoiceForm() // then, uncheck any checked subtopic_choices
	showContextContent()  // finally, show all context content dependent on the new context
}
function clickedSubtopicChoice( strContext )
{
	setSubtopicChoiceContext( strContext ) // remember the context
	hideContextContent()  // first, hide all context content
	showContextContent()  // finally, show all context content dependent on the new context
}
function clickedSubtopicChoiceText( strContext, strID )
{
	oSubtopicChoice = getElementById( strID );
	oSubtopicChoice.checked = true;
	clickedSubtopicChoice( strContext );
}
function isContextElement( oElement )
{
	// Is oElement an element that uses the id attribute to indicate the context?
	for (var i=0; i < g_arrContextElements.length; i++)
	{
		if (oElement.className == g_arrContextElements[i])
		{
			return true
		}
	}
	return false
}
function isContextRelatedElement( oElement )
{
	// Is oElement an element whose display depends on a child elements context?
	for (var i=0; i < g_arrContextRelatedElements.length; i++)
	{
		if (oElement.className == g_arrContextRelatedElements[i])
		{
			return true
		}
	}
	return false
}
function showContentForContextElement(oElement,oLinksElement)
{
	if (isContextElement(oElement))
	{
		if (hasTheContext(getElementContext(oElement),g_strCurrentContext))
		{
			if (oElement.className == 'LINK')
			{
				setStylePropertyByElement( oLinksElement, 'display', '' ) // display the related LINKS container
			}
			if (oElement.className == 'SUBTOPIC_CHOICE')
			{
				var oSubtopicChoicesElement = getElementById('SUBTOPIC_CHOICES')
				if (oSubtopicChoicesElement != null)
				{
					setStylePropertyByElement( oSubtopicChoicesElement, 'display', '' ) // display the related LINKS container
				}
			}
			setStylePropertyByElement( oElement, 'display', '' ) // display the element
		}
		else
		{
			setStylePropertyByElement( oElement, 'display', 'none' ) // hide the element
		}
	}
}
function showContextContent()
{
	// Display all elements appropriate for the context
	var oElement
	var oLinksElement
	
	//First, make sure that the current context is up-to-date
	setCurrentContext()
	
	if (isIE4)
	{
		for (var i = 0; i != document.all.length; i++)
		{
			oElement = document.all[i]
			if (oElement.className == 'LINKS') 
			{
				oLinksElement = oElement
			}
			showContentForContextElement(oElement,oLinksElement)
		}
	}
	else if (isNav6)
	{
		var colElements
		//elements with INSTRUCTIONS, MORE_INFO, LINKS className are <div> tags
		colElements = document.getElementsByTagName("div")
		for (var i = 0; i != colElements.length; i++)
		{
			oElement = colElements[i]
			if (oElement.className == 'LINKS') 
			{
				oLinksElement = oElement
			}
			showContentForContextElement(oElement,oLinksElement)
		}
		//elements with LINK className are <a> tags
		colElements = document.getElementsByTagName("a")
		for (var i = 0; i != colElements.length; i++)
		{
			oElement = colElements[i]
			showContentForContextElement(oElement,oLinksElement)
		}
	}
}
function hideContentForContextElement(oElement)
{
	if ( (isContextElement(oElement)) || (isContextRelatedElement(oElement)) )
	{
	//alert(oElement.tagName + ", " + oElement.className)
		setStylePropertyByElement( oElement, 'display', 'none' ) // hide the element
	}
}

function hideContextContent()
{
	// Hide all elements that have context
	var oElement
	
	if (isIE4)
	{
		for (var i = 0; i != document.all.length; i++)
		{
			oElement = document.all[i]
			hideContentForContextElement(oElement)
		}
	}
	else if (isNav6)
	{
		var colElements
		//elements with INSTRUCTIONS, MORE_INFO, LINKS className are <div> tags
		colElements = document.getElementsByTagName("div")
		for (var i = 0; i != colElements.length; i++)
		{
			oElement = colElements[i]
			hideContentForContextElement(oElement)
		}
		//elements with LINK className are <a> tags
		colElements = document.getElementsByTagName("a")
		for (var i = 0; i != colElements.length; i++)
		{
			oElement = colElements[i]
			hideContentForContextElement(oElement)
		}
	}
}

function toggleTips( strTipsID, strTipsImageID )
{
	var oTipsImageElement = getElementById( strTipsImageID )
	
	if (isIE4 || isNav6)
	{
		if ( getStylePropertyById( strTipsID,'display')=='none' )
		{
			setStylePropertyById( strTipsID, 'display', 'block' ) //show the Tips
			if (oTipsImageElement != null)
			{
				//show the down arrow
				oTipsImageElement.src = g_arrArrowImages[1]
			}
		}
		else
		{
			setStylePropertyById( strTipsID, 'display', 'none' ) //hide the Tips
			if (oTipsImageElement != null)
			{
				//show the right arrow
				oTipsImageElement.src = g_arrArrowImages[0]
			}
		}
	}
    else
	{}	//so Nav4 won't return error
}

function takeMeThere( strURL, strWindowName )
{
	window.open( strURL, strWindowName )
}

function toggleALTTOCImg( element, strEvent )
{
	// redirect to the ti function
	ti( element, strEvent )
}

function ti( element, strEvent )
{
	var oElement;
	oElement = getElementObject( element );
	
	if (oElement != null)
	{
		var strSrc = oElement.src;
		var intIndex = strSrc.lastIndexOf("/");
		var strSrcRoot = "";
		var strImgName = strSrc;
		var strNewImgName;
		if (intIndex >= 0) 
		{
			strSrcRoot = strSrc.substring(0,intIndex+1);
			strImgName = strSrc.substring(intIndex+1,strSrc.length);
		}
		strImgName = strImgName.substring(0,strImgName.length - 4);
		switch (strImgName)
		{
			case "question_icon":
				strNewImgName = 'question_icon_hover';
				break;
			case "question_icon_hover":
				strNewImgName = 'question_icon';
				break;
			case "widget_plus":
				if ( strEvent == 'r')
					strNewImgName = 'widget_plus_hvr';
				else if ( strEvent == 't')
					strNewImgName = 'widget_plus';
				else
					strNewImgName = 'widget_minus';
				break;
			case "widget_plus_hvr":
				if ( strEvent == 't')
					strNewImgName = 'widget_plus';
				else
					strNewImgName = 'widget_minus';
				break;
			case "widget_minus":
				if ( strEvent == 'r')
					strNewImgName = 'widget_minus_hvr';
				else if ( strEvent == 't')
					strNewImgName = 'widget_minus';
				else
					strNewImgName = 'widget_plus';
				break;
			case "widget_minus_hvr":
				if ( strEvent == 't')
					strNewImgName = 'widget_minus';
				else
					strNewImgName = 'widget_plus';
				break;
		}
		oElement.src = strSrcRoot + strNewImgName + '.gif';
	}
}

/**********************************************************************
	Generic Cross Browser Code
**********************************************************************/

function setBrowser()
{
	//Simple browser sniffer
	
	if (document.all)
	{
		isIE4 = true
	}
	else if (document.layers)
	{
		isNav4 = true
	}
	else if (document.getElementById)
	{
		isNav6 = true //also true for IE5.5
	}
}

setBrowser(); // determine which browser we are using

function blur( oElement )
{
	oElement.blur()
}

function existsForm( name )
{
    if ( typeof document.forms[name] == "object" )
		return true
	else
		return false
}
function getElementById( strId )
{
    if (isNav6)
    {
        return document.getElementById( strId );
    }
    else if (isIE4)
    {
        return document.all[strId]
    }
    else
	{
		return null
	}
}
function getElementObject( element )
{
	var oElement = null;
	
	// get the element
	if (typeof element == "object")
	{
		oElement = element;
	}
	else if (typeof element == "string")
	{
		oElement = getElementById( element );
	}
	return oElement;	
}
function getStyleBySelector( selector )
{
    if (!isNav6)
    {
        return null;
    }
    var sheetList = document.styleSheets;
    var ruleList;
    var i, j;

    /* look through stylesheets in reverse order that
       they appear in the document */
    for (i=sheetList.length-1; i >= 0; i--)
    {
        ruleList = sheetList[i].cssRules;
        for (j=0; j<ruleList.length; j++)
        {
            if (ruleList[j].type == CSSRule.STYLE_RULE && ruleList[j].selectorText == selector)
            {
                return ruleList[j].style;
            }   
        }
    }
    return null;
}
function getStylePropertyById( strId, strProperty )
{
    if (isNav6)
    {
        var styleObject = document.getElementById( strId );
        if (styleObject != null)
        {
            styleObject = styleObject.style;
            if (styleObject[strProperty])
            {
                return styleObject[ strProperty ];
            }
        }
        styleObject = getStyleBySelector( "#" + strId );
        return (styleObject != null) ?
            styleObject[strProperty] :
            null;
    }
    else if (isIE4)
    {
        return document.all[strId].style[strProperty];
    }
    else
	{
		return ""
	}
}
function setStylePropertyById( strId, strProperty, strValue )
{
    if (isNav6)
    {
        var styleObject = document.getElementById( strId );
        if (styleObject != null)
        {
            styleObject = styleObject.style;
            styleObject[ strProperty ] = strValue;
        }
    }
    else if (isIE4)
    {
		if (document.all[strId] != null)
			document.all[strId].style[strProperty] = strValue;
    }
    else
	{}	//so Nav4 won't return error
}
function setStylePropertyByElement( oElement, strProperty, strValue )
{
    if (isNav6)
    {
        var styleObject = oElement;
        if (styleObject != null)
        {
            styleObject = styleObject.style;
            styleObject[ strProperty ] = strValue;
        }
    }
    else if (isIE4)
    {
		if (oElement != null)
			oElement.style[strProperty] = strValue;
    }
    else
	{}	//so Nav4 won't return error
}
function toggleElementDisplay( element, strStyle )
{
	// strStyle = (none,block,inline)
	var strID
	
	//get the element id
	if (typeof element == "object")
	{
		strID = element.id	
	}
	else if (typeof element == "string")
	{
		strID = element
	}
	
	if ((strID != "") && (strID != null))
	{
		if (isIE4 || isNav6)
		{
			if ( getStylePropertyById( strID,'display')=='none' )
			{
				setStylePropertyById( strID, 'display', strStyle ) //show the element
			}
			else
			{
				setStylePropertyById( strID, 'display', 'none' ) //hide the element
			}
		}
	}
}
function toggleImg( element, strImg1, strImg2 )
{
	var oElement;
	oElement = getElementObject( element );
	
	if (oElement != null)
	{
		var strSrc = oElement.src;
		var dirRoot=strSrc.substring(0,strSrc.lastIndexOf("/")+1);
		
		if ( strSrc.indexOf(strImg1) > -1 )
		{
			oElement.src =dirRoot+ strImg2;
		}
		else
		{
			oElement.src =dirRoot+ strImg1;
		}
	}
}
function changeImg( element, strImg )
{
	var oElement;
	oElement = getElementObject( element );
	
	if (oElement != null)
	{
		var strSrc = oElement.src;
			oElement.src = strImg;
	}
}

/**********************************************************************
	End Generic Cross-Browser Code
**********************************************************************/

/**********************************************************************
  Query String Code
**********************************************************************/

function HasQueryString(strURL)
{
	if (strURL.indexOf("?") > -1) 
		return true
	else
		return false
}

function GetValFromQueryString( strAttribute )
{
	var strURL = document.location + "";
	var strValue = "";
	var intIndex;
	intIndex = strURL.indexOf( strAttribute + "=" ); //find the index of the beginning of the attribute text
	if ( intIndex > -1 ) {
		strValue = strURL.substr( intIndex + strAttribute.length + 1 ) //may contain other query string text after the attribute
		intIndex = strValue.indexOf( "&" ) // check for other query string text
		if ( intIndex > -1 ) {
			strValue = strValue.substr(0,intIndex) // remove "&" plus text that follows
		}
	} else {
		strValue = ""
	}
	return strValue
}

function GetQueryString( strURL )
{
	var strQueryString = ""
	var intIndex = strURL.indexOf("?")
	if ( intIndex > -1 ) {
		strQueryString = strURL.substr( intIndex + 1 ) //get the query string
	}
	return URLDecode( strQueryString ) //return the URLDecoded query string
}

function URLDecode( strEncodedString )
{
	var strDecodedString = ""
	strDecodedString = strEncodedString.replace( /\%20/g , ' ' ) //replace %20 with space

	return strDecodedString
}

/**********************************************************************
	End Query String Code
**********************************************************************/


