/**************************************************
Trivantis (http://www.trivantis.com)
**************************************************/

function saveVariable(name,value,days) {
  if (days) {
    var date = new Date();
	date.setTime(date.getTime()+(days*24*60*60*1000))
	var expires = "; expires="+date.toGMTString()
  }
  else expires = ""
  document.cookie = name+"="+value+expires+"; path=/"
}

function readVariable(name,defval) {
  var nameEQ = name + "="
  var ca = document.cookie.split(';')
  for(var i=0;i<ca.length;i++) {
    var c = ca[i];
	while (c.charAt(0)==' ') c = c.substring(1)
	if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length)
  }
  return defval
}

function deleteVariable(name) {
  saveVariable(name,"",-1)
}

// Variable Object
function Variable(name,defval,f,cm,frame) {
  this.f=f
  this.eTS=null
  this.tV=null
  this.aiccframe=frame
  this.aiccgroup=null
  this.aicccore=false
  if( cm ) {
    if(name=='CM_Course_ID')this.name='TrivantisCourse'
    else if(name=='CM_Course_Name')this.name='TrivantisCourseName'
    else if(name=='CM_Student_ID')this.name='TrivantisLogin'
    else this.name='TrivantisLoginName'
    this.value=readVariable(this.name,defval)
  }
  else if( frame ) {
    var underPos = name.indexOf('_')
    this.name=name.substring(underPos+1)
    if( frame == 'scorm' ) {
      this.name = this.name.toLowerCase()
      var core_check = this.name.substring(0,5)
      if( core_check == 'core_' ) this.name = this.name.substring(5)
      if(this.name=='core_lesson') this.name='cmi.suspend_data'
      else if(this.name=='core_vendor') this.name='cmi.launch_data'
      else if(this.name=='time') this.name='cmi.core.total_time'
      else if(this.name=='score') this.name='cmi.core.score.raw'
      else this.name = 'cmi.core.' + this.name
      if( this.name=='cmi.core.course_id' ) this.value=defval
      else this.value=LMSGetValue( this.name )
    }
    else if(this.name=='Core_Lesson') {
      this.aiccgroup='[CORE_LESSON]'
      this.value=getParam(this.aiccgroup,this.aiccframe)
    }
    else if(this.name=='Core_Vendor') {
      this.aiccgroup='[CORE_VENDOR]'
      this.value=getParam(this.aiccgroup,this.aiccframe)
    }
    else if(this.name=='Course_ID') {
      this.aiccgroup='[EVALUATION]'
      this.value=getParam(this.name,this.aiccframe)
    }
    else {
      this.aiccgroup='[CORE]'
      this.aicccore=true
      this.value=getParam(this.name,this.aiccframe)
    }
  }
  else if( this.f > 0 ) {
    this.name=name
    this.uDT()
  }
  else {
    this.name=name;
    var val = readVariable(this.name,defval)
    var subval = val.substr( 0, 7 )
    if( subval == "~~f=1~~" ) {
      this.tV = parseInt( val.substr( 7, val.length-7 ))
      this.f = 1
      this.uDTV()
    }
    else if( subval == "~~f=2~~" ) {
      this.tV = parseInt( val.substr( 7, val.length-7 ))
      this.f = 2
      this.uDTV()
    }
    else if( subval == "~~f=4~~" ) {
      var now = new Date()
      this.tV = parseInt( val.substr( 7, val.length-7 ))
      this.eTS = now.getTime() - this.tV
      this.f = 4
      this.uDTV()
    }
    else this.value=val
  }
  if( this.value == null ) this.value = "~~~null~~~"
}

function VarSave() {
  if(this.aiccframe){
    if( this.aiccframe == 'scorm' ) {
      if( this.name == 'cmi.core.total_time' ) LMSSetValue( 'cmi.core.session_time', this.value )
      else LMSSetValue( this.name, this.value )
    }
    else {
      if(this.aicccore) putParam(this.aiccgroup,this.name+'='+this.value,this.aiccframe)
      else putParam(this.aiccgroup,this.value,this.aiccframe)
    }
  }
  else{
    if( this.f != 0 && this.tV >= 0 ) {
      if( this.f == 4 ) saveVariable( this.name, "~~f=4~~" + this.tV )
      else if ( this.f == 2 ) saveVariable( this.name, "~~f=2~~" + this.tV )
      else if ( this.f == 1 ) saveVariable( this.name, "~~f=1~~" + this.tV )
    } 
    else if( this.value == null || this.value == "" ) saveVariable( this.name, "~~~null~~~" )
    else saveVariable(this.name,this.value)
  }
}

function VarSet(setVal) {
  if( setVal == null || setVal == "" ) this.value = "~~~null~~~"
  else this.value=setVal 
  this.save() 
}

function VarSetVar(setVar) {
  if( setVar.f > 0 ) setVar.uDT()
  this.value = setVar.value
  this.f = setVar.f
  this.eTS = setVar.eTS
  this.tV = setVar.tV
  this.save() 
}

