/*------------------setFont--------------------------
Anytime the font or size is changed, this function needs to be called to change
styles on the page and store it to the cookie.  If a null is passed into one of
the values, the needed value will be taken from the cookie.  If it isn't in the
cookie, then it is set to the default of "Verdana" for the font family and 12 for
the size.  This function should not be called directly from the html.  Please use
changeFont and changeSize instead.
font - The new font family.  Pass in null to use the stored cookie value.
size - The new font size.  Pass in null to use the stored cookie value.
---------------------------------------------------*/
//Size of the font
var fontSize = null;
var fontFamily = null;
var font = null;
function setFont(size)
{
	var cookie = fontCookie();
	if(size == null && cookie != "none") size = parseInt(cookie.substring(cookie.indexOf("size:")+5,cookie.length));
	else if(size == null && cookie == "none") size = 14;
	
	fontSize = size;

	fontCookie("font:" + font + ",size:" + size);
	document.getElementById("contentblock").style.fontSize = (size + "pt");
}
//------------------End setFont----------------------



/*------------------changeSize-----------------------
Changes the size of the font and stroes it in a cookie to be used on every page.
direction - either "increase" to increase font size by one or "decrease" to
	decrease font size by one.
---------------------------------------------------*/
function changeSize(direction){
	if (direction == "increase") fontSize += 1;
	else 
	{
		if (fontSize > 8) fontSize -= 1;
		else fontSize = 8;
	}
	setFont(fontSize);
}
/*------------------changeSize-----------------------



/*------------------fontCookie-----------------------
Stores a cookie with the FONT name.
sel - The storing to be stored in the cookie
---------------------------------------------------*/
function fontCookie(sel)
{
	var p;
	var l = new String(location);
	if(l.indexOf("/") != -1) p= "/";
	else p = "";
	var FONT = new Cookie(document, "FONT", 8760, p, ".byu.edu");
	oldCookie = FONT.load();
	
	if(!oldCookie){
			if (sel != null) FONT.font = sel;	 // stores the drive 
			else return "none";
			FONT.store();
			return FONT.font;
	}
	else {
		if (sel != null) {
			FONT.font = sel;
			FONT.store();
		}
		return FONT.font;
	}
}
//------------------End FontCookie-------------------


/*------------------fontDel--------------------------
Deletes the FONT value in the cookie.
---------------------------------------------------*/
function fontDel()
{
	var FONT = new Cookie(document, "FONT", 8760, "/", ".byu.edu");
	FONT.remove();
} 
//------------------End fontDel----------------------



/*------------------Cookie Code-----------------------
The cookie object greatly eases the use of cookies on our pages
It starts out with no value.  It has several properties, most of which
are useless, but I'll leave there just in case.  

--------------------Section 1-------------------------
 
document = usually the current document object or literally "document" - without the quotes
name		= the name of the cookie

The following are properties of all cookies that are used by the browser.
Each property shares the same name as the parameter, except "days" where the real property name is "expiration"
 
days		= the days until the cookie expires
path		= the directories on this server where the cookie is available, in addition to this one.
domain	= other servers where the cookie is available
secure 	= boolean, if the cookie should only be sent encrypted	 
		
--------------------Section 2-------------------------

When one wants to store a value or values to a cookie, they have but to use the following convention:

	var myCookie = new Cookie(document, "thisCookie", 365);
	myCookie.value1 = "The stuff I want to put in";
	myCookie.value2 = "The stuff I want to put in";
	myCookie.store();

********** "value1", and "value2" don't need to be called that.  They could be "crazy_monkey_value" or "sinisterWaterMan1" whatever

This will store the cookie under the following format:
   
	var allCookies = document.cookie;
	alert(unescape(allCookies));	   // unescape replaces the %20's with spaces etc.
	 

Which would alert:
	"nameOfFirstCookie=value1:The stuff I want to put in&value2:The stuff I want to put in; 	  ...
	... expiration=Fri, 02-Jan-2002 00:00:00 GMT; path=/mypath; domain=http://www.mydomain.com; secure; ...
	... nameOfSecondCookie=value1:The stuff I want to put in ... etc. "
---------------------------------------------------*/

