//var IDCount=0;
//function GetNextID() {return "UniqueID"+IDCount;}
var ImagePath="../images/";
var split_continue = "no";
var split_new = "no";
var ClientID="";
var show_exam_info="no";
var AllLicensingDone=true;
var shown_exams = false;

function SetClientID(CI) {ClientID=CI;}
//Classes

function subject(Title,Hours,Code,Status,ExpirationDate)
    {
    this.Title=Title;
    this.Hours=Hours;
    this.Code=Code;
    this.Status=Status;
    this.ExpirationDate=ExpirationDate;
    this.Modules=new Array();
    this.Expanded=false;
    }

function module(Title,Hours,Code,Status)
    {
    this.Title=Title;
    this.Hours=Hours;
    this.Code=Code;
    this.Status=Status;
    this.Lessons=new Array();
    this.Expanded=false;
    }

function lesson(Title,Code,Status)
    {
    this.Title=Title;
    this.Code=Code;
    this.Status=Status;
    }

//Globals
var Subjects=new Array();
var CurrentSubject;
var CurrentModule;
var AcresCodeToObject=new Object();

//Build JS Data Structure
function aS(Title,Hour,Code,Status,ExpirationDate)
    {
    CurrentSubject=new subject(Title,Hour,Code,Status,ExpirationDate);
    Subjects[Subjects.length]=CurrentSubject;
    AcresCodeToObject[Code]=CurrentSubject;
    }

function aM(Title,Hour,Code,Status)
    {
    CurrentModule=new module(Title,Hour,Code,Status);
    CurrentSubject.Modules[CurrentSubject.Modules.length]=CurrentModule;
    AcresCodeToObject[Code]=CurrentModule;
    }

function aL(Title,Code,Status)
    {
    CurrentModule.Lessons[CurrentModule.Lessons.length]=new lesson(Title,Code,Status);
    }

//Generate Page
function GetTag(TagID)
    {
    //return document.all[TagID];
    var Ret=document.getElementById(TagID);
    if (!Ret) alert("GetTag: tag '"+TagID+"' does not exist.");
    return Ret;
    }

function RewriteInnerHTML(TagID,NewInnerHTML)
    {
//    alert(NewInnerHTML);
    GetTag(TagID).innerHTML=NewInnerHTML;
    }

function GenerateTopLevelHTML(TableInsides)
    {
    return  "<TABLE cellSpacing=0 cellPadding=0 width=\"100%\" border=1>\n"+
            "<TR>"+ TableInsides + "</tr></TABLE>";
    }

var CollapseImage="minus_20.gif";
var ExpandImage="plus_20.gif";
var DoneImage="check_20.gif";
var XImage="x_20.gif";

function GetStatusImageHTML(Obj)
    {
    var SI="";
    if (Obj.Status=="done")
        return "<IMG src=\""+ImagePath+DoneImage+"\">";
    else
        return "";
    }

function GetToggleImageHTML(Obj)
    {
    var EI;
    if (Obj.Expanded) EI=CollapseImage;
    else EI=ExpandImage;
    return "<IMG src=\""+ImagePath+EI+"\" onclick=\"\ToggleExpand('"+Obj.Code+"')\">";
    }

function ShowExpiredPage(ClassID)
    {
    window.open("class_expired.asp");
    }

function GenerateLessonHTML(Lesson)
    {
	if (Lesson.Status=="pending")
	   {
    var OutHTML="<TR>"+
        "<TD width = 99% height = 25><P style=\"font-family: arial, HelveticaTimes, 'sans serif'; font-size: 12; TEXT-DECORATION: underline; color: black; TEXT-ALIGN: left;\" >"+Lesson.Title+"</TD>"+
        "<TD width=20>"+GetStatusImageHTML(Lesson)+"</TD>"+
        "</TR>";
		}

    else
        {
		var OutHTML="<TR style=\"cursor: hand\" onClick=\"document.gotolocation.full_location.value='"+
		Lesson.Code+"'; document.gotolocation.submit();\">"+
        "<TD width = 435 height = 25><P style=\"font-family: arial, HelveticaTimes, 'sans serif'; font-size: 12; TEXT-DECORATION: underline; color: black; TEXT-ALIGN: left;\" >"+Lesson.Title+"</TD>"+
        "<TD width=20>"+GetStatusImageHTML(Lesson)+"</TD>"+
        "</TR>";
		}

    return "<TABLE width = '100%' border = 0 cellspacing=0 cellpadding=0 >"+OutHTML+"</TABLE>";
    }

