/*
 JavaScript Code for the lighting form of the Report a Fault Wizard - 
 Displays and hides particular check boxes, depending on which are checked by the user.
 
 Innovations, Capita Symonds
 January 2007
*/
var lightingArray = null;
var otherBox = null;

function lightingCheck(checkbox)
{
	if (lightingArray == null)
		fillArray();
	if (checkbox.checked)
	{
		hideAllElements();
		showCheckbox(checkbox);
	}
	else
		showAllElements();
}

function hideAllElements()
{
	for (var i = 0; i < lightingArray.length; i++)
	{
		lightingArray[i].style.display='none';
	}
	otherBox.style.display = 'none';
	lightingArray[lightingArray.length-1].style.display='block';
	lightingArray[lightingArray.length-1].style.visibility='hidden';
}

function showAllElements()
{
	for (var i = 0; i < lightingArray.length; i++)
	{
		lightingArray[i].style.display='inline';
	}
	otherBox.style.display = 'none';
	lightingArray[lightingArray.length-1].style.visibility='visible';
}	

function showCheckbox(obj)
{
	if (obj == lightingArray[lightingArray.length - 2]) //if is the other checkbox (assuming other is the last element)
	{
		otherBox.style.display = 'inline';
		lightingArray[lightingArray.length-1].style.visibility = 'visible';
	}
	obj.style.display='inline';
	for (var i = 0; i < lightingArray.length; i++)
	{
		if (lightingArray[i] == obj)
			lightingArray[i+1].style.display="inline"; //show associated label
	}
}

function refreshSelection()
{
	if (lightingArray != null)
	{
		var oneChecked = false;
		var otherBoxChecked = false;
		for (var i = 0; i < lightingArray.length - 1; i++)
		{
			var checkbox = lightingArray[i];
			if (checkbox.checked != null) //is a checkbox
			{
				if (checkbox.checked == true)
				{
					lightingArray[i].style.display="inline";
					lightingArray[i+1].style.display="inline";
					oneChecked = true;
					if (i + 2 >= lightingArray.length)
						otherBoxChecked = true;
				}
				else
				{
					lightingArray[i].style.display="none";
					lightingArray[i+1].style.display="none";
				}
			}
		}
	

		if (otherBoxChecked)
		{
			otherBox.style.display="inline";
			otherBox.style.visibility = "visible";
			lightingArray[lightingArray.length - 1].style.display = "inline";
			lightingArray[lightingArray.length - 1].style.visibility = "visible";
		}
		else
		{
			otherBox.style.display="inline";
			otherBox.style.visibility = "hidden";
			lightingArray[lightingArray.length - 1].style.display = "inline";
			lightingArray[lightingArray.length - 1].style.visibility = "hidden";
			
		}
		
		if (!oneChecked)
		{
			for (var i =0; i < lightingArray.length; i++)
			{
				lightingArray[i].style.display="inline";
			}
			otherBox.style.display="inline";
			otherBox.style.visibility="visible";
			lightingArray[lightingArray.length - 1].style.visibility = "visible";
		}
	}
}

function fillArray()
{
	lightingArray = new Array();
	var counter = 1;
	var element = null;
	while ((element = document.getElementById('Lighting_'+counter)) != null)
	{
		attachHandlerTo(element, "click", refreshSelection);
		lightingArray[lightingArray.length] = element;
		lightingArray[lightingArray.length] = document.getElementById('Lighting_'+counter+'_label');
		counter++;
	}
	otherBox = document.getElementById('Lighting_OtherDescription');
}

function attachHandlerTo(obj, event, funcName)
{
    if (isSet(event) && isSet(funcName))
    {
        try
        {
            if (window.attachEvent)
            {
                obj.attachEvent("on" + event, funcName);
                window.event.cancelBubble = true;
                return true;    
            }
            else if (window.addEventListener)
            {
                obj.addEventListener(event, funcName, true);
                return true;
            }
        }
        catch (ex)
        {
            return false;
        }
    }
    else
        return false;
}

attachHandlerTo(window, "load", fillArray);

function removeHandlerFrom(obj, event, funcName)
{
    if (isSet(event) && isSet(funcName))
    {
        try
        {
            if (window.attachEvent)
            {
                obj.detachEvent("on" + event, funcName);
                return true;
            }
            else if (window.addEventListener)
            {
                obj.removeEventListener(event, funcName, true);
                return true;
            }   
        }
        catch (ex)
        {
            return false;
        }
    }
    return false;
}

function isSet(obj)
{
	return !(obj == null | obj == "");
}

