var WBM_global = 
{
	addEvent: function(elm, evType, fn, useCapture)
	// cross-browser event handling for IE5+, NS6+ and Mozilla/Gecko
	// By Scott Andrew
	{
		if(elm.addEventListener)
		{
			elm.addEventListener(evType, fn, useCapture); 
			return true; 
		}
		else if(elm.attachEvent)
		{
			var r = elm.attachEvent("on" + evType, fn); 
			return r; 
		}
		else
		{
			elm["on" + evType] = fn;
		}
	}, 

	getFunction: function(obj, func)
	{
		var _f = obj + "." + func;
		
		_f += "(e";

		for(var i = 2; i < arguments.length; i++)
		{
			_f += ",\"" + arguments[i] + "\"";
		}
		
		_f += ")";
		
		return function(e) { eval(_f); }
	}, 

	getSimpleFunction: function(func)
	{
		var _f = func;
		var _tmp;
		
		alert(arguments.length);
		for(var i = 1; i < arguments.length; i++)
		{
			alert(arguments[i]);
			_tmp += ",\"" + arguments[i] + "\"";
		}
		_tmp = _tmp.substr(1);
		
		_f += "(" + _tmp + ")";
		
		return function(e) { eval(_f); }
	}
}

function WBM_toggleStyleActionBar(_id, _action)
{
	var _seed = _id.replace(/ToggleBar_/, "");
	
	var _bar = document.getElementById("ToggleBar_" + _seed);
	var _icon = document.getElementById("ToggleBar_" + _seed + "_icon");
	var _heading = document.getElementById("ToggleBar_" + _seed + "_heading");
	
	if(_action == "over")
	{
		_bar.className = "ToggleActionBar_over";
		_heading.style.color = "#ffffff";
		_icon.src = _icon.src.replace(".gif", "_over.gif");
	}
	
	if(_action == "out")
	{
		_bar.className = "ToggleActionBar";
		_heading.style.color = "#000000";
		_icon.src = _icon.src.replace("_over.gif", ".gif");
	}
}

var WBM_ToggleBar = 
{	
	init: function()
	{
		if(!document.getElementById || !document.getElementsByTagName)
			return;
		
		var arr_div = document.getElementsByTagName("div")
		
		for(var i = 0; i < arr_div.length; ++i)
		{
			if(arr_div[i].id.indexOf("ToggleBar") != -1 && arr_div[i].id.indexOf("heading") == -1 && arr_div[i].id.indexOf("container") == -1)
			{
				WBM_global.addEvent(arr_div[i], 'click', WBM_global.getFunction("WBM_ToggleBar", "toggle", arr_div[i].id), false);	
			}
		}
	}, 
	
	toggle: function(e, _id, _status)
	{
		var _seed = _id.replace(/ToggleBar_/, "");
		
		var _icon = document.getElementById("ToggleBar_" + _seed + "_icon");
		var _container = document.getElementById("ToggleBar_" + _seed + "_container");
		if(_status == "" || _status == undefined)
		{
			var _status = _icon.getAttribute("status");
		}
		
		if(_status != "expanded" && _status != "collapsed")
		{
			_icon.setAttribute("status", "collapsed");
			_status = _icon.getAttribute("status");
		}

		if(_status == "expanded")
		{
			WBM_ToggleBar.collapse(_icon, _container);

			WBM_ToggleBar.afterCollapse(_id);
		}
		else
		{
			WBM_ToggleBar.beforeExpand(_id);

			WBM_ToggleBar.expand(_icon, _container);
		}

		if(window.event)
		{
			window.event.cancelBubble = true;
			window.event.returnValue = false;
		}
		if(e && e.stopPropagation && e.preventDefault)
		{
			e.stopPropagation();
			e.preventDefault();
		}
	}, 
	
	expand: function(_icon, _container)
	{
		_icon.setAttribute("status", "expanded");
		_icon.src = _icon.src.replace("arrow_down", "arrow_up");

		_container.style.display = "";
	}, 
	
	collapse: function(_icon, _container)
	{
		_icon.setAttribute("status", "collapsed");
		_icon.src = _icon.src.replace("arrow_up", "arrow_down");

		_container.style.display = "none";
	}, 
	
	expandAll: function()
	{
	}, 
	
	collapseAll: function()
	{
	}, 
	
	beforeExpand: function(_id)
	{
	}, 
	
	afterExpand: function(_id)
	{
	}, 
	
	afterCollapse: function(_id)
	{
	}
}

