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;
		
		for(var i = 1; i < arguments.length; i++)
		{
			_tmp += ",\"" + arguments[i] + "\"";
		}
		_tmp = _tmp.substr(1);
		
		_f += "(" + _tmp + ")";
		
		return function(e) { eval(_f); }
	}
}

function WBM_toggleStyleActionBar(_id, _action)
{
	if(_id.indexOf("ToggleBar_") != -1)
	{
		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(_id.indexOf("action-bar-") != -1)
	{
		var _seed = _id.replace(/action-bar-/, "");
		
		var _bar = document.getElementById("action-bar-" + _seed);
		var _icon = document.getElementById("action-bar-" + _seed + "-icon");
		var _heading = document.getElementById("action-bar-" + _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;
					}
					
					if(this.tab_scheme[tab_item.id]["disabled"] == "0")
					{
						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)
		{
			if(document.getElementById(_tab_id).nodeName.toLowerCase() == "img")
			{
				document.getElementById(_tab_id).src = this.tab_scheme[_tab_id]["over"];
			}
			if(document.getElementById(_tab_id).nodeName.toLowerCase() == "div")
			{
				var _class_name = document.getElementById(_tab_id).className;
				document.getElementById(_tab_id).className = _class_name.replace(this.tab_scheme[_tab_id]["normal"], 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)
		{
			if(document.getElementById(_tab_id).nodeName.toLowerCase() == "img")
			{
				document.getElementById(_tab_id).src = this.tab_scheme[_tab_id]["normal"];
			}
			if(document.getElementById(_tab_id).nodeName.toLowerCase() == "div")
			{
				var _class_name = document.getElementById(_tab_id).className;
				document.getElementById(_tab_id).className = _class_name.replace(this.tab_scheme[_tab_id]["over"], 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)
			{
				if(document.getElementById(item_id).nodeName.toLowerCase() == "img")
				{
					document.getElementById(item_id).src = this.tab_scheme[item_id]["normal"];
				}
				if(document.getElementById(item_id).nodeName.toLowerCase() == "div")
				{
					var _class_name = document.getElementById(item_id).className;
					document.getElementById(item_id).className = _class_name.replace(this.tab_scheme[_tab_id]["over"], this.tab_scheme[_tab_id]["normal"]);
				}
			}
			else
			{
				if(document.getElementById(item_id).nodeName.toLowerCase() == "img")
				{
					document.getElementById(item_id).src = this.tab_scheme[item_id]["over"];
				}
				if(document.getElementById(item_id).nodeName.toLowerCase() == "div")
				{
					var _class_name = document.getElementById(item_id).className;
					document.getElementById(item_id).className = _class_name.replace(this.tab_scheme[_tab_id]["normal"], this.tab_scheme[_tab_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 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();
		}
	}
}

var WBM_TabularReport = 
{	
	init: function(e, _id)
	{
		if(!document.getElementById || !document.getElementsByTagName)
			return;
		
		if(_id && document.getElementById(_id))
		{
			var tabular_report_container = document.getElementById(_id);
		}
		if(tabular_report_container)
		{
			for(var i = 0; i < tabular_report_container.childNodes[0].childNodes.length; ++i)
			{
				if(tabular_report_container.childNodes[0].childNodes[i].nodeName.toLowerCase() == "tr")
				{
					var _row = tabular_report_container.childNodes[0].childNodes[i];
					
					if(_row.className == "tabularReportParentNormalRow")
					{
						WBM_global.addEvent(_row, 'mouseover', WBM_TabularReport.getFunction(_row, "over"), false);
						WBM_global.addEvent(_row, 'mouseout', WBM_TabularReport.getFunction(_row, "out"), false);
						WBM_global.addEvent(_row, 'click', WBM_TabularReport.getFunction(_row, "click"), false);
					}
				}
			}
		}
	}, 

	getFunction: function(target, func)
	{
		return function(e){ eval("WBM_TabularReport." + func)(e, target) }
	}, 
	
	over: function(e, targetElement)
	{
		var el = window.event ? targetElement : e ? e.currentTarget : null;
		if (!el) return;
		
		el.className = "tabularReportParentHoverRow";
	}, 
	
	out: function(e, targetElement)
	{
		var el = window.event ? targetElement : e ? e.currentTarget : null;
		if (!el) return;
		
		el.className = "tabularReportParentNormalRow";
	}, 
	
	click: function(e, targetElement)
	{
	}
}

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);
}


function WBM_inArray(arr, value)
{
	var i;
	for(i = 0; i < arr.length; i++)
	{
		if(arr[i] === value)
		{
			return true;
		}
	}
	return false;
};

function WBM_push(arr, el)
{
	if(arr == undefined)
	{
		arr = new Array;
	}
	arr[arr.length] = el;
}

function WBM_pop(arr, el)
{
	for(i = 0; i < arr.length; i++)
	{
		if(arr[i] == el)
		{
			arr.splice(i, 1);
			i = i - 1;
		}
	}
}

function WBM_emptyArray(arr)
{
	for(i = 0; i < arr.length; i++)
	{
		arr.splice(i, 1);
		i = i - 1;
	}
}

function WBM_replaceAll(str, searchTerm, replaceWith, ignoreCase)
{
	var regex = "/" + searchTerm + "/g";
	if(ignoreCase)
	{
		regex += "i";
	}
	
	return str.replace(eval(regex), replaceWith);
}

/* get, set, and delete cookies */
function getCookie(name)
{
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) )
	{
		return null;
	}
	if ( start == -1 ) return null;
	var end = document.cookie.indexOf( ";", len );
	if ( end == -1 ) end = document.cookie.length;
	return unescape( document.cookie.substring( len, end ) );
}
	
function setCookie( name, value, expires, path, domain, secure )
{
	var today = new Date();
	today.setTime( today.getTime() );
	if ( expires )
	{
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );
	document.cookie = name+"="+escape( value ) +
		( ( expires ) ? ";expires="+expires_date.toGMTString() : "" ) + //expires.toGMTString()
		( ( path ) ? ";path=" + path : "" ) +
		( ( domain ) ? ";domain=" + domain : "" ) +
		( ( secure ) ? ";secure" : "" );
}
	
function deleteCookie( name, path, domain )
{
	if ( getCookie( name ) ) document.cookie = name + "=" +
			( ( path ) ? ";path=" + path : "") +
			( ( domain ) ? ";domain=" + domain : "" ) +
			";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

/* quick getElement reference */
function $()
{
	var elements = new Array();
	for (var i = 0; i < arguments.length; i++)
	{
		var element = arguments[i];
		if (typeof element == 'string')
		{
			element = document.getElementById(element);
		}
		if (arguments.length == 1)
		{
			return element;
		}
		elements.push(element);
	}
	return elements;
}

function oc(a)
{
    var o = {};
    for(var i=0;i<a.length;i++)
    {
        o[a[i]]='';
    }
    return o;
}

String.prototype.trim = function() { return this.replace(/(^[\s\xA0]+|[\s\xA0]+$)/g, ''); }

function change_button_img_src(_button_id,_to_url) {

    document.getElementById(_button_id).src = _static_url;
}

function is_email(email) {
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   if(reg.test(email) == false) {
      return false;
   }
   return true;
}

function WBM_getRand() 
{
    return parseInt(Math.random()*99999999);
}

function WBM_scrollTop() {

    scroll(0,0);
    wrapper.scrollTop = 0;
}

function WBM_loadJSFile(filename) 
{
    var fileref=document.createElement('script')
    fileref.setAttribute("type","text/javascript")
    fileref.setAttribute("src", filename)
    if (typeof fileref!="undefined")
    {
        document.getElementsByTagName("head")[0].appendChild(fileref);
    }
}

function WBM_loadCSSFile(filename) 
{
    var fileref=document.createElement("link")
    fileref.setAttribute("rel", "stylesheet")
    fileref.setAttribute("type", "text/css")
    fileref.setAttribute("href", filename)
    if (typeof fileref!="undefined")
    {
        document.getElementsByTagName("head")[0].appendChild(fileref);
    }
}
		
// This function gets called when the end-user clicks on some date.
function onCalendarSelectDate(cal, date)
{
	if(cal.input_field)
	{
		cal.input_field.value = cal.date.print(calendar.input_field_format);
		if(typeof cal.input_field.onchange == "function")
		{
			cal.input_field.onchange();
		}
	}
	if(cal.display_area && cal.display_area.innerHTML)
	{
		cal.display_area.innerHTML = cal.date.print(calendar.display_area_format);
	}

	if(cal.dateClicked)
	{
	    cal.callCloseHandler();
	}
}

// And this gets called when the end-user clicks on the _selected_ date,
// or clicks on the "Close" button.  It just hides the calendar without
// destroying it.
function onCalendarClose(cal)
{
	cal.hide(); // hide the calendar
	calendar = null;
}

// This function shows the calendar under the element having the given id.
// It takes care of catching "mousedown" signals on document and hiding the
// calendar if the click was outside.
function showCalendar(_input_field, _input_field_format, _display_area, _display_area_format, _disable_date_func, _align, _shows_other_months, _shows_week_numbers, _shows_time)
{
	if(!$(_input_field) || !$(_display_area))
	{
		return false;
	}
	
	if(calendar != null)
	{
		calendar.hide();
	}
	else
	{
		var cal = new Calendar(true, null, onCalendarSelectDate, onCalendarClose);
		
		if(typeof _disable_date_func == "function")
		{
			cal.setDateStatusHandler(_disable_date_func);
		}
		
		if(typeof _shows_time == "string")
		{
			cal.showsTime = true;
			cal.time24 = (_shows_time == "24");
		}
		cal.showsOtherMonths = _shows_other_months ? _shows_other_months : true;
		cal.weekNumbers = _shows_week_numbers ? _shows_week_numbers : false;
		
		calendar = cal;
		cal.create();
	}

	calendar.setDateFormat(_input_field_format); // set the specified date format
	calendar.parseDate($(_input_field).value); // try to parse the text in field
	calendar.input_field = $(_input_field);
	calendar.input_field_format = _input_field_format;
	calendar.display_area = $(_display_area);
	calendar.display_area_format = _display_area_format;
	
	calendar.showAtElement($(_display_area), _align ? _align : "BL");
	
	return false;
}

function compare_dates(date1, date2)
{
	var _date1 = new Date(date1.getFullYear(), date1.getMonth(), date1.getDate());
	var _date2 = new Date(date2.getFullYear(), date2.getMonth(), date2.getDate());
	
	return _date1 < _date2 ? true : false
}
