var API=this;
var strError="0";
var bAPIInit=false;
var STUDENT_ID="";
var STUDENT_NAME="";
var LESSON_LOCATION="";
var CREDIT="";
var LESSON_STATUS="";
var ENTRY="";
var RAW="";
var MAX="";
var MIN="";
var TOTAL_TIME="";
var LESSON_MODE="";
var LAUNCH_DATA="";
var SUSPEND_DATA="";
var LANGUAGE="";
var FEEDBACK="";
var data=new Array(
  new sde("cmi._version","3.4",new Array("CMIString255"),true,true,false,true),
  new sde("cmi.core._children","student_id,student_name,lesson_location,credit,lesson_status,entry,score,total_time,lesson_mode,exit,session_time",new Array("CMIString255"),true,true,false,true),
  new sde("cmi.core.student_id",STUDENT_ID,new Array("CMIIdentifier"),true,true,false,false),
  new sde("cmi.core.student_name",STUDENT_NAME,new Array("CMIString255"),true,true,false,false),
  new sde("cmi.core.lesson_location",LESSON_LOCATION,new Array("CMIString255"),true,true,true,false),
  new sde("cmi.core.credit",CREDIT,new Array("CMIVocabulary(Credit)"),true,true,false,false),
  new sde("cmi.core.lesson_status",LESSON_STATUS,new Array("CMIVocabulary(Status)"),true,true,true,false),
  new sde("cmi.core.entry",ENTRY,new Array("CMIVocabulary(Entry)"),true,true,false,false),
  new sde("cmi.core.score._children","raw,min,max",new Array("CMIString255"),true,true,false,true),
  new sde("cmi.core.score.raw",RAW,new Array("CMIBlank","CMIDecimal"),true,true,true,false),
  new sde("cmi.core.score.max",MAX,new Array("CMIBlank","CMIDecimal"),true,true,true,false),
  new sde("cmi.core.score.min",MIN,new Array("CMIBlank","CMIDecimal"),true,true,true,false),
  new sde("cmi.core.total_time",TOTAL_TIME,new Array("CMITimespan"),true,true,false,false),
  new sde("cmi.core.lesson_mode",LESSON_MODE,new Array("CMIVocabulary(Mode)"),true,true,false,false),
  new sde("cmi.core.exit","",new Array("CMIVocabulary(Exit)"),true,false,true,false),
  new sde("cmi.core.session_time","",new Array("CMITimespan"),true,false,true,false),
  new sde("cmi.launch_data",LAUNCH_DATA,new Array("CMIString4096"),true,true,false,false),
  new sde("cmi.suspend_data",SUSPEND_DATA,new Array("CMIString4096"),true,true,true,false),
  new sde("cmi.student_preference._children","language",new Array("CMIString255"),true,true,false,true),
  new sde("cmi.student_preference.language",LANGUAGE,new Array("CMIString255"),true,true,true,false),
  new sde("id.feedback",FEEDBACK,new Array("CMIString4096"),true,false,true,false)
)
if(LESSON_STATUS=="completed"||LESSON_STATUS=="passed"||LESSON_STATUS=="failed")
  window.opener.setSCORMComplete();

function sde(strName,strValue,arrDataType,bIsImplemented,bIsReadable,bIsWritable,bIsKeyword){
//alert("sde("+strName+", "+strValue+", "+arrDataType+", "+bIsImplemented+", "+bIsReadable+", "+bIsWritable+", "+bIsKeyword+")")
  this.strName=strName;
  this.strValue=strValue;
  this.arrDataType=arrDataType;
  this.bIsImplemented=bIsImplemented;
  this.bIsReadable=bIsReadable;
  this.bIsWritable=bIsWritable;
  this.bIsKeyword=bIsKeyword;
  this.bIsCommitted=true;
  this.strNewValue="";
  this.validate=validate;
  this.getReturnValue=getReturnValue;
}

function setSCORMValue(strName,strValue){
  bSet=false;
  for(svi=0;svi<data.length;svi++){
    if(data[svi].strName==strName){
      data[svi].strValue=strValue;
      bSet=true;
    }
  }
}

function APIPresent(strURL){
  if(strURL!=""&&strURL!="[URL]"&&!bContentSet){
    frames['ContentFrame'].location.href=strURL;
    if(window.opener&&window.opener.isScormOpen){
      window.opener.setCurrTakenId(TAKEN_ID);
      window.opener.isScormOpen();
    }
    bContentSet=true;
  }
}

