//'################################################################################
//' Project	: In2Net (Internet Phase autumn 2002 - spring 2003
//'################################################################################
//' Filename	: outline.js
//' Version	: 1.1
//' Path	: *
//' Created By	: Rune Ødegård
//' Date Created: 13.12.2002
//'--------------------------------------------------------------------------------
//' Modified Date : 13.01.03
//' Modified By	: Knut Hamang, BEKK
//' Changes Made: - Added functionality for more than one outline pr page (3D Array).
//'--------------------------------------------------------------------------------
//' Revised by	: 
//' Revised Date: 
//' Changes Made: 
//'--------------------------------------------------------------------------------
//' Module Type	:  Javascript Include file
//'--------------------------------------------------------------------------------
//' Functionality:
//'---------------- 
//' Common code to be referenced in the head section of every page which uses collapsible
//' outlines
//'--------------------------------------------------------------------------------
//' To-do	:
//'---------------- 
//' 
//'--------------------------------------------------------------------------------
//' Declared Vars :
//'---------------- 
//'
//'################################################################################

// Extending the code
// This array will hold the parent name and the status of collapsed
// expanded. It is an 3D Array, typical OutlineArray[InBetweenDivTag][i][StaticIndex]
// (defined in runInitExpandible function)
// The Array contains the following indexes:
// - InBetweenDivTag = the parent div for the whole outline. Datatype = string, thus a hashtable (hasharray)
// - i = dynamic index. Typical count of div tags between the parent div. Datatype = int
// - StaticIndex = Usually 0, 1 or 2, where 0 = ParentName of divs id , 1 = Collapsed status, 2 = ChildName of divs id.

// Typical html code with two div categories (two outlines on same page)
// Outline 1 is Maritime, and outline 2 is Consulting

/*

This outline only has one clickable div
<a onClick="changeAll('Maritime','div','ChildFaq_Maritime','AllSearch_Maritime')">Expand all! (Maritime)</a>
<div id="Maritime">
	<div id="ParentFaq_Maritime 1" onClick="javascript:hideshow('Maritime', 'ChildFaq_Maritime 1','ParentFaq_Maritime 1', '1');">
		Click me to expand...
	</div>
	<div id="ChildFaq_Maritime 1">
		Collapsed HTML, which is expanded when above div is clicked.
	</div>
</div>

This outline has two clickable divs
<a onClick="changeAll('Consulting','div','ChildFaq_Consulting','AllSearch_Consulting')">Expand all! (Consulting)</a>
<div id = "Consulting">
	<div id="ParentFaq_Consulting 1" onClick="javascript:hideshow('Consulting', 'ChildFaq_Consulting 1','ParentFaq_Consulting 1', '1');">
		Click me to expand...
	</div>
	<div id="ChildFaq_Consulting 1">
		Collapsed HTML, which is expanded when above div is clicked.
	</div>
	
	<div id="ParentFaq_Consulting 2" onClick="javascript:hideshow('Consulting', 'ChildFaq_Consulting 2','ParentFaq_Consulting 2', '2');">
		Click me to expand...
	</div>
	<div id="ChildFaq_Consulting 2">
		Collapsed HTML, which is expanded when above div is clicked.
	</div>
</div>

*/


var OutlineArray = new Array ();
/*
var imagepath = '/images/'
var collapsedImage = 'plus_minus_icon_15x9_down.gif'
var expandedImage = 'plus_minus_icon_15x9_up.gif'
*/

/*
var imagepath = ''
var collapsedImage = ''
var expandedImage = ''
*/

var ToggleCollapse = 0 

// This function count all the html tags specified in the 
// TagToNumerate input parameter which exists within a specified
// DIV tag given the id="InBetweenDivTag", id can be any valid textstring
// but has to be given as a input parameter to this function

function outlineNumerate (InBetweenDivTag,TagToNumerate){
	//alert(InBetweenDivTag + "," + TagToNumerate)
    var sObj = document.getElementById(InBetweenDivTag)
	var tag = sObj.getElementsByTagName(TagToNumerate)
	var lengde = tag.length;
	return (lengde);
	
}


