/*
	InspiroForm form provider
	Copyright (c) 2008, Inspiro / Saraden Technologies Limited.
	All rights reserved
*/


/*
*** Control: CheckList
*/
function CheckList(listID)
{
	//Properties
	this.options = new Array();
	this.id = listID;
	
	//Methods
	this.Clear = function()
	{
		this.options = new Array();
	}
	
	this.Update = function()
	{
		var fieldset = document.getElementById(this.id);
		var name = fieldset.getAttribute("name");
		
		var script = fieldset.getAttribute("OnCheckClick");
		
		var i;
		for (i = 0; i < this.options.length; i++)
		{
			var container = document.getElementById(this.id+"_option"+i);
			var changetype;
			if (this.options[i].sourceDiv==null || this.options[i].sourceDiv!=container) changetype=1;
			else changetype = this.options[i].GetChangeType();
			if (changetype==0) continue;
			var checkbox = document.getElementById(this.id+"_option"+i+"check")
			var label = document.getElementById(this.id+"_option"+i+"label")
			if (container==null)
			{
				container = document.createElement("div");
				container.id = this.id+"_option"+i;
				fieldset.appendChild(container);
				
				checkbox = document.createElement("input");
				checkbox.type = "checkbox";
				checkbox.id = container.id+"check";
				checkbox.name = name;
				if (script!=null) eval ("checkbox.onclick=function(){"+script+"};");
				container.appendChild(checkbox);
				
				label = document.createElement("label");
				label.htmlFor = checkbox.id;
				label.id = container.id+"label";
				container.appendChild(label);
			}
			if (changetype == 1)
			{
				if (this.options[i].highlight) container.className="CheckListHighlight";
				else container.className="";
				checkbox.value = this.options[i].value;
				label.innerHTML = this.options[i].text;
			}
			else
				checkbox.checked = this.options[i].selected;
		}
		
		var removeOpt;
		while ((removeOpt = document.getElementById(this.id+"_option"+i))!=null)
		{
			fieldset.removeChild(removeOpt);
			i++;
		}
	}
	
	//Initilise
	this.cotr = function()
	{
		var fieldset = document.getElementById(this.id);
		for (var i = 0; i < fieldset.childNodes.length; i++)
			if (fieldset.childNodes[i].tagName && fieldset.childNodes[i].tagName.toUpperCase()=="DIV")
			{
				var forid = fieldset.childNodes[i].id;
				var checkbox = document.getElementById(forid+"check");
				var label = document.getElementById(forid+"label");
				
				var opt = new CheckListOption(label.innerHTML, checkbox.value);
				opt.selected = checkbox.checked;
				opt.sourceDiv = fieldset.childNodes[i];
				opt.sourceCheck = checkbox;
				opt.sourceLabel = label;
				
				this.options[this.options.length] = opt;
			}
	}
	this.cotr();
}

//Checklist option object
function CheckListOption(opttext, optvalue)
{
	this.value = typeof(optvalue)=="undefined"?opttext:optvalue;
	this.text = opttext;
	this.selected = false;
	this.sourceDiv = null;
	this.sourceLabel = null;
	this.sourceCheck = null;
	this.highlight = false;
	
	this.lastValues = {value:this.value,text:this.text,selected:this.selected,highlight:this.highlight};
	this.GetChangeType = function() //0=none,1=all,2=check-only
	{
		if (this.lastValues.value != this.value) return 1;
		if (this.lastValues.text != this.text) return 1;
		if (this.lastValues.highlight != this.highlight) return 1;
		if (this.lastValues.selected != this.selected) return 2;
		return 0;
	}
}