/**
*
* The following functions are for the home.html web page
* Written by Bernard Dallaire
* Date July 31st 2009
* Version 1.0
*
**/

// This function read info.xml to display all information on the home page in the proper info table
function getNews(language) {
	// First let's get the cookie is set and what's the value
	// UP mean that the user prefers not to see the news
	var newsCookie = getCookie("newsTablecastorsCookie");
	if (newsCookie == "UP") {
		ReverseContentDisplay("newsTable");
	}
	var newsNumber = 0;
	var xmlDoc = new ActiveXObject("MSXML.DOMDocument");
	xmlDoc.async = "false";
	xmlDoc.load("xml/news.xml");
	var informationXML = xmlDoc.documentElement;
	// Check if there's anything in the file
	if (informationXML.childNodes.length == 1) {
		//putSignOnPage(language);
		return false;
	}
	
	// Now go through the XML file to read the news
	for (var i = 0; i < informationXML.childNodes.length; i++) {
		if (informationXML.childNodes.item(i).childNodes.item(0).text + "_" == language) {
			if (newsNumber > 0) {
				addRowToNews(newsNumber);
			}
			document.getElementById("newsTitle" + newsNumber).innerHTML = informationXML.childNodes.item(i).childNodes.item(1).text;
			document.getElementById("newsBody" + newsNumber).innerHTML = informationXML.childNodes.item(i).childNodes.item(2).text;
			newsNumber = newsNumber + 1;
		}
	}
}

// This function is used to add rows to the table to add information in the information table
function addRowToNews(rowID) {
    //add a row to the rows collection and get a reference to the newly added row
    var newRow = document.all("newsTable").insertRow();
    
    var oCell = newRow.insertCell();
    oCell.innerHTML = "<hr/><div id='news' align='center'><p><span id='newsTitle" +
	                  rowID +
					  "' class='titleStyle'></span><br><span id='newsBody" +
					  rowID +
					  "' class='bodyStyle'></span><br><br></p></div>";
}

