function getScreenWidth() {
  xWidth = null;
  if(window.screen != null)
    xWidth = window.screen.availWidth;

  if(window.innerWidth != null)
    xWidth = window.innerWidth;

  if(document.body != null)
    xWidth = document.body.clientWidth;

  return xWidth;
}

function getScreenHeight() {
  xHeight = null;
  if(window.screen != null)
    xHeight = window.screen.availHeight;

  if(window.innerHeight != null)
    xHeight = window.innerHeight;

  //if(document.body != null) xHeight = document.body.clientHeight;

  return xHeight;
}

function loadjscssfile(filename, filetype){
 if (filetype=="js"){ //if filename is a external JavaScript file
  var fileref=document.createElement('script')
  fileref.setAttribute("type","text/javascript")
  fileref.setAttribute("src", filename)
 }
 else if (filetype=="css"){ //if filename is an external CSS file
  var fileref=document.createElement("link")
  fileref.setAttribute("rel", "stylesheet")
  fileref.setAttribute("type", "text/css")
  fileref.setAttribute("href", filename)
 }
 if (typeof fileref!="undefined")
  document.getElementsByTagName("head")[0].appendChild(fileref)
}

function removejscssfile(filename, filetype){
 var targetelement=(filetype=="js")? "script" : (filetype=="css")? "link" : "none" //determine element type to create nodelist from
 var targetattr=(filetype=="js")? "src" : (filetype=="css")? "href" : "none" //determine corresponding attribute to test for
 var allsuspects=document.getElementsByTagName(targetelement)
 for (var i=allsuspects.length; i>=0; i--){ //search backwards within nodelist for matching elements to remove
  if (allsuspects[i] && allsuspects[i].getAttribute(targetattr)!=null && allsuspects[i].getAttribute(targetattr).indexOf(filename)!=-1)
   allsuspects[i].parentNode.removeChild(allsuspects[i]) //remove element by calling parentNode.removeChild()
 }
}


function turnAutocompleteOff() {
	try {
		var inputs = document.getElementsByTagName('INPUT');
		for (var i = 0; i < inputs.length; i++) {
			inputs[i].setAttribute('autocomplete', 'off');
		}
	} catch (e) {}
}

