
function showAds() {
	
	if(adsContainers !== undefined) {
		
		for(i=0; i< adsContainers.length; i++) {
			container = document.getElementById('adContainer-' + adsContainers[i]);
			if(container != null) {
				container.style.display = "block";
			}
		}
	}
}

function checkFormDoubleClick(form_el) {
	if (form_el.form_already_sent) return false;
	form_el.form_already_sent = true;
 	return true;
}

function showHideById(id) {
	
	var element = document.getElementById(id);
	if(element != null) {
	
		if(element.style.display == 'none') {
			element.style.display = '';
		} else {
			element.style.display = 'none';
		}
	}
	return false;
}

function submitTip(url, selection_id, selection_name, odds, market_id, market_name, subevent_date, subevent_time, subevent_name, url_path, tipping_ew) {
	var params = selection_id + ',' + selection_name + ',' + odds + ',' + market_id + ',' + market_name + ',' + subevent_date + ',' + subevent_time + ',' + subevent_name + ',' + url_path + ',' + tipping_ew;
	var win_link = url + '/' + escape(params);
	
	var left = parseInt((screen.width/3)*2 - winSizes[9][0]/2);
	var top = parseInt(screen.height/2 - winSizes[9][1]/2);
	
	return showPopUp(win_link, winList[9], winSizes[9][0] , winSizes[9][1], left, top);
}

var hide = 0; 
var id = 0;
var timeout = false;

function menuSwitch(id,id_prefix,run_func) {
	i = 1; 
	hide = 1;
	if(!id_prefix) id_prefix = false;
	if(!run_func) run_func = false;

	if(id_prefix != false) {
		while (e = document.getElementById(id_prefix + i++)) e.style.display = 'none';
	}
	
	if(e = document.getElementById(id)) e.style.display = 'block';
	
	if(run_func != false) eval(run_func);
}
	
function menuEnd(id_prefix,run_func) {

    if(!run_func) run_func = false;
	if(timeout) clearTimeout(timeout);
	hide = 0; 
	timeout = setTimeout("menu_hide('" + id_prefix + "'," + '"' + run_func + '")', 150);
}
	
function menu_hide(id_prefix,run_func) {

	if (hide == 0) {
	    i = 1;
	    while (e = document.getElementById(id_prefix + i++)) e.style.display = 'none';
	    if(run_func != false) eval(run_func);
	}
}

function moreBets(nof_items) {
		
	var mbm_div = document.getElementById('more_bets_menu');
	
	if(nof_items > 12) {
		mbm_div.style.height = '220px';
	} else {
		mbm_div.style.overflow = 'hidden';
	}

	if(mbm_div.style.display == 'block') mbm_div.style.display = 'none';
	else mbm_div.style.display = 'block';
	
	if(mbm_div.scrollWidth && !mbm_div.style.width) {
		mbm_div.style.width = mbm_div.scrollWidth+20;
	}
	
	var sizes = getBrowserWindowInnerSizes();
	var win_width = sizes[0];
	var div_left = mbm_div.offsetLeft;
	
    var div_width = mbm_div.scrollWidth;
    var diff = win_width-div_left;

    if(diff < div_width) 
    {
		sum  = div_left - div_width + 20;
		if(nof_items <= 12) sum = sum + 20;		
		mbm_div.style.left = sum + 'px';
	}
	
	return false;
}

function showMenu() {
	shMenuClicked = true;
	document.getElementById('menu').style.display = 'block';
	if (!document.all)
	{
		document.getElementById('menu').style.display = "";
	}
	document.getElementById('menu_hidden').style.display = 'none';
	setCookie('menu', 'yes', 365*24*3600);
}

function hideMenu() {
	shMenuClicked = true;
   	document.getElementById('menu').style.display = 'none';
	document.getElementById('menu_hidden').style.display = 'block';
	if (!document.all)
	{
		document.getElementById('menu_hidden').style.display = "table-row";
	}	
	setCookie('menu', 'no', 365*24*3600);	
}

function showSubMenu(id) {
	sub = document.getElementById(id)
	if (sub != null) { 
		if (sub.style.display != 'block') {
			sub.style.display = 'block';
		} else {
			sub.style.display = 'none';
		}
	}
}

/**
 * Set cookie 
 */
function setCookie(name, value, sec_to_expire) {
  
  if(!sec_to_expire) {
  	var expire_date = 0;
  } else {
  	expire_date = new Date(new Date().getTime()+sec_to_expire*1000).toGMTString();
  }

  if (value != null && value != "") {
    document.cookie=name + "=" + escape(value) + "; expires=" + expire_date + '; path=/';
  }
}
function getCookie(name)
 {
 if (document.cookie.length>0)
   {
   c_start=document.cookie.indexOf(name + "=");
   if (c_start!=-1)
     {
     c_start=c_start + name.length+1;
     c_end=document.cookie.indexOf(";",c_start);
     if (c_end==-1) c_end=document.cookie.length;
     return unescape(document.cookie.substring(c_start,c_end));
     }
   }
 return null;
 }

