<!--

// MyDHTML 1.0 Common external functions

function c()
{
	alert('CHECK');
}

function d(v)
{
	alert(v);
}

function isNull(v)
{
    return (v == null);
} 

function isUndefined(v)
{
    return (typeof v == 'undefined');
}

function isEmpty(v)
{
	if (isUndefined(v))
		return true;
    if (isNull(v))
    	return true;
    if (v == '')
    	return true;
    if (v == 0)
    	return true;
    if (v == false)
    	return true;

    return false;
} 

function isArray(v)
{
	if (isUndefined(v))
		return false;
    if (isNull(v))
    	return false;
		
    return (v.constructor == Array);
} 

function isNumeric(v)
{
	if (isUndefined(v))
		return false;
	if (isNull(v))
		return false;

	var sValidChars = "0123456789.-";
	var bResult = true;

	if (v.length == 0)
		return false;

	for (var i = 0; i < v.length && (bResult == true); i++)
		if (sValidChars.indexOf(v.charAt(i)) == -1)
			bResult = false;

	return bResult;
}

function nullToEmptyString(v)
{
	if (isNull(v))
		v = '';

	return v;
}

function stringToNumber(v)
{
	v = v.replace(/ /g, '');
	v = v.replace(/,/g, '.');
	if (!isNumeric(v))
		v = null;
	return v;    
}

function trim(v) 
{ 
	v = v.replace(/[\s]+$/, '');
	v = v.replace(/^[\s]+/, '');

	return v;
} 

function in_array(needle, haystack)
{
	for (var i = 0; i < haystack.length; i++)
		if (haystack[i] == needle)
			return true;

    return false;
}

var bGoToURLBlocked = false;
function goToURL(sURL)
{
	if (bGoToURLBlocked)
		return;

	if (sURL == '')
	{
		return;
	}
	else
	{
		bGoToURLBlocked = true;
		window.location.href = sURL;
	}
}