function pointsBreakdown(player,info) {
	var html = '';
	html += '<h4>Player:<strong> '+player['first_name']+' '+player['last_name']+'</strong></h4>';
	html += '<table width="100%" class="pointsBreakdown">';
	html += '<tr><th width="80%">Statistic</th><th></th><th>Pts</th></tr>';
	if (info['breakdown']) {
		if (info['breakdown'] && 
				((info['breakdown']['mins_played'] && 
					(parseInt(info['breakdown']['mins_played']) > 0 || parseInt(info['breakdown']['start_minute']) > 0)) ||
					parseInt(info['breakdown']['cancelled_points']) != 0)
				) {
			if (info['breakdown']['mins_played'] > 0) 
				html += '<tr><td align="left">Minutes played:</td><td style="text-align: right;">'+info['breakdown']['mins_played']+'</td><td style="text-align: right;">'+info['breakdown']['mins_points']+'</td></tr>';
			if (info['breakdown']['goals_scored'] > 0) 
				html += '<tr><td align="left">Goals Scored:</td><td style="text-align: right;">'+info['breakdown']['goals_scored']+'</td><td style="text-align: right;">'+info['breakdown']['goals_scored_points']+'</td></tr>';
			if (info['breakdown']['penalty'] > 0) 
				html += '<tr><td align="left">Penalty Scored:</td><td style="text-align: right;">'+info['breakdown']['penalty']+'</td><td style="text-align: right;">'+info['breakdown']['penalty_points']+'</td></tr>';
			if (info['breakdown']['hat_trick_points'] > 0) 
				html += '<tr><td align="left">Hat Trick bonus:</td><td style="text-align: right;">&nbsp;</td><td style="text-align: right;">'+info['breakdown']['hat_trick_points']+'</td></tr>';
			if (info['breakdown']['goals_saved_points'] > 0) 
				html += '<tr><td align="left">Saves:</td><td style="text-align: right;">'+info['breakdown']['goals_saved']+'</td><td style="text-align: right;">'+info['breakdown']['goals_saved_points']+'</td></tr>';
			if (info['breakdown']['goals_conceded_points'] > 0 || info['breakdown']['goals_conceded'] > 0) 
				html += '<tr><td align="left">Goals Conceded:</td><td style="text-align: right;">'+info['breakdown']['goals_conceded']+'</td><td style="text-align: right;">'+info['breakdown']['goals_conceded_points']+'</td></tr>';
			if (info['breakdown']['goals_own'] > 0) 
				html += '<tr><td align="left">Own Goals:</td><td style="text-align: right;">'+info['breakdown']['goals_own']+'</td><td style="text-align: right;">'+info['breakdown']['goals_own_points']+'</td></tr>';
			if (info['breakdown']['assists'] > 0) 
				html += '<tr><td align="left">Assists:</td><td style="text-align: right;">'+info['breakdown']['assists']+'</td><td style="text-align: right;">'+info['breakdown']['assists_points']+'</td></tr>';
			if (info['breakdown']['penalty_saved'] > 0) 
				html += '<tr><td align="left">Penalties Saved:</td><td style="text-align: right;">'+info['breakdown']['penalty_saved']+'</td><td style="text-align: right;">'+info['breakdown']['penalty_saved_points']+'</td></tr>';
			if (info['breakdown']['penalty_missed'] > 0) 
				html += '<tr><td align="left">Penalties Missed:</td><td style="text-align: right;">'+info['breakdown']['penalty_missed']+'</td><td style="text-align: right;">'+info['breakdown']['penalty_missed_points']+'</td></tr>';
			if (info['breakdown']['yellow_cards'] > 0) 
				html += '<tr><td align="left">Yellow Cards:</td><td style="text-align: right;">'+info['breakdown']['yellow_cards']+'</td><td style="text-align: right;">'+info['breakdown']['yellow_cards_points']+'</td></tr>';
			if (info['breakdown']['red_cards'] > 0) 
				html += '<tr><td align="left">Red Cards:</td><td style="text-align: right;">'+info['breakdown']['red_cards']+'</td><td style="text-align: right;">'+info['breakdown']['red_cards_points']+'</td></tr>';
			if (info['breakdown']['best_player_points'] > 0)
				if (info['breakdown']['best_player'] == 1)
					html += '<tr><td align="left">Man of the Match:</td><td style="text-align: right;">&nbsp;</td><td style="text-align: right;">'+info['breakdown']['best_player_points']+'</td></tr>';
				else
					html += '<tr><td align="left">Player Bonus Points:</td><td style="text-align: right;">&nbsp;</td><td style="text-align: right;">'+info['breakdown']['best_player_points']+'</td></tr>';				
			if (info['breakdown']['cancelled_points'] > 0) 
				html += '<tr><td align="left">Suspended match allowance:</td><td style="text-align: right;">&nbsp;</td><td style="text-align: right;">'+info['breakdown']['cancelled_points']+'</td></tr>';				

		} else {
			html += '<tr><td align="left">Did not play:</td><td style="text-align: right;">&nbsp;</td><td style="text-align: right;">0</td></tr>';
		}		
	}	else {
		html += '<tr><td align="left">Did not play:</td><td style="text-align: right;">&nbsp;</td><td style="text-align: right;">0</td></tr>';
	}
	html += '</table>';
	return html;
}

