
/* ********************************************************************************************************** */

var JO_DRAG_TRANSPARENCY 					= 80;

/* ********************************************************************************************************** */


var ie = navigator.userAgent.toLowerCase().indexOf("msie")>-1 && navigator.userAgent.toLowerCase().indexOf("opera")< 0;

var gjoMovingActive = false;
var gjoMovingDivNode = null;
var gjoMovingDivMousePos = null;
var gjoMovingDivPos = new Array();

/* ********************************************************************************************************** */

function joAddImageChildNode (node, imgsrc, imgid)
{
	var new_img = document.createElement("IMG"); 
	node.appendChild (new_img);
	new_img.setAttribute ("src", imgsrc);
	if (imgid && imgid != '')
		joDOM.SetAttribute (new_img, "id", imgid);
	
	return new_img;
}

/* ********************************************************************************************************** */

function joAddTableChildNode (parent_node, table_class, table_id)
{
	var new_table = joAddHTMLChildNode (parent_node, "TABLE", table_class, table_id);
	joDOM.SetAttribute (new_table, "cellspacing", "0");
	joDOM.SetAttribute (new_table, "cellpadding", "0");
	return joAddHTMLChildNode (new_table, "TBODY");
}

/* ********************************************************************************************************** */

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 != "")	joDOM.SetAttribute (new_node, "class", node_class);
	if (node_id && node_id != "")	joDOM.SetAttribute (new_node, "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 joAddFormFieldChildNode (parent_node, field_type, field_name, field_class, field_size, length_max, field_title)
{
	var field_node = document.createElement ("input");
	joDOM.SetAttribute (field_node, "type", field_type);
	joDOM.SetAttribute (field_node, "name", field_name);
	if (field_class && field_class != "")		joDOM.SetAttribute (field_node, "class", field_class);
	if (field_size && field_size != "") 		joDOM.SetAttribute (field_node, "size", field_size);
	if (field_title && field_title != "") 	joDOM.SetAttribute (field_node, "title", field_title);
	if (length_max && length_max > 0) joDOM.SetAttribute (field_node, "maxlength", length_max);
	parent_node.appendChild (field_node);
	return field_node;
}

/* ********************************************************************************************************** */

function joAddSelectOptionNode (select_node, option_value, option_text)
{
	var option_node;
	
	option_node								= joAddHTMLChildNode (select_node, "OPTION");
		option_node.value					= option_value;
		option_node.innerHTML			= option_text;
	
}

/* ********************************************************************************************************** */

function joSetTextColor (node, textcolor)
{
	if (node)
	{
		node.style.color = textcolor;
	}
}

/* ********************************************************************************************************** */
<!-- dynamicly adding event handlers to nodes (fixes ie bug) -->

function joAddEvent (node, oneEvent, handler) 
{
    if(document.body.addEventListener) node.addEventListener (oneEvent, handler, false);
    if(document.body.attachEvent) node.attachEvent ("on" + oneEvent, handler);
}

/* ********************************************************************************************************** */

function joShowDivDependend (div_name, condition)
{
	if (condition) 	joShowDivByName (div_name); 
	else 					joHideDivByName (div_name);
}

/* ********************************************************************************************************** */

function joShowDiv (obj_div)
{
	if (obj_div)
	{
		obj_div.style.visibility = "visible";
		obj_div.style.display = "block";
	}
}

/* ********************************************************************************************************** */

function joShowDivByName (div_name)
{
	var obj_div = joFIND (div_name);
	if (obj_div)
	{
		obj_div.style.visibility = "visible";
		obj_div.style.display = "block";
	}
}

/* ********************************************************************************************************** */

function joHideDiv (obj_div)
{
	if (obj_div)
	{
		obj_div.style.visibility = "hidden";
		obj_div.style.display = "none";
	}
}

/* ********************************************************************************************************** */

function joHideDivByName (div_name)
{
	var obj_div = joFIND (div_name);
	if (obj_div)
	{
		obj_div.style.visibility = "hidden";
		obj_div.style.display = "none";
	}
}

/* ********************************************************************************************************** */

function joShowPopupByName (div_name)
{
	var obj_div = joFIND (div_name);
	if (obj_div)
	{
		obj_div.style.visibility = "visible";
	}
}

/* ********************************************************************************************************** */

function joHidePopupByName (div_name)
{
	var obj_div = joFIND (div_name);
	if (obj_div)
	{
		obj_div.style.visibility = "hidden";
	}
}

/* ********************************************************************************************************** */

function joGetDivInfo (node, info_type)
{
	var result = 0;
	switch (info_type)
	{
		case "left": result = Number (node.style.left.replace(/px/, "")); break;
		case "top": result = Number (node.style.top.replace(/px/, "")); break;
		case "width": result = Number (node.style.width.replace(/px/, "")); break;
		case "height": result = Number (node.style.height.replace(/px/, "")); break;
	}
	return result;
}

/* ********************************************************************************************************** */

function joGetWinInfo (info_type)
{
	var result = 0;
	switch (info_type)
	{
		case "width": result = ie ? document.body.clientWidth : window.innerWidth; break;
		case "height": result = ie ? document.body.clientHeight : window.innerHeight; break;
	}
	return result;
}

/* ********************************************************************************************************** */

function joCenterPopupDiv (div_id, div_width, div_height)
{
	var divnode = joFIND (div_id);
	if (divnode)
	{
		var win_height = joGetWinInfo ("height");
		var win_width = joGetWinInfo ("width");
		divnode.style.left = Math.floor ((win_width - div_width) / 2);
		divnode.style.width = div_width;
		if (div_height > 0) 
		{
			divnode.style.top = Math.floor ((win_height - div_height) / 3);
			divnode.style.height = div_height;
		}
		else
		{
			divnode.style.top = Math.floor ((win_height ) / 4);
		}
	}
}

/* ********************************************************************************************************** */

function joInitMovableDivs()
{
	var allObjects = document.getElementsByTagName ("div");
	var ObjClass;

	for (i = 0; i < allObjects.length; i++)
	{
		ObjClass = allObjects[i].className + "";
		if (ObjClass.indexOf ("joMovableDiv") != -1)
		{
			allObjects[i].onmousedown = joInitMoveDiv;
		}
	}

	for (i = 0; i < allObjects.length; i++)
	{
		ObjClass = allObjects[i].className + "";
		if (ObjClass.indexOf ("joMovableActivator") != -1)
		{
			allObjects[i].onmousedown = joInitMoveDivActivator;
		}
	}

	document.body.onmouseup = joStopMoveDiv;	
	document.body.onmousedown = joStartMoveDiv
	document.body.onmousemove = joMoveDiv;
}	

/* ********************************************************************************************************** */


function joInitMoveDiv()
{
	if (gjoMovingActive)
	{
		gjoMovingDivNode = this;
		gjoMovingDivPos["x"] = joGetDivInfo (gjoMovingDivNode, "left"); 
		gjoMovingDivPos["y"] = joGetDivInfo (gjoMovingDivNode, "top");
		joSetTransparency (gjoMovingDivNode, JO_DRAG_TRANSPARENCY);
	}
}	

function joInitMoveDivActivator()
{
	gjoMovingActive = true;
}	

/* ********************************************************************************************************** */

function joStopMoveDiv (event)
{
	if (gjoMovingDivNode)
	{
		joSetTransparency (gjoMovingDivNode, 100);
		gjoMovingDivNode = null;
		gjoMovingActive = false;
	}
}

/* ********************************************************************************************************** */

function joSetTransparency (node, percent)
{
	if (node)
	{
		node.style.filter = "alpha (opacity=" + percent + ");";
		node.style.opacity = + (percent == 100 ? "1" : "0." + percent) + ";";
	}
}

/* ********************************************************************************************************** */

function joMoveDiv (event)
{
	if (gjoMovingActive)
	{
		var currMousePos = joGetMousePos (event);
		if (gjoMovingDivNode)
		{
			if (ie) document.selection.clear();
			var x_delta = currMousePos["x"] - gjoMovingDivMousePos["x"];
			var y_delta = currMousePos["y"] - gjoMovingDivMousePos["y"];
			
			gjoMovingDivNode.style.left = Math.min (Math.max (gjoMovingDivPos["x"] + x_delta, 10), joGetWinInfo ("width") - joGetDivInfo (gjoMovingDivNode, "width") - 10);
			gjoMovingDivNode.style.top =  Math.min (Math.max (gjoMovingDivPos["y"] + y_delta, 10), joGetWinInfo ("height") - joGetDivInfo (gjoMovingDivNode, "height") - 10);
		}
//		joDOM.SetTextByID ('dialog_loadsave_header', currMousePos["x"] + " - " + currMousePos["y"]);
	}
}

/* ********************************************************************************************************** */

function joGetMousePos (event)
{
	var result = new Array;
// ???	if (event)
	{	
		if (ie || (window.event && window.event.clientX))
		{
			result["x"] = window.event.clientX + document.body.scrollLeft;
			result["y"] = window.event.clientY + document.body.scrollTop;
		}
		else
		{
			result["x"] = event.pageX + window.pageXOffset;
			result["y"] = event.pageY + window.pageYOffset;
		}
	}
	return result;
}

/* ********************************************************************************************************** */

function joStartMoveDiv (event)
{
	if (gjoMovingDivNode)
	{
		gjoMovingDivMousePos = joGetMousePos (event);
	}
}

/* ********************************************************************************************************** */

function joGetFormField (form_node, field_name)
{
	var result = null;
	for (var j = 0; !result && j < form_node.elements.length; j++)
	{
		if (form_node.elements[j].name == field_name)
			result = form_node.elements[j];
	}
	return result;
}

/* ********************************************************************************************************** */

function joGetQueryValue (query_name)
{
	var result = "";
	var url_str = document.location.href;
	var query_pos = url_str.indexOf ("?") + 1;
	if (query_pos >= 0)
	{
		var query_str = url_str.substr (query_pos);
		
		if (query_name != "")
		{
			var query_collection = query_str.split ("&");
			var single_query_str;
			var single_query;
			var query_val;
			var found = false;
			
			for (i = 0; i < query_collection.length && !found; i++)
			{
				single_query= query_collection [i].split ("=");
				if (single_query[0] == query_name)
				{
					query_val = single_query[1] + "";
					if (query_val != "undefined")
					{
						result = query_val;
					}
					found = true;
				}
			}
		}
		else
		{
			result = query_str;
		}
		return unescape (result);
	}
}

/* ********************************************************************************************************** */

function joIsBoolean (data)
{
	return ((!isNaN (data) && Number (data)) || data.toUpperCase() == "TRUE" || data.toUpperCase() == "YES" || data == "1");
}

/* ********************************************************************************************************** */

function NumStr (string, length)
{
	string += "";
	var result = "";
	var zerocount = length - string.length;
	if (zerocount > 0)
	{
		for (i = 0; i != zerocount; i++)
			result += "0";
	}
	result += string;
	return result;
}

/* ********************************************************************************************************** */

function joTimeStampStr (date_obj)
{
	var result 	= NumStr (date_obj.getDate(), 2)			+ "."
					+ NumStr ((date_obj.getMonth() + 1), 2) + "."
					+ NumStr (date_obj.getFullYear(), 4) 		+ " "
					+ NumStr (date_obj.getHours(), 2)			+ ":"
					+ NumStr (date_obj.getMinutes(), 2)		+ ":"
					+ NumStr (date_obj.getSeconds(), 2);
	return result;
}