var WBM_Tab = 
{	
	tab_scheme : [], 
	
	selected_tab_id : "", 
	
	init: function(_id, _tab_scheme)
	{
		if(!document.getElementById || !document.getElementsByTagName)
			return;
		
		if(_id && document.getElementById(_id))
		{
			var tab_container = document.getElementById(_id);
		}
		
		if(_tab_scheme)
		{
			this.tab_scheme = _tab_scheme;
		}

		for(var i = 0; i < tab_container.childNodes.length; ++i)
		{
			if(tab_container.childNodes[i].id)
			{
				if(tab_container.childNodes[i].id.indexOf(_id + "_idTab_") != -1)
				{
					var tab_item = document.getElementById(tab_container.childNodes[i].id);
					
					if(this.tab_scheme[tab_container.childNodes[i].id]["default"] == 1)
					{
						this.selected_tab_id = tab_container.childNodes[i].id;
					}
				
					WBM_global.addEvent(tab_item, 'mouseover', WBM_global.getFunction("WBM_Tab", "over", tab_item.id), false);
					WBM_global.addEvent(tab_item, 'mouseout', WBM_global.getFunction("WBM_Tab", "out", tab_item.id), false);
					WBM_global.addEvent(tab_item, 'click', WBM_global.getFunction("WBM_Tab", "click", tab_item.id), false);
				}
			}
		}
	}, 
	
	over: function(e, _tab_id)
	{
		if(this.selected_tab_id != _tab_id)
		{
			document.getElementById(_tab_id).src = this.tab_scheme[_tab_id]["over"];
		}
		
		if(window.event)
		{
			window.event.cancelBubble = true;
			window.event.returnValue = false;
		}
		if(e && e.stopPropagation && e.preventDefault)
		{
			e.stopPropagation();
			e.preventDefault();
		}
	}, 
	
	out: function(e, _tab_id)
	{
		if(this.selected_tab_id != _tab_id)
		{
			document.getElementById(_tab_id).src = this.tab_scheme[_tab_id]["normal"];
		}
		
		if(window.event)
		{
			window.event.cancelBubble = true;
			window.event.returnValue = false;
		}
		if(e && e.stopPropagation && e.preventDefault)
		{
			e.stopPropagation();
			e.preventDefault();
		}
	}, 
	
	click: function(e, _tab_id)
	{
		this.selected_tab_id = _tab_id;
		
		for(var item_id in this.tab_scheme)
		{
			if(item_id != this.selected_tab_id)
			{
				document.getElementById(item_id).src = this.tab_scheme[item_id]["normal"];
			}
			else
			{
				document.getElementById(item_id).src = this.tab_scheme[item_id]["over"];
			}
		}
		
		eval(this.tab_scheme[_tab_id]["onclick"]);
		
		if(window.event)
		{
			window.event.cancelBubble = true;
			window.event.returnValue = false;
		}
		if(e && e.stopPropagation && e.preventDefault)
		{
			e.stopPropagation();
			e.preventDefault();
		}
	}
}

var help_area_toggle_status = "collapsed";