function showTooltip(el,tooltip,params) {	
	if (!params) params = {};
	//$(tooltip).style.zIndex = 1000;
	$(tooltip).style.opacity=0;
	$(tooltip).show();
	var maxRight,minLeft;
	var dim = $(tooltip).getDimensions();
	var pos = $(tooltip).cumulativeOffset();

	var elDim = $(el).getDimensions();
	var elPos = $(el).cumulativeOffset();
	
	var vpDim = document.viewport.getDimensions();
	var vpOff = document.viewport.getScrollOffsets();

	maxRight = vpDim['width']-dim['width']-5;
	minLeft = 5;
	
	if (params['setAbsolutePosition']) 
		$(tooltip).absolutize();

	if (vpDim['height']+vpOff['top'] > elPos['top']+dim['height']+125) {
		if (params['setAbsolutePosition']) {
			$(tooltip).style.top = (elPos['top']+elDim['height']+3)+'px';			
			$(tooltip).style.left = Math.max(minLeft,Math.min(maxRight,elPos['left']-dim['width']/2))+'px';
			if (params['setTop']) {
				$(tooltip).style.top = params['setTop'];
				$(tooltip).style.left = Math.max(minLeft,Math.min(maxRight,elPos['left']-dim['width']/2)) + 50 +'px';
			}
			
			
		}
		
		//$(tooltip).style.top = (elDim['height']+3)+'px';
	} else {
		if (params['setAbsolutePosition']) {
			$(tooltip).style.top = (elPos['top']-dim['height']-10)+'px';
			$(tooltip).style.left = Math.max(minLeft,Math.min(maxRight,elPos['left']-dim['width']/2))+'px';
			if (params['setTop']) {
				$(tooltip).style.top = params['setTop'];
				$(tooltip).style.left = Math.max(minLeft,Math.min(maxRight,elPos['left']-dim['width']/2)) + 50 +'px';
			}			
			
		} else {
			$(tooltip).style.top = (-dim['height']-3)+'px';
		}
	}
	if (params['setTop']) {
		$(tooltip).style.top = params['setTop'];
	}
	if (params['setLeft']) {
		//alert(' ' +params['setLeft']+ ' ');
		$(tooltip).style.left = findPosX(document.getElementById(params['setLeft'])) + 20 +'px';
	}

	//$(tooltip).style.top = '-'+($(tooltip).style.top);
	//alert($(tooltip).style.top);
	$(tooltip).appear({delay: 0,duration: 0.2});
	//$(tooltip).show();
}
function showTooltip2(el,tooltip,params) {	
	if (!params) params = {};
	//$(tooltip).style.zIndex = 1000;
	$(tooltip).style.opacity=0;
	$(tooltip).show();
	var maxRight,minLeft;
	var dim = $(tooltip).getDimensions();
	var pos = $(tooltip).cumulativeOffset();

	var elDim = $(el).getDimensions();
	var elPos = $(el).cumulativeOffset();
	
	var vpDim = document.viewport.getDimensions();
	var vpOff = document.viewport.getScrollOffsets();

	maxRight = vpDim['width']-dim['width']-5;
	minLeft = 5;
	
	//$(tooltip).style.top = '-'+($(tooltip).style.top);
	//alert($(tooltip).style.top);
	$(tooltip).appear({delay: 0,duration: 0.2});
	//$(tooltip).show();
}

function hideTooltip(tooltip) {
	$(tooltip).fade({delay: 0,duration: 0.1});
	//$($(tooltip).parentNode).style.zIndex = 10;
}
  function findPosX(obj)
  {
    var curleft = 0;
    if(obj.offsetParent)
        while(1) 
        {
          curleft += obj.offsetLeft;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.x)
        curleft += obj.x;
    return curleft;
  }
  
function setEntryCost(id,val)
{
	for (var i=0 ; i<document.getElementById(id).length ; i++)
	{
		if (document.getElementById(id).options[i].value == val)
		{
			document.getElementById(id).options[i].selected=true;
		}
	}
}

function blogPost(allowed,havebets,blocked,params) {

	params = params || {};

	var logged = false;

	var url = web_root+'blog.php?action=ajax_report_precheck';
  new Ajax.Request(url, {
		parameters: params,
	  asynchronous: false,
	  onComplete:function(request) {
	  	var json, i;
	    eval('json = ' + request.responseText);
	    
	    allowed = json['allowed'];
	    blocked = json['blocked'];
	    havebets = json['havebets'];
	    logged = json['logged'];
		}
	});

	if (!logged) {
		alert('You must log in to make a report.');
		return;
	}
	
	if (blocked) {
			alert('Your account is blocked');	
	} else {
	
		if (isNaN(allowed))
			var allowed = true;
		if (allowed) {
				
			if (havebets) {
				
				var message = $('message').value;
				var leagueId = $('blog_league_id').value;
				//alert(message);
	
				var url = web_root+'blog.php?action=ajax_blogpost';
				var x = new Ajax.Request(url,{
				method: 'post',
					parameters: {
						str: message,
						league_id: leagueId
					},
					onCreate: function (){
					},
					onComplete: function(r) {
						var res = eval('(' + r.responseText + ')');
						if ($('blog_post1')) {
							$('RC-microblog-content').innerHTML = res['posts'];
						}
						
						$('post_list').innerHTML = res['posts'];
						
						$('message').value = "Type your message here...";
						
						if (res['alert']!='empty') {
							alert(res['alert']);
						}
					}		
				});
			} else {
				alert('You must place a bet first');			
			}
		} else {
			alert('You must log in to post a message.');	
		}
		
	} // end blocked
}

