// ========================= Copyright Epistema 2002 ========================
//
// AICC
//
// Returns the AICC information to the LMS
//
// $Revision: 1.4 $
//
// $Log: AICC.js,v $
// Revision 1.4  2003/02/19 12:08:03  bertrand
// Added multimedia
//
// Revision 1.3  2002/12/05 17:24:52  bertrand
// Added the CVS header
//
// ==========================================================================

// -----------------------------------------------------------------------
// Get a parameter from a url
// return null if the parameter doesn't exist
// -----------------------------------------------------------------------
function GetParameter(url,param)
{
	param += "=";

	var pos1 = String(url).toLowerCase().indexOf(String(param).toLowerCase());

	// check if the parameter exist
	if( pos1 == -1)
		return null;
	else
		pos1 += param.length;

	var pos2 = url.indexOf("&",pos1);

	if(pos2 == -1)
	{
		// we want the last parameter
		return url.substr(pos1);
	}
	else
	{
		return url.substring(pos1,pos2);
	}
}

function GetAICC_URL()
{
	var url = unescape( window.parent.location.toString());
	return GetParameter( url, "aicc_url");
}

function GetAICC_SID()
{
	var url = unescape( window.parent.location.toString());
	return GetParameter( url, "aicc_sid");
}

function GetAICC_REFID()
{
	var url = unescape( window.parent.location.toString());
	return GetParameter( url, "aicc_refid");
}

function IntTo2DigitString(a)
{
  if (a < 10) return "0"+a;
  return a;
}

// =======================================================================
// Class AiccPutParamRequest
// =======================================================================
function AiccPutParamRequest()
{
	this.LessonStatus = "";
	this.Time = "";

	// Internal variables
	this._time = 0;

	// Methods
	this.SetLessonStatus = function(strStatus)
	{
		// TODO : check parameter
		this.LessonStatus = strStatus;
	};

	// -----------------------------------------------------------------------
	// time est du type Date()
	// Attention : ne tient compte que du format HH:MM:SS et non [HH]HH:MM:SS
	// -----------------------------------------------------------------------
	this.SetTime = function(time)
	{
		var hh = time.getUTCHours();
		var min = time.getUTCMinutes();
		var ss = time.getUTCSeconds();

		this._time = hh * 3600 + min * 60 + ss;

		this.Time = IntTo2DigitString(hh)
				+ ":" + IntTo2DigitString(min)
				+ ":" + IntTo2DigitString(ss);
	};

	this.SetTime = function(time)
	{
		this._time = (time.getUTCHours() * 3600
							 +  time.getUTCMinutes() * 60
							 +  time.getUTCSeconds());

		var t_hh = Math.floor( this._time / 3600);
		var t_min = Math.floor( ( this._time - (t_hh * 3600)) / 60);
		var t_ss = this._time - (t_hh * 3600) - (t_min * 60);

		this.Time = IntTo2DigitString(t_hh)
				+ ":" + IntTo2DigitString(t_min)
				+ ":" + IntTo2DigitString(t_ss);
	};

	this.Send = function()
	{
		var strAiccReq = "";

		strAiccReq += "[core]\r\n";
		strAiccReq += ( "Lesson_Status=" + this.LessonStatus + "\r\n");
		strAiccReq += ( "Time=" + this.Time + "\r\n");

		document.SendAICCForm.aicc_data.value = strAiccReq;

		document.SendAICCForm.session_id.value = GetAICC_SID();
		document.SendAICCForm.ref_id.value = GetAICC_REFID();
		var targetURL = GetAICC_URL();

		if (targetURL != '')
		{
			document.SendAICCForm.action = targetURL;
			document.SendAICCForm.command.value = "PutParam";
			document.SendAICCForm.submit();
		}
	};
}
