YAHOO.namespace("example.container");

YAHOO.util.Event.addListener(window, "load", init);

function loadGameResults(id)
{ 
	var mytime= "&scw="+new Date().getTime();
    var url = "loadGameResults.ajax?id=" + escape(id) + mytime;
    
    var callback = {
		success: function(o) {
		
			// Parsing JSON strings can throw a SyntaxError exception, so we wrap the call
			// in a try catch block
			try {
    			var myObject = YAHOO.lang.JSON.parse(o.responseText);
   	     		var homeScore = document.getElementById('homeScore');
    	    	var awayScore = document.getElementById('awayScore');
        		var gameId = document.getElementById('gameId');
        		var homeName = document.getElementById('homeTeamName');
        		var awayName = document.getElementById('awayTeamName');
        		homeScore.value = myObject.home;
        		awayScore.value = myObject.away;
        		homeName.innerHTML=myObject.homeName;
        		awayName.innerHTML=myObject.awayName;
        		gameId.value = myObject.id;
        		showSurvey()
			}
			catch (e) {
    			displayErrorAlert();
			}
		},
		
		failure: function(o) {
			displayErrorAlert();
			}
			} 
    var transaction = YAHOO.util.Connect.asyncRequest('GET', url, callback); 
}

function submitGameResults() {
	var url = "submitGameResults.ajax"; 

	var callback = {
		success: function(o) {
			// If we got here, the game was already updated.
			// The only thing left to do is to update the DOM.
			// Get these values from the form that was just submitted.
			
			var gameId = document.getElementById("gameId").value;
			var homeScore =	document.getElementById("homeScore").value;
			var awayScore =	document.getElementById("awayScore").value;			

			// Update the DOM
			document.getElementById('home'+gameId).innerHTML=homeScore;
			document.getElementById('away'+gameId).innerHTML=awayScore;
		},
		failure: function(o) {
			displayErrorAlert();
		}
	}
	
	var formObject = document.getElementById('submitGameResultsForm'); 
	YAHOO.util.Connect.setForm(formObject);
	var transaction = YAHOO.util.Connect.asyncRequest('POST', url, callback, null); 
	YAHOO.example.container.panel1.hide();		
}

function displayErrorAlert()
{
	alert("Unexpected Problem....PLEASE send an email to Service@StoryCreekWeb.com to report this message.  Please refresh this screen so you can see what was saved in the database prior to the error."); //FAILURE
}

function init() {
	// Instantiate a Panel from markup
	YAHOO.example.container.panel1 = new YAHOO.widget.Overlay("panel1", { fixedcenter:true, visible:false, width:"300px" } );  
	YAHOO.example.container.panel1.render();
}

		
function showSurvey() {
	YAHOO.example.container.panel1.show();
}