var xMultiCheck = function ()
{
	this.vDataFieldName = 'name';
	this.vDataFieldId = 'id';
	this.vDataFieldChild = 'child';

	this.vControlClassName = 'xMultiCheck_';
	this.vPostElementName = 'multi_check';
	this.vPostElementArray = [];

	this.vWidth = 150;
	this.vHeight = 200;
	this.vPicWidth = 18;

//	this.vLevelIndent = '\u00A0\u00A0';
	this.vLevelIndent = '\u00A0\u00A0';
//	this.vLevelIndentPic = this.vPicPath + '../tree/tree_vertline.gif';

	this.vPostData		= [];
	this.vCheckedArray	= [];

	this.vMaxCheck = 3;
	this.vCheckedCount = 0;

	this.vCheckBoxPic			= this.vPicPath + '../checkbox.gif';
	this.vCheckBoxCheckedPic	= this.vPicPath + '../checkbox_checked.gif';

	this.tAlreadyMaxChecked = 'Max ' + this.vMaxCheck + ' category allowed.';
}

xMultiCheck.prototype = new xAjax;

xMultiCheck.prototype.fInit = function ()
{
	if (!this.vIdElement && this.vId)
	{
		this.vIdElement = document.getElementById(this.vId);
	}
	if (!this.vIdElement)
	{
		this.vError = 'Element `' + this.vId + '` not found in DOM';
		this.fError();
		return;
	}

	if (!this.vDataUrl)
	{
		this.vError = 'vDataUrl is empty!';
		this.fError();
		return;
	}

	this.vControlId = this.vId + Math.random();

	this.fGetData();
}

xMultiCheck.prototype.fGetData = function ()
{
	this.fLockControl();
	this.AJAXPostData(this.vDataUrl, this.vPostData);
}

xMultiCheck.prototype.fCleanContent = function ()
{
	if (this.vControlElement)
		this.vControlElement.parentNode.removeChild(this.vControlElement);

	if (this.vControlPathElement)
		this.vControlPathElement.parentNode.removeChild(this.vControlPathElement);
}

xMultiCheck.prototype.fShow = function ()
{
	var tDiv, tTable, tTbody, tTr, tTd, tInput, ti, tImg, tText;

	var p = this;

	this.vControlLocked = false;
	this.fCleanContent();

	tDiv = document.createElement('DIV');
	tDiv.className = this.vControlClassName + 'main_div';
	tDiv.style.width = this.vWidth;
	tDiv.style.height = this.vHeight;

	tTable = document.createElement('TABLE');
	tTable.border = 1;
	tTbody = document.createElement('TBODY');

	this.vCheckedCount = 0;
	for (var i in this.vCheckedArray)
	{
		this.vCheckedCount++;
	}

	this.fRecurseFill(tTbody, this.vData, '', 0);

	tTable.appendChild(tTbody);

	
	tDiv.appendChild(tTable);
	this.vIdElement.innerHTML = '';
	this.vIdElement.appendChild(tDiv);

	this.vControlElement = tTable;
}

xMultiCheck.prototype.fRecurseFill = function ()
{
	var tTr, tTd, ti, tImg, tText;

	var to = arguments[0];
	var data = arguments[1];
	var tSel = arguments[2];
	var level = arguments[3];

	var p = this;

	for (var i in data)
	{
		tTr = document.createElement('TR');
		tTd = document.createElement('TD');
		tTd.width = this.vPicWidth;

		tImg = document.createElement('IMG');
		tImg.src = (this.vCheckedArray[i]?this.vCheckBoxCheckedPic:this.vCheckBoxPic);
		tImg.setAttribute('check_id', i);
		tImg.checked = (this.vCheckedArray[i]?true:false);
		tImg.onclick = function ()
		{
			if (p.vMaxCheck > 0 && (p.vCheckedCount == p.vMaxCheck) && !this.checked)
			{
				alert(p.tAlreadyMaxChecked);
			}
			else
			{
				this.checked = !this.checked;
				this.src = (this.checked?p.vCheckBoxCheckedPic:p.vCheckBoxPic);
				if (this.checked)
				{
					p.vCheckedArray[this.getAttribute('check_id')] = 1;
					p.vCheckedCount++;
				}
				else
				{
					delete p.vCheckedArray[this.getAttribute('check_id')];
					p.vCheckedCount--;
				}
			}
		}

		tTd.appendChild(tImg);
		tTr.appendChild(tTd);

		tTd = document.createElement('TD');

		ti = '';

		if (this.vLevelIndentPic)
		{
			for (var k=0; k<level; k++)
			{
				tImg = document.createElement('IMG');
				tImg.src = this.vLevelIndentPic;
				tImg.align = 'middle';
				tTd.appendChild(tImg);
			}
			tText = document.createTextNode(data[i][this.vDataFieldName]);
		}
		else
		{
			for (var k=0; k<level; k++)
			{
				ti = ti + this.vLevelIndent;
			}
			tText = document.createTextNode(ti + data[i][this.vDataFieldName]);
		}
		tTd.appendChild(tText);

		tTd.onclick = function ()
		{
			var tt = this.previousSibling.childNodes[0];

			if (p.vMaxCheck > 0 && (p.vCheckedCount == p.vMaxCheck) && !tt.checked)
			{
				alert(p.tAlreadyMaxChecked);
			}
			else
			{
				tt.checked = !tt.checked;
				tt.src = (tt.checked?p.vCheckBoxCheckedPic:p.vCheckBoxPic);
				if (tt.checked)
				{
					p.vCheckedCount++;
					p.vCheckedArray[tt.getAttribute('check_id')] = 1;
				}
				else
				{
					delete p.vCheckedArray[tt.getAttribute('check_id')];
					p.vCheckedCount--;
				}
			}
		}
		tTr.appendChild(tTd);

		to.appendChild(tTr);

		if (data[i][this.vDataFieldChild])
		{
			this.fRecurseFill(to, data[i][this.vDataFieldChild], tSel, level+1);
		}
	}
}

xMultiCheck.prototype.fPreparePost = function ()
{
	var tI;
	var to = arguments[0];
	if (!to)
	{
		alert('xMultiCheck.fPreparePost() error: Please select target form!');
		return;
	}
	to = document.getElementById(to);
	if (!to)
	{
		alert('xMultiCheck.fPreparePost() error: Selected target form (' + arguments[0] + ') not found in DOM!');
		return;
	}

	for (var i in this.vPostElementArray)
	{
		this.vPostElementArray[i].parentNode.removeChild(this.vPostElementArray[i]);
	}
	this.vPostElementArray = [];

	for (var i in this.vCheckedArray)
	{
		tI = document.createElement('INPUT');
		tI.name = this.vPostElementName + '[' + i + ']';
		tI.type = 'hidden';
		tI.value = 1;
		to.appendChild(tI);
		this.vPostElementArray[this.vPostElementArray.length] = tI;
	}
}



