var MASTERY_CHALLENGE_LESSON = 6;
var AUDIO_PATH = "audio";

var ex_should_check = 0;
var ex_finished = 0;
var ex_isflashquiz = 0;
var ex_alert = "You must choose an answer before proceeding.?";
var post_load_functions = new Array();

mouse_x = 0;
mouse_y = 0;
try_to_hide = 1;

//this function is for flash
function setCookie(cookie_name, cookie_value) {
    document.cookie = cookie_name + "=" + escape(cookie_value) + "; expires=Thu, 01-Jan-2034 00:00:00 GMT"
}

//this function is used to determine if flash exercise has been completed.
function readCookie(cookie_name) {
    c = unescape(document.cookie);
    broken_cookie_temp = c.split(";");

    for (i = 0; i < broken_cookie_temp.length; i++) {
        broken_cookie = broken_cookie_temp[i].split("=");
        if (broken_cookie[0].trim() == cookie_name) return broken_cookie[1];
    }
 
    return 0;
}
function capture_mouse_position()
{
	mouse_x = window.event.x;
	mouse_y = window.event.y;
	window.status = "("+mouse_x+","+mouse_y+")";
}

function show_topics_for_lesson(lesson_number)
{
	toggle_menu_image(lesson_number, "on");

	_hide_all_topics_except(lesson_number);
	try_to_hide = 0;
	if (document.getElementById) {
		if (topic_div = _get_topic_div(lesson_number)) {
			if (topic_div.style.visibility == "hidden" || topic_div.style.visibility == "") {
					 				 
				topic_div.style.visibility = "visible";

			}
		}
	}
}

function toggle_menu_image(lesson_number, way)
{
	suffix = (way == "on") ? "-over.gif" : ".gif";

	menu_image_name = "topic" + lesson_number;
	if (lesson_number == 0) menu_image_name = "intro";
	if (lesson_number == MASTERY_CHALLENGE_LESSON) menu_image_name = "mc";

	if (document[menu_image_name]) {
		document[menu_image_name].src = "images/" + menu_image_name + suffix;
	}
}

function hide_topics_for_lesson(lesson_number)
{
	try_to_hide = 1;
	setTimeout("_hide_topics(" + lesson_number + ")", 300);
}

function _hide_topics(lesson_number)
{
	if (try_to_hide == 0) {
		return;
	}
	if (document.getElementById) {
		if (topic_div = _get_topic_div(lesson_number)) {
			if (topic_div.style.visibility == "visible") {
				topic_div.style.visibility = "hidden";

				toggle_menu_image(lesson_number, "off");
			}
		}
	}
}

function _get_topic_div(lesson_number)
{
	id = "lesson_" + lesson_number + "_topics"
	return document.getElementById(id);
}

function _hide_all_topics_except(lesson_number)
{
	var reg1 = /lesson_([0-9])+_topics/;
	var reg2 = /lesson_([10-19])+_topics/;

	for (i = 0; i < document.all.length; i++) {
		if ( reg1.test(document.all[i].id) ) {
			if (RegExp.$1 == lesson_number) continue;
			_hide_topics(RegExp.$1);
			if (lesson_number == 10 || lesson_number == 9)
			_hide_topics(10);
		}
	}
}

function check_for_completion()
{
	ex_should_check = 1;
}

function exercise_completed()
{
	ex_finished = 1;
}

function is_completed()
{   
	// modified to check for skipping lesson
	if (ex_should_check && !ex_finished) {
		if (confirm(ex_alert)) {
	      document.forms[0].action.value = "submitcancel1";
	      return true;  		
		} else {
		  return false;
		}
	}
	return true;
}

function PostLoad()
{
	if (post_load_functions && post_load_functions.length > 0) {
		for (i = 0; i < post_load_functions.length; i++) {
			eval(post_load_functions[i]);
		}
	}
}

function add_post_load_function(code)
{
  eval(code);
	post_load_functions.push(code);
}

//
// for getting the variable out of the query string
//
function getQueryVariable(variable) {
  var q_str = window.location.search.substring(1);
  var vars = q_str.split("&");
  for (var i = 0; i < vars.length; i++) {
    var pair = vars[i].split("=");
    if (pair[0] == variable) {
      return pair[1];
    }
  } 
}

function changeTrack(track)
{
 		
 //   document.all.sound.src = AUDIO_PATH + "/" + track;
 
var pNewInner = "<OBJECT ID='Player' height='0' CLASSID='CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6'><param name='autostart' value='true'><param id='URL' name='URL' value='"+AUDIO_PATH + "/" + track+"'></OBJECT>";
document.getElementById("objPlayer").innerHTML = pNewInner;
}



//////////////////

var viewed = new Array;
var current;

function change_row_color(id, way)
{
	new_class = (way == "over") ? "highlighted" : (current == id) ? "current" : (viewed[id] == 1) ? "viewed" : "default";
	
	element = document.getElementById(id);

	for (var i = 0; i < element.childNodes.length; i++) {
		var td = element.childNodes.item(i);
		change_class(td, new_class);
	}
}

