
var $j = jQuery.noConflict();


function updateDivisionSelectList() {
	var id=jQuery("#season_id").attr("value");	
	jQuery.getJSON('index.php?option=com_jleague&tmpl=component&format=raw&controller=ajax&task=getDivisionListForSeason',{ seasonid: id },function(j) {
    	  var options = '';
	      for (var i = 0; i < j.length; i++) {
	      	sel = '';
	      	if (i == 0) {
	      		sel = 'selected';
	      	} 
	        options += '<option value="' + j[i].optionValue + '" ' + sel + '>' + j[i].optionDisplay + '</option>';
	      }
	      jQuery("select#division_id").html(options);
	      updateTeamsSelectList();
	});
}

function updateTeamsSelectList() {
	var id=jQuery("#division_id").attr("value");
	jQuery.getJSON('index.php?option=com_jleague&tmpl=component&format=raw&controller=ajax&task=getDivisionCompetingTeams',{ divid: id },function(j) {
    	  var options = '';
	      for (var i = 0; i < j.length; i++) {
	        options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
	      }
	      jQuery("select#hometeam_id").html(options);
	      jQuery("select#awayteam_id").html(options);
	});
}

function getStandings(leagueid,seasonid) { 

			jQuery.ajax({
			    url: 'index.php?option=com_jleague&tmpl=component&format=raw&controller=standings&task=ajaxGetStandings',
			    data: 'leagueid=' + leagueid + '&seasonid=' + seasonid,
			    type: 'POST',
				dataType: 'html',
				beforeSend: function() {
					jQuery('#standings-activity-image').html("<strong><span class='ajax-loading-text'>Loading .... </span></strong>"); 
				},				
	    		error: function (XMLHttpRequest, textStatus, errorThrown) {
	        		jQuery("#standings-wrapper").html(textStatus);
	    		},
	    		success: function(data){
	    			jQuery("#standings-wrapper").html(data);
	    		},
	    		complete: function() {
	  				jQuery("#standings-activity-image").html("");   
	    		}	    		
			});		
			
}

function getGameHistory(teamid,seasonid) { 

			jQuery.ajax({
			    url: 'index.php?option=com_jleague&tmpl=component&format=raw&controller=teams&task=ajaxGetGameHistory',
			    data: 'teamid=' + teamid + '&seasonid=' + seasonid,
			    type: 'POST',
				dataType: 'html',
	    		error: function (XMLHttpRequest, textStatus, errorThrown) {
	        		jQuery("#teamprofile-gamehistory-detail").html(textStatus);
	    		},
	    		success: function(data){
	    			jQuery("#teamprofile-gamehistory-detail").html(data);
	    		},
	    		complete: function() {
	    		}	    		
			});		
			
}

function removeTeamContact(id,teamid) {
	if (!confirm("Confirm DELETE request")) {
		return;
	}
	jQuery.ajax({
	    url: 'index.php?option=com_jleague&tmpl=component&format=raw&controller=teams&task=ajaxRemoveTeamContact',
	    data: 'id=' + id + '&teamid=' + teamid,
	    type: 'POST',
		dataType: 'html',
   		error: function (XMLHttpRequest, textStatus, errorThrown) {
       		jQuery("#team-current-contacts-list").html(textStatus);
   		},
   		success: function(data){
   			jQuery("#team-current-contacts-list").html(data);
   		},
   		complete: function() {
   		}	    		
	});		
}


function addTeamContact(form) {

	if (form.contactname.value == '') {
			alert('Contact Name is required');
			form.contactname.focus();
			return;
	}
	if (!confirm("Confirm ADD request")) {
		return;
	}
	var str = jQuery("#team-contact-input-form").serialize();
	jQuery.ajax({
	    url: 'index.php?option=com_jleague&tmpl=component&format=raw&controller=teams&task=ajaxAddTeamContact',
	    data: str,
	    type: 'POST',
		dataType: 'html',
   		error: function (XMLHttpRequest, textStatus, errorThrown) {
   		},
   		success: function(data){
   			jQuery("#team-current-contacts-list").html(data);
   			form.contactname.value="";
   			form.contactemail.value="";
   			form.contactphone.value="";
   		},
   		complete: function() {
   		}	    		
	});		
}


