
function readCookie() {
	lesson 		= document.track.lesson.value;
	searchCookie 	= "name=LESSON_" + lesson + "#";
    	offset 		= document.cookie.indexOf(searchCookie);

	if (offset != -1) {
		return true;
	} else {
		document.cookie = searchCookie;
	}
}

function setCookie(endofSection) {
	var letters	= new Array("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o");
	
	lesson 		= document.cookie.substring(document.cookie.indexOf('_')+1, document.cookie.indexOf('#'));
	whichSections 	= document.cookie.substring(document.cookie.indexOf('_')+3, document.cookie.length);
	whichSections	= whichSections + endofSection + "#";
	
	setcookieValue 	= "name=LESSON_" + lesson + "#";
	
	// iterate through the letters array
	// append letter if section already completed
	for (i=0; i<letters.length; i++) {
		if (whichSections.indexOf(letters[i] + "#") != -1) { setcookieValue = setcookieValue + letters[i] + "#";}
	}
	
	// set the new cookie value
	setcookieValue	= setcookieValue;

	
	// set the new cookie variable
	document.cookie = setcookieValue;
	
	// uncomment if you want to see the cookie being set
	//alert("oldValue: " + document.cookie + "\nsetcookieValue: " + setcookieValue + "\ndocument.cookie: " + document.cookie);
}

function swapImage(coachItem,menuItem,imageLink) {
	document.images[menuItem].alt	= "This section has been completed.";
	document.images[menuItem].src	= "images/webimages/" + imageLink;
}  

function  showCertificate() {
	if (document.layers) {
		menu_certificate = "menu_certificate";
		document.layers[menu_certificate].visibility = "show";
	} else if (document.all) {
		document.all.menu_certificate.style.visibility = "visible";
	} else if (document.getElementById) {
		menu_certificate = document.getElementById('menu_certificate');
		menu_certificate.style.visibility = "visible";
	}
	document.cookie = "";
}

function controlPage() {
	a = 0;
	b = 0;
	c = 0;
	d = 0;
	e = 0;
	f = 0;
	g = 0;
	h = 0;
	i = 0;
	j = 0;
	k = 0;
	l = 0;
	m = 0;
	n = 0;
	o = 0;
	findCookie = readCookie();
	if (findCookie == true) {
		lesson = document.track.lesson.value;
		sections = document.track.sections.value;
		
		searchCookie = "lesson_" + lesson;
		whichSections = document.cookie.substring(document.cookie.indexOf('_')+3, document.cookie.length);
		if (whichSections.indexOf("a#") != -1) { swapImage('section','menu1','menu_done.gif'); a=1;}
		if (whichSections.indexOf("b#") != -1) { swapImage('section','menu2','menu_done.gif'); b=2;}
		if (whichSections.indexOf("c#") != -1) { swapImage('section','menu3','menu_done.gif'); c=3;}
		if (whichSections.indexOf("d#") != -1) { swapImage('section','menu4','menu_done.gif'); d=4;}
		if (whichSections.indexOf("e#") != -1) { swapImage('section','menu5','menu_done.gif'); e=5;}
		if (whichSections.indexOf("f#") != -1) { swapImage('section','menu6','menu_done.gif'); f=6;}
		if (whichSections.indexOf("g#") != -1) { swapImage('section','menu7','menu_done.gif'); g=7;}
		if (whichSections.indexOf("h#") != -1) { swapImage('section','menu8','menu_done.gif'); h=8;}
		if (whichSections.indexOf("i#") != -1) { swapImage('section','menu9','menu_done.gif'); i=9;}
		if (whichSections.indexOf("j#") != -1) { swapImage('section','menu10','menu_done.gif'); j=10;}
		if (whichSections.indexOf("k#") != -1) { swapImage('section','menu11','menu_done.gif'); k=11;}
		if (whichSections.indexOf("l#") != -1) { swapImage('section','menu12','menu_done.gif'); l=12;}
		if (whichSections.indexOf("m#") != -1) { swapImage('section','menu13','menu_done.gif'); m=13;}
		if (whichSections.indexOf("n#") != -1) { swapImage('section','menu14','menu_done.gif'); n=14;}
		if (whichSections.indexOf("o#") != -1) { swapImage('section','menu15','menu_done.gif'); o=15;}
		
		if(c == sections) { 
			showCertificate();
		} else if (d == sections) {
			showCertificate();	
		} else if (e == sections) {
			showCertificate();	
		} else if(f == sections) {
			showCertificate();		
		} else if(g == sections) {
			showCertificate();		
		} else if(h == sections) {
			showCertificate();		
		} else if(i == sections) {
			showCertificate();		
		} else if(j == sections) {
			showCertificate();		
		} else if(k == sections) {
			showCertificate();		
		} else if(l == sections) {
			showCertificate();		
		} else if(m == sections) {
			showCertificate();		
		} else if(n == sections) {
			showCertificate();		
		} else if(o == sections) {
			showCertificate();		
		}
	}
}
