/* SET COOKIE
====================================================================================================
==	DESCRIPTION
==		Creates a cookie on the client machine to allow us to store user preferences, e.g. preferred
==		font size, colour scheme, recent searches, etc.
==
==	PARAMETERS
==		cookieName		string
==		cookieValue 	string
==		cookieExpire	datetime
==
==	HISTORY
==		2006-10-31	v1.0	Initial version																							Richard Kingston
==
====================================================================================================
==	THIS IS A CLIENT SIDE SCRIPT!
================================================================================================= */
function SetCookie(cookieName, cookieValue, cookieExpire)
{
	var cookie = GetCookie(cookieName);
  if (cookie != cookieValue)
  {
  	// Set the cookie
  	document.cookie = cookieName + "=" + cookieValue + ((cookieExpire == null) ? "" : 
  		("; expires=" + cookieExpire.toGMTString())) + ";path=/;";
  }
}

/* GET COOKIE
====================================================================================================
==	DESCRIPTION
==		Reads a cookie on the client machine and returns the value associated with the given 
==		cookieName.
==
==	PARAMETERS
==		cookieName	string
==
==	HISTORY
==		2006-10-30	v1.0	Initial version																							Richard Kingston
==
====================================================================================================
==	THIS IS A CLIENT SIDE SCRIPT
================================================================================================= */
function GetCookie(cookieName)
{
	// Create a return value
	var cookie = null;
	var search = cookieName + '=';
	var startAt;
	var endAt;
  
  // If there are some cookies
  if (document.cookie.length > 0)
  { 
  	// Look for the cookie name we want
		startAt = document.cookie.indexOf(search);
		
		// If we find the cookie name we're looking for
		if (startAt != -1)
    { 
    	// Set the index of the beginning and end of it's value
    	startAt += search.length;
      endAt = document.cookie.indexOf(";", startAt);
      if (endAt == -1) { endAt = document.cookie.length; }
      
      var cookie_value = document.cookie.substring(startAt, endAt);
      if (!cookie_value) { cookie_value = ''; }
      cookie = cookie_value;
		}
	}
  
	// Return the cookie value
	return cookie;
}