//13.01.03 Added InBetweenDivTag parameter

function ChangeImageSource (InBetweenDivTag, ImageId, ArrayElement) {
	
	var ThisOutlineStatus = OutlineArray[InBetweenDivTag][ArrayElement][1]
	if (ThisOutlineStatus == 1) {
		var strText = document.images[ImageId].src
		var newstr = strText.replace(collapsedImage,expandedImage) 		
		document.images[ImageId].src = newstr	
		OutlineArray[InBetweenDivTag][ArrayElement][1] = 0
		}
	if (ThisOutlineStatus == 0) {	
		var strText = document.images[ImageId].src
		var newstr = strText.replace(expandedImage, collapsedImage) 		
		document.images[ImageId].src = newstr
		OutlineArray[InBetweenDivTag][ArrayElement][1] = 1
		}
		
}	

//23.01.03 : Changed functionality and signature!
function ChangeImageSourceRaw (ImageId, existingImageSrc, newImageSrc ) {		
	var strText = document.images[ImageId].src
	var newstr = strText.replace(newImageSrc, existingImageSrc )
	document.images[ImageId].src = newstr	
}	

// This function expands/collapses the Child specified in the input parameters
// 13.01.03 : Not in use anymore!
/*
function initExpandible(InBetweenDivTag, ChildName, numChildren){
	hideshow(InBetweenDivTag, ChildName + ' ' +  numChildren)
}
*/


//begin IE 4+ And NS6 dHTML Outlines

// 13.01.03 Added InbetweenDivTag parameter for 3D Array compability
function hideshow(InBetweenDivTag, which, ImageId, ElementIndex){
    //alert(ImageId);
	// If the ElementIndex is not given then assume it is the same as the
	// number in the which parameter
	if (ElementIndex == null ) {	
		var StringSplit = which.split(" ")
		ElementIndex = StringSplit[1]
		// below added 18.12.03
		
	}
	ElementIndex = ElementIndex - 1
	
	if (!document.getElementById | document.all){
		return
	}
	else
	{
		if (document.getElementById){
			oWhich = eval ("document.getElementById('" + which + "')")
		}
		else{
			oWhich = eval ("document.all." + which)
		}
	}

	window.focus()
    if (ImageId != "none")
    {
        if (ImageId != null){
		    ChangeImageSource (InBetweenDivTag, ImageId, ElementIndex)
		}		
    }
    
	if (oWhich.style.display=="none"){
		oWhich.style.display=""		
	}
	else{
		oWhich.style.display="none"	
	}	    
}


function hide(which){
if (!document.getElementById|document.all){
	return
	}
else{
	if (document.getElementById){
		oWhich = eval ("document.getElementById('" + which + "')")
		}
	else{
		oWhich = eval ("document.all." + which)
		}
	}
	window.focus()
	oWhich.style.display="none"
}

function show(which){
if (!document.getElementById|document.all){
	return
	}
else{
	if (document.getElementById){
		oWhich = eval ("document.getElementById('" + which + "')")
		}
	else{
		oWhich = eval ("document.all." + which)
		}
	}
	window.focus()
	oWhich.style.display=""

}
//end IE 4+ And NS6 dHTML Outlines




// This function does the initialization of the outlines and makes them collapsed as default
// this function is called imediately after the ending </DIV> tag for the outline area "InBetweenDivTag"
// In the html source.
// It also puts the Parent's Div ID tag into an array
      // This call has the folloving Syntax
      // runInitExpandible (InBetweenDivTag,TagToNumerate,ChildName, ParentName, ParentImageId)
      // InBetweenDivTag - The Tag in which between the start and ending tag the outline is contained
      // TagToNumerate -The tag which is going to be counted
      // ChildName - The id of the child which will be counted
      // ParentName - The id of tha parent
      // ParentImageId - The id of the parent image 
