
var openDivs = new Array();

function createXMLHttpRequest()
{
    try { return new XMLHttpRequest(); } catch(e) {}
    try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {}
    try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {}
    return null;
}

function closeOpenDivs()
{
	var arrayLength = openDivs.length;
	for(i=0;i<arrayLength;i++)
	{
		showVoteBlockReverse(openDivs[i]);
	}
}

var login_fixture_id = 0;
var login_team_type = 0;
var login_win_type = 0;

function voteNow(divToChange,fixture_id,team_type,win_type)
{
	login_fixture_id = fixture_id;
	login_team_type = team_type;
	login_win_type = win_type;

	/*
	 * Close any openDivs
	 */
	closeOpenDivs();
	openDivs.push(divToChange);

	/*
	 * Load the content
	 */
	var urlContent = "ajax.php?type=vote_now&fixture_id=" + fixture_id + "&team_type=" + team_type + "&win_type=" + win_type;

    var area = document.getElementById(divToChange);
    if (area)
	{
		var xmlHttp = createXMLHttpRequest();

		if (xmlHttp)
		{
			xmlHttp.onreadystatechange = function()
			{
				if (xmlHttp.readyState==4)
				{
					/*
					 * Style the Area
					 */
					setAreaStyle(area);

					if(xmlHttp.responseText=='NO LOGIN')
					{
						area.innerHTML = "<p>You must be logged onto the site to VOTE!<br /><br /><strong>You will re-directed to login/join page in 5 seconds....</strong></p>";

						setTimeout("SendToLoginPage()",5000);
					}
					else
					{
						area.innerHTML = xmlHttp.responseText;
					}
				}
			}

			xmlHttp.open("GET", urlContent, true);
			xmlHttp.send(null);
		}
	}
}

function setAreaStyle(area)
{
	area.style.background = "#6599FF";
	area.style.width = "260px";
	area.style.height = "175px";
	area.style.border="2px solid #097054";
}

function SendToLoginPage()
{
	var url = "login.php?login_fixture_id=" + login_fixture_id + "&login_team_type=" + login_team_type  + "&login_win_type=" + login_win_type;
	document.location.href=url;
}

function showVoteBlock(divToChange,fixture_id)
{
	/*
	 * Close any openDivs
	 */
	closeOpenDivs();
	openDivs.push(divToChange);

	/*
	 * Load the content
	 */
	var urlContent = "ajax.php?type=show_votes&fixture_id=" + fixture_id;
    var area = document.getElementById(divToChange);

	if (area)
	{
		var xmlHttp = createXMLHttpRequest();

		if (xmlHttp)
		{
			xmlHttp.onreadystatechange = function()
			{
				if (xmlHttp.readyState==4)
				{
					/*
					 * Style the Area
					 */
					setAreaStyle(area);

					area.innerHTML = xmlHttp.responseText;
				}
			}

			xmlHttp.open("GET", urlContent, true);
			xmlHttp.send(null);
		}
	}
}

function showVoteBlockReverse(divToChange)
{
    var area = document.getElementById(divToChange);
	if (area)
	{
		area.innerHTML = "";
		area.style.width = "0px";
		area.style.height = "0px";
		area.style.border="0px";
	}

	running = false;
}

/******************************/
/******************************/

function reloadFormTable(season_id,division_id,num_games,home_team_id,away_team_id,fixture_date,home_results_div,away_results_div)
{
	var divToChange = "formTable";

	/*
	 * Load the content
	 */
	var urlContent = "ajax.php?type=form_table&season_id=" + season_id + "&division_id=" + division_id + "&num_games=" + num_games + "&home_team_id=" + home_team_id + "&away_team_id=" + away_team_id + "&fixture_date=" + fixture_date;

    var area = document.getElementById(divToChange);
    if (area)
	{
		var xmlHttp = createXMLHttpRequest();

		if (xmlHttp)
		{
			xmlHttp.onreadystatechange = function()
			{
				if (xmlHttp.readyState==4)
				{
					area.innerHTML = xmlHttp.responseText;
					recentResults1(season_id,division_id,num_games,home_team_id,away_team_id,fixture_date,home_results_div,away_results_div);
				}
			}

			xmlHttp.open("GET", urlContent, true);
			xmlHttp.send(null);
		}
	}
}

