
 function showError(errorTxt)
  {
  		var errMsg = document.getElementById("error");
  		var errBox = document.getElementById("errBox");
  		errBox.setAttribute("class", "errorBox");
		errMsg.innerHTML = errorTxt;
//	    errMsg.style.display = "";
  }
  
function checkValidEmail(str)
{
	var at="@";
	var dot=".";
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);
	var comma=str.indexOf(",");
	
	if (comma > -1)
		return false;
		
	if (str.indexOf(at)==-1)
		return false;

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)
		   return false;

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)
		  return false;

   	if (str.indexOf(at,(lat+1))!=-1)
		return false;	

	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)
		return false;

	if (str.indexOf(dot,(lat+2))==-1)
		return false;
		
	if (str.indexOf(" ")!=-1)
    	return false;

 	return true;					
}	

function submitPrefChange()
{
	var password = document.getElementById("prefpassword1").value;
	var password2 = document.getElementById("prefpassword2").value;
	if (password2 != password)
	{
		showError('Your passwords do not match');
		return false;
	}
	return true;
}

function clearMessage()
{
	var msgBox = document.getElementById("error");
	msgBox.innerHTML = '';
	msgBox.style.display = 'none';
}


function showMessage(msg)
{
	var data = YAHOO.lang.JSON.parse(msg);
    
	//var data = JSON.parse(msg);
	if (data.msg)
	{
		showError(data.msg);
		//setTimeout ( "clearMessage()", 2000 );
	}
	if (data.gameId)
	{
		var gameId = data.gameId;
		var userId = data.userId;
		var predictionRowName = 'row' + gameId;
		var predictionRow = document.getElementById(predictionRowName);	
		var html = data.html;
		predictionRow.innerHTML = html;
	}
	if (data.realScoreId)
	{
		var gameId = data.realScoreId;
		var predictButtonBoxName = 'predictButtonBox' + gameId;
		var predictButtonBox = document.getElementById(predictButtonBoxName);	
		var html = '<a href="admin.php?delRealScore=true&gameId='+gameId+'">edit</a>';
		predictButtonBox.innerHTML = html;
	}
}



function ajaxUpdate(url)
{
	var xmlHttp;
	
	try
 	{
  		// Firefox, Opera 8.0+, Safari
  		xmlHttp=new XMLHttpRequest();
  	}
	catch (e)
  	{
  		// Internet Explorer
  		try
   	 	{
    		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    	}
  		catch (e)
    	{
    		try
      		{
      			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      		}
    		catch (e)
      		{
      			alert("Your browser does not support AJAX!");
      			return false;
      		}
    	}
  	}	
  
  	xmlHttp.onreadystatechange=function(type)
    {
    	if(xmlHttp.readyState==4)
      	{
      		showMessage(xmlHttp.responseText);
      	}
    }

    xmlHttp.open("GET",url,true);
  	xmlHttp.send(null);
  }


  
function addPrediction(gameId, userId, winner, aTeam, hTeam)
{
	var rowName = 'row' + gameId;
	var rowDiv = document.getElementById(rowName);
		
	var aScoreDivName = 'aTeamDivBox' + gameId;
	var aScoreDiv = document.getElementById(aScoreDivName);
	var hScoreDivName = 'hTeamDivBox' + gameId;
	var hScoreDiv = document.getElementById(hScoreDivName);	
	
	var predictButtonBoxName = 'predictButtonBox' + gameId;
	var predictButtonBox = document.getElementById(predictButtonBoxName);	
	
	var aScoreName = 'aTeam' + gameId;
	var aScoreBox = document.getElementById(aScoreName);
	var hScoreName = 'hTeam' + gameId;
	var hScoreBox = document.getElementById(hScoreName);	
	var aScore;
	var hScore;
	
	if (winner == 0)
	{
		aScore = aScoreBox.value;
		hScore = hScoreBox.value;
	
		if (aScore.indexOf(".") != -1 || hScore.indexOf(".") != -1)
		{
			showError('Scores must be integers');
			return false;
		}
	
		if ((hScore-0) != hScore || (aScore-0) != aScore)
		{
			showError('Scores must be an integer');
			return false;
		}
		
		aScore = aScore - 0;
		hScore = hScore - 0;
		
		if (aScore == 0 && hScore == 0 )
		{
			showError('Please enter your predicted score to earn additional points');
			return false;
		}	
		
		if (aScore < 0 || hScore < 0 )
		{
			showError('Scores can not be negative');
			return false;
		}
	
		
		if (aScore > 0 || hScore > 0) //if a score was entered
		{
			if (aScore > hScore)
			{
				winner = aTeam;
			}
			else
			{
				winner = hTeam;
			}
		}
	}
	if (predictButtonBox)
	{
		predictButtonBox.innerHTML = '<img src="images/spinner.gif" height="20">';
	}
//	aScoreDiv.innerHTML = aScore;
//	hScoreDiv.innerHTML = hScore;

	aScoreDiv.innerHTML =  '<img src="images/spinner.gif" height="20">';
	hScoreDiv.innerHTML =  '<img src="images/spinner.gif" height="20">';


	url = 'ajax.php?addprediction=true&userId=' + userId + '&gameId=' + gameId +'&hScore=' + hScore + '&aScore=' + aScore;
	if (winner)
		url += '&winner=' + winner;
  	ajaxUpdate(url);
}

