function getEventKeyCode(oEvent)
{
	return oEvent.keyCode || oEvent.which || null;
}

function stopEvent(oEvent)
{
	var oElement = getEventElement(oEvent);

	oEvent.returnValue = false;
	if (oEvent.preventDefault) {
		oEvent.preventDefault();
	}

	oEvent.cancelBubble = true;
	if (oEvent.stopPropagation) {
		oEvent.stopPropagation();
	}
}

function addEvent(oElement, sEvent, sCallback, bCapture)
{
	if( !oElement ) {
		return;
	}

	if (oElement.attachEvent) {
		oElement.detachEvent('on' + sEvent, sCallback);
		oElement.attachEvent('on' + sEvent, sCallback);
    } else if (oElement.addEventListener) {
		oElement.addEventListener(sEvent, sCallback, bCapture);
    } else {
        oElement['on' + sEvent] = sCallback;
    }

    return oElement;
}

function removeEvent(oElement, sEvent, sCallback, bCapture)
{
	if (oElement.detachEvent) {
        oElement.detachEvent('on' + sEvent, sCallback);
    } else if (oElement.removeEventListener) {
        oElement.removeEventListener(sEvent, sCallback, bCapture);
    } else {
        oElement['on' + sEvent] = null;
    }
    return oElement;
}

function getEvent(oEvent)
{
	return oEvent || window.event;
}

function trim(sArg)
{
	return sArg.replace(/^\s+|\s+$/g, '');
}

function in_array(sArg, aArg)
{
    for (var i = 0; i < aArg.length; i++) {
		if (sArg === aArg[i]) {
			return true;
		}
	}
	return false;
}

function getEventElement(oEvent)
{
	var oElement = oEvent.srcElement || oEvent.target;

	if (oElement.nodeType == 3) {
		oElement = oEvent.oElement.parentNode;
	}

	return oElement;
}

function setOpacity(oElement, iOpacityPercentage)
{
	oElement.style.opacity = iOpacityPercentage / 100;
	oElement.style.filter = 'alpha(opacity: ' + iOpacityPercentage + ')';
}

function setAttributeStyle(oElement, sValue)
{
	if (oElement.style.setAttribute) {
		oElement.style.cssText = sValue;
	} else {
		oElement.setAttribute('style', sValue)
	}
}

function htmlspecialchars(sArg)
{
	sArg = sArg.replace(/&/g, '&amp;');
	sArg = sArg.replace(/</g, '&lt;');
	sArg = sArg.replace(/>/g, '&gt;');
	return sArg.replace(/"/g, '&quot;');
}

function $(sId)
{
	return document.getElementById(sId);
}

function getElementsByClassName(oParentElement, sClassName)
{
	var oParentElement = oParentElement;
	/*
	if (document.getElementsByClassName) { //https://developer.mozilla.org/en/DOM/document.getElementsByClassName
		return oParentElement.getElementsByClassName(sClassName);
	}
	*/

	var aReturnElements = [];
	var sClassName = sClassName.replace(/\-/g, '\\-');
	var aElements = oParentElement.all ? oParentElement.all : oParentElement.getElementsByTagName('*');

	for (var i = 0; i < aElements.length; i++) {
		if (aElements[i].className.match('(^|\\s)' + sClassName + '(\\s|$)')) {
			aReturnElements.push(aElements[i]);
		}
	}

	return aReturnElements;
}

function getSelectedIndexOption(oSelect)
{
	try {
		return $(oSelect.options[oSelect.selectedIndex].id);
	} catch (oException) {}
}

function getOffset(oElement)
{
	var iTop = 0;
	var iLeft = 0;

	while (oElement.tagName != 'HTML' && oElement.tagName != 'BODY')
	{
		iTop += oElement.offsetTop;
		iLeft += oElement.offsetLeft;

		oElement = oElement.offsetParent;
	}

	iTop += oElement.offsetTop;
	iLeft += oElement.offsetLeft;

	return {'top': iTop, 'left': iLeft};
}

function constrain(mInit, mMin, mMax)
{
    if (mInit < mMin) {
        mInit = mMin;
    } else if (mInit > mMax) {
        mInit = mMax;
	}

    return mInit;
}

function loadImage(sImageSrc)
{
	var oImage = new Image();
	oImage.src = sImageSrc;
}

function decToHex(i)
{
	return i.toString(16);
}

function hexToDec(s)
{
	return parseInt(s, 16);
}

function interpolation(x1, x2, y1, y2, y)
{
	return parseInt(x1 + (y - y1) / (y2 - y1) * (x2 - x1));
}

function addLeadingZeroCouple(s)
{
	return s.length > 1 ? s : '0' + s;
}

function getRGB(sStartRGB, sEndRGB, iStart, iEnd, iGiven)
{
	var iGiven = constrain(iGiven, iStart, iEnd);

	var r = interpolation(hexToDec(sStartRGB.substr(0, 2)), hexToDec(sEndRGB.substr(0, 2)), iStart, iEnd, iGiven);
	var g = interpolation(hexToDec(sStartRGB.substr(2, 2)), hexToDec(sEndRGB.substr(2, 2)), iStart, iEnd, iGiven);
	var b = interpolation(hexToDec(sStartRGB.substr(4, 2)), hexToDec(sEndRGB.substr(4, 2)), iStart, iEnd, iGiven);

	r = addLeadingZeroCouple(decToHex(r));
	g = addLeadingZeroCouple(decToHex(g));
	b = addLeadingZeroCouple(decToHex(b));

	return r + g + b;
}

// Varankin
// 2010

function setCookies( sName, sValue, sExpire, sPath ) {
	document.cookie = sName + '=' + (window.encodeURI ? encodeURI(sValue) : escape(sValue)) +
		((sExpire == null) ? '' : ('; expires=' + sExpire.toGMTString())) +
		((sPath == null) ? '' : ('; path=' + sPath));
}

function getCookies( sName ) {
	var sSearch = sName + '=';

	if(document.cookie.length > 0) {
		var iOffset = document.cookie.indexOf(sSearch);

		if(iOffset != -1) {
			iOffset += sSearch.length;

			var iEnd = document.cookie.indexOf(';', iOffset);
			if(iEnd == -1) {
				iEnd = document.cookie.length;
			}

			return window.decodeURI?
				decodeURI(document.cookie.substring(iOffset, iEnd)) : unescape(document.cookie.substring(iOffset, iEnd));
		}
	}

	return '';
}