/* DELETE COOKIE
====================================================================================================
==	DESCRIPTION
==		Deletes a cookie on the client machine.
==
==	PARAMETERS
==		cookieName	string
==
==	HISTORY
==		2008-12-22	v1.0	Initial version																							Richard Kingston
==
====================================================================================================
==	THIS IS A CLIENT SIDE SCRIPT
================================================================================================= */
function Delete_Cookie( name, path, domain ) {
	if ( GetCookie( name ) ) document.cookie = name + "=" +
	( ( path ) ? ";path=" + path : "") +
	( ( domain ) ? ";domain=" + domain : "" ) +
	";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

/* CHANGE STYLE
====================================================================================================
==	DESCRIPTION
==		Begins the process required to change a client side stylesheet reference. This usually results
==		in a stylesheet link being added or removed from the head tag.
==
==	PARAMETERS
==		styleId 		string
==		styleValue 	string
==		styleExpire	string
==
==	HISTORY
==		2006-11-14	v1.0	Initial version																							Richard Kingston
==
====================================================================================================
==	THIS IS A CLIENT SIDE SCRIPT
================================================================================================= */
function ChangeStyle(styleId, styleValue)
{
	// Set the lifetime of this preference to 1 year
	var currentDate = new Date();
	var styleExpire = new Date();
	styleExpire.setTime(currentDate.getTime() + 365 * 24 * 60 * 1000);
	
	// If no value has been passed the preference needs to be forgotten on the client
	if (!styleValue || styleValue == '') { styleExpire.setTime(currentDate.getTime() - 1); }
	
	// Set a cookie for this preference so we can remember it on the client
	SetCookie(styleId, styleValue, styleExpire);
	
	// Remove any existing style with this styleId
	RemoveStyle(styleId);
	
	// If a value was passed, load the changed style
	if (styleValue != '')
	{
		LoadStyle(styleId, styleValue);
	}
}

/* REMOVE STYLE
====================================================================================================
==	DESCRIPTION
==		Removes the stylesheet link with the given styleId from the head
==
==	PARAMETERS
==		styleId 		string
==
==	HISTORY
==		2006-11-14	v1.0	Initial version																							Richard Kingston
==
====================================================================================================
==	THIS IS A CLIENT SIDE SCRIPT
================================================================================================= */
function RemoveStyle(styleId)
{
	// Get a reference to the documents head tag
	var headRef = document.getElementsByTagName("head")[0];
	
	// Get a reference to the relevant stylesheet
	var styleRef = document.getElementById(styleId);
	
	// Remove the style reference from the head if it exists
	if (styleRef) { headRef.removeChild(styleRef); }
}


/* LOAD STYLE
====================================================================================================
==	DESCRIPTION
==		Adds a stylesheet reference to the head
==
==	PARAMETERS
==		styleId 		string
==		styleValue 	string
==
==	HISTORY
==		2006-11-14	v1.0	Initial version																							Richard Kingston
==
====================================================================================================
==	THIS IS A CLIENT SIDE SCRIPT
================================================================================================= */
function LoadStyle(styleId, styleValue)
{
	// If the styleValue hasn't been passed, get it from the cookie
	if (!styleValue || styleValue == '') { styleValue = GetCookie(styleId); }
	
	// If there is a valid value, load it
	if (styleValue && styleValue != '')
	{
		// Get a reference to the documents head tag
		var headRef = document.getElementsByTagName("head")[0];
		
		// Get a reference to the font stylesheet
		var styleRef = document.getElementById("defaultCSS");
			
		// If we've found the default stylesheet
		if (styleRef)
		{
			// Create a new styleRef
			var newRef = document.createElement('link');
			newRef.setAttribute('rel', 'stylesheet');
			newRef.setAttribute('type', 'text/css');
			newRef.setAttribute('media', 'screen');
			newRef.setAttribute('id', styleId);
			newRef.setAttribute('href', styleRef.href.replace('screen.css', styleValue + '.css'));
		
			// Duplicate the ref
			headRef.appendChild(newRef);
		}
	}
}

/* ROLL OVER HREF
====================================================================================================
==	DESCRIPTION
==		Changes the src of the object with the given targetObjectId to the given newSrc value. This 
==		is useful for changing an image src to that of a highlight image on rollover for example.
==
==	PARAMETERS
==		targetObjectId	string
==		newSrc					string
==
==	HISTORY
==		2006-11-14	v1.0	Initial version																							Richard Kingston
==
====================================================================================================
==	THIS IS A CLIENT SIDE SCRIPT
================================================================================================= */
function RollOverSrc(targetObjectId, newSrc)
{
	// Get a reference to the targetObject
	var targetObject = document.getElementById(targetObjectId);
	
	// Replace the targetObjects src with the new value
	targetObject.src = newSrc;
}

/* REDIRECT
====================================================================================================
==	DESCRIPTION
==		Redirects a client to the given url
==
==	PARAMETERS
==		redirUrl		string
==
==	HISTORY
==		2006-11-17	v1.0	Initial version																							Richard Kingston
==
====================================================================================================
==	THIS IS A CLIENT SIDE SCRIPT
================================================================================================= */
function Redirect(redirUrl)
{
	// Redirect instantly to the given url
	window.location.replace(redirUrl);
}


/* GOOGLEMAPS
====================================================================================================
==	DESCRIPTION
==		Sets up the Google Maps interface
==
==	PARAMETERS
==		None
==
==	HISTORY
==		2007-01-11	v1.0	Initial version																							Richard Kingston
==
====================================================================================================
==	THIS IS A CLIENT SIDE SCRIPT
================================================================================================= */
/*function LoadGoogleMap(latitude, longitude, zoom)
{
	if (GBrowserIsCompatible()) 
	{
		var map = new GMap2(document.getElementById("divGoogleMap"));
		var loc = new GLatLng(latitude, longitude);
		map.disableDragging();
		map.addControl(new GScaleControl());
		map.addControl(new GSmallZoomControl());
		map.addControl(new GMapTypeControl());
		map.setCenter(loc, zoom);
		map.addOverlay(new GMarker(loc, {clickable: false, title:'Barnsley Town Hall'}));
  }
}*/

function LoadGoogleMap(latitude, longitude, postcode, zoom)
{
	if (GBrowserIsCompatible())
	{
		var map = new GMap2(document.getElementById("divMap"));
		var geo = new new GClientGeocoder();
		var loc = (postcode) ? geo.getLatLng(postcode) : new GLatLng(latitude, longitude);
				
		map.disableDragging();
		map.addControl(new GScaleControl());
		map.addControl(new GSmallZoomControl());
		map.addControl(new GMapTypeControl());
		map.setCenter(loc, (zoom) ? zoom : 13);
		map.addOverlay(new GMarker(loc, {clickable: false, title: loc}));
	}
}
		


/* ------------------------ FUNCTIONS TO BE DISCONTINUED ---------------------------------------- */


function CreateStyleReference(id, name)
{
	// Get a reference to the documents head tag
	var headRef = document.getElementsByTagName("head")[0];
	
	// Get a reference to the font stylesheet
	var styleRef = document.getElementById("defaultCSS");
	
	// Create a new styleRef
	var newRef = document.createElement('link');
	newRef.setAttribute('rel', 'stylesheet');
	newRef.setAttribute('type', 'text/css');
	newRef.setAttribute('media', 'screen');
	newRef.setAttribute('href', styleRef.href.replace('screen.css', name + '.css'));
	
	// Duplicate the ref
	headRef.appendChild(newRef);
}


function SetColourScheme()
{
	// Get the users preferred colour scheme from their cookie
	var colourScheme = GetCookie('prefColourScheme');
	
	// If they have a preferred font size
	if (colourScheme)
	{
		CreateStyleReference('cssColourScheme', colourScheme);
	}
}


function SetFontScheme()
{
	// Get the users preferred font size from their cookie
	var fontSize = GetCookie('prefFontSize');
	
	// If they have a preferred font size
	if (fontSize)
	{
		CreateStyleReference('cssFontSize', fontSize);
	}
}