function delPrediction(gameId, userId)
{
	url = 'ajax.php?delprediction=true&userId=' + userId + '&gameId=' + gameId;
  	ajaxUpdate(url);
}


function addRealScore(gameId)
{
	var rowName = 'row' + gameId;
	var rowDiv = document.getElementById(rowName);
	var aScoreName = 'aTeam' + gameId;
	var aScore = document.getElementById(aScoreName).value;
	var hScoreName = 'hTeam' + gameId;
	var hScore = document.getElementById(hScoreName).value;	
		
	var aScoreDivName = 'aTeamDivBox' + gameId;
	var aScoreDiv = document.getElementById(aScoreDivName);
	var hScoreDivName = 'hTeamDivBox' + gameId;
	var hScoreDiv = document.getElementById(hScoreDivName);	
	
	var predictButtonBoxName = 'predictButtonBox' + gameId;
	var predictButtonBox = document.getElementById(predictButtonBoxName);	
	
	if (aScore == '' || hScore == '')
	{
		showError('You must fill in a score for both teams');
		return false;
	}
	
	if (aScore < 0 || hScore < 0 )
	{
		showError('Scores can not be negative');
		return false;
	}	
	
	
	if (aScore.indexOf(".") != -1 || hScore.indexOf(".") != -1)
	{
		showError('Scores must be integers');
		return false;
	}
	
	
	if ((hScore-0) != hScore || (aScore-0) != aScore)
	{
		showError('Scores must be an integer');
		return false;
	}
	predictButtonBox.innerHTML = '<img src="images/spinner.gif" height="20">';
	aScoreDiv.innerHTML = aScore;
	hScoreDiv.innerHTML = hScore;
	
	url = 'ajax.php?addrealscore=true&gameId=' + gameId +'&hScore=' + hScore + '&aScore=' + aScore;
  	ajaxUpdate(url);
}

function addNewGame()
{
	var homeTeam = document.getElementById('homeTeam').value;
	var awayTeam = document.getElementById('awayTeam').value;
	var month = document.getElementById('month').value;
	var day = document.getElementById('day').value;
	var year = document.getElementById('year').value;
	var hour = document.getElementById('hour').value;
	var minute = document.getElementById('minute').value;
	var gameTime = year+'-'+month+'-'+day+' '+hour+':'+minute;
	url = 'ajax.php?addnewgame=true&hTeam=' + homeTeam +'&aTeam=' + awayTeam + '&gameTime=' + gameTime;
  	ajaxUpdate(url);
}

  
  function submitLogin()
  {
  var email = document.getElementById("email").value;
	var password = document.getElementById("password").value;
	if (email == '')
	{
		showError('Please enter your email address');
		return false;
	}
	if (password == '')
	{
		showError('Please enter your password');;
		return false;
	}
	return true;

  }
  
  
  function submitRegForm()
  {
	var email = document.getElementById("regemail").value;
	var nickname = document.getElementById("nickname").value;

	var password = document.getElementById("regpassword1").value;
	var password2 = document.getElementById("regpassword2").value;
	var teston = document.getElementById("teston");
	teston.value = 'crowded_scores';
	if (checkValidEmail(email) != true)
	{
		showError('Please enter a valid email address');
		return false;
	}
	if (nickname == '')
	{
		showError('Please enter a nickname for display on the leaderboard');
		return false;
	}
	if (password == '')
	{
		showError('Please enter a password');
		return false;
	}
	if (password2 != password)
	{
		showError('Your passwords do not match');
		return false;
	}
	return true;

  }
  
  function showGroupLeaders()
  {
 	var redirect;
	redirect = document.getElementById('changeGroup').value;
	document.location.href = redirect;
  }
  
   function addGroup()
  {
  	var group = document.getElementById("newGroup").value;
	if (group == '')
	{
		showError('Please enter a name for your pool');
		return false;
	}
	
	document.location.href = 'groups.php?group='+group;
	return true;

  }
  