function WBM_displayHelpInformation(_action, _help_source)
{
	if(help_area_toggle_status == "collapsed")
	{
		// get HTML
		var callback =
		{
			success : function(obj)
						{ 
							document.getElementById("containerHelpInformation").innerHTML = obj.responseText;
							
							help_area_toggle_status = "expanded";
						},
			failure : function(obj){ alert("FAILURE"); alert(obj);}
		}

		var obj = WBM_XHR.asyncRequest('GET', _help_source + '?action=' + _action, callback);
	}
	else
	{
		document.getElementById("containerHelpInformation").innerHTML = "";
		help_area_toggle_status = "collapsed";
	}
}

var WBM_ExpandCollapse = 
{	
	init: function()
	{
		if(!document.getElementById)
			return;

		var vertical_button = document.getElementById("verticalSeparator");
		var horizontal_button = document.getElementById("horizontalSeparator");
		
		if(vertical_button)
		{
			WBM_global.addEvent(vertical_button, 'click', WBM_global.getFunction("WBM_ExpandCollapse", "clickButtonVerticalExpandCollapse", "verticalSeparator"), false);
		}
		if(horizontal_button)
		{
			WBM_global.addEvent(horizontal_button, 'click', WBM_global.getFunction("WBM_ExpandCollapse", "clickButtonHorizontalExpandCollapse", "horizontalSeparator"), false);
		}
	}, 
		
	clickButtonVerticalExpandCollapse: function(e, btn_name)
	{
		var button = document.getElementById(btn_name);
		
		var action = button.getAttribute("action");
		var param = button.getAttribute("param");
		var arr_param = button.getAttribute("param").split("|");
		
		if(action != "expand" && action != "collapse")
		{
			button.setAttribute("action", "expand");
			action = button.getAttribute("action");
		}

		if(action == "expand")
		{
			//change button action property
			button.setAttribute("action", "collapse");
			
			document.getElementById(arr_param[0]).style.display = "";
			document.getElementById(arr_param[0]).style.width = arr_param[2];
			document.getElementById(arr_param[1]).style.width = arr_param[3];
		}
		else if(action == "collapse")
		{
			//change button action property
			button.setAttribute("action", "expand");
			
			document.getElementById(arr_param[0]).style.display = "none";
			document.getElementById(arr_param[0]).style.width = "0";
			document.getElementById(arr_param[1]).style.width = "100%";
		}
		
		afterExpandCollapse();

		if(window.event)
		{
			window.event.cancelBubble = true;
			window.event.returnValue = false;
		}
		if(e && e.stopPropagation && e.preventDefault)
		{
			e.stopPropagation();
			e.preventDefault();
		}
	}, 
		
	clickButtonHorizontalExpandCollapse: function(e, btn_name)
	{
		var button = document.getElementById(btn_name);

		var contentArea = document.getElementById("contentArea");
		
		if(contentArea)
		{
			var action = button.getAttribute("action");
			
			if(action != "expand" && action != "collapse")
			{
				button.setAttribute("action", "expand");
				action = button.getAttribute("action");
			}


			if(action == "expand")
			{
				//change button action property
				button.setAttribute("action", "collapse");
				
				var strDisplayTableRow = "block";
				if(navigator.appName.indexOf("Netscape") > -1)
				{
					strDisplayTableRow = "table-row";
				}
				
				contentArea.style.display = strDisplayTableRow;
				//document.getElementById(id1).style.height = "20%";
				//document.getElementById(id2).style.height = "80%";
				document.getElementById("horizontalSeparator").style.height = "9";
			}
			else if(action == "collapse")
			{
				//change button action property
				button.setAttribute("action", "expand");
				
				//document.getElementById(id2).style.height = "100%";
				//document.getElementById(id2).style.verticalAlign = "top";
				contentArea.style.display = "none";
				contentArea.style.height = "0";
				document.getElementById("horizontalSeparator").style.height = "9";
				document.getElementById("horizontalSeparatorTable").style.height = "9";
			}
		}

		if(window.event)
		{
			window.event.cancelBubble = true;
			window.event.returnValue = false;
		}
		if(e && e.stopPropagation && e.preventDefault)
		{
			e.stopPropagation();
			e.preventDefault();
		}
	}
}

