// ###########################################################################
// #							<< NOTICE >>								 #
// # THIS PROGRAM CODE BELONGS TO SOUTHERN CROSS VIRTUAL CO., LTD. IT IS	 #
// # CONSIDERED A TRADE SECRET AND IS NOT TO BE DIVULUGED OR USED BY PARTIES #
// # WHO HAVE NOT RECEIVED WRITTEN AUTHORIZATION FROM THE OWNER.			 #
// # ----------------------------------------------------------------------- #
// #							<< 通  知 >>								 #
// # 本プログラムはSOUTHERN CROSS VIRTUAL CO., LTD.の財産です。本プログラム  #
// # はトレード・シークレットとみなされ、当該権利者の書面による許可なくして、#
// # 漏洩または使用を禁じます。 											 #
// ###########################################################################
// (C) 2001 Southern Cross Virtual Co., Ltd. All Rights Reserved.
@set @DEBUG = 0	// Program Debug Flag: ON(=1)/OFF(=0)
var LessonMax = 47;
var LastTopicNo;
function makeDayOfWeek(){
	var i = 0;
	this[i++] = "Sunday";		// 0
	this[i++] = "Monday";		// 1
	this[i++] = "Tuesday";		// 2
	this[i++] = "Wednesday";	// 3
	this[i++] = "Thursday";		// 4
	this[i++] = "Friday";		// 5
	this[i++] = "Saturday";		// 6
	this[i++] = "Once a Week";	// 7
	this[i	] = null;
	this.length = i;
}
function makeMonthNumber(){
	var i = 0;
	this[i++] = 31; // Jan
	this[i++] = 28; // Feb: to be modified by program
	this[i++] = 31; // Mar
	this[i++] = 30; // Apr
	this[i++] = 31; // May
	this[i++] = 30; // Jun
	this[i++] = 31; // Jul
	this[i++] = 31; // Aug
	this[i++] = 30; // Sep
	this[i++] = 31; // Oct
	this[i++] = 30; // Nov
	this[i++] = 31; // Dec
	this[i	] = null;
	this.length = i;
}

function makeDDStr(num){
	for (var i=0; i<num; i++){
		this[i] = "";
	}
	this[num] = null;
	this.length = num;
}

var DayOfWeek = new makeDayOfWeek();
var strDueDD  = new makeDDStr(LessonMax);
var NumMonth  = new makeMonthNumber();
var gSw;

