function deleteOldCookies() {
	old_cookie_count = getCookie('bm_count');
	if (old_cookie_count != null)
	{
		for (i = 0; i < old_cookie_count; ++i)
		{
			setCookie('bm_n_' + i, '', -1);
			setCookie('bm_i_' + i, '', -1);
			setCookie('bm_name_' + i, '', -1);
			setCookie('bm_index_' + i, '', -1);
		}
		setCookie('bm_count', -1, -1);
	}
}

function getCookie(cookie_name)
{
	if (document.cookie.length > 0)
	{
		cookie_start = document.cookie.indexOf(cookie_name + "=");
		if (cookie_start != -1)
		{ 
			cookie_start	= cookie_start + cookie_name.length + 1;
			cookie_end		= document.cookie.indexOf(";", cookie_start);
			if (cookie_end == -1)
			{
				cookie_end	= document.cookie.length;
			}
			return unescape(document.cookie.substring(cookie_start, cookie_end));
		}
	}
	return null
}

function setCookie(cookie_name,value,expiredays)
{
	var exdate = new Date();
	exdate.setDate(exdate.getDate() + expiredays);
//	document.cookie = cookie_name + "=" + escape(value) +";expires=-1; path=/";  // these 2 are to make sure the only cookies are the ones for the master domain
//	document.cookie = cookie_name + "=" + escape(value) +";expires=-1";
	document.cookie = cookie_name + "=" + escape(value) +
						((expiredays==null) ? "" : ";expires=" + exdate)+"; path=/; domain=.sfu.ca";
}

function getCustomBookmarks()
{
	bookmarknames = new Array();
	bookmarkitems = new Array();
	bm_size = getCookie('bm_size');
	if (bm_size == null)
	{
		setCustomBookmarks();
		bm_size = 0;
	}
	for(i = 0; i < bm_size; ++i)
	{
		bookmarknames[i] = getCookie('bmn_' + (i + 1));
		bookmarkitems[i] = getCookie('bmi_' + (i + 1));
	}
	return new Array(bm_size, bookmarknames, bookmarkitems);
}

function updateHardBookmarks(myform)
{
	expiredays = 365 * 10;
	defbms = defaultBookmarkList();
	bitstring = '';
	for(i = 0; i < defbms.length/2; ++i)
	{
		if (defbms[(i * 2) + 1] != '#')
		{
			eval('myvalue = myform.defbm_' + i + '.checked;');
			if (myvalue == 1)
			{
				bitstring = bitstring + '1';
			}
			else
			{
				bitstring = bitstring + '0';
			}
		}
		else
		{
			bitstring = bitstring + '0';
		}
	}
	setCookie('bm_list', bitstring, expiredays);
}

function getBookmarks()
{
	deleteOldCookies();
	results = getCustomBookmarks();
	bm_list = getCookie('bm_list');
	if (bm_list == null)
	{
		setDefaultBookmarks();
		bm_list = getCookie('bm_list');
	}
	last_header = '';
	defaultbookmarks = defaultBookmarkList();
	for (j = 0; j < defaultbookmarks.length; j += 2)
	{
		if (defaultbookmarks[j + 1] == '#')
		{
			last_header = defaultbookmarks[j];
		}
		else if (bm_list.substring(j/2,(j/2)+1) == '1')
		{
			if (last_header != '')
			{
				results[1][results[0]] = last_header;
				results[2][results[0]] = '#';
				results[0]++;
				last_header = '';
			}
			results[1][results[0]] = defaultbookmarks[j];
			results[2][results[0]] = defaultbookmarks[j+1];
			results[0]++;
		}
	}
	return results;
}