function WBM_getCheckedItems(form, param_scheme)
{
	var str_checked_items = "";
	var is_correct_item = true;
	
	if(form != undefined)
	{
		for(var i = 0; i < form.elements.length; i++)
		{
			if(form.elements[i].type == "checkbox")
			{
				if(param_scheme != "")
				{
					var tmp = param_scheme.split("=");
					if(form.elements[i].getAttribute(tmp[0]) == tmp[1])
					{
						is_correct_item = true;
					}
					else
					{
						is_correct_item = false;
					}
					
				}
				if(form.elements[i].checked == true && is_correct_item == true)
				{
					str_checked_items += "," + form.elements[i].value;
				}
			}
		}
		str_checked_items = str_checked_items.substring(1, str_checked_items.length);
	}
	
	return str_checked_items;
}

function WBM_countCheckedItems(form_element)
{
	var checked_no = 0;
	
	if(form_element != undefined)
	{
		var count  = (typeof(form_element.length) != undefined) ? form_element.length : 0;
		
		if(count) 
		{
			for(var i = 0; i < count; i++) 
			{
				if(form_element[i].checked == true)
				{
					checked_no++;
				}
			}
		}
		else 
		{
			if(form_element.checked == true)
			{
				checked_no++;
			}
		}
	}
	
	return checked_no;
}

function WBM_validateCheckedItemsNo(checkbox_field, valid_items_checked_no)
{
	if(WBM_countCheckedItems(checkbox_field) != valid_items_checked_no)
	{
		return false;
	}
	else
	{
		return true;
	}
}

function round(number, decimals)
{
    decimals = (!decimals ? 2 : decimals);
    
    return Math.round(number * Math.pow(10, decimals)) / Math.pow(10, decimals);
}





/* ---------- BLOG COMMENT WORD LIMIT ---------------*/
var wordLimit = 350;
var holdText;
var disabledBox = false;

function WBM_blogComment_countWords(field)
{
	var text1 = document.getElementById(field).value;
	var numberOfWords = WBM_blogComment_doCount(text1);

	if(numberOfWords == wordLimit)
	{
		holdText = text1;
	}

	document.getElementById(field+'_words').innerHTML = numberOfWords; //wordLimit-

	if(numberOfWords >= wordLimit)
	{
		document.getElementById(field+'_words').style.color = '#FF0000';
		disabledBox = true;
	}
	else
	{
		document.getElementById(field+'_words').style.color = '#000000';
		disabledBox = false;
	}
}

function WBM_blogComment_doCount(textParam)
{
	//replace all instances of one-or-more spaces with a single space
	var text2 = textParam.replace(/\s+/g, ' ');

	//trim leading and tailing spaces
	while(text2.substring(0, 1) == ' ')
		text2 = text2.substring(1);
	while(text2.substring(text2.length-2, text2.length-1) == ' ')
		text2 = text2.substring(0,text2.length-1);

	var text3 = text2.split(' ');

	return text3.length;
}

function WBM_blogComment_maybeReset(field)
{
	if(disabledBox)
	{
		var currText = document.getElementById(field).value;
		var newLength = WBM_blogComment_doCount(currText);

		if(newLength > wordLimit)
		{
			document.getElementById(field).value = holdText;
		}
	}
}
/* -- end -------- BLOG COMMENT WORD LIMIT ---------------*/

function WBM_blogComment_mouseover()
{
	var _blog_comment = document.getElementById('comment');
	var _blog_comment_missing = document.getElementById('blog-comment-missing');

	_blog_comment_missing.style.visibility = (_blog_comment.value.length == 0) ? 'visible' : 'hidden';
}	

function WBM_blogComment_mouseout()
{
	var _blog_comment_missing = document.getElementById('blog-comment-missing');
	_blog_comment_missing.style.visibility = 'hidden';
}



function WBM_guideLines()
{
	window.open('user_guidelines.php', '', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=700,height=600,left = 290,top = 100');
}