function validDate(dStr){
	// expeced format: yyyy/mm/dd
	if (dStr == ""	  ) return false;
	if (dStr.length != 10) return false;
	if (dStr.substr( 4, 1 ) != "/") return false;
	if (dStr.substr( 7, 1 ) != "/") return false;
	if (isNaN(parseInt(dStr.substr( 0, 4 ), 10))) return false;	// year is not digit
	if (isNaN(parseInt(dStr.substr( 5, 2 ), 10))) return false;	// month is not digit
	if (isNaN(parseInt(dStr.substr( 8, 2 ), 10))) return false;	// date is not digit
	var d = new Date();
	var tYear  = d.getFullYear();
	var iYear  = (parseInt(dStr.substr( 0, 4 ), 10));
	var iMonth = (parseInt(dStr.substr( 5, 2 ), 10));
	var iDay   = (parseInt(dStr.substr( 8, 2 ), 10));
	if (iYear < tYear) return false;
	if ((1 > iMonth) || (iMonth > 12)) return false;
	if ((1 > iDay)	 || (iDay > NumMonth[iMonth-1])) return false;
	return true; // valid date string
}
function getDueDate(curDate, curDisp){
// parm1: curDate:String;  format yyyy/mm/dd
// parm2: curDisp:Integer; num of displacement

	var newDateStr = "";
	var strYYYY = curDate.substr( 0, 4 );	// YYYY
	var strMM	= curDate.substr( 5, 2 );	// MM
	var strDD	= curDate.substr( 8, 2 );	// DD
	var intYYYY = parseInt(strYYYY, 10);
	var intMM	= parseInt(strMM, 10);
	var intDD	= parseInt(strDD, 10);
	var mx		= intMM - 1;
	var newDate = intDD + parseInt(curDisp, 10);
	if (newDate > NumMonth[mx]){
		newDate = newDate - NumMonth[mx];
		intMM ++;
		if (intMM > 12){
			intMM = 1;
			intYYYY ++;
			strYYYY = intYYYY.toString();
		}
		strMM = (intMM < 10) ? ("0" + intMM.toString()) : intMM.toString();
	}
	strDD = (newDate < 10) ? ("0" + newDate.toString()) : newDate.toString();
	newDateStr = strYYYY + "/" + strMM + "/" + strDD;	// "yyyy/mm/dd"
	return newDateStr;
}
function getTargetDay(dStr){
	var numYear  = parseInt(dStr.substr(0,4), 10);
	var numMonth = parseInt(dStr.substr(5,2), 10) - 1;
	var numDate  = parseInt(dStr.substr(8,2), 10);
	var d = new Date();
	d.setFullYear(numYear, numMonth, numDate);
	return(d.getDay());
}
function getNearestDay(dStr, day){
	if ((0 > day) || (day > 6)) return(dStr);
	var i, j, k;
	var cDay;
	var strYear;
	var strMonth;
	var strDate;
	var newDate;
	var numYear  = parseInt(dStr.substr(0,4), 10);
	var numMonth = parseInt(dStr.substr(5,2), 10) - 1;
	var numDate  = parseInt(dStr.substr(8,2), 10);
	var d = new Date();
	newDate = numDate + 1;
	do{
		d.setFullYear(numYear, numMonth, newDate);
		newDate ++;
	}while(day != d.getDay());
	with(d){
		strYear  = getFullYear();
		strMonth = getMonth() + 1;
		strDate  = getDate();
	}
	strMonth = (strMonth < 10) ? ("0" + strMonth) : strMonth;
	strDate  = (strDate  < 10) ? ("0" + strDate)  : strDate;
	var s = strYear + "/" + strMonth + "/" + strDate;
	return s;
}
function getToday(){
	var d = new Date();
	with(d){
		var tmpYear  = getFullYear();
		var tmpMonth = getMonth() + 1;
		var tmpDD	 = getDate();
	}
	tmpMonth = (tmpMonth < 10) ? ("0" + tmpMonth) : tmpMonth;
	tmpDD	 = (tmpDD	 < 10) ? ("0" + tmpDD)	  : tmpDD;
	return(tmpYear + "/" + tmpMonth + "/" + tmpDD);
}
function initSchedule(StartDate, TargetDay, StudentName, sp, ep, sw, nt){
	// StartDate: Lesson Start Date (yyyy/mm/dd)
	// TargetDay: (0-6: Sun-Sat)
	// StudentName; Up to 20 letters
	// sp: Start Lesson chapter to be modified
	// ep: End Lesson chapter to be modified
	// sw: Initial Control flag (=1:Auto Initialize, !=1:Manual Initialize)
	// nt: Student Name Type (=1:English, =2:Japanese)

	var ua = window.navigator;
	var d		= new Date();
	var TheYear = parseInt(d.getFullYear(), 10);
	var ToDay	= StartDate;
	var n		= strDueDD.length;
	strDueDD[0] = DayOfWeek[TargetDay];
	var wrtStr	= "";
	var strID, strIM, stepMark;
	var i, x, v;
	var tmpWork;
	var TargetFlag = false;
	var dateFlag = false;

	if (((F.oBody.connectionType == "offline") ||
		(F.oBody.connectionType == "lan")) && (sw == 1) && (StudentName == "")) sw = 8;

	gSw = sw;
	if ((((TheYear % 4 == 0) && (TheYear % 100 != 0)) || (TheYear % 400 == 0))){
		 NumMonth[1] = 29;	// leap year
	}
	if (sw == 9){
		setCookieValue( coSTUDENT_NAME, "dummy", yesterday );
		updateCookieNameValue( StudentName, yesterday );
		updateCookieTopicValue( StudentName, "0", yesterday );
		window.alert("Cookie: Student Name has been removed by the program.");
		location.reload();
	} else if (sw == 8){
		var cStudentName = getCurrentStudentName();	// Cookie
		if (cStudentName != null){
			StudentName = getStudentName(cStudentName, nt);
			ToDay 		= getStudentStartDate(StudentName);
			TargetDay 	= getStudentDay(StudentName, 2); // Numeric Day
			var cStudentDate = getCookieValue( coSTUDENT_DATE );
			if (ToDay < cStudentDate){
				ToDay = (cStudentDate != null) ? cStudentDate : getToday();
				TargetDay = getTargetDay(ToDay);
			}
		} else {
			StudentName = window.prompt("(C)Please specify your name:", "Guest Student");
			setCookieValue( coSTUDENT_NAME, StudentName, twoWeeks );
			LastTopicNo = getLastTopicNO(StudentName);
		}
	}
	if ((sw == 1) &&
		(F.oBody.connectionType != "offline") &&
		(F.oBody.connectionType != "lan") &&
		(StartDate == "") &&
		(StudentName == "")){
		ToDay = "";
		if (!ua.cookieEnabled){
			window.alert("This page requires Cookie function.\nCurrently your browser Cookie is Off.");
			StudentName = window.prompt("(D)Please specify your name:", "Guest Student");
		} else {
			var cStudentName = getCurrentStudentName();
			if (cStudentName != null){
				StudentName = cStudentName;
				ToDay = getStudentStartDate(StudentName);
				TargetDay = getStudentDay(StudentName, 2); // Numeric Day
				var cStudentDate = getCookieValue( coSTUDENT_DATE );
				if (ToDay < cStudentDate){
					ToDay = (cStudentDate != null) ? cStudentDate : getToday();
					TargetDay = getTargetDay(ToDay);
				}
				dateFlag = true;
			} else {
				StudentName = window.prompt("(E)Please specify your name:", "Guest Student");
				setCookieValue( coSTUDENT_NAME, StudentName, twoWeeks );
				LastTopicNo = getLastTopicNO(StudentName);
			}
		}
		if (StudentName == "") StudentName = "UnDefined Student";
		StudentName = getStudentName(StudentName, nt);
		if ((StudentName != "UnDefined Student") && (StudentName != "Guest Student")){
			if (!dateFlag){
				ToDay = getStudentStartDate(StudentName);
				TargetDay = getStudentDay(StudentName, 2); // Numeric Day
			}
			if ((0 <= TargetDay) && (TargetDay < 7)){
				TargetFlag = true;
				if (ToDay == ""){
					ToDay = getNearestDay(getToday(), TargetDay);
					ToDay = getDueDate(ToDay, 7);
				}
			}
		}
	}
	v  = 0;
	if ((ToDay == "") && (StudentName == "")){
		ToDay = getToday();
		if (!TargetFlag) TargetDay = d.getDay();	// 0-6: Sun - Sat
		v = 7;
//	} else if ((ToDay == "") && (StudentName != "")){	// Student Name Only
	} else if (StudentName != ""){	// Student Name Only
		if (TargetDay == 7){
			var tmpTargetDay = getStudentDay(StudentName, 2);
			TargetDay = (tmpTargetDay != TargetDay) ? tmpTargetDay : TargetDay;
		}
		tmpWork = getStudentName(StudentName, nt);
		if (tmpWork != StudentName) StudentName = tmpWork;
		ToDay = getStudentStartDate(StudentName);
		if (ToDay == getToday()) ToDay = getDueDate(ToDay, 7);
		v = 0;
	}
	sp = ((0 > sp) || (sp > LessonMax)) ? 1 : sp;
	ep = ((0 > ep) || (ep > LessonMax)) ? LessonMax : ep;
	for (i=1; i<=n; i++){
		if ((sp > i) || (i > ep)) continue;
		strDueDD[i] = getDueDate(ToDay, v);
		ToDay = strDueDD[i];
		v = 7;
	}
	for (i=1; i<=n; i++){
		if ((sp > i) || (i > ep)) continue;
		stepMark = (i <= LastTopicNo) ? "■ " : "□ ";
		strIM = "M" + itoStr(2, i);
		strID = "L" + itoStr(2, i);
		x = F[ strIM ];
		if (x != null){
			x.innerText = "";
		}
		x = F[ strID ];
		if (x != null){
//			 wrtStr = "[Due By " + strDueDD[i].substr(2,8) + "]: Topic " + itoStr(2, i) + " ";
			 wrtStr = stepMark + "[Due By " + strDueDD[i].substr(2,8) + "]: Topic " + itoStr(2, i) + " ";
			 x.innerText = wrtStr;
		}
	}
	if (StudentName == "") StudentName = getStudentName(StudentName, nt);

	x = F[ "STN" ];
//	if (x != null) x.innerText = "- Reading Schedule for " + StudentName + " (" + TheYear + ":" + strDueDD[0] + ") -";
	if (x != null) x.string = "- Reading Schedule for " + StudentName + " (" + TheYear + ":" + DayOfWeek[TargetDay] + ") -";
}
function calcSchedule(nt){
	var ua = window.navigator;
	var tmpWork;
	var StudentName;
	var NewDateStr;
	var TargetDay;
	var sp;
	var ep;
	var iSp;
	var iEp;
	var iTp;
	var validFalg = false;
	var strMsg;
	var ToDay;
	var dateFlag1 = false;
	var dateFlag2 = false;
	var d = new Date();
	var oOL = F[ OLIST ];
	if (oOL != null){
		@if ( @DEBUG == 1 )
			alert("oOL.children.length=" + oOL.children.length);
		@end
		LessonMax = oOL.children.length;
	}

	do {
		StudentName = window.prompt("(F)Specify Student First Name(new student available):", "");
		if (StudentName == null) return;
		if (StudentName == "") window.alert("You can't skip this field.");
	}while(StudentName == "");
	if (!ua.cookieEnabled){
		window.alert("This page requires Cookie function.\nCurrently your browser Cookie is Off.");
	} else {
		if (((F.oBody.connectionType != "offline") &&
			(F.oBody.connectionType != "lan")) || (gSw == 8 || gSw == 1)){	// 8:Local Mode only
			setCookieValue( coSTUDENT_NAME, StudentName, twoWeeks );
		}
	}
	ToDay = getToday();
	ToDay = getDueDate(ToDay, REPEAT);
	tmpWork = getStudentName(StudentName, 1);	// Get Registered Student Name
	if ((tmpWork != "Japanese Beginners") && (tmpWork != StudentName)){
		NewDateStr = getStudentStartDate(tmpWork);
		if (NewDateStr != getToday()) dateFlag2 = true;
		if (NewDateStr == getToday()) NewDateStr = getDueDate(NewDateStr, 7);
		dateFlag1 = true;
	} else {	// New Student
		do {
			NewDateStr = window.prompt("(G)Date: Enter new Starting Date as [yyyy/mm/dd]:", ToDay);
			if (NewDateStr == null) return;
			if (!(validFlag = validDate(NewDateStr))) window.alert("'" + NewDateStr + "' is invalid data.\nSpecify valid date format as yyyy/mm/dd.");
		}while(!validFlag);
//		NewDateStr = getDueDate(NewDateStr, REPEAT);	// optional
		setCookieValue( coSTUDENT_DATE, NewDateStr, twoWeeks );
	}
	if (tmpWork != StudentName){
		sp	= window.prompt("(H)Enter modifying Start Lesson No[1-" + (LessonMax) + "]:","1");
		if (sp == null) return;
		ep	= window.prompt("(I)Enter modifying End   Lesson No[1-" + (LessonMax) + "]:",(LessonMax));
		if (ep == null) return;
		iSp = parseInt(sp, 10);
		iEp = parseInt(ep, 10);
		iSp = ((1 > iSp) || (iSp > (LessonMax))) ? 1 : iSp;
		iEp = ((1 > iEp) || (iEp > (LessonMax))) ? (LessonMax) : iEp;
		if (iSp > iEp){
			iTp = iSp;
			iSp = iEp;
			iEp = iTp;
			window.alert("Start-End Lesson No has been Swapped.");
		}
		var dispDay = getStudentStartDate(StudentName);
		do {
			NewDateStr = window.prompt("(J)Date: Enter new Starting Date as [yyyy/mm/dd]:", dispDay);
			if (NewDateStr == null) return;
			if (!(validFlag = validDate(NewDateStr))) window.alert("'" + NewDateStr + "' is invalid data.\nSpecify valid date format as yyyy/mm/dd.");
		}while(!validFlag);
		setCookieValue( coSTUDENT_DATE, NewDateStr, twoWeeks );
	} else {
		iSp = 1;
		iEp = LessonMax;
	}
	if (((tmpWork != StudentName) && (dateFlag2 == false)) || (gSw == 8 || gSw == 1)){
		StudentName = tmpWork;	// Replace with registered name
		TargetDay	= getTargetDay(NewDateStr); // get new numeric lesson day
		tmpWork 	= getStudentDay(StudentName, 2);
		if ((TargetDay != tmpWork) && (tmpWork != 7)){
			strMsg	= "Your specified new lesson day is '" + DayOfWeek[TargetDay] + "', but\n";
			strMsg += "Current " + StudentName + "'s lesson day is '" + DayOfWeek[tmpWork] + "'\n";
			strMsg += "Click [OK] to continue, or [CANCEL] to the nearest '" + DayOfWeek[tmpWork] + "'";
			if (!window.confirm(strMsg)){
				NewDateStr = getNearestDay(NewDateStr, tmpWork);
				TargetDay = tmpWork;
				setCookieValue( coSTUDENT_DATE, NewDateStr, twoWeeks );
			}
		}
	} else {	// New Student
		TargetDay = getTargetDay(NewDateStr); // get numeric lesson day
	}

	LastTopicNo = getLastTopicNO(StudentName);

	strMsg	= "Sudent Name:\t" + StudentName + "\n";
	strMsg += "Start Date :\t" + NewDateStr + "\n";
	strMsg += "Lesson Day :\t" + DayOfWeek[TargetDay] + "\n";
	strMsg += "Modify No  :\t" + iSp + "-" + iEp + "\n\n";
	strMsg += "Click [OK] to continue, or [CANCEL] to stop.";

	if (window.confirm(strMsg)){
		initSchedule(NewDateStr, TargetDay, StudentName, iSp, iEp, 2, nt);
		headRotate("vAttr");
	} else {
		if (window.confirm("Do you want to remove current Cookie Data?")){
			initSchedule(NewDateStr, TargetDay, StudentName, iSp, iEp, 9, nt);
		}
	}
}
function headRotate(sID){
	var x = F[sID];
	if (x != null){
   		var d = new Date();
   		with(d){
   			var s = (getSeconds() * 6);
   			var m = (getMilliseconds() % 100);
   		}
   		var strS = "-" + s.toString();
   		var strM = m.toString() + "%";
   		with(x){
			Angle.textValue = strS;
			Focus.textValue = strM;
		}
	}
}