function GenerateModuleHTML(Module)
    {
    var OutHTML=
        "<TR style=\"cursor: hand\" onclick=\"\ToggleExpand('"+Module.Code+"')\">"+
		"<TD width=25 height = 25><ALIGN='RIGHT'>"+GetToggleImageHTML(Module)+"</ALIGN></TD>"+
        "<TD width = 370><P style=\"font-family: arial, HelveticaTimes, 'sans serif'; font-size: 12; font-weight: bold; color: blue;\" >"+Module.Title+"</TD>"+
        "<TD width = 120><P style=\"font-family: arial, HelveticaTimes, 'sans serif'; font-size: 10; font-weight: bold; color: blue; TEXT-ALIGN: right;\" >"+Module.Hours+" hours</TD>"+
        "<TD width=30 align=right><SPAN style=\"WIDTH: 20px; TEXT-ALIGN: right\">"+GetStatusImageHTML(Module)+"</span></TD>"+
        "</TR>";
    if (Module.Expanded)
        {
        var L;
        var LOut="";
        for (L=0;L<Module.Lessons.length;L++)
            {
            LOut=LOut+GenerateLessonHTML(Module.Lessons[L]);
            }
        OutHTML=OutHTML+"<TR><td width = 20><TD colspan = 3>"+LOut+"</TD></TR>";
        if (Module.Status=="pending")
	   {
       	OutHTML=OutHTML+"<TR ><TD colspan = 4><P style=\"font-family: arial, HelveticaTimes, 'sans serif'; font-size: 12; font-weight: bold; TEXT-DECORATION: underline; color: black; TEXT-ALIGN: left;\" >"+Module.Title+" Test</td></TR>";
		}
        else
        {
		OutHTML=OutHTML+"<TR>"+
		"<TD colspan = 4 style=\"cursor: hand\" onClick=\"location.href='/loggedin_v2/testengine/av2starttest.aspx?DashedTestID="+Module.Code+"'\"><P style=\"font-family: arial, HelveticaTimes, 'sans serif'; font-size: 12; TEXT-DECORATION: underline; color: #03bd28; font-weight: bold; TEXT-ALIGN: left;\" >"+Module.Title+" Test</TD></TR>\n";
		}
		}
        //Add lines for Module test to OutHTML as TRs
    //OutHTML=OutHTML+"module test";
    return "<TABLE width = '100%' border = 0 cellspacing=0 cellpadding=0>"+OutHTML+"</TABLE>";
    }