function removePlayerFromRoster(id,teamid,season) {
	if (!confirm("Confirm DELETE request")) {
		return;
	}
	jQuery.ajax({
	    url: 'index.php?option=com_jleague&tmpl=component&format=raw&controller=teams&task=ajaxRemovePlayerFromRoster',
	    data: 'id=' + id + '&teamid=' + teamid + '&seasonid=' + season,
	    type: 'POST',
		dataType: 'html',
   		error: function (XMLHttpRequest, textStatus, errorThrown) {
       		jQuery("#roster-players-list").html(textStatus);
   		},
   		success: function(data){
   			jQuery("#roster-players-list").html(data);
   		},
   		complete: function() {
   		}	    		
	});		
}


/**
  This function will open GAME form.
  */
function openGameForm() {
	jQuery('#scheduledgameform').dialog('open');
	form = document.getElementById("scheduled-game-form");
	form.conference_game.focus();
}

/**
 This function is used to reset the game form after an add/edit has been performed.
 */
function resetGameForm() {
	form = document.getElementById("scheduled-game-form");
	form.task.value='doSaveGame';
	form.id.value = "0";
	form.gamedate.value = "";
	form.gametime.value = "";
	form.location.value = "";
	form.hometeam_id.selectedIndex = 0;
	form.hometeam_name.value = "";
	form.awayteam_id.selectedIndex = 0
	form.awayteam_name.value = "";
	form.conference_game.value = "Y";
	form.gamestatus.value = "S";	
	form.hometeam_score.value = 0;
	form.awayteam_score.value = 0;	
	form.highlights.value = "";
}

/**
 This function is used to validate the input on the game form.
 */
function validateForm() {
		form = document.getElementById("scheduled-game-form");
		
		var dt=form.gamedate;
		
		// Validate that a division was selected
		if (isDate(dt.value)==false){
			dt.focus();
			return false;
		}	
	
		// Validate that if the hometeam league checkbox is checked that the hometeam_id != 0
		if (form.cb_league_hometeam.checked == true) {
				// Validate that both a home team have been selected
				if (form.hometeam_id.value == 0) {
					form.hometeam_id.focus();
					alert("You must select a home team");
					return false;
				}
		}
		// Validate that if the hometeam league checkbox is NOT checked that hometeam_name != ''
		if (form.cb_league_hometeam.checked == false) {
				// Validate that both a home team have been selected
				form.conference_game.value = 'N';	
				if (form.hometeam_name.value == '') {
					form.hometeam_name.focus();
					alert("You must enter the name of the home team");
					return false;
				}
		}
				
		// Validate that if the awayteam league checkbox is checked that the awayteam_id != 0
		if (form.cb_league_awayteam.checked == true) {
				// Validate that both a home team have been selected
				if (form.awayteam_id.value == 0) {
					form.awayteam_id.focus();
					alert("You must select a away team");
					return false;
				}
		}		
		// Validate that if the awayteam league checkbox is NOT checked that awayteam_name != ''
		if (form.cb_league_awayteam.checked == false) {
				form.conference_game.value = 'N';			
				// Validate that both a home team have been selected
				if (form.awayteam_name.value == '') {
					form.awayteam_name.focus();
					alert("You must enter the name of the away team");
					return false;
				}
		}		

		// Validate the home team and away team are not the same
		if (form.hometeam_id.value ==
			form.awayteam_id.value) {
			alert("The home and away teams cannot be the same");
			form.hometeam_id.focus();
			return false;
		}
	
		if (form.hometeam_id.value != form.teamid.value && form.awayteam_id.value != form.teamid.value) {
			alert("You have not selected your team as either the home team or the away team");
			return false;
		}
		if (form.season_id == 0) {
			alert("Season hasn't been defined");
			return false;
		}
		if (form.division_id == 0) {
			alert("Division hasn't been defined");
			return false;
		}
		return true;
}

