var isWizard;
var submitButton;
var dhtmlControls;
var formSections;
var formSectionTitles;
var prevSec;
var nextSec;
var activeSection;
var activeSectionIndex
var secCheckboxes;

function RegisterSectionCheckbox(checkboxID, sectionIndex)
{
	if(!secCheckboxes) secCheckboxes = new Array();
	var newObj = { checkbox : document.getElementById(checkboxID), sectionIndex : sectionIndex };
	secCheckboxes.push(newObj);
}

function ShowFormSection(newActiveSectionIndex)
{
	while(!IsAllowedSection(newActiveSectionIndex))
	{
		newActiveSectionIndex++;
	}

	var min = 0;
	var max = (formSections.length - 1);
	
	if(submitButton)
	{
		if(isWizard && newActiveSectionIndex < max)
		{
			submitButton.style.display = 'none';	
		}
		else
		{
			submitButton.style.display = 'inline';
		}
	}
	
	for(var i=0; i<=max; i++)
	{
		var sec = document.getElementById(formSections[i]);
		
		if(submitButton && sec.style.display == 'block' && document.all)
		{
			// VALIDATE CURRENT ACTIVE SECTION IF WE ARE MOVING FORWARDS (WIZARD) OR IN ANY DIRECTION (STANDARD)
			var doValidation = false;
			for(var v=0; v<Page_Validators.length; v++)
			{
				var val = Page_Validators[v];
				
				if(!isWizard || newActiveSectionIndex > i)
				{
					var validateThis = false;
					var p = val.parentNode;
					while(p && !validateThis)
					{
						if(p == sec)
						{
							validateThis = true;
							break;
						}
						p = p.parentNode;
					}
					if(validateThis)
					{
						doValidation = true;
						ValidatorValidate(val);
					}
				}
				else
				{
					doValidation = true;
					val.isvalid = true;
				}
			}
			if(doValidation)
			{					
				ValidatorUpdateIsValid();    
				ValidationSummaryOnSubmit();
				if(!Page_IsValid) return;
			}
		}
		
		if(i == newActiveSectionIndex)
		{
			prevSec = i - 1;
			while(!IsAllowedSection(prevSec))
			{
				if(--prevSec < min)
				{
					prevSec = -1;
					break;
				}
			}
			nextSec = i + 1;
			while(!IsAllowedSection(nextSec))
			{
				if(++nextSec > max)
				{
					nextSec = -1;
					break;
				}
			}
			if (sec != null)
			{
				sec.style.display = 'block';
				activeSection = sec;
			}
			activeSectionIndex = newActiveSectionIndex;
		}
		else
		{
			if (sec != null)
			{
				sec.style.display = 'none';
			}
		}
	}

	for(var c=0; c < dhtmlControls.length; c++)
	{
		var controlsTable = dhtmlControls[c];
		
		if (controlsTable != null)
		{
			controlsTable.style.display = 'block';
			
			var directLinksCell = null;
			var prevLink = null;
			var nextLink = null;
			
			switch(controlsTable.rows[0].cells.length)
			{
				case 1:
				{
					directLinksCell = controlsTable.rows[0].cells[0];
					break;
				}
				case 2:
				{
					prevLink = controlsTable.rows[0].cells[0].childNodes[0];
					nextLink = controlsTable.rows[0].cells[1].childNodes[0];	
					break;
				}
				case 3:
				{
					prevLink = controlsTable.rows[0].cells[0].childNodes[0];
					directLinksCell = controlsTable.rows[0].cells[1];
					nextLink = controlsTable.rows[0].cells[2].childNodes[0];	
					break;
				}
			}
			
			var usesTextLinks = false;
			if(directLinksCell && directLinksCell.childNodes[0].className.indexOf('textLinksContainer') > -1)
			{
				usesTextLinks = true;
				directLinksCell = directLinksCell.childNodes[0];
			}
			
			for(var i=0; i<=max; i++)
			{
				if(i == newActiveSectionIndex)
				{
					if(directLinksCell)
					{
						directLinksCell.childNodes[i].className = 'linkActive';
					}
					if(prevSec > -1)
					{
						SetLink(prevLink, 'Previous', formSectionTitles[prevSec], ShowPrevSection);
					}
					else
					{
						SetEmptyLink(prevLink)
					}
					//Added the second part of the if statement below to stop the Next button being added on the last section of the form.
					//LR 27/03/2009 CAS-05849-XJJNKS
					if(nextSec > -1 && newActiveSectionIndex < max)
					{
						SetLink(nextLink, 'Next', formSectionTitles[nextSec], ShowNextSection);
					}
					else
					{
						SetEmptyLink(nextLink)
					}
				}
				else
				{
					if(directLinksCell)
					{
						if(IsAllowedSection(i))
						{
							directLinksCell.childNodes[i].className = 'linkInactive';
						}
						else
						{
							directLinksCell.childNodes[i].className = 'linkNotAllowed';
						}
					}
				}
			}
			
			if(usesTextLinks)
			{
				TextLinksDropdown(directLinksCell, false);
			}
		}	
	}	
}

function IsAllowedSection(sectionIndex)
{
	if(secCheckboxes)
	{
		for(var i=0; i < secCheckboxes.length; i++)
		{
			if(secCheckboxes[i].sectionIndex == sectionIndex + 1)
			{
				return secCheckboxes[i].checkbox.checked;
			}
		}
	}
	return true;
}

function SetEmptyLink(link)
{
	if(!link) return;
	link.innerHTML = '';
	link.title = '';
	link.onclick = '';
	link.style.display = 'none';
}

function SetLink(link, linkText, linkTitle, func)
{
	if(!link) return;
	link.innerHTML = linkText;
	link.title = linkTitle;
	link.onclick = func;
	link.style.display = 'inline';
}

function ShowNextSection()
{
	ShowFormSection(nextSec);
}

function ShowPrevSection()
{
	ShowFormSection(prevSec);
}

function TextLinksDropdown(div, show)
{
	if(show)
	{
		div.className = 'textLinksContainer textLinksContainerAllVisible';
	}
	else
	{
		div.className = 'textLinksContainer textLinksContainerActiveVisible';
	}
	var toMove = null;
	for(var i=0; i<div.childNodes.length; i++)
	{
		var a = div.childNodes[i];
		var isActive = (a.className == 'linkActive');
		if(show || isActive)
		{
			a.style.display = 'block';
		}
		else
		{
			a.style.display = 'none';
		}
	}
	if(activeSection)
	{
		// HIDE TITLE TO REPLACE WITH DROPDOWN
		activeSection.rows[0].style.display = 'none';
	}
}

function SectionCheckboxClick()
{
	window.setTimeout('ShowFormSection(' + activeSectionIndex + ');', 500);
}