function defaultBookmarkList()
{
	defaultbookmarks = new Array(
		'--Faculties--',						'#',
		'Faculty of Applied Sciences',				'http://fas.sfu.ca/',
		'Faculty of Arts & Social Sciences',		'http://www.sfu.ca/arts/',
		'Faculty of Business Administration',		'http://www.sfu.ca/sfubusiness/',
		'Faculty of Education',						'http://www.educ.sfu.ca/',
		'Faculty of Health Sciences',				'http://www.fhs.sfu.ca/',
		'Faculty of Science',						'http://www.sfu.ca/science/',
		'--Schools--',							'#',
		'Communication',							'http://www.sfu.ca/communication/',
		'Computing Science',						'http://www.cs.sfu.ca/',
		'Contemporary Arts',						'http://www.sfu.ca/sca/',
		'Criminology',								'http://www.sfu.ca/criminology/',
		'Engineering Science',						'http://www.ensc.sfu.ca/',
		'International Studies',					'http://www.sfu.ca/internationalstudies/',
		'Interactive Arts & Technology',			'http://cgi.sfu.ca/~surreywb/cgi-bin/students/index.php?q=p_students/learn/programs&p=iat_grad',
		'Resource and Environmental Management',	'http://www.rem.sfu.ca/',
		'Segal Graduate School of Business',		'http://www.sfubusiness.ca/programs/segal/',
		'--Miscellaneous--',					'#',
		'Bookstore',								'http://www.sfu.ca/bookstore/',
		'Microcomputer Store',						'http://www.microstore.sfu.ca/sfu/index_en.jsp',
		'Academic Computing Services',				'http://www.sfu.ca/acs/',
		'Human Resources',							'http://www.sfu.ca/human-resources/',
		'Corporate Time login',						'https://webcal.sfu.ca/',
		'--Events--',							'#',
		'Academic Calendar',						'http://students.sfu.ca/calendar/',
		'Academic Calendar of Events',				'http://students.sfu.ca/importantdates/',
		'Convocation',								'http://www.sfu.ca/convocation/',
		'Campus Events',							'http://www.sfu.ca/mediapr/events.html',
		'Athletics Events',							'http://cgi.sfu.ca/~athlwww/index.php',
		'--Getting to SFU--',					'#',
		'Maps & Directions',						'http://www.sfu.ca/about/maps.html',
		'U-Pass',									'http://students.sfu.ca/upass/',
		'Parking',									'http://www.sfu.ca/security/Parking/',
		'Security',									'http://www.sfu.ca/security/',
		'--Directories--',						'#',
		'Phone Book',								'http://www.sfu.ca/phonebook/',
		'Email Directory',							'http://www.sfu.ca/sfuds/',
		'--WebCT--',							'#',
		'WebCT Login',								'http://webct.sfu.ca/',
		'WebCT Help for Instructions',				'http://wiki.sfu.ca/webct',
		'WebCT course request form',				'http://webctold.sfu.ca/webct/sfu/courserequest.pl',
		'--Road Conditions and Parking--',		'#',
		'Burnaby Road Conditions',					'http://www.sfu.ca/security/sfuroadconditions/',
		'Parking Permits',							'http://www.sfu.ca/security/Parking/'
	);
	return defaultbookmarks;
}

function setCustomBookmarks()
{
	expiredays = 365 * 10;
	setCookie('bm_size', 0, expiredays);
}

function setDefaultBookmarks()
{
	expiredays = 365 * 10;
	defbms = defaultBookmarkList();
	var liststring = '';
	for (i = defbms.length / 2; i > 0; --i)
	{
		liststring = liststring + '1';
	}
	setCookie('bm_list', liststring, expiredays);
}

function addBookmark(bm_name, bm_url)
{
	expiredays = 365 * 10;
	bm_size = getCookie('bm_size');
	bm_size++;
	
	if ((bm_url.substring(0, 7) != 'http://') && (bm_url.substring(0, 8) != 'https://'))
	{
		bm_url = 'http://' + bm_url;
	}
	
	setCookie('bmn_' + bm_size, bm_name, expiredays);
	setCookie('bmi_' + bm_size, bm_url, expiredays);
	setCookie('bm_size', bm_size, expiredays);
	
	refreshBookmarks();
}

function refreshBookmarks()
{
	location.href = document.URL;
}

function launchBookmark()
{
	bm_form = MM_findObj( 'bookmarks' );
	selectbox = bm_form.bm_urls;
	bookmark = selectbox.options[selectbox.selectedIndex].value;
	location.href = bookmark;
}

function removeBookmarks()
{
	expiredays = 365 * 10;
	bm_form = document.forms['bm_management'];
	bm_size_final = bm_size = getCookie('bm_size');
	cur_pointer = 1;
	next_pointer = 1;
	while (next_pointer <= bm_size)
	{
		removing = false;
//		eval('removing = bm_form.bm_delete_' + next_pointer + '.checked');
		removing = bm_form['bm_delete_' + next_pointer ].checked;
		if (removing)
		{
			next_pointer++;
			bm_size_final--;
			continue;
		}
		setCookie('bmn_' + cur_pointer, getCookie('bmn_' + next_pointer), expiredays);
		setCookie('bmi_' + cur_pointer, getCookie('bmi_' + next_pointer), expiredays);
		cur_pointer++;
		next_pointer++;
	}
	setCookie('bm_size', bm_size_final);
	
	//clear out old cookies
	while (cur_pointer < next_pointer)
	{
		setCookie('bmn_' + cur_pointer, '', -1);
		setCookie('bmi_' + cur_pointer, '', -1);
		cur_pointer++;
	}
	
	refreshBookmarks();
}

function launchBookmark()
{
	bm_form = MM_findObj( 'bookmarks' );
	selectbox = bm_form.bm_urls;
	bookmark = selectbox.options[selectbox.selectedIndex].value;
	if (bookmark)
	{
		location.href = bookmark;
	}
}

