/*
    This file is part of LibSCORM 2004.

    LibSCORM 2004 is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    LibSCORM 2004 is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with LibSCORM 2004; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

*/

function InterScoSeq(lms, onBeforeTerminate) {
	this._lms             = lms;
	this._canExitForward  = false;
	this._canExitBackward = false;
	this._onBeforeTerminate = onBeforeTerminate;

	if(lms) {
		try {
			this._canExitForward  = (lms.GetValue("adl.nav.request_valid.continue") == "true");
			this._canExitBackward = (lms.GetValue("adl.nav.request_valid.previous") == "true");
		} catch(e) {
			if(e.code != 401) { // something other than simple non-support
			                    // for inter-sco sequencing
				throw e;
			}
		}
	}
}

InterScoSeq.prototype.CanExitForward = function() {
	return this._canExitForward;
}

InterScoSeq.prototype.CanExitBackward = function() {
	return this._canExitBackward;
}

InterScoSeq.prototype.ExitForward = function() {
	if(!this.CanExitForward()) {
		return false;
	}
	this._onBeforeTerminate();
	this._lms.SetValue("adl.nav.request","continue");
	this._lms.Terminate();
	return true;
}

InterScoSeq.prototype.ExitBackward = function() {
	if(!this.CanExitBackward()) {
		return false;
	}
	this._onBeforeTerminate();
	this._lms.SetValue("adl.nav.request","previous");
	this._lms.Terminate();
	return true;
}

InterScoSeq.prototype.ExitChoice = function(target) {
	this._onBeforeTerminate();
	this._lms.SetValue("adl.nav.request", "{target = " + target + "}choice");
	this._lms.Terminate();
	return true;
}

InterScoSeq.prototype._lms;
InterScoSeq.prototype._canExitBackward;
InterScoSeq.prototype._canExitForward;
InterScoSeq.prototype._onBeforeTerminate;
