/*
	name - name of the cookie
	value - value of the cookie
	[expires] - expiration date of the cookie (defaults to end of current session)
	[path] - path for which the cookie is valid (defaults to path of calling document)
	[domain] - domain for which the cookie is valid (defaults to domain of calling document)
	[secure] - Boolean value indicating if the cookie transmission requires a secure transmission
	* an argument defaults when it is assigned null as a placeholder
	* a null placeholder is not required for trailing omitted arguments
*/

function setCookie(name, value, expires, path, domain, secure) {
	var curCookie = name + "=" + escape(value) +
	((expires) ? "; expires=" + expires.toGMTString() : "") +
	((path) ? "; path=" + path : "") +
	((domain) ? "; domain=" + domain : "") +
	((secure) ? "; secure" : "");
	document.cookie = curCookie;
}

/*
	name - name of the desired cookie
	return string containing value of specified cookie or null if cookie does not exist
*/

function getCookie(name) {
	var dc = document.cookie;
	var prefix = name + "=";
	var begin = dc.indexOf("; " + prefix);
	if(begin == -1)
	{
    	begin = dc.indexOf(prefix);
		if(begin != 0) return null;
	}
	else
		begin += 2;
	var end = document.cookie.indexOf(";", begin);
	if(end == -1) end = dc.length;
	return unescape(dc.substring(begin + prefix.length, end));
}

/*
	name - name of the cookie
	[path] - path of the cookie (must be same as path used to create cookie)
	[domain] - domain of the cookie (must be same as domain used to create cookie) path and domain default if assigned null or omitted if no explicit argument proceeds
*/

function deleteCookie(name, path, domain) {
	if(getCookie(name))
	{
		document.cookie = name + "=" + ((path) ? "; path=" + path : "") + ((domain) ? "; domain=" + domain : "") + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
	}
}

// date - any instance of the Date object
// * hand all instances of the Date object to this function for "repairs"

function fixDate(date) {
	var base = new Date(0);
	var skew = base.getTime();
	if(skew > 0) date.setTime(date.getTime() - skew);
}

// pobierz obiekt
function _f(objName)
{
	return document.getElementById(objName);
}

// przypisz zdarzenie
 function addEvent(node, eventName, func)
 {
	if(node.addEventListener)
		node.addEventListener(eventName, func, false);
	else
		node.attachEvent('on' + eventName, func);
}

// formatuj date
function formatDate(aDate, aFormat)
{
	if(arguments.length < 2) aFormat = '%Y-%m-%d';
	if(aDate instanceof Date)
	{
		var year = aDate.getYear();
		if(year < 1000) year += 1900;
		var month = aDate.getMonth() + 1;
		if(month < 10) month = '0'+month;
		var day = aDate.getDate();
		if(day < 10) day = '0'+day;
	}
	else
	{
		year = aDate.substr(0, 4);
		month = aDate.substr(5, 2);
		day = aDate.substr(8, 2);
	}
	aFormat = aFormat.replace('%Y', year);
	aFormat = aFormat.replace('%m', month);
	aFormat = aFormat.replace('%d', day);
	
	return aFormat;
}

function showMainScrollBar(AState)
{
	var Body = document.getElementsByTagName('html');
	if(Body.length == 1)
	{		
		if(AState) Body[0].style.overflow = 'auto';
		else Body[0].style.overflow = 'hidden';
	}	
}

function setObjectDisabled(AObjectID, AState)
{
	if(AObjectID != '')
	{
		var Obj = document.getElementById(AObjectID);
		if(Obj) Obj.disabled = AState;
	}
}

function removeObject(AObjectID)
{
	if(AObjectID != '')
	{
		var Obj = document.getElementById(AObjectID);
		if(Obj) Obj.parentNode.removeChild(Obj);
	}
}

function getXMLValue(xmldoc, tag)
{
	var info = xmldoc.getElementsByTagName(tag);
	if(info.length > 0 && info[0].firstChild)
		return info[0].firstChild.nodeValue;
	else
		return null;
}

function ShowHide(AId)
{
	var Obj = document.getElementById(AId);
	if(!Obj) return;
	
	if(Obj.style.display == '')
		Obj.style.display = 'none';
	else
		Obj.style.display = '';	
}

// return the value of the radio button that is checked
// return an empty string if none are checked, or
// there are no radio buttons
function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

// set the radio button with the given value as being checked
// do nothing if there are no radio buttons
// if the given value does not exist, all the radio buttons
// are reset to unchecked
function setCheckedValue(radioObj, newValue) {
	if(!radioObj)
		return;
	var radioLength = radioObj.length;
	if(radioLength == undefined) {
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
	for(var i = 0; i < radioLength; i++) {
		radioObj[i].checked = false;
		if(radioObj[i].value == newValue.toString()) {
			radioObj[i].checked = true;
		}
	}
}