function getXMLHTTPObject()
{
	var oXMLHTTP = null;

	try
	{
		// Firefox, Opera 8.0+, Safari
		oXMLHTTP = new XMLHttpRequest();
	}
	catch (e)
	{
		// Internet Explorer
		try
		{
			oXMLHTTP = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			oXMLHTTP = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}

	return oXMLHTTP;
}

// Error processing >>> ---------------------------------------

function doNothing()
{
	return true;
}

function doHalt(sText)
{
	window.onerror = doNothing;
	alert('[MyDHTML] Error: ' + sText + '!');
}

// <<< Error processing ---------------------------------------

// serviceHandler >>> -----------------------------------------

function serviceHandler()
{
	this.res = new Array();
	this.urls = new Array();
	this.bExecUrlBusy = false;
	this.aURLStack = new Array();
	this.oXmlHttp = null;
	this.iSessionExpirationTimestamp = null;
	this.iSessionGCMaxLifeTime = 3600;

	this.onEnter = serviceHandler_onEnter;
	this.serialize = serviceHandler_serialize;
	this.setWindowSize = serviceHandler_setWindowSize;
	this.openWindow = serviceHandler_openWindow;
	this.autoHideBanners = serviceHandler_autoHideBanners;
	this.getTimestamp = serviceHandler_getTimestamp;
	this.getDateTime = serviceHandler_getDateTime;
	this.getCSSAttribute = serviceHandler_getCSSAttribute;
	this.subStrCount = serviceHandler_subStrCount;
	this.execURL = serviceHandler_execURL;
	this.manageSessionExpiration = serviceHandler_manageSessionExpiration;
	this.callX24 = serviceHandler_callX24;
	this.setURL = serviceHandler_setURL;
	this.getURLArg = serviceHandler_getURLArg;
	this.askToRegister = serviceHandler_askToRegister;
	this.handleAdultOnlyURL = serviceHandler_handleAdultOnlyURL;
}

function serviceHandler_getURLArg(sURL, sKey)
{
	sKey = sKey.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var pPattern = "[\\?&]" + sKey + "=([^&#]*)";
	var rRegExp = new RegExp(pPattern);
	var aResults = rRegExp.exec(sURL);
	if	(aResults == null)
		return null;
	else
		return aResults[1];
}

function serviceHandler_setURL(sId, sURL)
{
	this.urls[sId] = sURL;
}

function serviceHandler_manageSessionExpiration(sAction, iTimeout)
{
	var iWarningTimeout = null;

	if (SESSION_EXPIRATION_HANDLING == 'silent_redirect') 
		iWarningTimeout = 0;
	else if (SESSION_EXPIRATION_HANDLING == 'warning_and_redirect') 
	{
		iWarningTimeout = 300000;
		//iWarningTimeout = 2500;
	}

	if (sAction == 'set_gc_maxlifetime')
	{
		//iTimeout = 5;
		this.iSessionGCMaxLifeTime = iTimeout  * 1000;
	}
	else if (sAction == 'start')
	{
		if (this.getURLArg(window.location.href, 'mec_session_expiration') != '0')
		{
			this.iSessionExpirationTimestamp = this.getTimestamp() + this.iSessionGCMaxLifeTime;
			setTimeout('oService.manageSessionExpiration("wait")', (this.iSessionExpirationTimestamp - this.getTimestamp() - iWarningTimeout));
		}
	}
	else if (sAction == 'wait')
	{
		if (SESSION_EXPIRATION_HANDLING == 'silent_redirect')
		{
			goToURL(this.urls['front_page']);
		}
		else if (SESSION_EXPIRATION_HANDLING == 'warning_and_redirect')
		{
			alert(this.res['session_will_expire'] + ' ' + this.getDateTime(this.iSessionExpirationTimestamp) + '\n' + this.res['prevent_session_expiration']);

			if (this.getTimestamp() >= this.iSessionExpirationTimestamp)
			{
				goToURL(this.urls['front_page']);
			}
			else
			{
				this.manageSessionExpiration('start');
				this.execURL(this.urls['noop'], false);
			}
		}
	}
}

function serviceHandler_getDateTime(iTimestamp)
{
	var oDate = new Date();
	oDate.setTime(iTimestamp);

	var iYear = oDate.getFullYear();
	var iMonth = oDate.getMonth() + 1;
	var iDay = oDate.getDate();
	var iHours = oDate.getHours();
	var iMinutes = oDate.getMinutes();

	if (iMonth < 10) iMonth = '0' + iMonth;
	if (iDay < 10) iDay = '0' + iDay;
	if (iHours < 10) iHours = '0' + iHours;
	if (iMinutes < 10) iMinutes = '0' + iMinutes;

	return iHours + ':' + iMinutes + ' ' + iDay + '.' + iMonth + '.' + iYear;
}

function serviceHandler_getTimestamp()
{
	var oDate = new Date();
	return oDate.getTime();
}

function serviceHandler_onEnter(e)
{
	if (e.keyCode == 13)
		return true;
	else
		return false;
}

function serviceHandler_serialize(aData)
{
	var sData = '';
	for (var sKey in aData)
	{
		sData += sKey + '|=>|' + aData[sKey] + '|,|';
	}
	return sData;
}

function serviceHandler_setWindowSize(iWidth, iHeight)
{
	var bReturn = false;
	if (document.documentElement)
	{
		window.resizeTo(iWidth, iHeight);
    	bReturn = true;
	}
	return bReturn;
}

function serviceHandler_openWindow(sURL, iWidth, iHeight, sParams)
{
	var iLeft = parseInt((screen.availWidth/2) - (iWidth/2));
    var iTop = parseInt((screen.availHeight/2) - (iHeight/2));
	if (isUndefined(sParams))
		sParams = 'toolbar=no, status=no, scrollbars=yes, location=no, menubar=no, resizable=yes, screenX=' + iLeft + ', screenY=' + iTop + ', left=' + iLeft + ', top=' + iTop;
	oWindow = window.open(sURL, 'pop', 'width=' + iWidth + ', height=' + iHeight + ', '+sParams);
	oWindow.focus();
}

function serviceHandler_autoHideBanners(aBanners)
{
	var divBanner = null;
	var sHTML = null;
	for (var iIndex = 0; iIndex < aBanners.length; iIndex++)
	{
		if (document.getElementById(aBanners[iIndex]))
		{
			divBanner = document.getElementById(aBanners[iIndex]);
			sHTML = divBanner.innerHTML;
			if (sHTML.indexOf('kbobannerads') == -1)
			 	divBanner.style.display = 'none';
		}
		else
		{
			MSG = "Banner DIV '" + aBanners[iIndex] + "' does not exist!";
		}
	}
}

function serviceHandler_getCSSAttribute(sElementId, sCSSAttribute) // OK
{
	var sCSSValue = null;
	var oElement = document.getElementById(sElementId);
	
	if (oElement.currentStyle)
		sCSSValue = oElement.currentStyle[sCSSAttribute];
	else if (window.getComputedStyle)
		sCSSValue = document.defaultView.getComputedStyle(oElement, null).getPropertyValue(sCSSAttribute);

	return sCSSValue;
}

function serviceHandler_subStrCount(sHaystack, sNeedele)
{
	var iCount = 0;
	var iPos = sHaystack.indexOf(sNeedele);

	while (iPos != -1)
	{
   		iCount++;
   		iPos = str.indexOf(sNeedle, iPos + 1);
	}

	return iCount;
}

function serviceHandler_execURL(sURL, bAsync)
{
	var aURLStackElement = null;
	
	if (this.bExecUrlBusy)
	{
		aURLStackElement = new Array();
		aURLStackElement['url'] = sURL;
		aURLStackElement['async'] = bAsync;
		this.aURLStack.push(aURLStackElement);
	}
	else
	{
		if ((this.oXmlHttp != null) || ((this.oXmlHttp = getXMLHTTPObject()) != null))
		{
			this.bExecUrlBusy = true;

			this.oXmlHttp.open('POST', sURL, bAsync);

			this.oXmlHttp.onreadystatechange = function()
			{
				var aURLStackElement = null;
				if (oService.oXmlHttp.readyState == 4)
				{
					oService.bExecUrlBusy = false;
					if (aURLStackElement = oService.aURLStack.shift())
						oService.execURL(aURLStackElement['url'], aURLStackElement['async']);
				}
			}

			this.oXmlHttp.setRequestHeader("Content-Type", "text/plain;charset=UTF-8");
			this.oXmlHttp.send(null);
		}
	}
}

function serviceHandler_callX24()
{
	var oX24 = document.getElementById('x24_resize');
	var iHeight = null;
	var sURL = null;

	if (oX24)
	{
		sURL = this.urls['x24'];
		
		if (/Firefox[\/\s](\d+\.\d+)/.test(navigator.userAgent))
			iHeight = document.body.offsetHeight;
		else
			iHeight = document.body.scrollHeight;
		sURL = sURL + '?height=' + iHeight;

		if (oPost)
			if (isNumeric(oPost.iPostId))
				sURL = sURL + '&post_id=' + oPost.iPostId;

		oX24.src = sURL;
	}
}

function serviceHandler_askToRegister()
{
	oTopTabs.renderTabs(0);
	alert(this.res['ask_to_register']);
}

function serviceHandler_handleAdultOnlyURL()
{
	var sURL = null;

	if (confirm(this.res['i_am_adult']))
	{
		sURL = this.urls['global_attribute'];
		sURL = sURL.replace('%action%', 'set');
		sURL = sURL.replace('%k%', 'i_am_adult');
		sURL = sURL.replace('%v%', '1');

		this.execURL(sURL, false);

		return true;
	}

	return false;
}

// <<< serviceHandler -----------------------------------------

// -->
