function initmain() {
init('');
initButtons();
var book="";
if ((typeof BCS2P=='object')&&(BCS2P.length>1)) {book=BCS2P.book;}
if (book.indexOf("_alg1_")>0) WTITLE=WTITLE1;
else if (book.indexOf("_alg2_")>0) WTITLE=WTITLE2;
else if (book.indexOf("_geom_")>0) WTITLE=WTITLE3;
mainmenu();
}
function mainmenu() {
makeButtons('mainmenu');showButtons();
top.document.title.innerHTML=WTITLE+" - Main Menu"; 
var start_mainmenu=
'<table class="menu" align="center"><tr>\n'+
'<td><img src="../images/labels/mainmenu.gif" border="0"></td>\n'+
'</tr><tr><td class="instructions">Click on one of the chapters\n'+
' below to access the lessons.</td>\n'+
'</tr><tr><td height="20">&nbsp;</td>\n'+
'</tr><tr><td><table border="0" cellspacing="4">\n';
var tmpl_mainmenu=
'<tr><td class="caption">Chapter CHN</td>'+
'<td nowrap="nowrap"><a class="menu" '+
' href="javascript:void(parent.chaptermenu(CHN))" onClick="window.status=\'\';"'+
' onMouseOver="window.status=\'GO TO CHAPTER CHN\';return true;" '+
' onMouseOut="window.status=\'\';return true;">&nbsp;&nbsp;CHT</a></td></tr>';
var end_mainmenu='</table></td></tr></table>\n';
/**** With Link
	var end_mainmenu='</table></td>\n'+
	'</tr><tr><td height="20">&nbsp;</td>\n'+
	' href="../../eTutorial/media/mainmenu.htm"\n'+
	' target="_top">Go to the eTutorial</a></td>\n'+
	'</tr></table>\n';
****/
var htmltext=start_mainmenu;
for(var i=1;i<CHAPTERTITLES.length;i++) 
htmltext+=tmpl_mainmenu.replace(/CHN/g,i).replace(/CHT/,CHAPTERTITLES[i]);
htmltext+=end_mainmenu;
startWriteMain(true);
writeMain(htmltext);
endWriteMain(true);
}
function chaptermenu(newChap) {
makeButtons('chapmenu');showButtons();
if (arguments.length==1) CURR_CHAP=newChap;
top.document.title.innerHTML=WTITLE+" - Chapter "+CURR_CHAP+" Menu";
var start_chaptermenu=
'<table class="menu" align="center" border="0"><tr><td>\n'+
'<table border="0"><tr>\n'+
'<td valign="middle" width="60">\n'+
'<table height="60" width="60"><tr><td class="shelltracking">'+
'<div class="shelltracking">'+CURR_CHAP+'</div></td></tr></table>\n'+
'</td><td valign="middle">\n'+
'<h3><span class="header">Chapter '+CURR_CHAP+' - Menu</span>\n'+
'<br>'+CHAPTERTITLES[CURR_CHAP]+'</h3></td></table></td>\n';
start_chaptermenu+=  
'</tr><tr><td class="instructions"><br>Click on one of the lessons below\n'+
'  to access the lesson worksheet.</td>\n'+
'</tr><tr><td height="20">&nbsp;</td>\n'+
'</tr><tr><td><table border="0" cellspacing="4">\n';
var tmpl_mainmenu=
'<tr><td class="caption">Lesson '+CURR_CHAP+'.LSN</td>'+
'<td nowrap="nowrap"><a class="menu" '+
' href="javascript:void(parent.worksheet(LSN))" onClick="window.status=\'\';"'+
' onMouseOver="window.status=\'GO TO WORKSHEET FOR LESSON '+CURR_CHAP+'.LSN\';return true;" '+
' onMouseOut="window.status=\'\';return true;">&nbsp;&nbsp;LST</a></td></tr>';
var end_mainmenu='</table></td></tr></table>\n';
/**** With Link
	var end_mainmenu='</table></td>\n'+
	'</tr><tr><td height="20">&nbsp;</td>\n'+
	'</tr><tr><td nowrap="nowrap"><a class="mainmenu" \n'+
	' href="../../eTutorial/media/chaptermenu.htm?chapter='+CURR_CHAP+'"\n'+
	' target="_top">Go to the eTutorial for Chapter '+CURR_CHAP+'</a></td>\n'+
	'</tr></table>\n';
****/
var htmltext=start_chaptermenu;
for(var i=1;i<SECTITLES[CURR_CHAP].length;i++) 
htmltext+=tmpl_mainmenu.replace(/LSN/g,i).replace(/LST/,SECTITLES[CURR_CHAP][i]);
htmltext+=end_mainmenu;
startWriteMain(true);
writeMain(htmltext);
endWriteMain(true);
}
function worksheet(newSect) {
loading_message("Q");
makeButtons('workbook');showButtons();
if (arguments.length==1) CURR_SECT=newSect;
top.document.title.innerHTML=WTITLE+" - Lesson "+CURR_CHAP+"."+CURR_SECT+" Worksheet"; 
mkWorkSheet(CURR_CHAP,CURR_SECT);
}
function checkanswers(){
makeButtons('reporter');showButtons();
top.document.title.innerHTML=WTITLE+" - Lesson "+CURR_CHAP+"."+CURR_SECT+" Results"; 
var htmltext=mkReportPage();
if (! htmltext) {
makeButtons('workbook');showButtons();
top.document.title.innerHTML=WTITLE+" - Lesson "+CURR_CHAP+"."+CURR_SECT+" Worksheet"; 
return;
} 
loading_message("R");
startWriteMain('report');
writeMain(htmltext);
endWriteMain(false);
}
function printpage() {
window.wvmain.focus();
setTimeout("window.wvmain.print();",50);
}
function makeButtons(state) {
var htmltext;
htmltext ='<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">\n';
htmltext+='<html><head><title>Worksheet</title></head>\n<body bgcolor="#cccccc" ';
htmltext+='marginheight=0 marginwidth=0 topmargin=0 leftmargin=0 rightmargin=0 \n>';
htmltext+='<center><table height=100% border=0 cellpadding=0 cellspacing=0>';
htmltext+='<tr><td align=center valign=middle><table height=100% border=0 ';
htmltext+='cellpadding=0 cellspacing=3><tr>';
if (state=='mainmenu') {
htmltext+=exitBtn;
} else if (state=='chapmenu') {
htmltext+=mainmenuBtn;
htmltext+=exitBtn;
} else if (state=='workbook') {
htmltext+=checkanswersBtn;
htmltext+=chaptermenuBtn;
htmltext+=mainmenuBtn;
htmltext+=printBtn;
htmltext+=exitBtn;
} else if (state=='reporter') {
htmltext+=chaptermenuBtn;
htmltext+=mainmenuBtn;
htmltext+=printBtn;
htmltext+=exitBtn;
}
htmltext+='</tr></table></td></tr></table></center></body></html>';
BOTTOM_HTML=htmltext;
showButtons();
}
var wait4bottom=0;
var bottom_attempts=0;
var BOTTOM_HTML="<html><body>&nbsp;</body></html>";
function showButtons() {
if (wait4bottom) clearTimeout(wait4bottom)
try {
window.wvbottom.document.clear();
window.wvbottom.document.open();
window.wvbottom.document.writeln(BOTTOM_HTML);
window.wvbottom.document.close();
} catch(e) {
if (++bottom_attempts>100) {
alert("This eWorkbook is currently unavailable. Please, try later.");
top.window.close(self);
} else {wait4bottom=setTimeout("showButtons()",300);}
};
}
function startWriteMain(isMenu) {
var htmltext;
htmltext ='<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">\n';
htmltext+='<html><head><title>Worksheet</title>';
htmltext+='<base href="'+BeRn.ccvURL+'">\n';
if (isMenu=='report') {
htmltext+='<link HREF="../style.css" REL="stylesheet" TYPE="text/css">';
htmltext+='<link HREF="bv_styles.css" REL="stylesheet" TYPE="text/css">';
} else if (isMenu) 
htmltext+='<link HREF="../style.css" REL="stylesheet" TYPE="text/css">';
else {
htmltext+='<link HREF="../style.css" REL="stylesheet" TYPE="text/css">';
htmltext+='<link HREF="bv_styles.css" REL="stylesheet" TYPE="text/css">';
}

htmltext+='</head>\n<body>';
window.wvmain.document.clear();
window.wvmain.document.open();
window.wvmain.document.writeln(htmltext);
}
function writeMain(htmltext) {
window.wvmain.document.writeln(htmltext);
}
function endWriteMain() {
window.wvmain.document.writeln('</body></html>');
window.wvmain.document.close();
window.wvmain.focus();
window.wvmain.scrollTo(0,0);
wloadingcnt=-100;
}
function initButtons() {
preloadBtn('exitB','exit');
preloadBtn('checkanswersB','checkanswers');
preloadBtn('mainmenuB','mainmenu');
preloadBtn('chaptermenuB','chaptermenu');
preloadBtn('printB','print');
this.checkanswersBtn=mkbutton('Check Answers','parent.checkanswers()','checkanswersB','checkanswers');
this.mainmenuBtn=mkbutton('Main Menu','parent.mainmenu()','mainmenuB','mainmenu');
this.chaptermenuBtn=mkbutton('Chapter Menu','parent.chaptermenu()','chaptermenuB','chaptermenu');
this.exitBtn=mkbutton('Exit','top.window.close(self)','exitB','exit');
this.printBtn=mkbutton('Print Page','parent.printpage()','printB','print');
}
function mkbutton(bTitle,bAction,iName,gifname) {
result='<td valign="middle" align="center">';
result+='<a href="javascript:void(0);" title="'+bTitle+'"';
result+=' onMouseOver="window.status=\' '+bTitle+'\';';
result+='document.'+iName+'.src=parent.'+iName+'_on.src;return true;"';
result+=' onMouseOut="window.status=\'\';';
result+='document.'+iName+'.src=parent.'+iName+'_off.src;return true;"';
result+=' onClick="'+bAction+'">';
result+='<img src="../images/navbtns/'+gifname+'_off.gif"';
result+=' alt="'+bTitle+'" name="'+iName+'" border=0></a></td>';
return result;
} 
function preloadBtn(iname,gifname) {
this[iname+"_off"]=new Image();
this[iname+"_off"].src="../images/navbtns/"+gifname+"_off.gif";
this[iname+"_on"]=new Image();
this[iname+"_on"].src="../images/navbtns/"+gifname+"_on.gif";
}
function extract(str,from,to) {
var i,j;
if (! str) return "";else str+="";
if (! from) i=0;
else {
i=str.indexOf(from);
if (i<0) return "";
i+=from.length;
}
if (! to) j=str.length;
else {
j=str.lastIndexOf(to);
if (j<0) return "";
}
return str.substring(i,j);
}
var tid_array = new Array();
var qobject   = new Array();
var qstatus   = new Array();
var wstatus   = new QueStat('all');
var wtimer,wStopLoop;  
function QueStat(ind) {
this.ind=ind;
this.rendered=false;
this.answered=false;
this.correct=false;
this.answer='';
this.qtext='';
this.ua='';
this.ca='';
}
function mkReportPage() {
var message='Exit and show results?';
var result='',qnum=qstatus.length,score=0;
if (!confirm(message)) return false;
var unanswered=new Array();
for(var i=0;i<qnum;i++) {
qstatus[i].answer=qobject[i].getAnswer(wvmain);
qstatus[i].correct=(qstatus[i].answer.indexOf('1')==0);
qstatus[i].answered=(qstatus[i].answer.indexOf('::')<0);
result=qobject[i].getDisplay();
qstatus[i].qtext=
extract(result,'<!--BIT-->','<!--EIT-->')+
extract(result,'<!--BQT-->','<!--EQT-->');
qstatus[i].ua=extract(result,'<!--BUA-->','<!--EUA-->');
qstatus[i].ca=extract(result,'<!--BCA-->','<!--ECA-->');
if (!qstatus[i].answered) {
qstatus[i].ua="<i>None Given</i>";
unanswered.push(i+1);
} 
}
if (unanswered.length>0) {
message="You have not answered problem"+((unanswered.length>1)?"s:\n":" ")+
unanswered.join(", ")+".\nDo you really want to exit the worksheet?";
if (!confirm(message)) return false;
}
result='<span class="btitle">LESSON '+
CURR_CHAP+'.'+CURR_SECT+' WORKSHEET RESULTS</span>\n<br>&nbsp;<br>';
for(var i=0;i<qnum;i++) {
result+='\n<p class="stitle">Problem '+(i+1)+' :</p>';
result+=qstatus[i].qtext;
result+='\n<table><tr><td valign="top"><span class="stitle">'+
'Your Answer:&nbsp;</span></td><td valign="top">'+qstatus[i].ua+
'</td></tr></table>';
if (qstatus[i].correct) {
score++;
result+='\n<table><tr><td><span class="sgtitle">'+
'Your Answer Is Correct.</span></td></tr></table>';
} else {
result+='\n<table><tr><td valign="top"><span class="stitle">'+
'Correct Answer:&nbsp;</span></td><td valign="top">'+
qstatus[i].ca+'</td></tr></table>';
result+='\n<table><tr><td><span class="srtitle">'+
'Your Answer Is Not Correct.</span></td></tr></table>';
} 
result+='\n<br><hr>';
}
result+='\n<p class="stitle">Your Score:&nbsp;'+score +' / ' +qnum+'</p>';
return result; 
}
function mkWorkSheet(chap,sect) {
if ((typeof BCS2P!='object')||(typeof BCS2P.book!='string')) {
BCS2P={s2_2:"1-2,8",book:"ml_hsm_alg2_c2007"};
}
var book,srng,tid,prng,i;
tid_array= new Array();
qobject  = new Array();
qstatus  = new Array();
wstatus  = new QueStat('all');
wsStopLoop=0;
book=BCS2P.book;srng='s'+chap+'_'+sect;
tid=book+'-chap'+chap+'-sect'+sect+'-prob';
prng=expandRange(BCS2P[srng]);
for(i=0;i<prng.length;i++) tid_array.push(tid+prng[i]);
for(i=0;i<tid_array.length;i++) {
qobject[i]=new Question(tid_array[i],'',0,'','');
qstatus[i]=new QueStat();
}
wtimer=setTimeout('wmainloop()',200);
}
function wmainloop() {
clearTimeout(wtimer);
var qnum=qstatus.length;
var i,j;
if (! wstatus.rendered) {
for(i=0;i<qnum;i++) {
if (qstatus[i].rendered) continue;
if (qobject[i].isReady()) {
if (i==0) {
startWriteMain(false);
writeMain('<span class="btitle">LESSON '+
CURR_CHAP+'.'+CURR_SECT+' WORKSHEET</span>\n<br>&nbsp;<br>');
writeMain('<form action="about:" name="wallq" onsubmit="return false;">\n');
}
writeMain('<p class="stitle">Problem '+(i+1)+' :</p>');
var str=qobject[i].renderWQuestion();
writeMain(str);
qstatus[i].rendered=true;
if (i<qnum-1) writeMain("<br><hr>");
else { 
wstatus.rendered=true;
writeMain('\n</form>');
endWriteMain();
} 
} else break;
}
if  ((! wstatus.rendered)&&(++wsStopLoop < 100)) wtimer=setTimeout('wmainloop()',200);
}
}
function expandRange(str) {
var i,j,k,l;
var result=new Array();
var parts=str.split(',');
for(i=0;i<parts.length;i++) {
if (!isNaN(parts[i])) result.push(parts[i]-0);
else {
k=parts[i].indexOf('-');
l=parts[i].substring(0,k)-0;
k=parts[i].substring(k+1)-0;
for(j=l;j<=k;j++) result.push(j);
}
}
return result;
}
var CURR_CHAP=-1;
var CURR_SECT=-1;
var WTITLE="McDougal Littell - High School Math - eWorkbook ";
var WTITLE1="McDougal Littell - Algebra 1 - eWorkbook ";
var WTITLE2="McDougal Littell - Algebra 2 - eWorkbook ";
var WTITLE3="McDougal Littell - Geometry - eWorkbook ";
var wloading,wloadingcnt=0,waitIDwl=null,message_txt="";
function loading_message(start) {
clearTimeout(waitIDwl);
if (start) {
if (start=="R") {
message_txt="Creating Report  ......................."
} else if (start=="Q") {
message_txt="Loading Questions  ....................." 
}
var hMid = eval(parseInt(screen.width/2) - 150);
var vMid = eval(parseInt(screen.height/2) - 25);
var winname = "loading_msg";
var winht = (navigator.appVersion.indexOf("Mac") != -1) ? 100:50;
var features = "menubar=no,scrollbars=no,width=400,height="+winht+","+
"resizable=no,left="+hMid+",top="+vMid+",screenX="+hMid+",screenY="+vMid+","+
"alwaysRaised,directories=no,status=no,toolbar=no";
wloading=window.open("",winname,features,true);
wloading.document.open();
wloading.document.write('<html><head><title>Loading Questions</title>'+
'</head><body bgcolor="#f0f0ff"><div ID="msg" STYLE="position: absolute; '+
'visibility: visible; z-index: 1; top: 35px; left: 80px;'+
'font-size:14pt;font-weight:700;color:#000000;'+
'">'+
message_txt.substring(0,22)+
"</div></body></html>");
wloadingcnt=0;
wloading.document.close();
wloading.focus();
waitIDwl=setTimeout("loading_message()",300);
} else if (wloadingcnt<0) {
wvmain.focus();
if ((wloading)&&(! wloading.closed)) wloading.close();
} else {
wloadingcnt=(wloadingcnt+1)%9;
if (wloading&&wloading.document&&wloading.document.getElementById) {
wloading.document.getElementById("msg").innerHTML=
message_txt.substring(0,22+2*wloadingcnt);
waitIDwl=setTimeout("loading_message()",300);
}   
}
return;
}
function close_popups() {
var BeRn=getBernEngine();
if ((BeRn) && (BeRn.popupWindow)&&(! BeRn.popupWindow.closed)) {
BeRn.popupWindow.close();
}
if ((wloading)&&(! wloading.closed)) wloading.close();
wloadingcnt=-100;
}
