/**
 * File for a XProCommon pseudo namespace
 *
 * @version	SVN: $Id: common.js 10323 2008-05-08 14:46:23Z msynak $
 */


/**
 * XProCommon pseudo namespace
 */
function XProCommon()
{
	// empty
}


/**
 * @type boolean
 */
XProCommon.ie = window.createPopup && window.showModalDialog;


/**
 * Session expiration
 *
 * @return void
 */
XProCommon.expireSession = function()
{
	alert("Your session has expired. Please log in again.");
	window.location = mainurl;
};


/* TODO: check if this should be run
if (session_expiration)
{
	window.setTimeout("XProCommon.expireSession()", session_expiration * 1000);
}
*/


/**
 * Open debug panel
 *
 * @return void
 */
XProCommon.openDebug = function()
{
	var dbg = document.getElementById("Debug");
	if (null == dbg)
	{
		return;
	}

	with (dbg.style)
	{
		height = "100px";
		width = "100%";
	}
};


/**
 * Close debug panel
 *
 * @return void
 */
XProCommon.closeDebug = function()
{
	var dbg = document.getElementById("Debug");
	if (null == dbg)
	{
		return;
	}

	with (dbg.style)
	{
		height = "30px";
		width = "30px";
	}
};


/**
 * @param s (string)
 * @return boolean
 */
XProCommon.isEmpty = function(s)
{
	return null == s || "" == s;
};


/**
 * @param obj (object)
 * @param event_name (string)
 * @param listener (function)
 * @return void
 */
XProCommon.addEventListener = function(obj, event_name, listener)
{
	if (obj.addEventListener)
	{
		obj.addEventListener(event_name, listener, false);
	}
	else if (obj.attachEvent)
	{
		obj.attachEvent("on" + event_name, listener);
	}
};


/**
 * @param e (object)
 * @return void
 */
XProCommon.stopPropagation = function(e)
{
	if (e.stopPropagation)
	{
		e.stopPropagation();
	}
	else if (window.event)
	{
		e.cancelBubble = true;
	}
};


/**
 * @param uri (string)
 * @return void
 */
XProCommon.go2 = function(uri)
{
	XProCommon.disableSubmits();

	document.location.href = uri;
};


/**
 * Disable inputs type submit
 *
 * @return void
 */
XProCommon.disableSubmits = function()
{
	// TODO: Other submit elements: button type submit, button without type
	var inputs = document.getElementsByTagName("input");

	for (var i = 0; i < inputs.length; i++)
	{
		if ("submit" == inputs[i].type || "image" == inputs[i].type)
		{
			inputs[i].setAttribute("disabled", "yes");
		}
	}

	setTimeout("XProCommon.showWaitMessage()", 1000);
};

XProCommon.showWaitMessage = function()
{
	var bg_box = document.getElementById("WaitBackground");
	if (null == bg_box)
	{
		return;
	}

	var msg_box = document.getElementById("WaitMessage");
	if (null == msg_box)
	{
		return;
	}
	bg_box.style.height = XProCommon.maxViewportProperty("Height") + "px";
	bg_box.style.width = XProCommon.maxViewportProperty("Width") + "px";

	if (XProCommon.ie)
	{
		XProCommon.switchAllSelects("hidden");
	}

	bg_box.style.display = "block";
	msg_box.style.display = "block";
};

/**
 * @return int
 */
XProCommon.maxViewportProperty = function(name)
{
	var value = document.body['scroll' + name];

	if (document.body['offset' + name] > value)
	{
		value = document.body['offset' + name];
	}

	if (self['inner' + name] && self['inner' + name] > value)
	{
		value = self['inner' + name];
	}

	if (document.documentElement['client' + name] && document.documentElement['client' + name] > value)
	{
		value = document.documentElement['client' + name];
	}

	return value;
};

/**
 * @param form (object)
 * @return boolean
 */