function VarAdd(addVal) {
  if ( this.f > 0 && !isNaN( addVal )) {
    this.tV += CalcTD( this.f, addVal )
    this.uDTV()            
  }
  else if( this.value == "~~~null~~~" ) {
    this.f = 0
    if( addVal != null && addVal != "" ) this.value = addVal
  }
  else {
    this.f = 0
    if( addVal != null && addVal != "" ) {
      if(!isNaN(this.value)&&!isNaN(addVal)&&!isNaN( parseFloat(addVal))&&!isNaN( parseFloat(this.value)) ) {
        var val=parseFloat(this.value)+parseFloat(addVal)
        this.value=val.toString()
      }
      else this.value+=addVal;
    }
  }
  this.save()
}

function VarAddVar(addVar) {
  if( addVar.f > 0 ) {
    addVar.uDT()
    if( this.f > 0 ) {
      this.tV += addVar.tV
      if( addVar.f == 1 ) this.f = 1
        this.uDTV()
    }
    else this.add( addVar.value )
  }
  else this.add( addVar.value )
}

function VarSub(subVal) {
  if ( this.f > 0 && !isNaN( subVal )) {
    this.tV -= CalcTD( this.f, subVal )
    this.uDTV()            
  }
  else if( this.value == "~~~null~~~" ) {
    this.f = 0
    if( !isNaN(subVal)&&!isNaN(parseFloat(subVal) ) ) {
      var val=this.value=parseFloat("-"+subVal)
      this.value=val.toString()
    }
  }
  else {
    this.f = 0
    if( subVal != null && subVal != "" ) {
      if(!isNaN(this.value)&&!isNaN(subVal)&&!isNaN( parseFloat(subVal))&&!isNaN( parseFloat(this.value)) ) {
        var val=parseFloat(this.value)-parseFloat(subVal)
        this.value=val.toString()
      }    
      else if( this.value.length >= subVal.length && this.value.substr( this.value.length - subVal.length) == subVal ) {
        this.value=this.value.substr( 0, this.value.length - subVal.length )
      }
    }
  }
  this.save()
}

function VarSubVar(subVar) {
  if( subVar.f > 0 ) {
    subVar.uDT()
    if( this.f > 0 ) {
      this.tV -= subVar.tV
      if( subVar.f == 1 ) this.f = 1
      this.uDTV()
    }
    else this.sub( subVar.value )
  }
  else this.sub( subVar.value )
}

function VarMult(multVal) {
  if( this.value != "~~~null~~~" ) {
    if(!isNaN(this.value)&&!isNaN(multVal)&&!isNaN( parseFloat(multVal))&&!isNaN( parseFloat(this.value)) ) {
      var val=parseFloat(this.value)*parseFloat(multVal)
      this.value=val.toString()
    }
    this.save()
  }
}

function VarDiv(divVal) {
  if( this.value != "~~~null~~~" ) {
    if(!isNaN(this.value)&&!isNaN(divVal)&&!isNaN( parseFloat(divVal))&&!isNaN( parseFloat(this.value)) ) {
      if( parseFloat(divVal) != 0 ) {
        var val=parseFloat(this.value)/parseFloat(divVal)
        this.value=val.toString()
      }
    }
    this.save()
  }
}

function VarCont(strCont) {
  if( this.value == "~~~null~~~" || this.value == "" ) return 0
  var src=this.value.toUpperCase()
  var test=strCont.toUpperCase()
  var result=src.indexOf( test )
  return (result >= 0)
}

function VarLT(strTest) {
  if( this.value == "~~~null~~~" || this.value == "" ) {
    if( strTest == "~~~null~~~" || strTest == "" ) return 0
    else return 1
  }
  if(isNaN(this.value)||isNaN(strTest))return this.value<strTest
  else return parseFloat(this.value)<parseFloat(strTest)
}

function VarGT(strTest) {
  if( this.value == "~~~null~~~" || this.value == "" ) {
    if( strTest == "~~~null~~~" || strTest == "" ) return 1
    else return 0
  }
  if(isNaN(this.value)||isNaN(strTest))return this.value>strTest
  else return parseFloat(this.value)>parseFloat(strTest)
}

function VarUDT() {
  var now = new Date()
  if( this.f == 1 ) {
    this.tV = now.getTime()
    this.value = FormatDS( now )
  }
  else if( this.f == 2 ) {
    this.tV = now.getTime()
    this.value = FormatTS( now )
  }
  else if( this.f == 4 ) {
    var dT = 0
    if( this.eTS == null ) {
      var val = readVariable( this.name, "~~f=4~~0" ) 
      dT = val.substr( 7, val.length - 7 )
      this.eTS = now.getTime() - parseInt( dT )
    }
    this.tV = now.getTime() - this.eTS
    this.value = FormatETS( this.tV )
    if( dT == 0 ) this.save()
  }
}

function VarUDTV() {
  if( this.f == 1 ) this.value = FormatDS( new Date( this.tV ))
  else if( this.f == 2 ) this.value = FormatTS( new Date( this.tV ))
  else if( this.f == 4 ) this.value = FormatETS( this.tV )
}

{ // Setup protpotypes
var p=Variable.prototype
p.save=VarSave
p.set=VarSet
p.add=VarAdd
p.sub=VarSub
p.mult=VarMult
p.div=VarDiv
p.setByVar=VarSetVar
p.addByVar=VarAddVar
p.subByVar=VarSubVar
p.contains=VarCont
p.lessThan=VarLT
p.greaterThan=VarGT
p.uDT=VarUDT
p.uDTV=VarUDTV
}