function change_cell_color(id, way)
{
	new_class = (way == "over") ? "highlighted" : (current == id) ? "current" : (viewed[id] == 1) ? "viewed" : "default";
				
	element = document.getElementById(id);
	change_class(element, new_class);
}

function update_info(id)
{
	var td;

	log_click(id);

	temp = document.getElementById(id + "_info");

	clone = temp.cloneNode(1);

	td = document.getElementById('info');

	remove_all_children(td);

	try
	{
	 	 if (!td.hasChildNodes()) {
				new_node = document.createElement("temp");
				td.appendChild(new_node);
	 	 }
  
	 	 td.replaceChild(clone, td.firstChild);
	 	 td.firstChild.style.display = 'block';
	 	 return true;
	 }
	 catch (e) {
	 	 return false;
	 }	 
	
}

function remove_all_children(node)
{

try
{

  if (isObject(node.childNodes)) {
		while (node.childNodes.length > 0)
					node.removeChild(node.firstChild);
	}					
}
catch(e)
{
	return false;
}					
	
}

function isObject(objectID){

try 
{
var test = eval(objectID);
}
catch(e) 
{

return false;
}
if(typeof(test)=='object') 
   return true;
else
   return false;
}

function log_click(id)
{
	viewed[id] = 1;
	
	set_current(id);
}

function set_current(id)
{
	current = id;
	var count = 1;

	if (id.substr(0,3) == "row") {
		while (element = document.getElementById("row_" + count++)) {
			change_row_color(element.id, 'out');
		}
	}

	count = 1;

	if (id.substr(0,4) == "cell") {
		while (element = document.getElementById("cell_" + count++)) {
			change_cell_color(element.id, 'out');
		}
	}
}

function change_class(node, new_class)
{
	for (var i = 0; i < node.attributes.length; i++) {
		if (node.attributes.item(i).nodeName == "class") {
			node.attributes.item(i).nodeValue = new_class;
		}
	}
}



function popWindow(url, winwidth, winheight) {
var winpos = "left=" + ((screen.width - winwidth) / 2) + ",top=" + ((screen.height - winheight) / 2);
	topFrame = window.open(url, "newwindow", "scrollbars=yes,resizable=yes,toolbar=no,menubar=no,status=no,width=" + winwidth + ",height=" + winheight + "," + winpos);
	topFrame.focus();
}
//
function popWindow(url, winwidth, winheight, winname) {
var winpos = "left=" + ((screen.width - winwidth) / 2) + ",top=" + ((screen.height - winheight) / 2);
	topFrame = window.open(url, winname, "scrollbars=yes,resizable=yes,toolbar=no,menubar=no,status=no,width=" + winwidth + ",height=" + winheight + "," + winpos);
	topFrame.focus();
	topFrame = null;
}
//
function popWindowTool(url, winwidth, winheight) {
var winpos = "left=" + ((screen.width - winwidth) / 2) + ",top=" + ((screen.height - winheight) / 2);
	topFrame = window.open(url, "newwindowt", "scrollbars=yes,resizable=yes,toolbar=yes,menubar=no,status=no,width=" + winwidth + ",height=" + winheight + "," + winpos);
	topFrame.focus();
}
//
function popWindowMenu(url, winwidth, winheight) {
var winpos = "left=" + ((screen.width - winwidth) / 2) + ",top=" + ((screen.height - winheight) / 2);
	topFrame = window.open(url, "newwindowm", "scrollbars=yes,resizable=yes,toolbar=yes,menubar=yes,status=no,width=" + winwidth + ",height=" + winheight + "," + winpos);
	topFrame.focus();
}
function popWindowScroll(url, xpos, ypos) {
var winpos = "left=" + ((screen.width - xpos) / 2) + ",top=" + ((screen.height - ypos) / 2);
	topFrame = window.open(url, "newwindow", "scrollbars=yes,resizable=yes,toolbar=no,menubar=no,status=no,width=" + xpos + ",height=" + ypos + "," + winpos);
	topFrame.focus();
	return topFrame;
}
function closeWindow(url) {
	topFrame.close();
	location.href = url
}

//Load flash object into embedded div tag
function loadFlashObject(name, divwidth, divheight) {

document.getElementById("frame1").style.height = divheight;
document.getElementById("frame1").style.width = divwidth;
document.getElementById("frame1").src=name;



}


//Dynamically Load Flash Object To Overcome Microsoft Issue
function createFlash(filename, width, height) {

    sFlash = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" width="'+width+'" height="'+height+'">';
    sFlash += '<param name="movie" value="'+filename+'">';
    sFlash += '<param name="quality" value="high">';
    sFlash += '<param NAME=wmode VALUE=opaque>'
    sFlash += '<embed src="'+filename+'" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="'+width+'" height="'+height+'"></embed></object>';
 
	document.write(sFlash);    

}

function lms_course_exit(page,lesson,maxpage) {
	//var sURL = document.location.href.split(".");
	var sNewURL = "http://www.surececourses.com/table_contents.asp?lesson="+lesson+"&page="+page+"&max="+maxpage;

	if (confirm("If you exit now you will have to start this section over to receive credit.")) 
	    window.open(sNewURL,"_self");
}