/**************************************************
Trivantis (http://www.trivantis.com)

This JavaScript code uses techniques found at
The Dynamic Duo - http://www.dansteinman.com/dynduo/
**************************************************/

function saveCookie(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 readCookie(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 deleteCookie(name) {
	saveCookie(name,"",-1)
}

// Variable Object
function Variable(name,defval,f,cm,aiccframe) {
	this.f=f
    this.eTS=null
    this.tV=null
    this.aiccframe=aiccframe
    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=readCookie(this.name,defval)
    }
    else if( aiccframe ) {
        var underPos = name.indexOf('_')
        this.name=name.substring(underPos+1)
        
        if( aiccframe == '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
          
          this.value = LMSGetValue( this.name )
        }
        else if(this.name=='Core_Lesson') {
            this.aiccgroup='[CORE_LESSON]'
            this.value=getParam(this.aiccgroup,aiccframe)
        }
        else if(this.name=='Core_Vendor') {
            this.aiccgroup='[CORE_VENDOR]'
            this.value=getParam(this.aiccgroup,aiccframe)
        }
        else {
            this.aiccgroup='[CORE]'
            this.aicccore=true
            this.value=getParam(this.name,aiccframe)
        }
    }
    else if( this.f > 0 ) {
        this.name=name
        this.uDT()
    }
    else {
        this.name=name;
        
        var val = readCookie(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 ) saveCookie( this.name, "~~f=4~~" + this.tV )
          else if ( this.f == 2 ) saveCookie( this.name, "~~f=2~~" + this.tV )
          else if ( this.f == 1 ) saveCookie( this.name, "~~f=1~~" + this.tV )
        } 
        else if( this.value == null || this.value == "" ) saveCookie( this.name, "~~~null~~~" )
        else saveCookie(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 = readCookie( 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 )
    }
}

Variable.prototype.save=VarSave
Variable.prototype.set=VarSet
Variable.prototype.add=VarAdd
Variable.prototype.sub=VarSub
Variable.prototype.mult=VarMult
Variable.prototype.div=VarDiv
Variable.prototype.setByVar=VarSetVar
Variable.prototype.addByVar=VarAddVar
Variable.prototype.subByVar=VarSubVar
Variable.prototype.contains=VarCont
Variable.prototype.lessThan=VarLT
Variable.prototype.greaterThan=VarGT
Variable.prototype.uDT=VarUDT
Variable.prototype.uDTV=VarUDTV

//functions for realtime date/time
function FormatDS( now ) {
    var monthList = new Array('January','February','March','April','May','June','July','August','September','October','November','December')
    var year = now.getYear()
    if( year < 1900 ) year += 1900
    return monthList[now.getMonth()]+' '+now.getDate()+', '+year
}
function FormatTS( now ) {
    var newmin = now.getMinutes()
    if (newmin<10) newmin = "0"+newmin
    var hour = now.getHours()
    var ampm = 'AM'
    if (hour>12) {
        hour-=12
        ampm = 'PM'
    }
    else if (hour==0) {
            hour = 12
    }
    if (hour<10) hour = ' '+hour
    return hour+':'+newmin+' '+ampm
}
function FormatETS( eT ) {
    var mills = eT % 1000
    eT -= mills
    eT /= 1000
    var secs = eT % 60
    eT -= secs
    eT /= 60
    var mins = eT % 60
    eT -= mins
    eT /= 60
    var hours = eT
    if( hours < 10 ) hours = "0" + hours
    if( mins < 10 ) mins = "0" + mins
    if( secs < 10 ) secs = "0" + secs
    return hours + ':' + mins + ':' + secs
}

function CalcTD( f, val ) {
    var tV = 0
    if( f == 1 )
        tV += 24 * 60 * 60 * 1000 * val
    else if( f == 2 )
        tV += 60 * 1000 * val
    else if( f == 4 )
        tV += 1000 * val
    return tV
}