XProCommon.checkNotification = function(form)
{
	var fields = form.getElementsByTagName("input");

	for (var n = 0; n < fields.length; n++)
	{
		if ("checkbox" == fields[n].type)
		{
			if (false == fields[n].checked)
			{
				alert("Please, read and confirm all notifications");
				return false;
			}
		}
	}

	return true;
};


/**
 * @param id (string)
 * @return boolean
 */
XProCommon.switchDisplay = function(id)
{
	var box = document.getElementById(id);
	if (null == box)
	{
		return false;
	}

	with (box.style)
	{
		if ("block" == display)
		{
			display = "none";
			return false;
		}
		else
		{
			display = "block";
			return true;
		}
	}
};


/**
 * @param string year_field_id
 * @param string month_field_id
 * @param string day_field_id
 * @return boolean
 */
XProCommon.checkDateFromFields = function(field_id_prefix, year_field_id, month_field_id, day_field_id)
{
	var year_field = document.getElementById(field_id_prefix + year_field_id);
	if (null == year_field)
	{
		return XProCommon.createStatus(false, null);
	}

	var month_field = document.getElementById(field_id_prefix + month_field_id);
	if (null == month_field)
	{
		return XProCommon.createStatus(false, null);
	}

	var day_field = document.getElementById(field_id_prefix + day_field_id);
	if (null == day_field)
	{
		return XProCommon.createStatus(false, null);
	}

	if ( !XProCommon.isEmpty(year_field.value) && !XProCommon.isEmpty(month_field.value) && !XProCommon.isEmpty(day_field.value))
	{
		var date = new Date(year_field.value, month_field.value - 1, day_field.value);

		if (date.getFullYear() != year_field.value ||
			date.getMonth() != month_field.value - 1 ||
			date.getDate() != day_field.value)
		{
			XProForm.switchFieldTip(year_field, true);
			XProForm.switchFieldTip(month_field, true);
			XProForm.switchFieldTip(day_field, true);
			return XProCommon.createStatus(false, year_field);
		}
	}
	else if ( !XProCommon.isEmpty(year_field.value) ||
		!XProCommon.isEmpty(month_field.value) ||
		!XProCommon.isEmpty(day_field.value))
	{
		XProForm.switchFieldTip(year_field, true);
		XProForm.switchFieldTip(month_field, true);
		XProForm.switchFieldTip(day_field, true);
		return XProCommon.createStatus(false, year_field);
	}

	return XProCommon.createStatus(true, null);
};

/**
 * Creates a standard status object/array
 *
 * @param correct (boolean)
 * @param data (boolean|number|string|object)
 */
XProCommon.createStatus = function(correct, data)
{
	var status = new Array();
	status['correct'] = correct;
	status['data'] = data;

	return status;
}

/**
 * Expands/collapses element such as table.
 * Collapsed table displays header only.
 *
 * @param id ID of element to collapse/expand
 */
XProCommon.expandCollapse = function(id)
{
	var body = document.getElementById(id + "_body");
	var foot = document.getElementById(id + "_foot");
	var collapse_button = document.getElementById(id + "_collapse_button");

	if ("" == body.style.display &&
		"" == foot.style.display)
	{
		body.style.display = "none";
		foot.style.display = "none";
		collapse_button.src = XProCommon.resurl + "img/expand.gif";
		collapse_button.alt = "+";
		collapse_button.title = "Click to expand";
	}
	else
	{
		body.style.display = "";
		foot.style.display = "";
		collapse_button.src = XProCommon.resurl + "img/collapse.gif";
		collapse_button.alt = "-";
		collapse_button.title = "Click to collapse";
	}
}

/**
 * Returns a string representing the source code of the object.
 *
 * @private
 * @param element (boolean|number|string|object)
 * @return string
 */
