//-------------------------------------------------------------------------------------
// Dynamic Collapsing Content V2.2 - By: Kim A. Kitchen, IntelligentWebsiteDesign.com
// This script locates spans 'cSpan_0', 'cSpan_1', 'cSpan_2', etc... and collapses them
// placing an image and the title (extracted from the beginning of the span). 
//-------------------------------------------------------------------------------------
// Global Arrays and Variables
var Data = new Array;
var Title = new Array; 
var State = new Array;
var Count = 0;
var Collapse_On_Load = true; // Automatically collapses upon Load
// NOTES: TitleStart and TitleEnd denote the Start and End of the Title
// in each section (this must be consistent in all sections). 
// You should modify these to match your specific content. 
// In this example, Our title begins at the 1st '<font>' tag.
var TitleStart = "<font"; 
var TitleEnd = "</font>"; 
var Initialized=false;
var closed_img="images/closed.gif"; // "images/plus.gif";
var opened_img="images/opened.gif"; // "images/minus.gif";

//----------------------------------------------------
// This routine loads the arrays, inserting the [+] and [-]
function LoadArrays() {
	var Name = "", Str = "", i = 0, j = 0;
	var StartStr = "", EndStr = "";
	try {
		StartStr = Form1.cSpan_TitleStart.value;
		EndStr = Form1.cSpan_TitleEnd.value;
	} catch(e) { ; }
	// if these exist in the form, we'll use them
	if (StartStr != "") { TitleStart = StartStr; }
	if (EndStr != "") { TitleEnd = EndStr; }
	
	while (j == 0) { // while there's another Span##
		Name = "cSpan_" + i + "";
		//alert(Name);
		try { 
			var s = document.getElementById(Name);
			//alert(Name+": '"+s.innerHTML+"'");
			if (s.innerHTML != "") {
				Str = "";
				Data[Count] = InsertMinus(s.innerHTML,Count);
				Title[Count] = GetTitle(s.innerHTML,Count);
				State[Count] = 0; // normal (Expanded)
				if (Collapse_On_Load == true) { Tgl(Count); } 
				else { State[Count] = 1;Tgl(Count); } // force update
				Count ++;
			} else { j = 1; } // Hit a blank one, Quit here
		} catch(ex) { j = 1; } // No more so We're Done !
		i++;
	}
	Initialized=true;
}
//----------------------------------------------------
// This routine toggles between Collapsed and Expanded states
function Tgl(index) {
	var Name = "cSpan_" + index + "", Str = "";
	try { 
		var s = document.getElementById(Name); //document.body.all(Name); 
		if (s.innerHTML != "") {
			if (State[index] == 0 ) { // Normal (Expanded), Collapse it.
				// these 2 lines allow modified form vars to be collapsed without losing values
				if (Initialized==true) Data[index] = s.innerHTML;
				else Data[index] = InsertMinus(s.innerHTML,index);
				s.innerHTML = Title[index]; // Replace content with [+] and Title 
				State[index] = 1;  // save this span's new state
			} else { // Already Collapsed, Expand it
				s.innerHTML = Data[index]; // Replace content with [-] and Title 
				State[index] = 0;  // save this span's new state
			}
		} 
	} catch(ex) { ; } 
}
//----------------------------------------------------
// This routine inserts the [-] before the title within the span.
function InsertMinus(MyData, MyIndex) {
	var i = MyData.toLowerCase().indexOf(TitleStart);
	var Str2 = MyData.substring(0,i) + "<a name='#A" + MyIndex + "' "; 
	Str2 = Str2 + "href='#A" + MyIndex + "' onClick='Tgl(" + MyIndex + ");return false'>";
	Str2 = Str2 + "<IMG SRC='" + opened_img + "' border=0>&nbsp;&nbsp;" + MyData.substring(i) + "</a>";
	return(Str2);
}
//----------------------------------------------------
// This routine extracts the Title from the Data. and 
// inserts the [+] before the title within the span.
function GetTitle(MyData, MyIndex) {
	var i = MyData.toLowerCase().indexOf(TitleStart);
	var j = MyData.toLowerCase().indexOf(TitleEnd);
	var MyTitle = MyData.substring(i,(j + TitleEnd.length));
	var Str = "<a name='#A" + MyIndex + "' href='#A" + MyIndex + "' onClick='Tgl(" + MyIndex + ");return false'>";
	Str = Str + "<IMG SRC='" + closed_img + "' border=0>&nbsp;&nbsp;" + MyTitle + "</a>";
	return(Str);
}
//----------------------------------------------------
// This routine expands all collapsed spans: 
// when POST variables are contained in a collapsed region, it must be expanded before posting
function ExpandAll() {
	var i=0, j=0;
	for (i=0;i<State.length;i++) {
		if (State[i]==1) { Tgl(i);j++; }
	}
	//alert("" + j + " Sections Expanded !");
}
//----------------------------------------------------

// - - - - - - - - - - - - - - - - - - - - - - - - - - 
// the onLoad Function 
function PageLoad() {
	// alert("PageLoad()");
	LoadArrays(); // Process Collapsable Content (cSpan_#)s
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - 
