// ThomsonNOW Demo Platform v1.0
// Created by Wayne Skipper ( wayne@concentricsky.com )
// Copyright 2006 Thomson Higher Education

// global variables

var demoContainsTestbankContent = 1; // control access to areas of the demo where testbanks are accessed. 0 = no testbank, 1 = testbank
var demoContainsEBookContent = 1; // display the ebook on the course materials page. 0 = no display, 1 = display
var demoContainsCoursewareContent = 1; // display the courseware on the course materials page. 0 = no display, 1 = display

var loc = "" + window.location; // globally available location string
var isIE = ( navigator.userAgent.indexOf( "MSIE" ) != -1 );
var questionArray = new Array(); // used to store question objects on pages which load question XML. see parseQuestionXML()
var questionCount = 20; // total number of testbank questions across all books, 1 based ( this is not taken from XML for good reason )
var bookArray = new Array(); // used to store book objects on pages which load book XML. see parseBookXML()
var bookCount = 1; // total number of books, 1 based ( this is not taken from XML for good reason )
var Loaded = false; // used to synchronize XML document loading and browser page loading
var xmlDoc; // a generic XML document
var xmlResponseFunction; // holds a generic function. See loadXML()
var supportedProblemTypes = new Array( "FITB","MC", "TF", "SUB" );

// primary navigation function
function globalNav( destination )
{
    var isStud = ( loc.indexOf( "student" ) != -1 );
    if ( destination == "home" )
    {
            window.location = "../studentHome/page1.html";
    }
    else if ( destination == "courses" )
    {
            window.location = "../studentCourses/page1.html";
    }
    else if ( destination == "students" )
    {
        window.location = "../instructorStudents/page1.html";
    }
    else if ( destination == "gradebook" )
    {
            window.location = "../studentGrades/page1.html";
    }
    else if ( destination == "assignments" )
    {
            window.location = "../studentAssignments/page1.html";
    }
    else if ( destination == "materials" )
    {
            window.location = "e21_index.html";
    }
    else if ( destination == "communication" )
    {
            window.location = "../studentCommunication/page1.html";
    }
    else if ( destination == "reset" )
    {
        window.location = "../index.html";
    }
    else if ( destination == "prefs" )
    {
        window.open( "../misc/prefs.html", "prefs", "toolbar=no,location=no,width=634,height=451,resizable=no,scrollbars=no" );
    }
    else if ( destination == "help" )
    {
        window.open( "http://ilrn.com/media/help_wwh/Instructor/index.htm", "help", "toolbar=no,location=no,width=740,height=573,resizable=yes,scrollbars=yes" );
    }
    else if ( destination == "socPop" )
    {
        window.open( "../misc/lectureOutline.html", "help", "toolbar=no,location=no,width=724,height=492,resizable=no,scrollbars=yes" );
    }
    else if ( destination == "admin" )
    {
        window.location = "../instructorHome/page3.html";
    }
    else if ( destination == "account" )
    {
        window.location = "../instructorHome/page4.html";
    }
}

function displayError( state, message )
{
    var bkgd = document.getElementById( 'bkgd' ).getAttribute( 'vB' );

    if ( state )
    {
        if ( getCookie( "displayError" ) == "" )
        {
            var resStr = ( getCookie( "resources" ) == "1" ) ? "-2" : "";
            document.getElementById( 'bkgd' ).src = "images/" + bkgd + "-error" + resStr + ".gif";
            shiftPageElementY( "underError", 20 );
            setSessionCookie( "displayError", "1" );
            setTimeout( "document.getElementById( 'errorText' ).innerHTML = \"" + message +"\"", 150 );
        }
    }
    else
    {
            document.getElementById( 'errorText' ).innerHTML = "";
            var resStr = ( getCookie( "resources" ) == "1" ) ? "-2" : "";
            document.getElementById( 'bkgd' ).src = "images/" + bkgd + resStr + ".gif";
            shiftPageElementY( "underError", -20 );
            setSessionCookie( "displayError", "" );
    }
}



function initDemo()
{
    setSessionCookie( "selectedCourse", "1" );
    setSessionCookie( "isStudent", "0" );
}

function changeCourseNew(node)
{
    // user chose to move to another course, from drop box
    if (node.params && node.params["id"] )
    {
        setSessionCookie( "selectedCourse", node.params["id"] );
        setSelectedCourse(); // implemented in each area
    }
}

function setStudent( val )
{
    setSessionCookie( "isStudent", val );
}

// general purpose grading function for testbank questions
function gradeQuestion( qSource )
{
    var questions = bookArray[0].chapters[0].questions; // TODO get correct book and chapter positions

    for ( var i=0; i < questions.length; i++ )
    {
        if ( questions[i].source == qSource )
        {
            if ( questions[i].qtype == "MC" || questions[i].qtype == "TF" )
            {
                for ( var j=0; j < document.forms[0].answerSet.length; j ++ )
                {
                    if ( document.forms[0].answerSet[j].checked )
                    {
                        // answered
                        displayQuestionFeedback( j == questions[i].correct );
                        return;
                    }
                }
            }
            else if ( questions[i].qtype == "FITB" )
            {
                if ( document.getElementById( "answerSet" ).value != "" )
                {
                    displayQuestionFeedback( document.getElementById( "answerSet" ).value == questions[i].correct );
                    return;
                }
            }
            else
            {
                // always correct
                displayQuestionFeedback( true );
                return;
            }
            // unanswered
            alert( "Please enter an answer before pressing the Submit button." );
            return;
        }
    }
}

function displayQuestionFeedback( cond )
{
    document.getElementById( "feedbackDisplay" ).innerHTML = ( cond ) ? document.getElementById( "feedbackCorrect" ).innerHTML : document.getElementById( "feedbackIncorrect" ).innerHTML;
}

// from integration demo - outlines for linked images
function assignLinkHandlers()
{
	var imgs = document.getElementsByTagName('img');

	for (var i = 0; i < imgs.length; i++)
	{
		img = imgs[i];
		if (img.className == "hand")
		{
			if (img.src.indexOf("emptyspace.gif") != -1)
			{
				img.onmouseover = linkMouseOver;
				img.onmouseout = linkMouseOut;
			}
			if (img.src.indexOf("cal.gif") != -1)
			{
				img.onmouseover = linkMouseOver;
				img.onmouseout = linkMouseOut;
			}
		}
	}
}

function linkMouseOver()
{
	this.className = 'handOver';
}

function linkMouseOut()
{
	this.className = 'hand';
}