XProCommon.toSource = function()
{
	var str = "(";

	switch (typeof this)
	{
		case "boolean":
			str += "new Boolean(" + this + ")";
			break;

		case "number":
			str += "new Number(" + this + ")";
			break;

		case "string":
			str += "new String(\"" + this + "\")";
			break;

		case "object":
			str += "{";

			for (var attribute in this)
			{
				if ("function" == typeof this[attribute])
				{
					continue;
				}

				if (str.length > 2)
				{
					str += ", ";
				}

				str += "'" + attribute + "':";

				if ("string" == typeof this[attribute])
				{
					str += "\"" + this[attribute] + "\"";
				}
				else
				{
					str += this[attribute];
				}
			}

			str += "}";
			break;

		default:
			throw("New type for XProCommon.toSource " + typeof this);
	}

	str += ")";

	return str;
};


/**
 * @param visibility (visible|hidden)
 * @return void
 */
XProCommon.switchAllSelects = function(visibility)
{
	var select_list = document.getElementsByTagName("select");

	if (null == select_list)
	{
		return;
	}

	for (var n = 0; n < select_list.length; n++)
	{

		select_list[n].style.visibility = visibility;
	}
};


/**
 * Method for DropDownMenu in IE
 *
 * @return void
 */
XProCommon.addMenuHover = function()
{
	var menu_node = document.getElementById("LeftMainMenu");

	if (null == menu_node)
	{
		return;
	}

	var li_list = menu_node.getElementsByTagName("li");

	if (null == li_list)
	{
		return;
	}

	for (var n = 0; n < li_list.length; n++)
	{
		li_list[n].onmouseover = function()
		{
			XProCommon.switchAllSelects("hidden");

			for (var x = 0; x < this.childNodes.length; x++)
			{
				this.childNodes[x].style.left = "auto";
			}
		}

		li_list[n].onmouseout = function()
		{
			for (var x = 0; x < this.childNodes.length; x++)
			{
				this.childNodes[x].style.left = "-9999px";
			}

			XProCommon.switchAllSelects("visible");
		}
	}
};


if ( !Object.prototype.toSource)
{
	/**
	 * Returns a string representing the source code of the object.
	 *
	 * @return string
	 */
	Object.prototype.toSource = XProCommon.toSource;
}

/**
 * Strip whitespace from the beginning of a string
 *
 * @return string
 */
String.prototype.leftTrim = new Function("return this.replace(/^\\s+/, '')");


/**
 * Strip whitespace from the end of a string
 *
 * @return string
 */
String.prototype.rightTrim = new Function("return this.replace(/\\s+$/, '')");


/**
 * Strip whitespace from a string
 *
 * @return string
 */
String.prototype.allTrim = new Function("return this.replace(/\\s+/g, '')");


/**
 * Strip whitespace from the beginning and end of a string
 *
 * @return string
 */
String.prototype.trim = new Function("return this.replace(/^\\s+|\\s+$/g, '')");


/**
 * Returns all child elements with given class name (execute on document.body to get all)
 *
 * @param string class_name
 * @param Object element
 * @return Array
 */
XProCommon.getElementsByClassName = function(class_name, element)
{
	var elements = new Array();

    if (class_name == element.className)
	{
		elements.push(element);
    }

    for (var i = 0; i < element.childNodes.length; i++)
	{
        elements = elements.concat(XProCommon.getElementsByClassName(class_name, element.childNodes[i]));
	}

	return elements;
}


/**
 * Adds event listeners to all tooltip links (executed on page load)
 */
XProCommon.setTooltips = function()
{
	var elements = XProCommon.getElementsByClassName('TooltipLink', document.body);

	for (var i = 0; i < elements.length; i++)
	{
		XProCommon.addEventListener(elements[i], "mouseover", Function("XProCommon.showElement('" + elements[i].id + "_tip')"));
		XProCommon.addEventListener(elements[i], "mouseout", Function("XProCommon.hideElement('" + elements[i].id + "_tip')"));
	}

}


/**
 * Shows block element
 *
 * @param {Object} id
 */
XProCommon.showElement = function(id)
{
	var tooltip_box = document.getElementById(id);

	tooltip_box.style.display = "block";
}


/**
 * Hides block element
 *
 * @param {Object} id
 */
XProCommon.hideElement = function(id)
{
	var tooltip_box = document.getElementById(id);

	tooltip_box.style.display = "none";
}