/**
 This function is called when adding a game to the database.  It will validate the form data and then call
 an AJAX function to store/update the game data.
 */
function addGame(form) {

		if (!validateForm()) {
			return;
		}
		
		if (confirm("Are you sure you wish to continue?")) {
			var str = jQuery("#scheduled-game-form").serialize();
			jQuery.ajax({
			    url: 'index.php?option=com_jleague&tmpl=component&format=raw&controller=ajax&task=doSaveGame',
			    data: str,
			    type: 'POST',
				dataType: 'html',
		   		error: function (XMLHttpRequest, textStatus, errorThrown) {
		   		},
		   		success: function(data){
		   			jQuery("#current-seasons-games").html(data);
		   			jQuery('#scheduledgameform').dialog('close');
		   			resetGameForm();
		   		},
		   		complete: function() {
		   		}	    		
			});
		}		
}


/**
  The editGame function will retrieve a specific game from the database based on its id.  It is an ajax call to the
  backend.  The return value is a JSON object.  The objected is parsed on the form is populated.
  */  
function editGame(id) {
	resetGameForm();
	form = document.getElementById("scheduled-game-form");
	jQuery.getJSON('index.php?option=com_jleague&tmpl=component&format=raw&controller=ajax&task=getGameJSON',{ id: id },function(game) {
		form.task.value='doSaveGame';
		form.id.value = game.id;
		form.gamedate.value = game.game_date;
		form.location.value = game.location;
		form.hometeam_id.value = game.hometeam_id;
		form.hometeam_name.value = game.hometeam;
		form.awayteam_id.value = game.awayteam_id;
		form.awayteam_name.value = game.awayteam;
		form.conference_game.value = game.conference_game;
		form.gamestatus.value = game.status;
		form.highlights.value = game.highlights;
		if (game.status == 'C') {
			form.gamestatus.disabled = true;
		} else {
			form.gamestatus.disabled = false;
		}
		form.hometeam_score.value = game.hometeam_score;
		form.awayteam_score.value = game.awayteam_score;
		if (game.hometeaminleague == 'Y') {
			form.cb_league_hometeam.checked = true;
			document.getElementById("hometeam_name").className = "inputOff"; 
			document.getElementById("hometeam_selectlist").className = "inputOn";					
		} else {
			document.getElementById("hometeam_name").className = "inputOn"; 
			document.getElementById("hometeam_selectlist").className = "inputOff";
			form.cb_league_hometeam.checked = false;
		}
		
		if (game.awayteaminleague == 'Y') {
			form.cb_league_awayteam.checked = true;
			document.getElementById("awayteam_name").className = "inputOff"; 
			document.getElementById("awayteam_selectlist").className = "inputOn";					
			
		} else {
			form.cb_league_awayteam.checked = false;
			document.getElementById("awayteam_name").className = "inputOn"; 
			document.getElementById("awayteam_selectlist").className = "inputOff";					
		}
		openGameForm();
		gameid = document.getElementById("game-id-display");
		gameid.innerHtml = game.id;		
	});		
}

/**
 This function is used to remove a game.
 */
function deleteGame(gameid,teamid) {
	if (confirm("Are you sure you wish to continue?")) {
		var str = jQuery("#scheduled-game-form").serialize();
		jQuery.ajax({
		    url: 'index.php?option=com_jleague&tmpl=component&format=raw&controller=ajax&task=doDeleteGame',
		    data: 'gameid=' + gameid + '&teamid=' + teamid,
		    type: 'POST',
			dataType: 'html',
	   		error: function (XMLHttpRequest, textStatus, errorThrown) {
	   		},
	   		success: function(data){
	   			jQuery("#current-seasons-games").html(data);
	   			resetGameForm();
	   		},
	   		complete: function() {
	   		}	    		
		});
	}		
}


