
var ie_version;
var ie = navigator.userAgent.toLowerCase().indexOf("msie")>-1 && navigator.userAgent.toLowerCase().indexOf("opera")< 0;
var ie7 = navigator.userAgent.toLowerCase().indexOf("msie 7.")>-1;
if (ie) { ie_version = parseInt (navigator.userAgent.toLowerCase().substr (navigator.userAgent.toLowerCase().indexOf("msie") + 5, 1)); }

/* ---------------------------------------------------------------------------------------------------------------------------------------- */

function joGetNode (node_id) { return document.getElementById (node_id); }

/* ---------------------------------------------------------------------------------------------------------------------------------------- */

function joToggleNode (node, new_state)
{
	if (!new_state) new_state = node.style.display == "none" ? "block" : "none";
	node.style.display = new_state;
}

/* ---------------------------------------------------------------------------------------------------------------------------------------- */

function joToggleNodes()
{
	var toggle_state = joToggleNodes.arguments[0];
	for (var i = 1; i < joToggleNodes.arguments.length; i++)
	{
		joToggleNode (joGetNode(joToggleNodes.arguments[i]), toggle_state);
	}
}

/* ---------------------------------------------------------------------------------------------------------------------------------------- */

function joShowExclusively (parent_node_id, show_node_id, nodes_type)
{
	var parent_node 	= joGetNode(parent_node_id);
	var show_mode		= "block";
	
	if (!ie)
	{
		switch (nodes_type)
		{
			case "TR": 		show_mode = "table-row"; break;
			case "TD": 		show_mode = "table-cell"; break;
			default: 		show_mode = "block"; break;
		}
	}
	
	if (parent_node)
	{
		if (!nodes_type) nodes_type = "DIV";
		var navblock_nodes = parent_node.childNodes;
		for (var i = 0; i < navblock_nodes.length; i++)
		{
			if (navblock_nodes[i].nodeName == nodes_type) 
			{
				var new_state = navblock_nodes[i].id == show_node_id ? show_mode : "none";
				joToggleNode (navblock_nodes[i], new_state);
			}
		}
	}
}

/* ---------------------------------------------------------------------------------------------------------------------------------------- */

function joNumStr (string, length)
{
	var result = "";
	var zerocount = length - string.length;
	if (zerocount > 0)
	{
		for (i = 0; i != zerocount; i++)
			result += "0";
	}
	result += string;
	return result;
}

/* ---------------------------------------------------------------------------------------------------------------------------------------- */

function joAddEvent (node, oneEvent, handler) 
{
    if(document.body.addEventListener) node.addEventListener (oneEvent, handler, false);
    if(document.body.attachEvent) node.attachEvent ("on" + oneEvent, handler);
}

/* ---------------------------------------------------------------------------------------------------------------------------------------- */

function joGetPos (node) 
{
	var curleft = curtop = 0;
	if (node.offsetParent) 
	{
		do 
		{
			curleft += node.offsetLeft;
			curtop += node.offsetTop;
		} 
		while (node = node.offsetParent);
	}
	return [curleft, curtop];
}

/* ---------------------------------------------------------------------------------------------------------------------------------------- */

function joGetSize (node)
{
	return [node.offsetWidth, node.offsetHeight];
}

/* ---------------------------------------------------------------------------------------------------------------------------------------- */

function joGetWinSize()
{
	return [ie ? document.body.clientWidth : window.innerWidth, ie ? document.body.clientHeight : window.innerHeight];
}

/* ---------------------------------------------------------------------------------------------------------------------------------------- */

function joAddHTMLChildNode (parent_node, node_type, node_class, node_id)
{
	var new_node = document.createElement (node_type);
	parent_node.appendChild (new_node);
	if (node_class && node_class != "")	new_node.setAttribute ("class", node_class);
	if (node_id && node_id != "")	new_node.setAttribute ("id", node_id);
	return new_node;
}

/* ---------------------------------------------------------------------------------------------------------------------------------------- */

function joAddTextChildNode (parent_node, node_text)
{
	var new_node = document.createTextNode (node_text);
	parent_node.appendChild (new_node);
	return new_node;
}

/* ---------------------------------------------------------------------------------------------------------------------------------------- */