function GenerateSubjectHTML(Subject,ShowTakeLicensingExitExams)
    {
	shown_exams = false;
    var OutHTML="";
	if (Subject.Code <= '02-01')
	   {
	   if (split_new == "no")
	      {
	   	  OutHTML=OutHTML+"<tr height = 35><td colspan = 4 ><center>"+course_title+"</center></td></tr>";

		  split_new = "yes";
		  }
	   }
	   	if (Subject.Code >= '02-01')
	   {
	   if (split_continue == "no")
	      {
	   	  OutHTML=OutHTML+"<tr><td colspan = 4><center><hr><center>Continuing Education Classes</center></td></tr>";
		  split_continue = "yes";
		  }
	   }

    //*******************************
    // Calculate DaysLeft until class expires
    //*******************************
    var CurrentDate=new Date();
    CurrentDate.setHours(0);
    CurrentDate.setMinutes(0);
    CurrentDate.setSeconds(0);
    CurrentDate.setMilliseconds(0);
    var ExpirationDate;
    var DaysLeft=0;
    if (Subject.ExpirationDate!="")
        {
        ExpirationDate=new Date(Subject.ExpirationDate);
        ExpirationDate.setHours(0);
        ExpirationDate.setMinutes(0);
        ExpirationDate.setSeconds(0);
        ExpirationDate.setMilliseconds(0);
        var DateDiffms=ExpirationDate-CurrentDate;    //the difference in miliseconds
        var DateDiffDay=DateDiffms/(24*3600*1000);
        DaysLeft=Math.floor(DateDiffDay);
        }


    //*******************************
    // Fill ExpirationString with UI-friendly text
    //*******************************
    var ExpirationString;
    var Expired=false;

    if (Subject.Status=="done")
        ExpirationString="<font color=green>Completed</font>";
    else if (Subject.ExpirationDate=="")
        ExpirationString="(no time limit)";
    else if (DaysLeft<0)
        {ExpirationString="<font color=red>Expired</font>";Expired=true;}
    else if (DaysLeft==0)
        ExpirationString="<font color=red>Expires today!</font>";
    else if (DaysLeft==1)
        ExpirationString="<font color=red>Expires in 1 day!</font>";
    else if (DaysLeft<=7)
        ExpirationString="<font color=red>Expires in "+DaysLeft+" days!</font>";
    else
        ExpirationString="Expires in "+DaysLeft+" days";

    if (Expired)
        {
        OutHTML=OutHTML+
            "<TR style=\"cursor: hand\" onclick=\"\ShowExpiredPage('"+Subject.Code+"')\">\n"+
            "<TD width=20 align=right><img src=\""+ImagePath+XImage+"\"></TD>";
        }
    else
        {
        OutHTML=OutHTML+
            "<TR style=\"cursor: hand\" onclick=\"\ToggleExpand('"+Subject.Code+"')\">\n"+
            "<TD width=20 align=right>"+GetToggleImageHTML(Subject)+"</TD>";
        }
//    ExpirationString="<font color=green>"+CurrentDate+"</font><br><font color=red>"+ExpirationDate+"</font>";

    OutHTML=OutHTML+
            "<TD width=450><P style=\"font-family: arial, HelveticaTimes, 'sans serif'; font-size: 14; font-weight: bold; color: blue;\" >"+
            Subject.Title+"</P></TD>"+
            "<TD width=120><P style=\"font-family: arial, HelveticaTimes, 'sans serif'; font-size: 10; font-weight: bold; color: blue; TEXT-ALIGN: right;\" >"+Subject.Hours+" hours<br>"+
                ExpirationString+"</P></TD>"+
            "<TD width=30 align=right><SPAN style=\"WIDTH: 20px; TEXT-ALIGN: right\">"+GetStatusImageHTML(Subject)+"</SPAN></TD>"+
            "</TR>\n";


    if (!Expired && Subject.Expanded)
        {
        var MOut="";
        for (M=0;M<Subject.Modules.length;M++)
            {
            MOut=MOut+GenerateModuleHTML(Subject.Modules[M]);
            }
        OutHTML=OutHTML+"<TR><TD width=20></TD><TD colspan = 3>"+MOut+"</TD></TR>";
        if (Subject.Status=="pending")
            {
            OutHTML=OutHTML+"<TR>"+
            "<TD colspan = 4><P style=\"font-family: arial, HelveticaTimes, 'sans serif'; font-size: 14; TEXT-DECORATION: underline;  font-weight: bold;color: black; TEXT-ALIGN: left;\" >"+Subject.Title+" Exam</td></TR>\n";
            }
        else
            {
            OutHTML=OutHTML+"<TR>"+
            "<TD colspan = 4 style=\"cursor: hand\" onClick=\"location.href='/loggedin_v2/testengine/av2starttest.aspx?DashedFinalID="+Subject.Code+"';\"><P style=\"font-family: arial, HelveticaTimes, 'sans serif'; font-size: 14; TEXT-DECORATION: underline;  font-weight: bold; color: #03bd28; TEXT-ALIGN: left;\" >"+Subject.Title+" Exam</td></TR>\n";
            }
        }

    var TakeFinalExamsText="";

    if (ShowTakeLicensingExitExams)
        {
//        var URL="/loggedin_v2/PrelicensingFinals/?clientid="+ClientID;
//        TakeFinalExamsText="<table width=500><tr><td><center><P style=\"font-family: arial, HelveticaTimes, 'sans serif'; font-size: //        14; font-weight: bold; color: blue;\" >"+
//            "<a style='cursor: hand' onclick='"+
//                "window.open(\""+URL+"\",\"\",\"fullscreen\")'>"+
//            "Licensing Exit Exam</a></p>"+
//            "</center></td></tr></table>";

		var GeneralURL="";
        var ColoradoURL="";
		var NationalURL="";
        var ExamURL="";
        var TestURL="";
		var SurveyURL="";
		var MorePrepTimes = 0;
		
		//MorePrepTimes = 4 - times_opened_reviews;
				
		if (completed_classes=="yes")
		{
		GeneralURL="href='/current/generalreview.asp'";
		//GeneralURL1="href='/current/fillin.asp?part=" + farthest_lesson + "'";		
		NationalURL="href='/current/nationalreview.asp'";
        ColoradoURL="href='/current/coloradoreview.asp'";
		//ColoradoURL1="href='/current/fillin.asp?part=" + farthest_lesson + "'";
		TestURL="href='/current/PassOfficialExam.asp'";
		
        //ExamURL="onclick = 'window.open(\"/loggedin_v2/PrelicensingFinals/?clientid=" +ClientID+"\",\"\",\"fullscreen\")'";

		//ExamURL = "onclick = 'alert(\"You will not be able to pass our exit exams or the offical exam with studying the exam prep sections in depth. Please look through the exam prep sections "+MorePrepTimes+ " more times.\")'";

		//ExamURL = "onclick = 'alert(\"You will not be able to pass our exit exams or the offical exam with studying the exam prep sections in depth.\n Please look through the exam prep sections at least " +MorePrepTimes+ " more times before taking our exit exams.\")'";
		
		}

		var GuarenteeLine = "";
		/*
		if (guarantee_applies == "yes")
		{
			var number_times_passed_both = parseInt(times_passed_both);
			var times_needed = 3 - number_times_passed_both; 
	
			if (number_times_passed_both < 2)
			{
			GuarenteeLine = "<tr height = 10><td width = 20>&nbsp; </td>"+
				"<td><center><p style='cursor: hand; text-decoration: underline; font-size: 10;'><a href='http://www.acreschool.com/newsystem/files/passguarantee.htm' target='_blank'>You must pass both tests "+
				times_needed + 
				" more times to get your pass guarantee. </a></p></center> </td><td width = 20></td></tr>"	;
			}		
	
			if (number_times_passed_both == 2)
			{
			GuarenteeLine = "<tr height = 10><td width = 20>&nbsp; </td>"+
				"<td><center><p style='cursor: hand; text-decoration: underline; font-size: 10;'><a href='http://www.acreschool.com/newsystem/files/passguarantee.htm' target='_blank'>You must pass both tests "+
				times_needed + 
				" more time to get your pass guarantee. </a></p></center> </td><td width = 20></td></tr>"	;
			}		
		
	
	
			if (number_times_passed_both > 2)
			{
			GuarenteeLine = "<tr height = 10><td width = 20>&nbsp; </td>"+
				"<td><center><p style='cursor: hand; text-decoration: underline; font-size: 10;'><a href='http://www.acreschool.com/newsystem/files/passguarantee.htm' target='_blank'>Your pass guarantee is valid! </a></p></center> </td>"+
				"<td width = 20></td></tr>"	;
			}
		}
		*/

        ExamURL="onclick = 'window.open(\"/loggedin_v2/PrelicensingFinals/?clientid=" +ClientID+"\",\"\",\"fullscreen\")'";
		SurveyURL = "href='/current/client_survey.asp'";

		
		TakeFinalExamsText="<table width=100% border = 0>"+
			"<tr><td width = 20>&nbsp; </td>"+
			"<td><center><P style=\"font-family: arial, HelveticaTimes, 'sans serif';"+
		    "font-size: 14;	font-weight: bold; color: blue;\" >"
			
			// include this line ONLY if they need the general exam
            if (needs_general=="yes")
			{
			TakeFinalExamsText=TakeFinalExamsText+"<a style='cursor: hand; text-decoration: underline; color: blue;' "+NationalURL+"><font color = 'red'>National Exam Prep Course</font></a> <br></td>"
			}
						
            TakeFinalExamsText=TakeFinalExamsText+"<td width = 20></td></tr>"+
			"<tr><td width = 20>&nbsp;</td>"+
			
			
			//"<td><center><P style=\"font-family: arial, HelveticaTimes, 'sans serif';"+
		   // "font-size: 14;	font-weight: bold; color: blue;\" >"+
			//"<a style='cursor: hand; text-decoration: underline;' "+GeneralURL1+">General Exam Prep Course Test</a><br></td>"+


			"<td><center><P style=\"font-family: arial, HelveticaTimes, 'sans serif';"+
		    "font-size: 14;	font-weight: bold; color: blue;\" >"+
			"<a style='cursor: hand; text-decoration: underline;' "+ColoradoURL+"><font color = 'red'>Colorado Exam Prep Course</font></a><br></td>"+
            "<td width = 20> </td></tr>"+
			
			"<tr><td width = 20>&nbsp; </td>"+
			"<td><center><P style=\"font-family: arial, HelveticaTimes, 'sans serif';"+
		    "font-size: 14;	font-weight: bold; color: blue;\" >"+
            "<a style='cursor: hand; text-decoration: underline;' href = ' http://www.acreschool.com/acresadmin/flash_cards/flash_card_display.asp' target = '_blank'>Flash Cards!</a><br></td>"+
            "<td width = 20></td></tr>"+
						"<tr><td width = 20>&nbsp;</td>"+
			"<td><center><P style=\"font-family: arial, HelveticaTimes, 'sans serif';"+
		    "font-size: 14;	font-weight: bold; color: blue;\" >"+
			"<a style='cursor: hand; text-decoration: underline;' "+TestURL+">How to Schedule the Offical Exam and Pass the First Time</a><br></td>"+
            "<td width = 20> </td></tr>"+
			"<tr><td width = 20>&nbsp; </td>"+
            "<td><center><P style=\"font-family: arial, HelveticaTimes, 'sans serif';"+
		    "font-size: 14;	font-weight: bold; color: blue;\" >"+
			"<a style='cursor: hand; text-decoration: underline; font-size: 18;' "+ExamURL+" >"+
            "Licensing Exit Exams</a></p></center>"+
            "<td width = 20></td></tr>"+
			GuarenteeLine +
			"<tr><td width = 20>&nbsp; </td>"+
			"<td><center><P style=\"font-family: arial, HelveticaTimes, 'sans serif';"+
		    "font-size: 14;	font-weight: bold; color: blue;\" >"+
			"<a style='cursor: hand; text-decoration: underline;' "+SurveyURL+" >"+
            "Take Our Exam Survey - Get the 2006 Update Course for Free!</a></p></center>"+
            "<td width = 20> </td></tr>"+
			"</table>";

 			 show_exam_info="no";
		     shown_exams = true;
			 ShowTakeLicensingExitExams = false;
		}
		//Add lines for Subject test to OutHTML as TRs
    return "<TABLE border=0 cellspacing=2 cellpadding=0 width = 100%>"+OutHTML+"</TABLE>"+TakeFinalExamsText;
    }