function Cookie(document, name, days, path, domain, secure)
{
	// These first are properties of Cookie.  It is essential that they use the convention _[property] because
	// by this they are distinguished from the values of the cookie	when in is stored.
	this._document = document;
	this._name = name;	  
	
	if (days) {
		this._expiration = new Date((new Date()).getTime() + days*24*60*60*1000);
	}
	else this._expiration = null;
	
	if (path) this._path = path; 		else this._path = null;
	if (domain) this._domain = domain; 	else this._domain = null;
	if (secure) this._secure = true; 	else this._secure = false;	 
	
	//The following are functions of the Object Cookie.  They are associated here, and defined right after this Object Constructor.
	this.store = _Cookie_store;	 
	this.load = _Cookie_load;
	this.remove = _Cookie_remove;

} // end Cookie
 
 
/*-----------function _Cookie_store, _Cookie_load, and _Cookie_remove-------------
These are functions of Cookie.  They are associated in the Cookie constructor, above.   
--------------------------------------------------------------------------------*/
function _Cookie_store()
{
	var cookievalue = "";
	
	// The following syntax "for ( var myProperty in myObject )".  puts the name of each property of Cookie
	// into "property" sequentially.  Then, the script tests to see if the property is either a function or 
	// a general property of a Cookie, like expiration, path, etc. (It does this by testing for the "_" as the 
	// first character, which is the convention) If it isn't, then it is a value and is stored into cookievalue 
	// following the convention described above.  
	
	for(var property in this) {	
		if ((property.charAt(0) == '_') || ((typeof this[property]) == 'function')) {continue;}	// typeof this[property] = what is the type of the current property?
		if (cookievalue != "") cookievalue += '&';
		cookievalue += property + ':' + escape(this[property]);  // escape encodes the spaces, ; " ', and other reserved characters, use unescape() to reverse.
	}
	
	// Now we are writing the actual cookie string.  We only need store one at a time, and will follow the convention described above
	// for the first cookie.  
	
	var cookie = this._name + '=' + cookievalue;  // myName=value1:my text output&value2=:my text output, etc.  
	if (this._expiration)	 
		cookie += '; expires=' + this._expiration.toGMTString();  // add "; expires=[the date]   toGMTString() is required.  
	if (this._path) cookie += '; path=' + this._path;		 //etc.
	if (this._domain) cookie += '; domain=' + this._domain;
	if (this._secure) cookie += '; secure';
	this._document.cookie = cookie;		//this actually stores the cookie into the _document defined in the Cookie constructor, usually document.
}
function _Cookie_load()
{
	var allcookies = this._document.cookie;	 // this string is exactly like the input described in Section 2, above. 
	if (allcookies == "") return false;		// if there are no cookies, then this isn't going to work
	var start = allcookies.indexOf(this._name + '=');  // getting the index in the string of the cookie we are referring to.  _name referres to the _name of the Cookie object's load method we called
		if (start == -1) return false;   // if it doesn't exist, we're gone.
		start += this._name.length + 1;
	var end = allcookies.indexOf(';', start);	// this feeds in information to the end of the values, right before we start into the general cookie properties.
		if (end == -1) end = allcookies.length;
	var cookieval = allcookies.substring(start, end);
	
	// The following section takes each "myValueName:what i typed", and splits it off into currentValue, then stores the data into "a".  
	// It then chops that section of leftString, and continues until it reads them all..
	
	var a = new Array();
	leftString = cookieval;
	currentValue = "";
	if (typeof leftString == "string") { 
		index = leftString.indexOf('&');   // refer to convention described in Section 2, above.
		while (index != -1) {
			currentValue = leftString.substring(0,index);
			a[a.length] = currentValue;
			leftString = leftString.substring(index+1,leftString.length);
			index = leftString.indexOf('&');

		}
		a[a.length] = leftString;
	}// end if
	
	// This section takes all of the "myValueName:what I typed" from the "a" array and stores them back in "a".
	// Each entry in "a" then becomes an array with a length of 2 a[i][0] = myValueName and a[i][1] = what I typed
	  
	for(var i=0; i < a.length; i++) {
		tmp = new Array();
		temp1 = a[i];
		temp2 = "";
		if (typeof temp1 == "string") { 
			index = temp1.indexOf(':');
			while (index != -1) {
				temp2 = temp1.substring(0,index);  // splits it along the : and takes the left side
				tmp[tmp.length] = temp2;
				temp1 = temp1.substring(index+1,temp1.length);			// takes the right side.
				index = temp1.indexOf(':');
			}
		tmp[tmp.length] = temp1;	// stores the right side in the tmp array
		}
		
		a[i] = tmp;		// stores the tmp array in an entry of a
	}
	
	// these lines do this:
	//
	// for each entry in "a"
	//		make a property (named whatever is stored in a[i][0]) of the current Cookie object and in it store whatever is in a[i][1]
	 
	for( var i=0; i < a.length; i++) {
		this[a[i][0]] = unescape(a[i][1]);
	}
	return true;
}

function _Cookie_remove()
{
	var cookie;
	cookie = this._name + '=';
	if (this._path) cookie += '; path=' + this._path;
	if (this._domain) cookie += '; domain=' + this._domain;
	cookie += '; expires=Fri, 02-Jan-1970 00:00:00 GMT';// this is the important line, it is set to expire in 1970, and the browser will delete it.
	this._document.cookie = cookie;
}