// JavaScript Document
<!--
var tokenArray = new Array(0,5,10,50);
var tmaxbonus = 200;
var pos = -1;
var mytoken = 0;
var d = new Date()
d.setDate(d.getDate()+1)

function CheckToken(){
 	ShowToken();
}

function ShowToken(){
	// token1 is today's 
	var bonus_today = checkEmptyNum( parseInt(GetCookie("SCLMAXBONUS")) );
	// tokenp is TOTAL
	var bonus_all = checkEmptyNum( parseInt(GetCookie("SCLBONUS")) );
//	var rice1 = checkEmptyNum( parseInt(GetCookie("RICE")) );

	document.getElementById('perviousToken').innerHTML = "<FONT size='-1' class='b5font9Black'><b>" + bonus_all + "</b></font>";
	if( bonus_today == tmaxbonus){
		document.getElementById('t1').innerHTML = "<FONT size='-1' class='b5font9Black'><b>" + bonus_today + " </b></font>";
		document.getElementById('t1d').innerHTML = "<FONT size='-1' class='b5font9Black'><b>已到達今日積分上限</b></font>";
	}else{
		document.getElementById('t1').innerHTML = "<FONT size='-1' class='b5font9Black'><b>" + bonus_today + "</b></font>";
	}
	
	document.getElementById('t2').innerHTML = "<FONT size='-1' class='b5font9Black'><b></b></font>";
}



function AddToken(tokenNum, tokenName){
	//test();
	if (parseInt(tokenNum) < 0 ) { tokenNum = 0 }
	var roleId = parseInt(GetCookie("SCLUSERROLEID"));	
//if(parseInt(GetCookie("SCLMAXBONUS"))< (tmaxbonus -10) ) 
//{
	// This tokenName determine if its trying to continously adding bonus points by multiple mouse clicks
	if (tokenName != GetCookie("TSTATUS"))	{
		// This prevent teacher from getting bonus point while in student section.
		if ( roleId == 2 ) { 
			tokenNum = 0;
			var serviceCode = GetCookie("SCLSERVICECODE");

			//alert(GetCookie("TSTATUS").indexOf("PT"));
			// Only add bonus if the teacher is in Teacher section
			if (GetCookie("TSTATUS").indexOf("PT")>=0 || GetCookie("TSTATUS").indexOf("ST")>=0 ) {
				if ( tokenName == "" ) {
					
		
					if ( serviceCode.indexOf("JSCL") >= 0 ) {
						//	alert("serviceCode related to JSCL");	
						pos = posInArray ( PT_eInfo, GetCookie("TSTATUS") );		
						if (pos >= 0 ) {
							tokenNum = parseInt(PT_ePoint[pos][4]);
						} else {
							//	alert("serviceCode related to SSCL");
							pos = posInArray ( ST_eInfo, GetCookie("TSTATUS") );
				
							if (pos >= 0 ) {
								tokenNum = parseInt(ST_ePoint[pos][4]);
							} 
						} 		
					} else {
							//	alert("serviceCode related to SSCL");
						pos = posInArray ( ST_eInfo, GetCookie("TSTATUS") );
				
						if (pos >= 0 ) {
							tokenNum = parseInt(ST_ePoint[pos][4]);
						} 
					}	
				} else {
					tokenNum = 0;
				}				
				// End of Experience
				
			}
			
		} else {  
			// Else in student section
			if (tokenName == "" ) {
				setExperience(GetCookie("TSTATUS"));
				tokenNum = mytoken;
				//alert ("Name: " + GetCookie("TSTATUS") + ", BONUS: " + tokenNum + ", POS: " + pos);
			} else if (tokenNum > 0) {
				setExperience(tokenName);
				tokenNum = mytoken;					
			} else {
				tokenNum = 0;
			}			
		}

//alert ("Name: " + GetCookie("TSTATUS") + ", BONUS: " + tokenNum + ", POS: " + pos);

		var bonus_today = checkEmptyNum( parseInt(GetCookie("SCLMAXBONUS")) );
		var bonus_diff = checkEmptyNum( parseInt(GetCookie("BONUSDIFF")) );
		var bonus_all = checkEmptyNum( parseInt(GetCookie("SCLBONUS")) );
		var bonus_tmp = bonus_today + tokenNum;
		// If today's bonus total is larger then the day's limit, then cat it to the limit
		if (bonus_tmp > tmaxbonus) {
			bonus_today = tmaxbonus;
			//bonus_diff = tmaxbonus;
		} else {
			bonus_today = bonus_tmp;
			// only add bonus if not yet reach today's limit
			bonus_diff = bonus_diff + tokenNum;
			//SetCookie("SCLBONUS", tokenNum+bonus_all , d, "/", null, null);
		}		
		SetCookie("SCLMAXBONUS", bonus_today, d, "/", null, null);
		SetCookie("BONUSDIFF", bonus_diff, d, "/", null, null);


		if (tokenName != "RICE") {
			if (tokenName != "") {
				SetCookie("TSTATUS", tokenName, d, "/", null, null);
			}
			AddRice(tokenNum, "AddTOKEN");
		}
	}
	//alert ("Name: " + GetCookie("TSTATUS") + ", BONUS: " + tokenNum + ", POS: " + pos);
//}
}