function recentResults1(season_id,division_id,num_games,home_team_id,away_team_id,fixture_date,home_results_div,away_results_div)
{
	/*
	 * Reload any results
	 */
	var divLength2 = home_results_div.length;
	if(divLength2 > 0)
	{
		var urlContent2 = "ajax.php?type=recent_results&season_id=" + season_id + "&division_id=" + division_id + "&num_games=" + num_games + "&team_id=" + home_team_id + "&fixture_date=" + fixture_date + "&div_id=" + home_results_div;

		var area2 = document.getElementById(home_results_div);
		if (area2)
		{
			var xmlHttp = createXMLHttpRequest();

			if (xmlHttp)
			{
				xmlHttp.onreadystatechange = function()
				{
					if (xmlHttp.readyState==4)
					{
						area2.innerHTML = xmlHttp.responseText;

						recentResults2(season_id,division_id,num_games,home_team_id,away_team_id,fixture_date,home_results_div,away_results_div);
					}
				}

				xmlHttp.open("GET", urlContent2, true);
				xmlHttp.send(null);
			}
		}
	}
}

function recentResults2(season_id,division_id,num_games,home_team_id,away_team_id,fixture_date,home_results_div,away_results_div)
{
	var divLength3 = away_results_div.length;
	if(divLength3 > 0)
	{
		var urlContent3 = "ajax.php?type=recent_results&season_id=" + season_id + "&division_id=" + division_id + "&num_games=" + num_games + "&team_id=" + away_team_id + "&fixture_date=" + fixture_date + "&div_id=" + away_results_div;

		var area3 = document.getElementById(away_results_div);
		if (area3)
		{
			var xmlHttp = createXMLHttpRequest();

			if (xmlHttp)
			{
				xmlHttp.onreadystatechange = function()
				{
					if (xmlHttp.readyState==4)
					{
						area3.innerHTML = xmlHttp.responseText;
					}
				}

				xmlHttp.open("GET", urlContent3, true);
				xmlHttp.send(null);
			}
		}
	}
}

/*********************************** SUBMIT COMMENTS *************************************/

function submitComment(fixture_id,member_id)
{
	var divToChange = "commentsDiv";
	var f = document.forms['commentsForm'];
	var comment = f.comment.value;
	if(comment.length == 0)
	{
		alert("Please enter Your Comment");
	}
	else
	{
		/*
		 * Load the content
		 */
		var urlContent = "ajax.php?type=add_comment&fixture_id=" + fixture_id + "&member_id=" + member_id + "&comment=" + comment;

		var area = document.getElementById(divToChange);
		if (area)
		{
			var xmlHttp = createXMLHttpRequest();

			if (xmlHttp)
			{
				xmlHttp.onreadystatechange = function()
				{
					if (xmlHttp.readyState==4)
					{
						area.innerHTML = xmlHttp.responseText;

						alert("Thank you. Your comment has been added");
					}
				}

				xmlHttp.open("GET", urlContent, true);
				xmlHttp.send(null);
			}
		}
	}
}

function showAllComments(fixture_id,num_comments)
{
	var divToChange = "commentsDiv";

	/*
	 * Load the content
	 */
	var urlContent = "ajax.php?type=show_all_comments&fixture_id=" + fixture_id + "&num_comments=" + num_comments;

	var area = document.getElementById(divToChange);
	if (area)
	{
		var xmlHttp = createXMLHttpRequest();

		if (xmlHttp)
		{
			xmlHttp.onreadystatechange = function()
			{
				if (xmlHttp.readyState==4)
				{
					area.innerHTML = xmlHttp.responseText;
				}
			}

			xmlHttp.open("GET", urlContent, true);
			xmlHttp.send(null);
		}
	}
}