function updateUserPreferences(form) {

	var str = jQuery("#userpreferences-form").serialize();
	jQuery.ajax({
	    url: 'index.php?option=com_jleague&tmpl=component&format=raw&controller=ajax&task=updateUserPreferences',
	    data: str,
	    type: 'POST',
		dataType: 'html',
   		error: function (XMLHttpRequest, textStatus, errorThrown) {
   		},
   		success: function(data){
   			jQuery("#userpreferences-status").html(data);
   		},
   		complete: function() {
   		}	    		
	});		
} 

	function refreshRoster() {
		teamid = document.getElementById("teamid").value;
		jQuery.ajax({
		    url: 'index.php?option=com_jleague&tmpl=component&format=raw&controller=ajax&task=getTeamRoster',
		    data: 'teamid=' + teamid,
		    type: 'POST',
			dataType: 'html',
	   		error: function (XMLHttpRequest, textStatus, errorThrown) {
	   		},
	   		success: function(data){
	   			jQuery("#roster-players-list").html(data);
	   		},
	   		complete: function() {
	   		}	    		
		});	
	}

/**
This function will return the list of available players to a team when
creating their roster.
*/ 
function getAvailablePlayerList(form) {

	jQuery.ajax({
	    url: 'index.php?option=com_jleague&tmpl=component&format=raw&controller=ajax&task=getavailableplayers',
	    type: 'POST',
		dataType: 'html',
   		error: function (XMLHttpRequest, textStatus, errorThrown) {
   		},
   		success: function(data){
   			jQuery("#playerlist-table").html(data);
   		},
   		complete: function() {
   		}	    		
	});		
}


function addPlayerToRoster(pid,name) {
	var rosterid = document.getElementById("rosterid").value; 
	if (!confirm("Add " + name + " to the roster?")) {
		return;
	}
	jQuery.ajax({
	    url: 'index.php?option=com_jleague&tmpl=component&format=raw&controller=ajax&task=ajaxAddPlayerToRoster',
	    data: 'rosterid=' + rosterid + '&playerid=' + pid,
	    type: 'POST',
		dataType: 'html',
   		error: function (XMLHttpRequest, textStatus, errorThrown) {
   		},
   		success: function(data){
  			jQuery("#manage-roster-status").html(data);
   			refreshRoster();
   			getAvailablePlayerList();
   		},
   		complete: function() {
   		}	    		
	});		
	
}

function removePlayerFromRoster(rid, pid) {
	if (!confirm("Confirm DELETE request?")) {
		return;
	}
	jQuery.ajax({
	    url: 'index.php?option=com_jleague&tmpl=component&format=raw&controller=ajax&task=ajaxRemovePlayerFromRoster',
	    data: 'rosterid=' + rid + '&playerid=' + pid,
	    type: 'POST',
		dataType: 'html',
   		error: function (XMLHttpRequest, textStatus, errorThrown) {
   		},
   		success: function(data){
   			jQuery("#manage-roster-status").html(data);
   			refreshRoster();
   			getAvailablePlayerList();
   		},
   		complete: function() {
   		}	    		
	});		
}

/*
function addPlayerToRoster(form) {

	if (form.playername.value == '') {
			alert('Player Name is required');
			form.playername.focus();
			return;
	}
	if (!confirm("Confirm ADD request")) {
		return;
	}
	var str = jQuery("#roster-player-input-form").serialize();
	jQuery.ajax({
	    url: 'index.php?option=com_jleague&tmpl=component&format=raw&controller=teams&task=ajaxAddPlayerToRoster',
	    data: str,
	    type: 'POST',
		dataType: 'html',
   		error: function (XMLHttpRequest, textStatus, errorThrown) {
   		},
   		success: function(data){
   			jQuery("#roster-players-list").html(data);
   			form.playername.value="";
   			form.playernumber.value="";
   			form.dateofbirth.value="";
   		},
   		complete: function() {
   		}	    		
	});		
}

*/