function TSGetDefineFunction()
{
	var node = this.target;
	while (node.nodeType != 1) node = node.parentNode;
	return node;
}

function TSHandlerFunction(type,handler)
{
	var tsType = type.replace(/on/,"");
	handler._EventHandler = function (e){
		window.event = e;
		return handler();
	};
	this.addEventListener(tsType, handler._EventHandler, false);
}

function SetTSEvent() {
	HTMLElement.prototype.attachEvent = HTMLDocument.prototype.attachEvent = TSHandlerFunction;	
	Event.prototype.__defineGetter__("srcElement", TSGetDefineFunction);
}

var browser_agt=navigator.userAgent.toLowerCase();
if ((browser_agt.indexOf("firefox")!=-1) || (browser_agt.indexOf("netscape")!=-1))//firefox or netscape
{
	SetTSEvent();
}

//BORDER STYLE
var _BLACK_BORDER = 1;
var _WHITE_BLACK_BORDER = 2;
var _SEMI_BORDER = 3;
var _INSIDE_BORDER = 4;

CreateTSCheckBox = function (id,name,classname, width, height, selectFunc, unselectFunc, sAlt)
{
	strTableStyle = '';
	if(height<20)
	{
		strTableStyle = ' style="position:absolute;left:0;top:-4;" ';
		height=20;
	}

	sAltAttributes = "";
	if(sAlt.length > 0)
	{
		sAltAttributes = ' ALT="'+sAlt+'" TITLE="'+sAlt+'"';
	}
	
	// Export HTML
	document.writeln('<TABLE '+strTableStyle+' border=0 style="width:'+width+';height:'+height+';overflow:hidden;">');
	
	document.writeln('<TD id="'+id+'_td0" valign="middle" align="center">');
	document.writeln('<input id="'+id+'" type="checkbox" onSelect="'+selectFunc+'" onUnselect="'+unselectFunc+'" />');
	document.writeln('</TD>');
	
	document.writeln('<TD id="'+id+'_td1">');
	document.writeln('<DIV id="'+id+'_tx">');
	document.writeln('<TABLE id="'+id+'_tb">');
	document.writeln('<TD '+sAltAttributes+' id="'+id+'_tb_td" valign="middle" class="'+classname+'">');
	document.writeln(name);
	document.writeln('</TD>');
	document.writeln('</TABLE>');
	document.writeln('</DIV>');
	document.writeln('</TD>');
	
	document.writeln('</TABLE>');
	
	/////////////////////////////////
	// Set Check Box properties
	var obj = document.getElementById(id+'_td0');
	obj.style.height = height-2;
	obj.style.paddingLeft	= 0;
	obj.style.paddingTop	= 0;
	obj.style.paddingRight	= 0;
	obj.style.paddingBottom	= 1;

	var obj = document.getElementById(id);
	obj.style.paddingLeft	= 0;
	obj.style.paddingTop	= 0;
	obj.style.paddingRight	= 0;
	obj.style.paddingBottom	= 0;
	obj._bIsTSCheckBox = true;
	//obj.checked = false;
	obj.setCheckStatus = CreateTSToggleButton.setCheckStatus;
	obj.attachEvent("onclick",	CreateTSToggleButton.clickCheckBox);
	
	/////////////////////////////////
	// Set Text properties
	var obj = document.getElementById(id+'_td1');
	obj.style.width = width-24;
	obj.style.height = height-2;
	obj.style.paddingLeft	= 0;
	obj.style.paddingTop	= 0;
	obj.style.paddingRight	= 0;
	obj.style.paddingBottom	= 0;
	
	var obj = document.getElementById(id+'_tx');
	obj.style.width = width-24;
	obj.style.height = height-2;
	obj.style.paddingLeft	= 0;
	obj.style.paddingTop	= 0;
	obj.style.paddingRight	= 0;
	obj.style.paddingBottom	= 0;
	obj.style.overflow = "hidden";

	var obj = document.getElementById(id+'_tb');
	obj.style.width = width-24;
	obj.style.height = height-4;

	var obj = document.getElementById(id+'_tb_td');
	obj.style.width = width-24;
	obj.style.height = height-6;

	return;
}

