function addParamToUrl(url, paramName, paramValue) {
	var _encode = (arguments.length>3)?arguments[3]:true;
	
	var AMP = "&"; if (_encode) AMP = UrlEncode(AMP).toLowerCase();
	var QM = "?"; if (_encode) QM = UrlEncode(QM).toLowerCase();
	
	var url = url.toLowerCase();
	var baseLen = url.indexOf(QM);
	var hasParams = baseLen > -1;
	baseLen = (hasParams)? baseLen + QM.length: baseLen = url.length;
	var url_firstPart = (hasParams)? url.substr(0, baseLen): url;
	var url_params = url.substr(baseLen).split(AMP);
	url = url_firstPart;
	paramName = (paramName + "=").toLowerCase();
	if (_encode) paramName = UrlEncode(paramName).toLowerCase();
	if (hasParams) {
		var upl = url_params.length;
		var pnl = paramName.length;
		for (var i=0; i<upl; ++i) {
			var param = url_params[i];
			if (param.substring(0, pnl) != paramName) {
				if (url.length > baseLen) url += AMP;
				url += param;
			}
		}
		url += AMP;
	} else {
		url += QM;
	}
	if (_encode) paramValue = UrlEncode(UrlEncode(paramValue));
	return url + paramName + paramValue;
}

function AuthCookieExists() {
	return ((typeof(COOKIE_AUTHENTICATION) != "undefined")
		&& (typeof(Get_Cookie) == "function"))
		? Get_Cookie(COOKIE_AUTHENTICATION) != null
		: true;
}

function canAccessTSR() {   // Can access TOTE sport radio
	return true;
}

function DialogWindow(url) {

	this.Url = url;
	this.Name = null;
	this.Width = null;
	this.Height = null;
	this.Scroll = false;
	this.Status = false;
	this.Resizable = true;
	this.Toolbar = false;
	this.TryModal = false;
	this.WasModal = false;
	this.ReturnValue = null;
	this.RequiresLogin = true;
	
	this.CanShowModal = function()
	{
		return typeof(window.showModalDialog) == "object";
	}
	
	this.Show = function() {	
		if (this.RequiresLogin)	{
			var bSecure = false;
			if (typeof(_loggedIn) != "undefined") {
				bSecure = _loggedIn;
			}	
			
			if (!bSecure || !AuthCookieExists()) {
				alert(Translate(MSG_PAGE_REQUIRES_LOGIN));
				this.ReturnValue = null;
				this.WasModal = true;  // Lets the caller know that it's done
				return;
			}
		}	
		if (this.TryModal && this.CanShowModal()) {			var iActHeight = this.Height + 40;
			//this.ReturnValue = window.showModalDialog(this.Url, null, "dialogHeight: " + iActHeight + "px; dialogWidth: " + this.Width + "px; edge: Raised; center: Yes; help: No; resizable: " + YN(this.Resizable) + "; status: " + YN(this.Status) + "; scroll: " + YN(this.Scroll) + ";");
			this.ReturnValue = window.showModalDialog(this.Url, null, "dialogHeight: " + iActHeight + "px; dialogWidth: 590px; edge: Raised; center: Yes; help: No; resizable: " + YN(this.Resizable) + "; status: " + YN(this.Status) + "; scroll: " + YN(this.Scroll) + ";");
			this.WasModal = true;
		} else {
			var iLeft = (screen.availWidth-10 - this.Width) / 2;
			var iTop = (screen.availHeight-20 - this.Height) / 2;
			//var oWin = window.open(this.Url, this.Name == null ? "" : this.Name, "top=" + iTop + "px, left=" + iLeft + "px, height=" + this.Height + "px, width=" + this.Width + "px, scrollbars=" + YN(this.Scroll) + ", resizable=" + YN(this.Resizable) + ", status=" + YN(this.Status) + ", toolbar=" + YN(this.Toolbar) + ", fullscreen=No, location=No, menubar=No");
			var oWin = window.open(this.Url, this.Name == null ? "" : this.Name, "top=" + iTop + "px, left=" + iLeft + "px, height=" + this.Height + "px, width=590px, scrollbars=" + YN(this.Scroll) + ", resizable=" + YN(this.Resizable) + ", status=" + YN(this.Status) + ", toolbar=" + YN(this.Toolbar) + ", fullscreen=No, location=No, menubar=No");
			//try { oWin.focus(); } catch(e) {}
		}
	}
}

function DisplayMessage(message) 
{
    for (var i=arguments.length-1; i>0; --i) 
    {
        message = message.replace("{"+(i-1)+"}", arguments[i]);   
    }
    alert(message);
}

function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

function QueryString() {
	var sThisUrl = document.location.href;
	var iQsPos = sThisUrl.indexOf("?");
	if (iQsPos == -1) {
		return null;
	} else {
		return sThisUrl.substring(iQsPos+1);
	}
}

function selectSubNav(id, currentEl) {
	if (typeof(currentEl) != "undefined" && currentEl != null) {
		SetElementClass(currentEl, "selected", false);
	}
	var oEl = document.getElementById(id);
	if (oEl != null) {
		SetElementClass(oEl, "selected", true);
	}
	return oEl;
}

function SetElementClass(element, className, on) {
	if (element.className.length > 0) {
		var sClasses = element.className.split(" ");
		var iIdx = -1;
		for (var i=0; i < sClasses.length; i++) {
			if (sClasses[i] == className) {
				iIdx = i;
				break;
			}
		}
		if (on) {
			if (iIdx == -1) {
				element.className = element.className + " " + className;
			}
		} else {
			if (iIdx != -1) {
				var sNew = "";
				for (var i=0; i < sClasses.length; i++) {
					if (i != iIdx) {
						sNew += (sNew.length > 0 ? " " : "") + sClasses[i];
					}
				}
				element.className = sNew;
			}			
		}
	} else {
		if (on) element.className = className;
	}
	//alert(element.id + ": turned '" + className + "' " + (on ? "on" : "off") + ", now '" + element.className + "'");
}

function stripCommas(str) {
	var re = /,/g;
	return str.replace(re,"");
} 

function toggleMultibet(elm) {
    
    var group = elm.parentNode.parentNode.className;
    var list = getElementsByClass('mulitbet' + group);
    elm.className = elm.className == 'toggleMultibet_closed' ? 'toggleMultibet_open' : 'toggleMultibet_closed';
    if(list != null){
        for(i = 0; i < list.length; i++) {
            list[i].style.display = list[i].style.display == 'none' ? '' : 'none';
           }
    }
    return false;
}

function UrlEncode(plaintext) {
	if (plaintext == null) return "";
	
    var SAFECHARS = "0123456789" +
                    "ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
                    "abcdefghijklmnopqrstuvwxyz" +
                    "-_.!~*'()";                      // RFC2396 Mark characters

    var HEX = "0123456789ABCDEF";
    var encoded = "";

    for (var i = 0; i < plaintext.length; i++ ) {
		var ch = plaintext.charAt(i);
        if (ch == " ") {
			encoded += "+";                         // x-www-urlencoded, rather than %20
		} else if (SAFECHARS.indexOf(ch) != -1) {
			encoded += ch;
		} else {
			var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
				alert( "Unicode Character '" 
					+ ch 
	                + "' cannot be encoded using standard URL encoding.\n"
                    + "(URL encoding only supports 8-bit characters.)\n" 
                    + "A space (+) will be substituted." );
                encoded += "+";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
    }

    return encoded;

}

function YN(bool) {
	return bool ? "yes" : "no";
}