function joRemoveChildNodes (node)
{
	while (node && node.hasChildNodes())
	{
		joRemoveChildNodes (node.firstChild);
		var subnode = node.removeChild (node.firstChild);
		subnode = null;
	}
}
/* ---------------------------------------------------------------------------------------------------------------------------------------- */

var one_day = 1000 * 60 * 60 * 24;

function joDateObj (offset_from_today)
{
	return new Date ((new Date()).getTime() + one_day * offset_from_today);
}

/* ---------------------------------------------------------------------------------------------------------------------------------------- */

function joGetWeekdayNo (dateObj)
{
	var day_no = (dateObj.getDay() + 6) % 7;
	return day_no;
}

/* ---------------------------------------------------------------------------------------------------------------------------------------- */

function joGetCalWeekNo (dateObj) 
{
  var jh 		= dateObj.getFullYear() + 1;
  var weekno 	= _calDiff (dateObj, jh);
  while (weekno < 1) { jh--; weekno = _calDiff (dateObj, jh); }
  return weekno;
}

function _calDiff (dateObj, year) 
{
  var d4j 		= new Date (year, 0, 4);
  var wt4j 		= (d4j.getDay()+6)%7;
  var m1wjT		= Math.floor (0.01 + d4j.getTime() / 864e5-wt4j);
  var dateT		= Math.floor (0.01 + dateObj.getTime() / 864e5);
  return 		Math.floor (1 + (dateT - m1wjT) / 7);
}

/* ---------------------------------------------------------------------------------------------------------------------------------------- */

function joGetDateIDString (dateObj)
{
	return dateObj.getFullYear() + joNumStr (dateObj.getMonth() + 1 + "", 2) + joNumStr (dateObj.getDate() + "", 2);
}

/* ---------------------------------------------------------------------------------------------------------------------------------------- */

function joGetFirstElement (node, tagname, attributes)
{
	var result = joGetElements (node, tagname, attributes, null, true);
	return result ? result[0] : null;
}

/* ---------------------------------------------------------------------------------------------------------------------------------------- */

function joGetElements (node, tagname, attributes, _result, _return_only_one)
{
	var result = [];
	var match = true;
	if (_result) result = _result;
	if (node.nodeType == 1 || node.nodeType == 9)
	{
		if (tagname != "" && node.nodeName != tagname) match = false;
		else if (attributes)
		{
			if (node.nodeType == 1)
			{
				for (var i = 0; i < attributes.length; i++)
				{
					if (node.getAttribute (attributes[i][0]) != attributes[i][1]) match = false;
				}
			}
			else match = false;
		}
				
		if (match) 	result.push (node);

		for (var i = 0; (!result.length || !_return_only_one) && i < node.childNodes.length; i++) 
		{
			joGetElements (node.childNodes[i], tagname, attributes, result, _return_only_one);
		}
	}
	return result;
}

/* ---------------------------------------------------------------------------------------------------------------------------------------- */

function OpenWindow (win_href, win_title, win_width, win_height, win_center, with_scrollbars, win_resizable, with_bar)
{

	if (win_height > screen.availHeight - 50)
		win_height = screen.availHeight - 50;

	win_width = win_width - 0;
	if (with_scrollbars == 'true')
	{
		win_width += 20;
	}
	
	var win_flags = "width=" + win_width + ",height=" + win_height;
	win_flags += ",resizable=yes,dependent=yes";
	
	if (with_scrollbars == 'true')
	{
		win_flags += ",scrollbars=yes";
	}
	
	if (win_center)
	{
		var win_left = Math.ceil ((screen.availWidth - win_width) / 2);
		var win_top  = Math.ceil ((screen.availHeight - win_height) / 3);
		win_flags	+= ",left=" + win_left + ",top=" + win_top;
	}
	
	win_flags += ",resizable=" + ((win_resizable == "true") ? "yes" : "no");
	win_flags += ",location=" + ((with_bar == "true") ? "yes" : "no");
	win_flags += ",menubar=" + ((with_bar == "true") ? "yes" : "no");
	win_flags += ",toolbar=" + ((with_bar == "true") ? "yes" : "no");

	window.open (win_href, win_title, win_flags);
}