function blogReport(allowed,havebets,reported,post_id,blocked,reported_by) {
	if (blocked) {
			alert('Your account is blocked');	
	} else {
	
		if (isNaN(allowed))
			var allowed = true;
		if (allowed) {
				
			if (havebets) {
	
				var url = web_root+'blog.php?action=ajax_blogreport';
				var x = new Ajax.Request(url,{
				method: 'post',
					parameters: {
						reported: reported,
						post_id: post_id,
						reported_by: reported_by
					},
					onCreate: function (){
					},
					onComplete: function(r) {
						var res = eval('(' + r.responseText + ')');
						$('post_list').innerHTML = res['posts'];
						
						if (res['alert']!='empty') {
							alert(res['alert']);
						}
					}		
				});
			} else {
				alert('You must place a bet first');			
			}
		} else {
			alert('You must log in to make a report.');	
		}
	} // end blocked
}



function blockUser(id,blocked,post_id) {
				var url = 'admin-member.php?action=ajax_blogblock';
				var x = new Ajax.Request(url,{
				method: 'post',
					parameters: {
						blocked: blocked,
						user_id: id,
						post_id: post_id
					},
					onCreate: function (){
					},
					onComplete: function(r) {
						var res = eval('(' + r.responseText + ')');
						//alert(r.responseText);
						//$$('.block'+id).each(function(item,index){
						$$('.block').each(function(item,index){
							if (blocked=='Y') {
						   		$(item).update('<a href="javascript://" onclick="blockUser('+id+',\'N\','+post_id+');">Unblock User</a>');
							} else {
								//$(item).update('<a href="javascript://" onclick="blockUser('+id+',\'Y\','+post_id+');">Block User</a>');
								$(item).update('<span style="color:green">User can post messages to blog</span>');
							}
						});

					}		
				});

}


function userRegCheck() {
	

				var first_name = $('textfield').value;
				var last_name = $('textfield2').value;
				var birth_day = $('select4').value;
				var birth_month = $('select3').value;
				var birth_year = $('select5').value;
				var country = $('select2').value;
				var address = $('textfield5').value;
				var city = $('textfield7').value;
				var ret = false;
				//alert(first_name);
	
				var url = web_root+'user_new.php?raction=ajax_reg_check';
				var x = new Ajax.Request(url,{
				asynchronous: false,
				method: 'post',
					parameters: {
						first_name: first_name,
						last_name: last_name,
						birth_day: birth_day,
						birth_month: birth_month,
						birth_year: birth_year,
						country: country,
						address: address,
						city: city
					},
					onCreate: function (){
					},
					onComplete: function(r) {
						var res = eval('(' + r.responseText + ')');
						
						var error_str = '';
						
						if (res['first_name']!='') {
							error_str += res['first_name']+"\n";
						}
						if (res['last_name']!='') {
							error_str += res['last_name']+"\n";
						}
						if (res['birth_day']!='') {
							error_str += res['birth_day']+"\n";
						}
						/*if (res['birth_month']!='') {
							error_str += res['birth_month']+"\n";
						}
						if (res['birth_year']!='') {
							error_str += res['birth_year']+"\n";
						}*/
						if (res['country']!='') {
							error_str += res['country']+"\n";
						}
						if (res['address']!='') {
							error_str += res['address']+"\n";
						}
						if (res['city']!='') {
							error_str += res['city']+"\n";
						}						
						
						if (error_str != '') {
							alert(error_str);
							ret = false;
						} else {
							//jQuery('#Image28').click();
							ret = true;
						}
					}		
				});
	
	return ret;
}

function userLoginCheck(formEl) {
	
	var formReturn = true;
				/*var uniqid = $('data[uniqid]').value;
				alert(uniqid);
				var log1 = 'data['+uniqid+'l]';
				alert(log1);
				var login = $(log1).value;
				alert(login);*/
				var uniqid = $('data[uniqid]').value;
				
				var login = $('username').value;
				var password = $('password').value;
				//alert(first_name);
	
				var url = affiliate_web_root+'user.php?laction=ajax_login_check';
				var x = new Ajax.Request(url,{
				method: 'post',
					asynchronous: false,
					parameters: {
						uniqid: uniqid,
						login: login,
						password: password,
						do_login: 1,
						return_url: $(formEl).elements['return_url'].value
					},
					onCreate: function (){
					},
					onComplete: function(r) {
						var res = eval('(' + r.responseText + ')');
						
						var error_str = '';
						
						
						if (res['login']!='') {
							error_str += res['login']+"\n";
						}
						if (res['password']!='') {
							error_str += res['password']+"\n";
						}
						
						if (res['login_failed']) {
							error_str += res['login_failed']+"\n";
						}
						
						
							if (error_str != '') {
								alert(error_str);
								formReturn = false;
							} else {
								//jQuery('#Image31').click();
								if (res['return_url']) {
									formReturn = false;
									location = res['return_url'];
								}
							}
					}		
				});
	return formReturn;
}

