// ========================= 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 CheckScore(score, max, min) {	
	if( (score < min) || (score > max) ) {
		alert("_AiccPutParamRequest_SetScore : invalid score <score="+score+", min="+min+", max="+max+">");
		return false;
	}
	
	return true;
}
// ----------------
function IntTo2DigitString(a)
{
  if (a < 10) return "0"+a;
  return a;
}

// =======================================================================
// Class AiccPutParamRequest
// =======================================================================

// -----------------------------------------------------------------------
// SetScore(5,10,0) -> "5,10,0"
// SetScore(15,50) -> "5,50,0"
// SetScore(15) -> "15,100,0"
// -----------------------------------------------------------------------
function _AiccPutParamRequest_SetScore(score, max, min) 
{
	// IE don't know undefined
	if (document.all) {
		// IE
		var undefined = null;
	}

	if(max == undefined)
		max = 100;

	if(min == undefined)
		min = 0;	
		
	CheckScore(score,max,min);

	this._score = score;
	this._min = min;
	this._max = max;
	
	this.Score = (MyRound(score) + "," + max + "," + min);
}

// -----------------------------------------------------------------------
function _AiccPutParamRequest_AddScore(score, max, min) 
{
	// IE don't know undefined
	if (document.all) {
		// IE
		var undefined = null;
	}

	if(max == undefined)
		max = 100;

	if(min == undefined)
		min = 0;	

	CheckScore(score,max,min);	

	this._score += score;
	
	if( min < this._min)
		this._min = min;
		
	this._max += max;
	
	this.Score = (MyRound(this._score) + "," + this._max + "," + this._min);		
}

// -----------------------------------------------------------------------
function _AiccPutParamRequest_SetLessonStatus( 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
// -----------------------------------------------------------------------
function _AiccPutParamRequest_SetTime( 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);
}

// -----------------------------------------------------------------------
function _AiccPutParamRequest_AddTime( 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);
}

// -----------------------------------------------------------------------
function _AiccPutParamRequest_Send() {
   var strAiccReq = "";
     
   strAiccReq += "[core]\r\n";
   strAiccReq += ( "Lesson_Status=" + this.LessonStatus + "\r\n" );
//   strAiccReq += ( "Score=" + this.Score + "\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();
   		
   document.SendAICCForm.command.value = "PutParam";
   
   document.SendAICCForm.submit();
}

// -----------------------------------------------------------------------
function _AiccPutParamRequest_SendCoreLesson(strSerializedData) {
   var strAiccReq = "";
   strAiccReq += "[core_lesson]\r\n";
   strAiccReq += ( strSerializedData + "\r\n" );

   document.SendAICCForm.aicc_data.value = strAiccReq;
     
   document.SendAICCForm.session_id.value = GetAICC_SID();
	 document.SendAICCForm.ref_id.value = GetAICC_REFID();   
	 
   document.SendAICCForm.command.value = "PutParam";
   
   document.SendAICCForm.submit();
}
// -----------------------------------------------------------------------
function AiccPutParamRequest() {
	this.LessonStatus = "";
	this.Score = "";
	this.Time = "";

	// Internal variables
	this._score = 0;
	this._min = 0;
	this._max = 0;
	
	this._time = 0;

	// Methods
	this.SetLessonStatus = _AiccPutParamRequest_SetLessonStatus;
		
	this.SetScore = _AiccPutParamRequest_SetScore;
	this.AddScore = _AiccPutParamRequest_AddScore;	

	this.SetTime = _AiccPutParamRequest_SetTime;
	this.AddTime = _AiccPutParamRequest_AddTime;
	
	this.Send = _AiccPutParamRequest_Send;
	this.SendCoreLesson = _AiccPutParamRequest_SendCoreLesson;	
}