function runInitExpandible (InBetweenDivTag,TagToNumerate,ChildName, ParentName, ParentImageId){
	//alert(ParentImageId);
    var numChildren = outlineNumerate (InBetweenDivTag,TagToNumerate)
	var strDebug = ""	;
	numChildren = numChildren / 2
	if (ParentImageId == null)
		ParentImageId = 1
	

	// Added 13.01.03
	// below line is to add content to the 3D Array.
	// OutlineArray[InBetweenDivTag] is a hashtable as InBetweenDivTag is a string
	OutlineArray[InBetweenDivTag] = new Array(numChildren);
	
	for (var i=1; i <= numChildren; i++) {
		OutlineArray[InBetweenDivTag][i-1] = new Array (3)
		OutlineArray[InBetweenDivTag][i-1][0] = ParentName + " " + i
		OutlineArray[InBetweenDivTag][i-1][1] = 0 // 0= collapsed, 1= expanded
		if (ParentImageId == "none")
        {
            OutlineArray[InBetweenDivTag][i-1][2] = ParentImageId;
        }
        else
        {
            OutlineArray[InBetweenDivTag][i-1][2] = ParentImageId + " " + i;
        }
		hideshow(InBetweenDivTag, ChildName + " " +  i,OutlineArray[InBetweenDivTag][i-1][2], i)
		strDebug += ParentImageId + " " + i;
	}	
	//alert(strDebug + "\n" + numChildren );
}


// This function toggles the status of all the outlines to be either expanded
// or collapsed depending on the previous status of ToggleCollapse. 
// It also changes the arrow image if it is specified as an input value (not mandatory).
// Input parameters :
// InBetweenDivTag - The DIV tag which all the outlines are contained within
// TagToNumerate - The html Tag which contains the Child
// ChildName - The ID of the above html tag
// ImageName - optional, If this is set to the ID of an image the image will
//             have it's source name changed each time the changeAll function is run,
//             the name will toggle between imagename_down and imagename_up
//             if the image does not have a name on the form something_up or _down
//             nothing will be done to it.

function changeAll (InBetweenDivTag,TagToNumerate,ChildName,ImageName){
	if (ToggleCollapse == 0) {
		ToggleCollapse = 1
	}
	else {
		ToggleCollapse = 0
	}
	
	// 13.01.03: Added 3D Array compability
	var ArraySize = OutlineArray[InBetweenDivTag].length;

	if (ToggleCollapse) {		
		for (var i=1; i <= ArraySize ; i++) {
			Childindex = i
			OutlineArray[InBetweenDivTag][i-1][1] = 1
			show(ChildName + ' ' +  Childindex)
			
			//Change the image for each children being expanded
			ChangeImageSource (InBetweenDivTag, OutlineArray[InBetweenDivTag][i-1][2], i-1)
		}

		if (ImageName != null){
			var myString = document.images[ImageName].src
			var pattern = new RegExp (collapsedImage,'ig')
			var result = myString.match(pattern)
			if (result) {
				// Replace image source for the link that initiated this function.
				var re = new RegExp (collapsedImage, 'gi') 
				var newstr = myString.replace(re, expandedImage) 
				document.images[ImageName].src = newstr;			
			}
			else {
   				// Do Nothing	
			}
		}
		
	}
	else
	{
		for (var i=1; i <= ArraySize ; i++) {
			Childindex = i
			OutlineArray[InBetweenDivTag][i-1][1] = 0
			hide(ChildName + ' ' +  Childindex)			
			//Change the image for each children being expanded
			ChangeImageSource (InBetweenDivTag, OutlineArray[InBetweenDivTag][i-1][2], i-1)
			}


		if (ImageName != null){		
			// Replace image sourcefor the link that initiated this function.
			var myString = document.images[ImageName].src
			var pattern = new RegExp (expandedImage,'ig')
			var result = myString.match(pattern)
			if (result) {
				// Replace image source for the link that initiated this function.
				var re = new RegExp (expandedImage, 'gi') 
				var newstr = myString.replace(re, collapsedImage) 
				document.images[ImageName].src = newstr;				
			}
			else {
   				// Do Nothing	
			}
		}
	}	
}