CreateTSToggleButton = function (id,name,classname, width, height, color, bgcolor, selectFunc, unselectFunc, sAlt)
{
	// Export HTML
	document.writeln('<DIV id="'+id+'_BD0'+'">');
	document.writeln('<DIV id="'+id+'_BD1'+'">');
	
	sAltAttributes = "";
	if(sAlt.length > 0)
	{
		sAltAttributes = ' ALT="'+sAlt+'" TITLE="'+sAlt+'"';
	}
	document.writeln('<TABLE>');
	document.writeln('<TD '+sAltAttributes+' id="'+id+'" align=center valign=center class="'+classname+'" onSelect="'+selectFunc+'" onUnselect="'+unselectFunc+'" style="cursor: default;word-wrap:break-word;">');
	document.writeln(name);
	document.writeln('</TD>');
	document.writeln('</TABLE>');
	
	document.writeln('</DIV>');
	document.writeln('</DIV>');
	
	// Get Object
	var obj_bd0 = document.getElementById(id+'_BD0');
	var obj_bd1 = document.getElementById(id+'_BD1');
	var obj = document.getElementById(id);
	
	// Set Properties
	obj._id = id;
	obj._width = width;
	obj._height = height;
	obj._color = color;
	obj._bgcolor = bgcolor;
	obj._up = true;
	obj._enable = true;
	obj._bIsTSToggleButton = true;
	obj.setCheckStatus = CreateTSToggleButton.setDownStatus;
	
	// Set style
	obj_bd0.style.width = width;
	obj_bd0.style.height = height;
	obj_bd0.style.overflow = "hidden";
	
	obj_bd1.style.width = width-2;
	obj_bd1.style.height = height-2;
	obj_bd1.style.overflow = "hidden";
	
	obj.style.width = width-4;
	obj.style.height = height-4;
	obj.style.overflow = "hidden";
	
	obj_bd1.style.background = bgcolor;

	// Update Button Status
	CreateTSToggleButton.SetTSButtonToggleStatus(id, true, true);
	
	// Set Event
	obj.attachEvent("onmousedown",	CreateTSToggleButton.downTSButton);
	obj.attachEvent("onmouseup",	CreateTSToggleButton.upTSButton);
	obj.attachEvent("onmouseout",	CreateTSToggleButton.outTSButton);
}

CreateTSToggleButton.clickCheckBox = function()
{
	var obj = window.event.srcElement;
	if (obj == null) return;
	if(obj.disabled) return;

	if(obj.checked)//Select
	{
		obj.onSelect = obj.getAttribute("onSelect");
		if (obj.onSelect == null || obj.onSelect == "") return;
		if (typeof obj.onSelect == "string")
		{
			obj.onSelect = new Function("", obj.onSelect);
		}
		obj.onSelect();

	}
	else//Unselect
	{
		obj.onUnselect = obj.getAttribute("onUnselect");
		if (obj.onUnselect == null || obj.onUnselect == "") return;
		if (typeof obj.onUnselect == "string")
		{
			obj.onUnselect = new Function("", obj.onUnselect);
		}
		obj.onUnselect();
	}

}
CreateTSToggleButton.setCheckStatus = function (bCheck)
{
	this.checked = bCheck;
}

CreateTSToggleButton.setDownStatus = function (bDown)
{
	if(this._up != !bDown)
	{
		this._up = !bDown;
		CreateTSToggleButton.SetTSButtonToggleStatus(this._id, this._up, this._enable);
	}
}

if ((browser_agt.indexOf("firefox")!=-1) || (browser_agt.indexOf("netscape")!=-1))//firefox or netscape
{
	HTMLTableCellElement.prototype.__defineGetter__("disabled", 
		function () { 
			if(typeof(this._bIsTSToggleButton) != 'undefined')
			{
				return(!this._enable); 
			}
		}
	);
	
    HTMLTableCellElement.prototype.__defineSetter__("disabled", 
		function (bDisabled) {
			if(typeof(this._bIsTSToggleButton) != 'undefined')
			{
				if(this._enable != !bDisabled)
				{
					this._enable = !bDisabled;
					CreateTSToggleButton.SetTSButtonToggleStatus(this._id, this._up, this._enable);
				}
			}
		}
	);

	CreateTSToggleButton.prototype.__defineGetter__("disabled", 
		function () { 
			return(!this._enable); 
		}
	);
	
    CreateTSToggleButton.prototype.__defineSetter__("disabled", 
		function (bDisabled) {
			if(this._enable != !bDisabled)
			{
				this._enable = !bDisabled;
				CreateTSToggleButton.SetTSButtonToggleStatus(this._id, this._up, this._enable);
			}
		}
	);
}

CreateTSToggleButton.outTSButton = function ()
{
	var obj = window.event.srcElement;
	if (obj == null) return;
	if(!obj._enable) return;
	
	CreateTSToggleButton.SetTSButtonToggleStatus(obj._id, obj._up, obj._enable);
}

CreateTSToggleButton.downTSButton = function ()
{
	var obj = window.event.srcElement;
	if (obj == null) return;
	if(!obj._enable)
	{	
		if(!obj.parentNode)
			return;
		
		obj = obj.parentNode;
		if(!obj._enable)
			return;
	}
	
	CreateTSToggleButton.SetTSButtonToggleStatus(obj._id, false, obj._enable);
}

