// JavaScript Document

//READ OUR COOKIE AND SET OUR BACKGROUND STYLE
window.onload = function(e) 
{
  	var cookie = readCookie("style");
	var cookieB = readCookie("bodyBg");
	if (cookie == null)
	{
		setActiveStyleSheet("Theme1");
		setActiveBodyBg("1");
	} else {
		var style = cookie;
		var bg = cookieB;
		setActiveStyleSheet(style);
		setActiveBodyBg(bg);
	}
}

window.onunload = function(e) {
  var style = getActiveStyleSheet();
  var bg = getActiveBodyBg();
  createCookie("style", style, 365);
  createCookie("bodyBg", bg, 365);
}

function setActiveStyleSheet(style)
{
   	var i, a, main;
   	for(i=0; (a = document.getElementsByTagName("link")[i]); i++) 
	{
    	if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) 
		{
       		a.disabled = true;
       		if(a.getAttribute("title") == style) a.disabled = false;
     	}
   	}
}

function getActiveStyleSheet() 
{
	var i, a;
 	for(i=0; (a = document.getElementsByTagName("link")[i]); i++) 
	{
 		if(a.getAttribute("rel").indexOf("style") != -1
  		&& a.getAttribute("title")
  		&& !a.disabled) return a.getAttribute("title");
  	}
  	return null;
}

function createCookie(name,value,days)
{
 	 if (days) 
 	 {
   		 var date = new Date();
   		 date.setTime(date.getTime()+(days*24*60*60*1000));
    	 var expires = "; expires="+date.toGMTString();
 	 }
  	else expires = "";
  	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) 
{
if (document.cookie.length>0)
  {
	 
  start=document.cookie.indexOf(name + "=");
  if (start!=-1)
    { 
    start=start + name.length+1; 
    end=document.cookie.indexOf(";",start);
    if (end==-1) end=document.cookie.length;
    return unescape(document.cookie.substring(start,end));
    } 
  }
return null;
}

function changeStyles()
{ 
	//RUN THROUGH ALL LINK ELEMENTS TO FIND THE ACTIVE BACKGROUND
	var i, a, b, e, bgcounter, style, actStyle, nextStyle;
	bgcounter=0;
    for(i=0; (a = document.getElementsByTagName("link")[i]); i++) 
	{
     	if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) 
		{
			bgcounter++;
			if(a.disabled != true) 
			{
				actStyle = bgcounter;
			}
     	}
	}
	//SET THE NEXT BACKGROUND TO BE DISPLAYED
	if(actStyle == bgcounter) 
	{
		nextStyle = "Theme1" ;
	} else {
		actStyle++;
		nextStyle = "Theme" + actStyle;
	}
	//RUN THROUGH ALL LINK ELEMENTS AND ACTIVATE THE NEXT BACKGROUND IN THE SEQUENCE
	for(e=0; (a = document.getElementsByTagName("link")[e]); e++) 
	{
     	if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) 
		{			
			a.disabled = true;
       		if(a.getAttribute("title") == nextStyle) 
			a.disabled = false;
     	}
	}
}
//GET THE ACTIVE BODY BG
function getActiveBodyBg()
{
	var i;
	for(i=1;i<=6;i++)
	{
		if(document.getElementById("nav_button2").src == "http://www.coolapproach.com/images/body_bgs/tn_background_"+ i +".jpg")
		return i;
	} 
}
//CHANGE THE BACKGROUND IMAGE OF THE BODY TAG
function setActiveBodyBg(bg)
{
	document.body.style.background="url(/images/body_bgs/background_"+ bg +".jpg)";
	document.getElementById("nav_button2").src = "/images/body_bgs/tn_background_"+ bg +".jpg";
	document.getElementById("nav_button2").alt = 'Change Header Colour'
}

//FIND WHICH BG TO CHANGE TO
function changeBodyBg()
{
	var actBg = getActiveBodyBg();
	var  i, next;
	if(actBg !=  6)
	{
		next = actBg + 1;
		
	} else {
		next = 1;
	}
	setActiveBodyBg(next);
}

function resetStylesAndBgs()
{
	setActiveBodyBg("1")
	setActiveStyleSheet("Theme1")
}

function hideLoginError()
{
	document.getElementById("login_error_container").style.display = "none";
}
function hideGroupError()
{
	document.getElementById("group_error_container").style.display = "none";
}
