function setCookie(sName, sValue, oExpires, sPath, sDomain, bSecure) {
	var sCookie = sName + "=" + encodeURIComponent(sValue);
	if(oExpires)
		sCookie += "; expires=" + oExpires.toGMTString();
	if(sPath)
		sCookie += "; path=" + sPath;
	if(sDomain)
		sCookie += "; domain=" + sDomain;
	if(bSecure)
		sCookie += "; secure";		
	document.cookie = sCookie;
}

function getCookie(sName) {
	
	var sRE = "(?:; )?" + sName + "=([^;]*);?";
	var oRE = new RegExp(sRE);
	
	if(oRE.test(document.cookie)) 
		return decodeURIComponent(RegExp["$1"]);
	else
		return null;

}


function getEventSrc(oEvent) {
	if(window.event && window.event.srcElement) { 
  		return window.event.srcElement; 
	}
	else if(oEvent && oEvent.target) { 
  		return oEvent.target; 
	}
}

function preventDefaultEvent(oEvent) {
	if(oEvent.preventDefault)
		oEvent.preventDefault();
	else	
		oEvent.returnValue = false;
}

function getTargetElement(oEvent) {
	return oEvent.relatedTarget.tagName;	
}

function addEventHandler(oTarget, sEventType, fnHandler) {
	if(oTarget.addEventListener) {

		oTarget.addEventListener(sEventType, fnHandler, false); 
	}
	else if(oTarget.attachEvent) {
		oTarget.attachEvent("on" + sEventType, fnHandler); 
	}
	else {
		oTarget["on" + sEventType] = fnHandler;
	}
}

function removeEventHandler(oTarget, sEventType, fnHandler) {
	if(oTarget.removeEventListener) {
		oTarget.removeEventListener(sEventType, fnHandler, false); 
	}
	else if(oTarget.detachEvent) {
		oTarget.detachEvent("on" + sEventType, fnHandler); 
//		if(oTarget.id == "linkPokazPodpisy")
//			alert("REMOVE "+oTarget.id + " " + fnHandler);
	} 
	else {
		oTarget["on" + sEventType] = null;
	}
}

// Removes leading whitespaces
function LTrim( value ) {
	
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
	
}

// Removes ending whitespaces
function RTrim( value ) {
	
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
	
}

// Removes leading and ending whitespaces
function trim( value ) {
	
	return LTrim(RTrim(value));
	
}
//http://snippets.dzone.com/posts/show/701
function showBulkPopup(sUrl, iWidth, iHeight) {
		var x = (screen.width - iWidth) / 2;
		var y = (screen.height - iHeight) / 2;
		window.open(sUrl,'BulkPopup','titlebar=no,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,top=' + x + ',left=' + y + ',width=' + iWidth + ',height=' + iHeight);
}

function showPopup(sUrl, iWidth, iHeight, bScroll) {
		var x = (screen.width - iWidth) / 2;
		var y = (screen.height - iHeight) / 2;
		if(bScroll) {
			window.open(sUrl,'popup','titlebar=no,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,top=' + x + ',left=' + y + ',width=' + iWidth + ',height=' + iHeight);
		}
		else {
			window.open(sUrl,'popup','titlebar=no,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,top=' + x + ',left=' + y + ',width=' + iWidth + ',height=' + iHeight);
		}
}

function showGadgetsPopup(iType, iId, sPath, bForParentType) {
	if(!sPath)
		sPath = "";
	if(bForParentType)
		showPopup(sPath + 'gadgetsPopup.php?type=' + iType + '&id=' + iId + '&forParent=1', 504, 530);
	else
		showPopup(sPath + 'gadgetsPopup.php?type=' + iType + '&id=' + iId, 504, 530);
}

function showJavaGamesPopup(iId, sPath) {
	if(!sPath)
		sPath = "";
	showPopup(sPath + 'javaGamesPopup.php?id=' + iId, 504, 530);
}

function addOptionToList(oList, sName, sValue, bSelected) {
	var oOption = document.createElement("option");
	oOption.appendChild(document.createTextNode(sName));
	
//if(arguments.length == 4) {
		oOption.setAttribute("value", sValue);
//	}
	if(bSelected)
		oOption.setAttribute("selected", "selected");
	
	oList.appendChild(oOption);
	return oOption;
}


function removeAllOptions(oList) {
	for (var i = oList.options.length-1; i >= 0; i--) {
		oList.remove(i);
	}	
}

function showProgress(bShow, sProgressDivId, sNormalDivId) { 
	var oProgressDiv = document.getElementById(sProgressDivId);
	var oNormalDiv = document.getElementById(sNormalDivId);
	if(bShow) {
//		oProgressDiv.className = "pokazBloczek";
//		oNormalDiv.className = "ukryjBloczek";
		oProgressDiv.style.display = "block";
		oNormalDiv.style.display = "none";
	}
	else {
//		oProgressDiv.className = "ukryjBloczek";
//		oNormalDiv.className = "pokazBloczek";	
		if(oProgressDiv)
			oProgressDiv.style.display = "none";
		if(oNormalDiv)			
			oNormalDiv.style.display = "block";	
	}
}


function disableLink(oLink, fnFunction) {
//	alert("disable " + oLink.onclick);
	removeEventHandler(oLink, "click", fnFunction);
	oLink.style.color = "gray";
	removeEventHandler(oLink, "click", preventDefaultEvent);
	addEventHandler(oLink, "click", preventDefaultEvent);
}

function enableLink(oLink, fnFunction) {
//	alert("enable" + oLink.onclick);
	removeEventHandler(oLink, "click", preventDefaultEvent);
	oLink.style.color = null;
	removeEventHandler(oLink, "click", fnFunction);
	addEventHandler(oLink, "click", fnFunction);
}

function enableButton(bEnable, oButton, fnFunction) {
	if(bEnable) {
	 	oButton.style.cursor = "pointer";
	 	oButton.style.color = "";
		addEventHandler(oButton, "click", fnFunction);
	}
	else {
		oButton.style.cursor = "default";
 		oButton.style.color = "gray";
		removeEventHandler(oButton, "click", fnFunction);
	}
}

function allowRegexpChar(event, object, regexpstr) {
   		var code = (event.which == null) ? event.keyCode : event.which;

   		if (code < 0x20 || code > 0x7e && code < 260 && code > 378) {
   			return true;
		}

   		var regexp = eval("/" + regexpstr + "/");
   		var str = String.fromCharCode(code);
   		if (str.match(regexp) == null)
   			return false;
   		return true;
}



function reloadSite() {
	if(!bIsIE) {
		if (sLanguage == "uk")
			document.write('<div align="center" style="margin-top:150px; font-family:Arial, Helvetica, sans-serif"><strong>Logging on in progress, please wait...</strong><br /><br /><img src="http://www.gsmservice.pl/img/ajax/progress.gif" alt=""/><br /><br />If this website doesn\'t load in 30 seconds, click <a href="' + location.href + '">here</a> please.</div>');
		else
			document.write('<div align="center" style="margin-top:150px; font-family:Arial, Helvetica, sans-serif"><strong>Trwa ładowanie strony, proszę czekać...</strong><br /><br /><img src="http://www.gsmservice.pl/img/ajax/progress.gif" alt=""/><br /><br />Jeżeli strona nie załaduje się w ciągu 30 sekund,proszę kliknąć <a href="' + location.href + '">tutaj</a>.</div>');
		window.location.href = location.href;	
	}
	else {
		window.location.reload(true);
	}
}

function getConfirmation(sQuestion) {
	if(!confirm(sQuestion)) {
		return false;
	}
}
