function setCookie(cookieName,cookieValue,nDays) {
 var today = new Date();
 var expire = new Date();
 if (nDays==null || nDays==0) nDays=365;
 expire.setTime(today.getTime() + 3600000*24*nDays);
 
 document.cookie = cookieName + "=" + escape(cookieValue)
                 + ";expires=" + expire.toGMTString();
}


// name - name of the desired cookie
// * return string containing value of specified cookie or null if cookie does not exist
function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}

// returns True or False rather then value
function getCookie_value(name){

 var cookies = document.cookie;
	if (cookies.indexOf(name) != -1){
	var startpos = cookies.indexOf(name)+name.length+1;
	var endpos = cookies.indexOf(";",startpos);
	if (endpos == -1) endpos = cookies.length;
		return unescape(cookies.substring(startpos,endpos));
	}else{
	return false; // the cookie couldn't be found! it was never set before, or it expired.
	}
}

function delete_cookie ( cookie_name )
{
  var cookie_date = new Date ( );  // current date & time
  cookie_date.setTime ( cookie_date.getTime() - 1 );
  document.cookie = cookie_name += "=; expires=" + cookie_date.toGMTString();
}

//need another function for splashpage, coz' it needs to expire at end of session
function setSplashCookie(cookieName,cookieValue,nDays) 
{

 var today = new Date();
 var expire = new Date();
 if (nDays==null || nDays==0) nDays=1;
 
 expire.setTime(today.getTime() + 3600000*24*nDays);
 
 //removed the expiry time on the cookie so that its expired everytime a browser window is closed.
 document.cookie = cookieName+"="+escape(cookieValue)
                 + ";path=/"
}


function setBgColour()
{
	if ((getCookie("siteSkin") != null) && (getCookie("siteSkin") != ''))
	{	
		document.body.className = getCookie("siteSkin");
	}else{
		document.body.className = "skinColor1";
	}
}
		
function changeSkin()
{
	//check if the user has siteskin cookie set
	if ((getCookie("siteSkin") != null) && (getCookie("siteSkin") != ''))
	{
		//if yes read the cookie value 
		var sClassName;
		sClassName = getCookie("siteSkin");
		
		//then change background color to the next colour 
		sClassName = parseInt(sClassName.substring(9));
		//once all the 5 options are done, start for the beginning
		if (sClassName >= 5)
		{
			sClassName = 0;
		}
		document.body.className = "skinColor" + (sClassName + 1);
		
		//set the cookie value to bgcolor
		var cookieValue;
		cookieValue = document.body.className;
		setCookie ("siteSkin", cookieValue, 365);
	}
	else
	{http://dev.manutd.twii.uk2/flash/masthead1.swf
		//first time user, set new cookie
		var cookieValue;
		cookieValue = document.body.className;
		setCookie ("siteSkin", cookieValue, 365);
	} 
	
	
	
	//Now Call Function in Flash
	
	//Get a pointer to the Flash object in the page using it's id attribute
	
	if(navigator.appName.indexOf("Microsoft") != -1)
	{
		flash = window.flash_masthead;
	}
	else
	{
		flash = window.document.flash_masthead;
	}
	flash.setSkin(cookieValue);
} 


function getSkin()
{
	var skinValue = document.body.className;
	//check if the user has siteskin cookie set
	if ((getCookie("siteSkin") != null) && (getCookie("siteSkin") != ''))
	{
		skinValue = getCookie("siteSkin");
	}
	return skinValue;
}