CreateTSToggleButton.upTSButton = function ()
{
	var obj = window.event.srcElement;
	if (obj == null) return;
	if(!obj._enable)
	{	
		if(!obj.parentNode)
			return;

		obj = obj.parentNode;
		if(!obj._enable)
			return;
	}

	obj._up = !obj._up;
	CreateTSToggleButton.SetTSButtonToggleStatus(obj._id, obj._up, obj._enable);
	if(!obj._up)//Select
	{
		obj.onSelect = obj.getAttribute("onSelect");
		if (obj.onSelect == null || obj.onSelect == "") return;
		if (typeof obj.onSelect == "string")
		{
			obj.onSelect = new Function("", obj.onSelect);
		}
		obj.onSelect();
	}
	else//Unselect
	{
		obj.onUnselect = obj.getAttribute("onUnselect");
		if (obj.onUnselect == null || obj.onUnselect == "") return;
		if (typeof obj.onUnselect == "string")
		{
			obj.onUnselect = new Function("", obj.onUnselect);
		}
		obj.onUnselect();
	}
}

CreateTSToggleButton.SetTSBorderStyle = function (id,style,bUp)
{
	var obj = document.getElementById(id);
	if(style == _BLACK_BORDER)//BLACK BORDER
	{
		obj.style.borderLeftStyle = obj.style.borderTopStyle = "solid";
		obj.style.borderLeftWidth = obj.style.borderTopWidth = "1px";
		obj.style.borderLeftColor = obj.style.borderTopColor = "#000000";

		obj.style.borderRightStyle = obj.style.borderBottomStyle = "solid";
		obj.style.borderRightWidth = obj.style.borderBottomWidth = "1px";
		obj.style.borderRightColor = obj.style.borderBottomColor = "#000000";
	}
	else if(style == _WHITE_BLACK_BORDER)//WHITE_BLACK BORDER
	{
		var color1 = "buttonhighlight";
		var color2 = "#444444";
		if(!bUp)
		{
			var temp = color1;
			color1 = color2;
			color2 = temp;
		}
		
		obj.style.borderLeftStyle = obj.style.borderTopStyle = "solid";
		obj.style.borderLeftWidth = obj.style.borderTopWidth = "1px";
		obj.style.borderLeftColor = obj.style.borderTopColor = color1;
		
		obj.style.borderRightStyle = obj.style.borderBottomStyle = "solid";
		obj.style.borderRightWidth = obj.style.borderBottomWidth = "1px";
		obj.style.borderRightColor = obj.style.borderBottomColor = color2;
	}
	else if(style == _SEMI_BORDER)//SEMI BORDER
	{
		var color1 = "buttonface";
		var color2 = "buttonshadow";
		if(!bUp)
		{
			var temp = color1;
			color1 = color2;
			color2 = temp;
		}

		obj.style.borderLeftStyle = obj.style.borderTopStyle = "solid";
		obj.style.borderLeftWidth = obj.style.borderTopWidth = "1px";
		obj.style.borderLeftColor = obj.style.borderTopColor = color1;

		obj.style.borderRightStyle = obj.style.borderBottomStyle = "solid";
		obj.style.borderRightWidth = obj.style.borderBottomWidth = "1px";
		obj.style.borderRightColor = obj.style.borderBottomColor = color2;
	}
	else if(style == _INSIDE_BORDER)//INSIDE BORDER
	{
		obj.style.borderLeftStyle = obj.style.borderTopStyle = "solid";
		obj.style.borderLeftWidth = obj.style.borderTopWidth = "1px";
		obj.style.borderLeftColor = obj.style.borderTopColor = "buttonface";

		obj.style.borderRightStyle = obj.style.borderBottomStyle = "solid";
		obj.style.borderRightWidth = obj.style.borderBottomWidth = "1px";
		obj.style.borderRightColor = obj.style.borderBottomColor = "buttonface";
	}
}

CreateTSToggleButton.SetTSButtonToggleStatus = function (id, bUp, bEnable)
{
	var obj = document.getElementById(id);
	
	if(bEnable)//Enable
		obj.style.color = obj._color;
	else//Disable
		obj.style.color = "graytext";
	
	if(bUp)//Up
	{
		CreateTSToggleButton.SetTSBorderStyle(id+'_BD0', _WHITE_BLACK_BORDER, true);
		CreateTSToggleButton.SetTSBorderStyle(id+'_BD1', _SEMI_BORDER, true);
		obj.style.paddingLeft	= 0;
		obj.style.paddingTop	= 0;
		obj.style.paddingRight	= 2;
		obj.style.paddingBottom	= 2;
	}
	else//Down
	{
		CreateTSToggleButton.SetTSBorderStyle(id+'_BD0', _WHITE_BLACK_BORDER, false);
		CreateTSToggleButton.SetTSBorderStyle(id+'_BD1', _SEMI_BORDER, false);
		obj.style.paddingLeft	= 2;
		obj.style.paddingTop	= 2;
		obj.style.paddingRight	= 2;
		obj.style.paddingBottom	= 2;
	}
}