// This Calculates Rice and Today's Bonus differences
function AddRice(tokenNum, tokenName){
	var btxns1 = "";
//	var rice1 = parseInt(GetCookie("RICE"));
	var roleId = parseInt(GetCookie("SCLUSERROLEID"));
	
	// This prevent teacher from getting bonus point while in student section.
	if ( roleId == 2 ) { 
		// if not in teacher section, but in student section, then set tokenNum to 0
		if (GetCookie("TSTATUS").indexOf("PS")>=0 || GetCookie("TSTATUS").indexOf("SS")>=0 ) {
//			rice1 = 0;
			tokenNum = 0;
		}
	}
	
//	if (isNaN(rice1) || (rice1 == "")) { rice1 = 0 }	
	if ( tokenNum < 0 ) { tokenNum = 0 }
	
	var bonus1 = parseInt(GetCookie("TBONUS")) + tokenNum;
	if (bonus1 > tmaxbonus){
		bonus1 = tmaxbonus;
//		rice1 += tmaxbonus - parseInt(GetCookie("TBONUS"));
		btxns1 = GetCookie("TSTATUS") + "," + (tmaxbonus - parseInt(GetCookie("TBONUS")));
	} else {
//		rice1 += tokenNum;
		btxns1 = GetCookie("TSTATUS") + "," + tokenNum;
	}
	
//	SetCookie("RICE", rice1, d, "/", null, null);
	if ((tokenNum > 0) && ( (tmaxbonus - parseInt(GetCookie("TBONUS"))) > 0) ){
		SetCookie("RSTATUS", "1", d, "/", null, null);
		if (GetCookie("BTXNS") != "") {
			btxns1 = GetCookie("BTXNS") + "," + btxns1;
		}
		SetCookie("BTXNS", btxns1, d, "/", null, null);
	}
	SetCookie("TBONUS", bonus1, d, "/", null, null);
	
	// Below is RICE
/*	if (tokenName != "AddTOKEN"){
		AddToken(tokenNum, "RICE");
	}
*/
//alert("ffff");
}

function SetToken() {
	var bonus_today = checkEmptyNum (parseInt(GetCookie("SCLMAXBONUS")) );
	var bonus_all = checkEmptyNum (parseInt(GetCookie("SCLBONUS")) );
	//var rice1 = checkEmptyNum ( parseInt(GetCookie("RICE")) );
	document.getElementById("token1").value = bonus_today;
	document.getElementById("bonustxn").value = GetCookie("BTXNS");
	document.getElementById("tokenForm").submit();
}


function updateTokens() {
	var bonus_today = checkEmptyNum (parseInt(GetCookie("SCLMAXBONUS")) );
	var bonus_all = checkEmptyNum (parseInt(GetCookie("SCLBONUS")) );

	document.getElementById("upd_todaybonus").value = bonus_today;
	document.getElementById("upd_btxns").value = GetCookie("BTXNS");
	SetCookie("BTXNS", "", null, "/", null, null);
	
	tmp_bonus = parseInt(GetCookie("SCLBONUS")) + parseInt(GetCookie("BONUSDIFF"))
	SetCookie("SCLBONUS", tmp_bonus , d, "/", null, null);
	SetCookie("BONUSDIFF", "0" , null, "/", null, null);

	document.getElementById("updateTokensForm").submit();
}