function setCookieArray(name, array, sec_to_expire) {

  if(!sec_to_expire) {
     var sec_to_expire = 24*3600;
  }

  var expire_date = new Date(new Date().getTime()+sec_to_expire*1000).toGMTString();
  
  sCookie = '';
  for (var key in array) {
  	if(key.length > 0 && array[key].length > 0) {
	  	sCookie +=  key + ',' + array[key] + ',';
	}
  }
  
  sCookie = sCookie.substr(0, sCookie.length - 1);

  document.cookie= name + "=" + sCookie + "; expires=" + expire_date + '; path=/';
}

function getCookieArray(name) {

	start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) && (name != document.cookie.substring( 0, name.length ) ) )
	{
		return null;
	}
	if ( start == -1 ) return null;

	var end = document.cookie.indexOf( ";", len );
	if ( end == -1 ) end = document.cookie.length;
	array = document.cookie.substring( len, end ).split(',');
	
	rArray = new Array();
	for (var i = 0; i < array.length; i += 2) {
		rArray[array[i]] = array[i + 1];
	}
	
	return rArray;
}

function eraseCookie(name) {

	var date = new Date();
	date.setTime(date.getTime()+(-1*24*60*60*1000)); //expire date set to yesterday
	var expires = "; expires="+date.toGMTString();
	document.cookie = name+"=''"+expires+"; path=/";
}

function addToCookieArray(name, key, value, sec_to_expire) {
	array = getCookieArray(name);
	if (!array) {
		array = new Array();
	}
	array[key] = value;

	setCookieArray(name, array, sec_to_expire);
}

function deleteFromCookieArray(name, element) {
	array = getCookieArray(name);
	array[element] = '';
	setCookieArray(name, array);
}


/**
 * Clock
 */
function runClock(){
	var oDate=new Date();
	var sSwitch="am";
	var iHours=oDate.getHours();
	/*
	if(iHours>11){
		sSwitch="pm";
	}
	if(iHours>12){
		iHours-=12;
		sSwitch="pm";
	}*/
	
	var sMinutes=oDate.getMinutes() + "";
	if(sMinutes.length==1){
		sMinutes="0" + sMinutes;
	}
	var sSeconds=oDate.getSeconds() + "";
	if(sSeconds.length==1){
		sSeconds="0" + sSeconds;
	}
	document.getElementById('hBCtimeContainer').innerHTML=iHours + ":" + sMinutes + ":" + sSeconds;
}

/**
 * Show popup
 */
function showPopUp(target, name, width, height, left, top) {
	strOptions = 'width=' + width + ',height=' + height;
	if(left && top) strOptions = strOptions + ',left=' + left + ',top=' + top;
	strOptions = strOptions + ',toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no';

	var popupWindow = window.open(target, name, strOptions);
	if(popupWindow) {
	
		popupWindow.focus();
		return popupWindow;
		
	} else {
		return false;
	}
}

function getUrlParam (paraName) {
	thisURL = document.location.search.slice(1);

	arrayURL = thisURL.split('&');

	for (i = 0; i < arrayURL.length; i++) {
		if (arrayURL[i].slice(0, arrayURL[i].indexOf('=')) == paraName) {
			return unescape(arrayURL[i].slice(arrayURL[i].indexOf('=') + 1));
		}
	}

	return '';
} 

function addTableBehavior() {	
	
	var l = document.getElementById('t1').getElementsByTagName('td');
	var last;
	for (i = 0; i < l.length; i++) {
		switch (l[i].className) {
			case 'o':
			case 'oo':
			case 'oi':
			case 'oback':
			case 'olay':
			case 'osell':
			case 'obuy':
				l[i].onmouseover = function () {
				
			        this.parentNode.style.backgroundColor="#f1f1f1"; 
			        if(this.firstChild)
			        {
						this.style.cursor="pointer"; 
						this.style.color="black"; 
					
						this.style.backgroundColor="#e74900"; 
					}
				}
				l[i].onmouseout = function () {
				
				    this.parentNode.style.backgroundColor="#e5e5e5";
				    if(this.firstChild)
					{
						this.style.color=""; 
						this.style.backgroundColor=""; 
				    }
				}
				break;
			case 'selections': 						
				l[i].onmouseover = function () {this.parentNode.style.backgroundColor="#f1f1f1";}
				l[i].onmouseout = function () {this.parentNode.style.backgroundColor="#e5e5e5";}
			default:
				break;			
		}
	}
	return true;
}

function setAllOddsTableFontSize() {
	
	if (screen.width <= 1024) {
		var table = document.getElementById('AllOddsTable');
	    var l = table.getElementsByTagName('tr');
	   	for (i = 0; i < l.length; i++) {
		   	if(l[i].className == 'eventTableRow') {
		   		
		   		x = l[i].getElementsByTagName('td');
		   		for (j = 0; j < x.length; j++) {
		   			if(x[j].firstChild.nodeValue >= 10000) {
		   				x[j].style.fontSize = '8px';
		   			} else {
		   				x[j].style.fontSize = '9px';
		   			}
		   		}
		   	}
	   	}
   	}		
}



