
//Returns an impolded string containing a ; separated list of checked items
function cl_getImploded(name, useAll)
{
	name += '_parent';
	var the_data = '';
    var count = 0;
	var source = document.getElementById(name);
	if (source != null)
	{
		var elements = source.getElementsByTagName('input');
		
		if (elements == null) 
		{
			alert("WARNING: Could not find checklist sub-elements!");
			return "";
		}
	
		for (var i = 0; i < elements.length; i++) {

			var element = elements[i];
			
			//alert(count);
			
			if (element == null) 
			{
				alert("WARNING: Checklist sub-element at " + i + " is null!");
			}
			
			if (element.type == "checkbox" && element.name != null) 
			{
				if (useAll || element.checked || element.value == "1")
				{
					if (count > 0) {
						the_data = the_data + ';';
					}
					the_data = the_data + element.name;
					if (useAll)
					{
						if (element.checked == true)
							the_data += ",1";
						else 
							the_data += ",0";
					}
					count++;
				}
			}
			else
			{
				alert("WARNING: Unexpected sub-element type, or element is null!");
				return "";
			}
		}
		if (the_data != '')
			the_data = the_data + ';'; 
	}
	else
	{
		alert("WARNING: Checklist source is null!");
	}
	
    return the_data;
}

//Implode the CL string into the target
function cl_impodeToTarget(name, useAll)
{
	var target = document.getElementById(name);
	if (target != null)
	{
		target.value = cl_getImploded(name, useAll);
	}
	else
	{
		alert("WARNING: Checklist target is null!");
	}

}

//Set the checkbox state
function cl_setCheckboxState(name, state) {
	name += '_parent';
	var elements = document.getElementById(name).getElementsByTagName('input');
    for (i = 0; i < elements.length; i++) {
        var element = elements[i];
        if (element.type == "checkbox") {
            element.checked = state;
        }
    }
}
