var drawBox =
{
	addEvent: function(obj, type, fn)
	{
		if( obj.attachEvent )
		{
			obj["e"+type+fn] 	= fn;
			obj[type+fn] 		= function(){obj["e"+type+fn]( window.event );}

			obj.attachEvent("on"+type, obj[type+fn] );
		}
		else
			obj.addEventListener( type, fn, false );
	},

	stopEvent: function(e)
	{
		e = e || window.event;

		if(e.stopPropagation)
		{
			e.stopPropagation();
			e.preventDefault();
		}

		/*@cc_on@*/
		/*@if(@_win32)
		e.cancelBubble = true;
		e.returnValue = false;
		/*@end@*/
		return false;
	},

	createButton: function(button_def_struct)
	{
		var table							= document.createElement('TABLE');
			table.cellPadding				= "0";
			table.cellSpacing				= "0";
			table.border					= "0";

		var tbody							= document.createElement('TBODY');
		var tr								= document.createElement('TR');
		var td_left							= document.createElement('TD');
		var td_mittle						= document.createElement('TD');
		var td_right						= document.createElement('TD');

		var img_left						= document.createElement('IMG');
			img_left.src					= "/images_pub/b_l.gif";
			img_left.width					= "14";
			img_left.height					= "20";
			img_left.border					= "0";

		var img_right						= document.createElement('IMG');
			img_right.src					= "/images_pub/b_r.gif";
			img_right.width					= "14";
			img_right.height				= "20";
			img_right.border				= "0";

		var input							= null;

		if(document.getElementById && !document.all)
		{
			input							= document.createElement('INPUT');

			if(button_def_struct.button_id != "button_id")
				input.id					=  button_def_struct.button_id;

			if(button_def_struct.button_name != "button_name")
				input.name					= button_def_struct.button_name;
		}
		else
		{
			if(button_def_struct.button_name != "button_name")
				input						= document.createElement("<INPUT name='"+button_def_struct.button_name+"'>");
			else
				input						= document.createElement('INPUT');
		}

			input.type						= button_def_struct.button_type;
			input.className					= button_def_struct.button_class;
			input.value						= button_def_struct.button_text;

			if(button_def_struct.button_action != null)
			{
				input.onclick					= function(event)
												  {
														return eval(button_def_struct.button_action);
												  };
			}

		td_left.appendChild(img_left);
		td_mittle.appendChild(input);
		td_right.appendChild(img_right);

		tr.appendChild(td_left);
		tr.appendChild(td_mittle);
		tr.appendChild(td_right);

		tbody.appendChild(tr);
		table.appendChild(tbody);

		return table;
	},

	createButtonContainer: function(button_def_struct,button1_def_struct)
	{
		var div_button						= document.createElement('DIV');
			div_button.className			= "div_button";
			div_button.align				= "center";

		var table_outter					= document.createElement('TABLE');
			table_outter.cellPadding		= "0";
			table_outter.cellSpacing		= "0";
			table_outter.border				= "0";
			table_outter.width				= "80%";

		var tbody_outter					= document.createElement('TBODY');
		var tr_outter						= document.createElement('TR');

		var td_outter						= document.createElement('TD');
			td_outter.align					= button_def_struct.button_align;

		var table = this.createButton(button_def_struct);

		td_outter.appendChild(table);
		tr_outter.appendChild(td_outter);

		if(!isEmpty(button1_def_struct.button_text))
		{
			var td_outter1						= document.createElement('TD');
				td_outter1.align				= button1_def_struct.button_align;

			var table1 							= this.createButton(button1_def_struct);

			td_outter1.appendChild(table1);
			tr_outter.appendChild(td_outter1);
		}

		tbody_outter.appendChild(tr_outter);
		table_outter.appendChild(tbody_outter);

		div_button.appendChild(table_outter);

		return div_button;
	},

	check: function(definition)
	{
		var w = parseInt(definition.box_width);

		if(isNaN(w) || w < 0)
		{
			alert("Invalid or missing box_width");
			return false;
		}

		if(!isEmpty(definition.label))
		{
			if(isEmpty(definition.label_class))
			{
				alert("Invalid or missing label_class");
				return false;
			}
		}

		if(!isEmpty(definition.sublabel))
		{
			if(isEmpty(definition.sublabel_class))
			{
				alert("Invalid or missing sublabel_class");
				return false;
			}
		}

		if(!isEmpty(definition.button_text))
		{
			if((isEmpty(definition.button_type) || definition.button_type == "button")  && isEmpty(definition.button_action))
			{
				alert('Invalid or missing button_action!');
				return false;
			}
		}

		if(!isEmpty(definition.button1_text))
		{
			if((isEmpty(definition.button1_type) || definition.button1_type == "button")  && isEmpty(definition.button1_action))
			{
				alert('Invalid or missing button1_action!');
				return false;
			}
		}

		return true;
	},

	getValue: function(definition,key)
	{
		if(typeof definition[key] != "undefined")
			return trim(definition[key]," ");

		return "";
	},

	copyButtonStruct: function(definition,ident)
	{
		var button_def_struct					= {	button_id:"button_id",
													button_name:"button_name",
													button_text:"button_text",
													button_align:"left",
													button_type:"button",
													button_action:null,
													button_class:"fxbutton"
												  };

		var button_id 		= this.getValue(definition,"button"+ident+"_id");
		var button_name 	= this.getValue(definition,"button"+ident+"_name");
		var button_text 	= this.getValue(definition,"button"+ident+"_text");
		var button_align 	= this.getValue(definition,"button"+ident+"_align");
		var button_type 	= this.getValue(definition,"button"+ident+"_type");
		var button_action 	= this.getValue(definition,"button"+ident+"_action");
		var button_class 	= this.getValue(definition,"button"+ident+"_class");

		if(!isEmpty(button_id)) 	button_def_struct["button_id"] 		= button_id;
		if(!isEmpty(button_name)) 	button_def_struct["button_name"] 	= button_name;
		if(!isEmpty(button_text)) 	button_def_struct["button_text"] 	= button_text;
		if(!isEmpty(button_align)) 	button_def_struct["button_align"] 	= button_align;
		if(!isEmpty(button_type)) 	button_def_struct["button_type"] 	= button_type;
		if(!isEmpty(button_action)) button_def_struct["button_action"] 	= button_action;
		if(!isEmpty(button_class)) 	button_def_struct["button_class"] 	= button_class;

		return button_def_struct;
	},

	drawBorder: function(table,definition)
	{
		if(drawBox.check(definition))
		{
			var button_def_struct 					= {};
			var button1_def_struct 					= {};

			var parent								= table.parentNode;
			var clientWidth							= definition.box_width;

			var div_container 						= document.createElement('DIV');
				div_container.className 			= 'div_box_container';

			var div_label							= document.createElement('DIV');
				div_label.className					= 'div_label';

			var font_label							= document.createElement('FONT');
				font_label.className				= definition.label_class;

			var b_top_outter						= document.createElement('B');
				b_top_outter.className				= 'b_top_bottom_outter';

			var b_top_inner							= document.createElement('B');
				b_top_inner.className				= 'b_top_inner';

			var b_bottom_outter						= document.createElement('B');
				b_bottom_outter.className			= 'b_top_bottom_outter';

			var b_bottom_inner						= document.createElement('B');
				b_bottom_inner.className			= 'b_bottom_inner';

			var div_outter							= document.createElement('DIV');
				div_outter.className 				= 'div_outter';

			var div_inner							= document.createElement('DIV');
				div_inner.className 				= 'div_inner';

			var table_outter						= document.createElement('TABLE');
				table_outter.cellPadding			= '0';
				table_outter.cellSpacing			= '0';
				table_outter.border					= '0';

			var do_css = getStyleSheetStyle('.div_outter');
			var di_css = getStyleSheetStyle('.div_inner');

			var outter_width						= clientWidth - ((parseInt(do_css.borderLeftWidth)+parseInt(di_css.borderLeftWidth))*2);
			table_outter.width						= outter_width+'px';

			var	body_outter							= document.createElement('TBODY');
			var	tr_outter							= document.createElement('TR');
			var	td_outter							= document.createElement('TD');

			if(!isNaN(parseInt(definition.padding)))
				td_outter.style.padding				= definition.padding;
			else
			{
				if(!isNaN(parseInt(definition.padding_left)))
					td_outter.style.paddingLeft				= definition.padding_left;

				if(!isNaN(parseInt(definition.padding_top)))
					td_outter.style.paddingTop				= definition.padding_top;

				if(!isNaN(parseInt(definition.padding_right)))
					td_outter.style.paddingRight			= definition.padding_right;

				if(!isNaN(parseInt(definition.padding_bottom)))
					td_outter.style.paddingBottom			= definition.padding_bottom;
			}

			if(!isEmpty(definition.label))
			{
				font_label.appendChild(document.createTextNode(definition.label));
				div_label.appendChild(font_label);

				if(!isEmpty(definition.sublabel))
				{
					var font_sublabel					= document.createElement('FONT')
						font_sublabel.className			= definition.sublabel_class;

					font_sublabel.style.paddingLeft		= '4px';
					font_sublabel.appendChild(document.createTextNode(definition.sublabel));

					div_label.appendChild(font_sublabel);
				}

				div_container.appendChild(div_label);
			}

			b_top_outter.appendChild(b_top_inner);
			b_bottom_outter.appendChild(b_bottom_inner);

			td_outter.appendChild(table); //.cloneNode(true)
			tr_outter.appendChild(td_outter);
			body_outter.appendChild(tr_outter);
			table_outter.appendChild(body_outter);

			div_inner.appendChild(table_outter);
			div_outter.appendChild(div_inner);

			div_container.appendChild(b_top_outter);
			div_container.appendChild(div_outter);
			div_container.appendChild(b_bottom_outter);

			if(!isEmpty(definition.button_text))
			{
				button_def_struct = this.copyButtonStruct(definition,"");

				if(!isEmpty(definition.button1_text))
					button1_def_struct = this.copyButtonStruct(definition,"1");

				var button_container = drawBox.createButtonContainer(button_def_struct,button1_def_struct);
				div_container.appendChild(button_container);
			}

			parent.appendChild(div_container);

			//flash redraw hack for IE
			if(document.all) this.scrollToCoordinates();
		}
	},

	scrollToCoordinates: function ()
	{
		window.scrollTo((document.all)?document.body.scrollLeft:window.pageXOffset, (document.all)?document.body.scrollTop:window.pageYOffset);
	},

	_boxBorderHandler: function()
	{
		var tables 		= getElementsByAttribName(document,"drawBorder");
		var pattern 	= /([^:]+)[:](.*)/;

		for(var t = 0, tbl; tbl = tables[t]; t++)
		{
			var attrib 		= tbl.getAttribute("drawBorder");
			var ar 			= attrib.split(';');
			var ar_c		= ar.length;
			if(ar_c > 0)
			{
				var definition	= {};

				for(var i=0; i < ar_c; i++)
				{
					var res = pattern.exec(ar[i]);

					if(!isNull(res) && res.length > 0)
					{
						var key = trim(res[1],' ');
						key		= key.replace('-','_');
						key		= key.toLowerCase();

						definition[key] = trim(res[2]," ");
					}
				}

				drawBox.drawBorder(tbl,definition);

				delete definition;
			}
		}
	}
}

drawBox.addEvent(window,"load",drawBox._boxBorderHandler);