function setMaxTotals(nof_trad_str,nof_ex_str) {

	var nof_trad = parseInt(nof_trad_str);
    var nof_ex = parseInt(nof_ex_str);

	arr = new Array();
	var tr = document.getElementById('e_totals');
	var tds = tr.getElementsByTagName('td');
	
	var ahref = tds[1].getElementsByTagName('a');
	
	for(var i=1;i<=nof_trad;i++) {
	
		ahref = tds[i].getElementsByTagName('a');

		if(ahref[0] && ahref[0].firstChild.nodeValue>0) arr.push(parseFloat(ahref[0].firstChild.nodeValue));
		else  arr.push(0);
	}
	
	var max = Math.max.apply(null, arr);

	for(var i=1;i<=nof_trad;i++) {
		ahref = tds[i].getElementsByTagName('a');
		if(ahref[0]) {
			if(parseFloat(ahref[0].firstChild.nodeValue) == max) tds[i].className = 'e_totals_best_acc';
		}
	}
	
	var arr = new Array();
	
	for(var i=nof_trad;i<=nof_trad+nof_ex;i++) {
		ahref = tds[i].getElementsByTagName('a');
		if(ahref[0] && ahref[0].firstChild.nodeValue>0) arr.push(parseFloat(ahref[0].firstChild.nodeValue));
		else  arr.push(0);
	}

	var max = Math.max.apply(null, arr);
	
	for(var i=nof_trad;i<=nof_trad+nof_ex;i++) {
		ahref = tds[i].getElementsByTagName('a');
		if(ahref[0]) {
			if(parseFloat(ahref[0].firstChild.nodeValue) == max) tds[i].className = 'e_totals_best_acc';
		}
	}
	
}

function mod(n, m)
{
	while (n >= m)
	{
		n = n-m;
	}
	return(n);
}

function GCD(a,b) {
    var c;
    while (b!=0) {
      c=a;
      a=b;
      b=mod(c,b);
    }
    return a;
}

function dec2frac(dec)
{
	var decString = dec.toString();
	var dslength = decString.length - 1;
	var div = 1;
 
 	for (i=0; i<dslength; i++)
 	{
  		dec = dec * 10;
  		div = div * 10;
  	}

    dec = Math.round(dec);
	div = Math.round(div);
	
	d = GCD(dec,div);
	if(d) {
		dec = dec/d;
		div = div/d;
	}
	
	dec = Math.round(dec);
	div = Math.round(div);

	return dec + (div !=1 ? "/" + div : "" );
}

function getWindowWidth()
{
	var x = 0;
	if (self.innerHeight)
	{
    	x = self.innerWidth;
    	
    } else if (document.documentElement && document.documentElement.clientHeight) {
		x = document.documentElement.clientWidth;
    } else if (document.body) {
    	x = document.body.clientWidth;
    } 
    
    return x;
}
    
function getWindowHeight()
{
	var y = 0;
	if (self.innerHeight) {
		y = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) {
        y = document.documentElement.clientHeight;
    } else if (document.body) {
		y = document.body.clientHeight;
    } 
    
    return y;
}

function getBrowserWindowInnerSizes() {
	
	var h = w = 0;
	if (typeof(window.innerWidth) == 'number') {
		w = window.innerWidth;
		h = window.innerHeight;
	} else if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
		w = document.documentElement.clientWidth;
		h = document.documentElement.clientHeight;
	} else if(document.body && (document.body.clientWidth || document.body.clientHeight)) {
		w = document.body.clientWidth;
		h = document.body.clientHeight;
	}
	
	sizes = new Array(w,h);
	return sizes;
}

document.getElementsByClassName = function(cl) {
	var retnode = [];
	var myclass = new RegExp('\\b'+cl+'\\b');
	var elem = this.getElementsByTagName('*');
	for (var i = 0; i < elem.length; i++) {
		var classes = elem[i].className;
		if (myclass.test(classes)) retnode.push(elem[i]);
	}
	return retnode;
};


/* for google maps */
function createMarker(point,html) {

    var marker = new GMarker(point);
    GEvent.addListener(marker, "click", function() {
       marker.openInfoWindowHtml(html);
    });
    return marker;
}

/*It close the current page and refresh or open a new 'url' page*/
function closeCurrentRefreshParent(url) {
	if(window.opener == null || window.opener.closed){
	  	window.open(url);
	}else{
    	window.opener.location.href = window.opener.location.href;
		if (window.opener.progressWindow)
		{
	    	window.opener.progressWindow.close();
	  	}
	}
  	window.close();
}

/*stop the excution for milliseconds */
function pause(milliseconds)
{
    var now = new Date();
    var exitTime = now.getTime() + milliseconds;

    while(true)
    {
        now = new Date();
        if(now.getTime() > exitTime) return;
    }
}

function getWinWH(){
	if (typeof window.innerWidth != 'undefined') {
	     winH = window.innerHeight;
	     winW = window.innerWidth;
	} else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0) {
	     winH = document.documentElement.clientHeight;
	     winW = document.documentElement.clientWidth
	} else {
	 	winH = document.getElementsByTagName('body')[0].clientHeight;
	 	winW = document.getElementsByTagName('body')[0].clientWidth
	}
	return [ winW, winH ];
}

function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}