function getReturnValue(){
  strDataType=","+this.arrDataType.join(",")+",";
  if(strDataType.indexOf("CMIDecimal")!=-1){
    if(this.strValue=="")return "0";
    else return this.strValue;
  }else if(strDataType.indexOf("CMITimespan")!=-1){
    if(this.strValue="")return "0";
    else{
      arrTimespan=this.strValue.split(":");
      nMill=0;
      if(arrTimespan.length>2){
        arrSecs=arrTimespan[2].split(".");
        if(arrSecs.length>0)nMill+=parseInt(arrSecs[0])*1000;
        if(arrSecs.length>1)nMill+=parseInt(arrSecs[1])*100;
        nMill+=(parseInt(arrTimespan[0])*3600000)+(parseInt(arrTimespan[1])*60000)
      }
      return ""+nMill;
    }
  }else{
    return this.strValue;
  }
}

function validate(strValue){
  bValid=false;
  for(vi=0;!bValid&&vi<this.arrDataType.length;vi++){
    switch(this.arrDataType[vi]){
      case "CMIBlank":
        bValid=(strValue=="");
        break;
      case "CMIString255":
        bValid=(strValue!=null&&strValue.length<=255)
        break;
      case "CMIString4096":
        bValid=(strValue!=null)
        break;
      case "CMIDecimal":
        bValid=(!isNaN(strValue))
        break;
      case "CMIIdentifier":
        bValid=(strValue!=null&&strValue.length<=255)
        for(apij=0;bValid&&apij<strValue.length;apij++)
          bValid=("0123456789AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz".indexOf(strValue.charAt(apij))!=-1);
        break;
      case "CMITimespan":
        arrTime=strValue.split(":");
        bValid=(arrTime.length==3&&(arrTime[0].length>1&&arrTime[0].length<5)&&(arrTime[1].length==2)&&(arrTime[2].length==2||arrTime[2].length==4||arrTime[2].length==5));
        for(vi=0;bValid&&vi<arrTime.length;vi++){
          for(apij=0;bValid&&apij<arrTime[vi].length;apij++){
            if(vi==2&&apij==2)
              bValid=(".".indexOf(arrTime[vi].charAt(apij))!=-1);
            else
              bValid=("0123456789".indexOf(arrTime[vi].charAt(apij))!=-1);
          }
        }
        break;
      case "CMIVocabulary(Credit)":
        bValid=(strValue=="credit"||strValue=="no-credit");
        break;
      case "CMIVocabulary(Status)":
        bValid=(strValue=="passed"||strValue=="completed"||strValue=="failed"||strValue=="incomplete"||strValue=="browsed"||strValue=="not attempted");
        break;
      case "CMIVocabulary(Entry)":
        bValid=(strValue=="ab-initio"||strValue=="resume"||strValue=="");
        break;
      case "CMIVocabulary(Mode)":
        bValid=(strValue=="normal"||strValue=="review"||strValue=="browse");
        break;
      case "CMIVocabulary(Exit)":
        bValid=(strValue=="time-out"||strValue=="suspend"||strValue=="logout"||strValue=="");
        break;
    }
  }
  return bValid;
}

function LMSInitialize(strParam){
//alert("LMSInitialize("+strParam+")");
  bAPIInit = true;
  return "true";
}

function LMSGetValue(strParam){
//alert("LMSGetValue("+strParam+")");
  if(!bAPIInit){
    strError="301";
//alert("LMSGetValue("+strParam+") !!ERROR 301 - Not initialized!!");
    return "";
  }
  for(gi=0;gi<data.length;gi++){
    if(data[gi].strName==strParam){
      // 201: Invalid argument error
      // 202: Element cannot have children
      // 203: Element cannot have count
      if(!data[gi].bIsReadable){
        strError="404";
//alert("LMSGetValue("+strParam+") !!ERROR 404 - Element is write only!!");
        return "";
      }else{
        strError="0";
//alert("LMSGetValue("+strParam+") returning '"+((data[gi].bIsCommitted)?data[gi].strValue:data[gi].strNewValue)+"'");
        return ((data[gi].bIsCommitted)?data[gi].strValue:data[gi].strNewValue);
      }
    }
  }
  strError="401";
//alert("LMSGetValue("+strParam+") !!ERROR 401 - Not implemented!!");
  return "";
}

