/**************************************************
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,cm,aiccframe) {
    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()
          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 {
        this.name=name;
        this.value=readCookie(this.name,defval)
    }
    
    if( this.value == null ) this.value = "~~~null~~~"
}
function VariableSave() {
    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.value == null || this.value == "" ) saveCookie( this.name, "~~~null~~~" )
        else saveCookie(this.name,this.value)
    }
}
function VariableSet(setVal) {
    if( setVal == null || setVal == "" ) this.value = "~~~null~~~"
    else this.value=setVal 
    this.save() 
}
function VariableAdd(addVal) {
    if( this.value == "~~~null~~~" ) {
        if( addVal != null && addVal != "" ) this.value = addVal
    }
    else {
        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 VariableSubtract(subVal) {
    if( this.value == "~~~null~~~" ) {
        if( !isNaN(subVal)&&!isNaN(parseFloat(subVal) ) ) {
            var val=this.value=parseFloat("-"+subVal)
            this.value=val.toString()
        }
    }
    else {
        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 VariableMultiply(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 VariableDivide(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 VariableContains(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 VariableLessThan(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 VariableGreaterThan(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)
}
Variable.prototype.save=VariableSave
Variable.prototype.set=VariableSet
Variable.prototype.add=VariableAdd
Variable.prototype.sub=VariableSubtract
Variable.prototype.mult=VariableMultiply
Variable.prototype.div=VariableDivide
Variable.prototype.contains=VariableContains
Variable.prototype.lessThan=VariableLessThan
Variable.prototype.greaterThan=VariableGreaterThan

//functions for realtime date/time
function GetCurrDateStr() {
    var monthList = new Array('January','February','March','April','May','June','July','August','September','October','November','December')
    now = new Date()
    var year = now.getYear()
    if( year < 1900 ) year += 1900
    return monthList[now.getMonth()]+' '+now.getDate()+', '+year
}
function GetCurrTimeStr() {
    var now = new Date()
    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
}
