//loadTemplate.js
//Justin Rohde
//
//Load the default template for KineticStorm.com pages.  All pages
//share a common header-right image, menu, and footer.

function getCookieValue(name)
{
	//Purpose
	//	Determines if the cookie name exists
	
	//Return Value
	//	If the cookie name is found, it is returned.  If no match
	//  is found, an empty string ("") is returned.
	
	//Parameters
	//	name: the name of the cookie whose corresponding value
	//        is to be found
	
	if (document.cookie.length > 0)
	{		
		//Find the index where the name starts
		var index = document.cookie.indexOf(name);
		if (index != -1)
		{
			//Find the index of the equal sign
			var start = document.cookie.indexOf('=',index);
			if (start == -1)
			{
				return "";
			}
			
			//Find the end of the value content
			start += 1;
			var end = document.cookie.indexOf(';',start);
			if (end == -1)
			{
				end = document.cookie.length;
			}
			return document.cookie.substring(start,end);
		}
		
		//Cookie not found
		return "";
	}

	//No cookies
	return "";
}

function loadTemplate()
{	
	//Purpose
	//	Load the elements shared by all kineticstorm.com pages, such as header
	//  images, menu items, colors, etc.
	
	//If the user is not logged in, show the login link at the 
	//top right of the page.  If logged in, show different links to
	//the account page and logout.	
	
	//Get existing content in the "content-wrapper" division
	var content = document.getElementById("content-wrapper").innerHTML;
	
	//Add login display strings to the content
	var link;
	var username = getCookieValue("kineticstormcom_user");
	if (username == "")
	{
		//User is not logged in; display "login"
		link = "<div style = \"width: 800px; text-align: right; margin-left: auto; margin-right: auto; font-size: 8pt;\">" +
			   "    <a class = \"login\" href = \"http://www.kineticstorm.com/login.php\">login</a>" +
			   "</div>";
	}
	else
	{
		//User is logged in; display username, account link, and logout link
		link = "<div style = \"width: 800px; text-align: right; margin-left: auto; margin-right: auto; font-size: 8pt;\">" + 
			   "    <span style = \"color: white;\">logged in as: " + username + "</span> | " + 
	           "    <a class = \"login\" href = \"http://www.kineticstorm.com/settings.php\">settings</a> | " + 
			   "    <a class = \"login\" href = \"http://www.kineticstorm.com/logout.php\">logout</a>" +
			   "</div>";
	}
	
	//Refresh the content
	document.getElementById("content-wrapper").innerHTML = link + content;
	
	//Set logo image in right header
	document.getElementById("header-right").innerHTML = 
		"<img src = \"http://www.kineticstorm.com/images/logos/Logo_80.png\">";
		
	//Set menu items
	document.getElementById("menu").innerHTML = 
		"<a href = \"http://www.kineticstorm.com/\">Home</a><br />" + 
		"<a href = \"http://www.kineticstorm.com/products.php\">Products</a>" +
		"<ul>" + 
		"	<li>" + 
		"		<a href = \"http://www.kineticstorm.com/products/filesplitterlite.php\">" + 
		"			File Splitter Lite" +
		"		</a>" +
		"	</li>" +
		"	<li>" +
		"		<a href = \"http://www.kineticstorm.com/products/kineticount.php\">" +
		"			KinetiCount" +
		"		</a>" +
		"	</li>" +
		"</ul>" +
		"<a href = \"http://www.kineticstorm.com/support.php\">Help and Support</a><br />" +
		"<a href = \"http://www.kineticstorm.com/feedback.php\">Feedback</a><br />" + 
		"<a href = \"http://www.kineticstorm.com/donate.php\">Donate</a><br />" +
		"<a href = \"http://www.kineticstorm.com/about.php\">About</a><br /><br >";
	
	//Set footer items
	document.getElementById("footer").innerHTML = 
		"&copy; 2009-2010 KineticStorm Software<br />" + 
		"	<ul>" + 
		"		<li><a href = \"http://www.kineticstorm.com/\">Home</a> |</li>" +
		"		<li><a href = \"http://www.kineticstorm.com/products.php\">Products</a> |</li>" +
		"		<li><a href = \"http://www.kineticstorm.com/support.php\">Help and Support</a> |</li>" +
		"		<li><a href = \"http://www.kineticstorm.com/feedback.php\">Feedback</a> |</li>" +
		"		<li><a href = \"http://www.kineticstorm.com/donate.php\">Donate</a> |</li>" + 
		"		<li><a href = \"http://www.kineticstorm.com/about.php\">About</a></li>" +
		"	</ul>";			
}