function LMSSetValue(strParam,strValue){
//alert("LMSSetValue("+strParam+", "+strValue+")");
  if(!bAPIInit){
    strError="301";
//alert("LMSSetValue("+strParam+", "+strValue+") !!ERROR 301 - Not initialized!!");
    return "false";
  }
  for(si=0;si<data.length;si++){
    if(data[si].strName==strParam){
      // 201: Invalid argument error
      if(data[si].bIsKeyword){
        strError="402";
//alert("LMSSetValue("+strParam+", "+strValue+") !!ERROR 402 - Invalid set value, element is a keyword!!");
        return "false";
      }else if(!data[si].bIsWritable){
        strError="403";
//alert("LMSSetValue("+strParam+", "+strValue+") !!ERROR 403 - Element is read only!!");
        return "false";
      }else if(!data[si].validate(strValue)){
        strError="405";
//alert("LMSSetValue("+strParam+", "+strValue+") !!ERROR 405 - Incorrect data type!!");
        return "false";
      }else{
        strError="0";
        data[si].bIsCommitted=false;
        data[si].strNewValue=strValue;
//alert("LMSSetValue("+strParam+", "+strValue+") success, "+data[si].strName+" = '"+data[si].strNewValue);
        return "true";
      }
    }
  }
  strError="401";
//alert("LMSSetValue("+strParam+", "+strValue+") !!ERROR 401 - Not implemented!!");
  return "false";
}

function LMSCommit(strParam){
//alert("LMSCommit("+strParam+")");
  if(!bAPIInit){
    strError="301";
//alert("LMSCommit("+strParam+") !!ERROR 301 - Not initialized!!");
    return "false";
  }
  if(frames['APIFrame'].document.forms['frmSCORM']){
    form=frames['APIFrame'].document.forms['frmSCORM'];
    for(ci=0;ci<data.length;ci++){
      if(!data[ci].bIsCommitted){
        data[ci].strValue=data[ci].strNewValue;
//alert("Committed "+data[ci].strName+" to new value '"+data[ci].strValue+"'")
        data[ci].bIsCommitted=true;
        if(data[ci].strName=="cmi.core.lesson_status"&&(data[ci].strValue=="completed"||data[ci].strValue=="passed"||data[ci].strValue=="failed"))
          if(parent&&parent.setSCORMComplete)
            parent.setSCORMComplete();
          else if(window.opener&&window.opener.setSCORMComplete)
            window.opener.setSCORMComplete();
      }
      if(data[ci].bIsWritable&&form.elements[data[ci].strName])
//{
//alert("Setting "+data[ci].strName+" form value to '"+data[ci].strValue+"'")
        form.elements[data[ci].strName].value=data[ci].getReturnValue();
//}
    }
//alert("takenid = '"+form.elements['takenid'].value+"'\nlesson_location = '"+form.elements['cmi.core.lesson_location'].value+"'\nlesson_status = '"+form.elements['cmi.core.lesson_status'].value+"'\nscore.raw = '"+form.elements['cmi.core.score.raw'].value+"'\nscore.max = '"+form.elements['cmi.core.score.max'].value+"'\nscore.min = '"+form.elements['cmi.core.score.min'].value+"'\nexit = '"+form.elements['cmi.core.exit'].value+"'\nsession_time = '"+form.elements['cmi.core.session_time'].value+"'\nsuspend_data = '"+form.elements['cmi.suspend_data'].value+"'");
    form.submit();
  }
  strError="0";
  return "true";
}

function LMSFinish(strParam){
//alert("LMSFinish("+strParam+")");
  if(!bAPIInit){
    strError="301";
//alert("LMSFinish("+strParam+") !!ERROR 301 - Not initialized!!");
    return "false";
  }
  LMSCommit("");
  bAPIInit = false;
  return "true";
}

function LMSGetLastError(){
//alert("LMSGetLastError()");
  return strError;
}

function LMSGetErrorString(strErrorNo){
//alert("LMSGetErrorString("+strErrorNo+")");
  if(arguments.length==0)
    strErrorNo=strError;
  switch(strErrorNo){
    case "0":return "No error";break;
    case "101":return "General Exception";break;
    case "201":return "Invalid argument error";break;
    case "202":return "Element cannot have children";break;
    case "203":return "Element cannot have count";break;
    case "301":return "Not initialized";break;
    case "401":return "Not implemented error";break;
    case "402":return "Invalid set value, element is a keyword";break;
    case "403":return "Element is read only";break;
    case "404":return "Element is write only";break;
    case "405":return "Incorrect Data Type";break;
  }
}

function LMSGetDiagnostic(strErrorNo){
//alert("LMSGetDiagnostic("+strErrorNo+")");
  return LMSGetErrorString(strErrorNo);
}