function GeneratePage()
    {
    var S;
	split_continue = "no";
	split_new = "no";
    var OutHTML="";


    var LastLicensing="";
    var ShowTakeLicensingExitExams=false;

	for (S=0;S<Subjects.length;S++)
        {
        if  (
            Subjects[S].Code == "01-01" ||
            Subjects[S].Code == "01-02" ||
            Subjects[S].Code == "01-03" ||
            Subjects[S].Code == "01-04" ||
            Subjects[S].Code == "01-05" ||
            Subjects[S].Code == "01-06"
            )
            {
            if (Subjects[S].Status!="done") AllLicensingDone=false;
		 	show_exam_info=true;
			LastLicensing=Subjects[S].Code;
            }
        }

    for (S=0;S<Subjects.length;S++)
        {

        //if (Subjects[S].Code==LastLicensing && AllLicensingDone)
        if (Subjects[S].Code==LastLicensing)
		 	ShowTakeLicensingExitExams=true;
		if (shown_exams)
		{
			ShowTakeLicensingExitExams=false;
		}
		OutHTML=OutHTML+GenerateSubjectHTML(Subjects[S],ShowTakeLicensingExitExams);
        }

    OutHTML="<FORM name = 'gotolocation' ACTION='/current/display.asp' METHOD='post'>"+
    	"<input type='hidden' name = 'module_id' value = '' >"+
    	"<input type='hidden' name = 'full_location' value = '' >"+
    	"<input type='hidden' name = 'current_lesson' value = '' >"+
        OutHTML+
        "</FORM>";
//    alert(OutHTML);
    RewriteInnerHTML("PageID",OutHTML);
    }

function ToggleExpand(AcresCode)
    {
    AcresCodeToObject[AcresCode].Expanded=(!AcresCodeToObject[AcresCode].Expanded);
    GeneratePage();
    }