// This function read schedule.xml to display the next 6 games in the schedule table
function getSchedule(language) {
	// First let's get the cookie is set and what's the value
	// UP mean that the user prefers not to see the next games schedule
	var scheduleCookie = getCookie("nextGamesTablecastorsCookie");
	if (scheduleCookie == "UP") {
		ReverseContentDisplay("nextGamesTable");
	}	
	
	// Let's get the cookie is set and what's the value
	// UP mean that the user prefers not to see the last game
	var scheduleCookie = getCookie("lastGameTablecastorsCookie");
	if (scheduleCookie == "UP") {
		ReverseContentDisplay("lastGameTable");
	}	
	
	// Until season starts and schedule is set lets get out
	return true;
	
	var xmlDoc = new ActiveXObject("MSXML.DOMDocument");
	xmlDoc.async = "false";
	xmlDoc.load("xml/schedule.xml");
	var scheduleXML = xmlDoc.documentElement;
	// Check if there's anything in the file
	if (scheduleXML.childNodes.length == 1) {
		//putSignOnPage(language);
		return false;
	}
	
	var scheduleRow = 0;
	var lastGameResult = "";
	for (var i = 0; i < scheduleXML.childNodes.length; i++) {
		// Set game time mask
		dateFormat.masks.gameTimeMsk = 'ddd, mmm dd @ h:MM tt';
		
		// Capture date from XML file
		var gameTime = new Date();
		parsedDate = scheduleXML.childNodes.item(i).childNodes.item(0).text.split("/");
		gameTime.setFullYear(parsedDate[2],
				             parsedDate[0] - 1,
							 parsedDate[1]);

		// Capture time from XML file
		gameTime.setHours(scheduleXML.childNodes.item(i).childNodes.item(1).text.substring(0,2),
						  scheduleXML.childNodes.item(i).childNodes.item(1).text.substring(2,4));
		
		// Check if date of game is in the future
		// Will list a maximum of 6 future games
		var currentDate = new Date();

		var convertedDateTime = "";
		if (language == "e_") {
			convertedDateTime = gameTime.format("gameTimeMsk");
		} else {
			convertedDateTime = translateDateTime(gameTime.format("gameTimeMsk"));
		}
			
		if (gameTime > currentDate && scheduleRow < 6) {
			if (scheduleRow != 0) {
				addRowToSchedule(scheduleRow);
			}
			document.getElementById("game" + scheduleRow).innerHTML = convertedDateTime +
		                                                " (" + scheduleXML.childNodes.item(i).childNodes.item(2).text +
														" vs. " + scheduleXML.childNodes.item(i).childNodes.item(3).text +
														")";
			scheduleRow += 1;
		}
		
		// Get the result of the last game
		if (gameTime < currentDate) {
			if (scheduleXML.childNodes.item(i).childNodes.item(5) == null) {
				lastGameResult = convertedDateTime +
								 " (" + scheduleXML.childNodes.item(i).childNodes.item(2).text +
								 " vs. " + scheduleXML.childNodes.item(i).childNodes.item(3).text + ")</br>";
				if (language == "f_") {
					lastGameResult = lastGameResult + "Pas encore disponible";
				} else {
					lastGameResult = lastGameResult + "Not available yet";
				}
			} else {
				var resultWL = scheduleXML.childNodes.item(i).childNodes.item(5).text;
				if (language == "f_") {
					if (resultWL == "L") {
						resultWL = "Défaite";
					} else {
						resultWL = "Victoire";
					}
				} else {
					if (resultWL == "L") {
						resultWL = "Loss";
					} else {
						resultWL = "Win";
					}
				}
				if (scheduleXML.childNodes.item(i).childNodes.item(6) != null) {
					lastGameResult = convertedDateTime +
									 " (" + scheduleXML.childNodes.item(i).childNodes.item(2).text +
									 " vs. " + scheduleXML.childNodes.item(i).childNodes.item(3).text + ")</br>" +
									 resultWL + " - " + scheduleXML.childNodes.item(i).childNodes.item(6).text;
				}
			}
		}
	}
	
	// Now the schedule is set, let's set the title (Need to say how many games there is in this table (Max 6)
	tag1 = "<a href=`javascript:ReverseContentDisplay('nextGamesTable')`>";
	tag2 = "<img id='nextGamesTableButton' src='images/arrow_up.png' width='10' height='10' border='0'>";
	if (language == "e_") {
		document.getElementById("scheduleTitle").innerHTML ="&#9484;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&nbsp;NEXT " +
															  scheduleRow +
															  " GAMES &#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&nbsp;" +
															  tag1 +
															  tag2 +
															  "</a>&nbsp;&#9472;&#9472;&#9488;";
	} else {
		document.getElementById("scheduleTitle").innerHTML ="&#9484;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&nbsp;PROCHAINES " +
															  scheduleRow +
															  " PARTIES &#9472;&#9472;&#9472;&#9472;&#9472;&nbsp;" +
															  tag1 +
															  tag2 +
															  "</a>&nbsp;&#9472;&#9472;&#9488;";
	}
	// Set the last game results
	if (lastGameResult != "") {
		document.getElementById("lastGame0").innerHTML = lastGameResult;
		// Now the last game is set, let's set the title
		tag1 = "<a href=`javascript:ReverseContentDisplay('lastGameTable')`>";
		tag2 = "<img id='lastGameTableButton' src='images/arrow_up.png' width='10' height='10' border='0'>";
		if (language == "e_") {
			document.getElementById("lastGameTitle").innerHTML ="&#9484;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&nbsp;LAST GAME" +
																  " &#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&nbsp;" +
																  tag1 +
																  tag2 +
																  "</a>&nbsp;&#9472;&#9472;&#9488;";
		} else {
			document.getElementById("lastGameTitle").innerHTML ="&#9484;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&nbsp;DERNIÈRE PARTIE" +
																  " &#9472;&#9472;&#9472;&#9472;&#9472;&nbsp;" +
																  tag1 +
																  tag2 +
																  "</a>&nbsp;&#9472;&#9472;&#9488;";
		}
	}
}

// This function is used to add rows to the table to add games to the schedule
function addRowToSchedule(rowID) {
    //add a row to the rows collection and get a reference to the newly added row
    var newRow = document.all("nextGamesTable").insertRow();
    
    //add 3 cells (<td>) to the new row and set the innerHTML to contain text boxes
    
    var oCell = newRow.insertCell();
    oCell.innerHTML = "<div id='schedule' align='center'><span id='game" +
	                  rowID +
					  "' class='bodyStyle'></span><br></div>";
}

function setWeatherNode() {
	// First let's get the cookie is set and what's the value
	// UP mean that the user prefers not to see the news
	var newsCookie = getCookie("weatherTablecastorsCookie");
	if (newsCookie == "UP") {
		ReverseContentDisplay("weatherTable");
	}
}

// This function hide and show the different tables
function ReverseContentDisplay(d) {
	if(d.length < 1) { return; }
	if(document.getElementById(d).style.display == "none") {
		document.getElementById(d).style.display = "block";
		document.getElementById(d + "Button").src = "images/arrow_up.png";
		setCookie(d + "castorsCookie","DOWN","365");
	} else {
		document.getElementById(d).style.display = "none";
		document.getElementById(d + "Button").src = "images/arrow_down.png";
		setCookie(d + "castorsCookie","UP","365");
	}
}