function setExperience(tokenName) {
	var expStr = GetCookie("SCLEXPERIENCE");
	var serviceCode = GetCookie("SCLSERVICECODE");
	//alert ("[BEFORE] Exp: " + expStr);
	var exp1 = 0;
	var exp2 = 0;
	var exp3 = 0;
	var exp4 = 0;		

	var expArray = expStr.split(",");

	if (expArray.length >= 4) {
		exp1 = parseInt(expArray[0]);
		exp2 = parseInt(expArray[1]);
		exp3 = parseInt(expArray[2]);
		exp4 = parseInt(expArray[3]);
		
	} else {
		exp1 = 0;
		exp2 = 0;
		exp3 = 0;
		exp4 = 0;
	}
//	alert(serviceCode);
	//alert ("[Before] Exp: " + exp1 + "," + exp2 + "," + exp3 + "," + exp4 );
	//alert ("TSTATUS: " +  tokenName);
	if ( serviceCode.indexOf("JSCL") >= 0 ) {
		//alert("JSCL");
		pos = posInArray ( PS_eInfo, tokenName );
		
		if (pos >= 0 ) {
			mytoken = parseInt(PS_ePoint[pos][4]);
			exp1 += parseInt(PS_ePoint[pos][0]);
			exp2 += parseInt(PS_ePoint[pos][1]);
			exp3 += parseInt(PS_ePoint[pos][2]);
			exp4 += parseInt(PS_ePoint[pos][3]);	
		} 
		
	} else {
		//alert("SSCL");
		pos = posInArray ( SS_eInfo, tokenName );
		
		if (pos >= 0 ) {
			mytoken = parseInt(SS_ePoint[pos][4]);
//alert('SSCL '+ pos  + ', ' + mytoken);
			exp1 += parseInt(SS_ePoint[pos][0]);
			exp2 += parseInt(SS_ePoint[pos][1]);
			exp3 += parseInt(SS_ePoint[pos][2]);
			exp4 += parseInt(SS_ePoint[pos][3]);	

		} 
	}	
	
	expStr = exp1 + "," + exp2 + "," + exp3 + "," + exp4;

	SetCookie("SCLEXPERIENCE", expStr, d, "/ilmscl/", null, null);
	
}





function GetCookie(name) {
  var search = name + "=";
  if (document.cookie.length > 0) { // if there are any cookies
      offset = document.cookie.indexOf(search);
      if (offset != -1) { // if cookie exists 
         offset += search.length ;
         // set index of beginning of value
         end = document.cookie.indexOf(";", offset) ;
         // set index of end of cookie value
         if (end == -1) 
            end = document.cookie.length;
         return unescape(document.cookie.substring(offset, end));
      } 
   }else {
	  return '';
   }
   return '';
}



function SetCookie (name, value, expires, path, domain, secure) {
	path = "/ilmscl/";
	//expires.toGMTString() 
//expires = new Date() ;
//expire.setTime(today.getTime() + 3600000*24*nDays);
	expires = null;
  	document.cookie = name + "=" + escape(value) +
         ((expires) ? "; expires=" + expires.toGMTString() : "") +
         ((path) ? "; path=" + path : "") +
         ((domain) ? "; domain=" + domain : "") +
         ((secure) ? "; secure" : "");
//alert("Name =" +name + " : " + value);
}


function checkEmptyNum (num){
	if (isNaN(num) || (num == "")) { num = 0 }
	return num;
}



// Handle all the the FSCommand messages in a Flash movie
var tTotal, tMarks;

function FlashGame_DoFSCommand(command, args) {	
	if (command == "setTotal"){
		tTotal = parseInt(args);
	}
	
	if (command == "setMarks"){
		tMarks = parseInt(args);
		//if ((tMarks/tTotal) > 0.5) {
		//	AddRice(2,"");
		//}
	}
}


function trim(str) {
  var i = 0, j = 0;
  while ( str.charAt(i)==" " ) i++;
  j = str.length-1;
  while ( str.charAt(j)==" " ) j--;
  return str.substring(i,j+1);
} 


// Hook for Internet Explorer
if (navigator.appName && navigator.appName.indexOf("Microsoft") != -1 && navigator.userAgent.indexOf("Windows") != -1 && navigator.userAgent.indexOf("Windows 3.1") == -1) {
	document.write('<SCRIPT LANGUAGE=VBScript\> \n');
	document.write('on error resume next \n');
	document.write('Sub FlashGame_FSCommand(ByVal command, ByVal args)\n');
	document.write('call FlashGame_DoFSCommand(command, args)\n');
	document.write('end sub\n');
	document.write('</SCRIPT\> \n');
}


function posInArray(v, s) {
	//alert(v);
	var i, dummyStr;
	for(i=0; i<= v.length-1; i++) {
		dummyStr = v[i][2];
		if ( s.toUpperCase() == dummyStr.toUpperCase() ) {
			return i;
		}
	}
	return -1;
}


//-----------------------------------------------------------------------


document.write('<script language="JavaScript" src="/ilmscl/inc/bonus_stud.js"></script>');
document.write('<script language="JavaScript" src="/ilmscl/inc/bonus_teach.js"></script>');


//-->