function DBConnectManager() {
	this.path = this.getPath();
	this.directoryName = this.getDirectoryName(this.path);
	this.phpPath = this.getPHPPath(this.directoryName);

	this.maestroID = 0;
	this.playerID = 0;
	this.appName = "";
	this.dateAndTime = null;
}

DBConnectManager.prototype.setMaestroID = function(maestroID) {
	this.maestroID = maestroID;
}

DBConnectManager.prototype.setPlayerID = function(playerID) {
	this.playerID = playerID;
}

DBConnectManager.prototype.setAppName = function(appName) {
	this.appName = appName;
}

DBConnectManager.prototype.setDateTime = function(date, time) {
	this.dateAndTime = date + time;
}

DBConnectManager.prototype.resetDateTime = function() {
	this.dateAndTime = null;
}

DBConnectManager.prototype.getPath = function() {
	return window.location.pathname;
}

DBConnectManager.prototype.getDirectoryName = function(path) {
	var pathAndFileNames = path.split("/");
	return pathAndFileNames[pathAndFileNames.length - 2];
}

DBConnectManager.prototype.getPHPPath = function(directoryName) {
	if(directoryName === "test")	// mouse_typing/test/
		return "../src/web/";
	else	// mousetyping/src/web/client/
		return "../";
}

DBConnectManager.prototype.requestCheckPlayerLogin = function(maestroName, playerName, enterCode, onSucceededListener, onFailedListener) {
	var xhr = new XMLHttpRequest();
	xhr.open("POST", this.phpPath + "server/player/login.php", true);
	xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xhr.onreadystatechange = function() {
		if(xhr.readyState == 4 && xhr.status == 200) {
			var replyJSON = JSON.parse(xhr.responseText);

			if(replyJSON != null && replyJSON["PlayerID"] != null)
				onSucceededListener(replyJSON);
			else
				onFailedListener(replyJSON);
		}
	};
	xhr.send("maestro_name=" + maestroName + "&name=" + playerName + "&enter_code=" + enterCode);
}

DBConnectManager.prototype.requestPlayerEnterCode = function(maestroID, playerID, onSucceededListener, onFailedListener) {
	var xhr = new XMLHttpRequest();
	xhr.open("POST", this.phpPath + "server/player/get_entercode.php", true);
	xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xhr.onreadystatechange = function() {
		if(xhr.readyState == 4 && xhr.status == 200) {
			var replyJSON = JSON.parse(xhr.responseText);

			if(replyJSON != null && replyJSON["EnterCode"] != null)
				onSucceededListener(replyJSON);
			else
				onFailedListener(replyJSON);
		}
	};
	xhr.send("maestro_id=" + maestroID + "&player_id=" + playerID);
}

DBConnectManager.prototype.updatePlayerEnterCode = function(maestroID, playerID, enterCode, onSucceededListener, onFailedListener) {
	var xhr = new XMLHttpRequest();
	xhr.open("POST", this.phpPath + "server/player/edit_entercode.php", true);
	xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xhr.onreadystatechange = function() {
		if(xhr.readyState == 4 && xhr.status == 200) {
			var replyJSON = JSON.parse(xhr.responseText);

			if(replyJSON != null && replyJSON["result"] == "success")
				onSucceededListener(replyJSON);
			else
				onFailedListener(replyJSON);
		}
	};
	xhr.send("maestro_id=" + maestroID + "&player_id=" + playerID + "&enter_code=" + enterCode);
}

DBConnectManager.prototype.requestAllowEditEnterCode = function(maestroID, onSucceededListener, onFailedListener) {
	var xhr = new XMLHttpRequest();
	xhr.open("POST", this.phpPath + "server/maestro/get_allow_edit_entercode.php", true);
	xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xhr.onreadystatechange = function() {
		if(xhr.readyState == 4 && xhr.status == 200) {
			var replyJSON = JSON.parse(xhr.responseText);

			if(replyJSON != null && replyJSON["AllowEditEnterCode"] != null)
				onSucceededListener(replyJSON);
			else
				onFailedListener(replyJSON);
		}
	};
	xhr.send("maestro_id=" + maestroID);
}

/*
DBConnectManager.prototype.requestAppList = function(maestroID, onSucceededListener, onFailedListener) {
	var xhr = new XMLHttpRequest();
	xhr.open("POST", this.phpPath + "server/app/app_data.php", true);
	xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xhr.onreadystatechange = function() {
		if(xhr.readyState == 4 && xhr.status == 200) {
			var replyJSON = JSON.parse(xhr.responseText);

			if(replyJSON != null)
				onSucceededListener(replyJSON);
			else
				onFailedListener(replyJSON);
		}
	};
	xhr.send("maestro_id=" + maestroID);
}

DBConnectManager.prototype.requestActiveAppList = function(maestroID, onSucceededListener, onFailedListener) {
	var xhr = new XMLHttpRequest();
	xhr.open("POST", this.phpPath + "server/app/active_app.php", true);
	xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xhr.onreadystatechange = function() {
		if(xhr.readyState == 4 && xhr.status == 200) {
			var replyJSON = JSON.parse(xhr.responseText);

			if(replyJSON != null)
				onSucceededListener(replyJSON);
			else
				onFailedListener(replyJSON);
		}
	};
	xhr.send("maestro_id=" + maestroID);
}
*/

DBConnectManager.prototype.requestMenuAppList = function(maestroID, onSucceededListener, onFailedListener) {
	var xhr = new XMLHttpRequest();
	xhr.open("POST", this.phpPath + "server/app/menu_active_app_list.php", true);
	xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xhr.onreadystatechange = function() {
		if(xhr.readyState == 4 && xhr.status == 200) {
			var replyJSON = JSON.parse(xhr.responseText);

			if(replyJSON != null)
				onSucceededListener(replyJSON);
			else
				onFailedListener(replyJSON);
		}
	};
	xhr.send("maestro_id=" + maestroID);
}

DBConnectManager.prototype.requestTypingPracticeAppList = function(maestroID, playerID, onSucceededListener, onFailedListener) {
	var xhr = new XMLHttpRequest();
	xhr.open("POST", this.phpPath + "server/app/menu_active_typing_practice_app_list.php", true);
	xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xhr.onreadystatechange = function() {
		if(xhr.readyState == 4 && xhr.status == 200) {
			var replyJSON = JSON.parse(xhr.responseText);

			if(replyJSON != null)
				onSucceededListener(replyJSON);
			else
				onFailedListener(replyJSON);
		}
	};
	xhr.send("maestro_id=" + maestroID + "&player_id=" + playerID);
}

DBConnectManager.prototype.requestTypingTestAppList = function(maestroID, playerID, onSucceededListener, onFailedListener) {
	var xhr = new XMLHttpRequest();
	xhr.open("POST", this.phpPath + "server/app/menu_active_typing_test_app_list.php", true);
	xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xhr.onreadystatechange = function() {
		if(xhr.readyState == 4 && xhr.status == 200) {
			var replyJSON = JSON.parse(xhr.responseText);

			if(replyJSON != null)
				onSucceededListener(replyJSON);
			else
				onFailedListener(replyJSON);
		}
	};
	xhr.send("maestro_id=" + maestroID + "&player_id=" + playerID);
}

DBConnectManager.prototype.requestPlayerHistory = function(date, onSucceededListener, onFailedListener) {
	var historyRecordManager = new HistoryRecordManager();

	/*
	if(isDebugMode()) {
		this.loadTempPlayerHistory(historyRecordManager);

		if(onSucceededListener !== null)
			onSucceededListener(historyRecordManager);

		return;
	}
	*/

	var self = this;

	var xhr = new XMLHttpRequest();
	xhr.open("POST", this.phpPath + "server/record/history_record.php", true);
	xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xhr.onreadystatechange = function() {
		if(xhr.readyState == 4 && xhr.status == 200) {
			var replyJSON = JSON.parse(xhr.responseText);

			if(replyJSON != null)
				onSucceededListener(self.parseJSONtoHistoryRecord(replyJSON));
			else
				onFailedListener(replyJSON);
		}
	};
	var params = "MaestroID=" + sessionStorageManager.getMaestroID()
		+ "&AppID=" + sessionStorageManager.getPlayingAppID()
		+ "&PlayerID=" + sessionStorageManager.getPlayerID()
		+ "&Date=" + date;
	xhr.send(params);
}

DBConnectManager.prototype.parseJSONtoHistoryRecord = function(json) {
	var historyRecordManager = new HistoryRecordManager();

	if(typeof json === "undefined")
		return historyRecordManager;

	if(typeof json.history === "undefined")
		return historyRecordManager;

	var count = json.history.length;
	for(var i = 0; i < count; i++) {
		var record = new HistoryRecord(
			json.history[i]["Date"],
			json.history[i]["AppName"],
			json.history[i]["HighScore"]
		);
		historyRecordManager.push(record);
	}

	return historyRecordManager;
}

DBConnectManager.prototype.requestRanking = function(type, maestroID, date, time, onSucceededListener, onFailedListener) {
	var rankingRecordManager = new RankingRecordManager();

	/*
	if(isDebugMode()) {
		this.loadTempRanking(rankingRecordManager);

		if(onSucceededListener !== null)
			onSucceededListener(rankingRecordManager);

		return;
	}
	*/

	var self = this;

	var xhr = new XMLHttpRequest();
	xhr.open("POST", this.getRankingRecordUrl(type), true);
	xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xhr.onreadystatechange = function() {
		if(xhr.readyState == 4 && xhr.status == 200) {
			var replyJSON = JSON.parse(xhr.responseText);

			if(replyJSON != null) {
				onSucceededListener(type, self.parseJSONtoRankingRecord(type, replyJSON));
			}
			else
				onFailedListener(replyJSON);
		}
	};
	var params = "MaestroID=" + sessionStorageManager.getMaestroID()
		+ "&AppID=" + sessionStorageManager.getPlayingAppID()
		+ "&Date=" + date
		+ "&Time=" + time;
	xhr.send(params);
}

DBConnectManager.prototype.getRankingRecordUrl = function(type) {
	var rankingRecordURL = this.phpPath;

	switch(type) {
		case DBConnectManager.TYPE_MY_RANKING_HOUR:
		case DBConnectManager.TYPE_ALL_RANKING_HOUR:
		rankingRecordURL += "server/record/ranking_record_hour.php";
		break;

		case DBConnectManager.TYPE_MY_RANKING_DAY:
		case DBConnectManager.TYPE_ALL_RANKING_DAY:
		rankingRecordURL += "server/record/ranking_record_day.php";
		break;

		case DBConnectManager.TYPE_MY_RANKING_MONTH:
		case DBConnectManager.TYPE_ALL_RANKING_MONTH:
		rankingRecordURL += "server/record/ranking_record_month.php";
		break;
	}

	return rankingRecordURL;
}

DBConnectManager.prototype.parseJSONtoRankingRecord = function(type, json) {
	var rankingRecordManager = new RankingRecordManager();

	if(typeof json === "undefined")
		return rankingRecordManager;

	var replyJSON = null;
	switch(type) {
		case DBConnectManager.TYPE_MY_RANKING_HOUR:
		case DBConnectManager.TYPE_ALL_RANKING_HOUR:
		replyJSON = json.rankingHour;
		break;

		case DBConnectManager.TYPE_MY_RANKING_DAY:
		case DBConnectManager.TYPE_ALL_RANKING_DAY:
		replyJSON = json.rankingDay;
		break;

		case DBConnectManager.TYPE_MY_RANKING_MONTH:
		case DBConnectManager.TYPE_ALL_RANKING_MONTH:
		replyJSON = json.rankingMonth;
		break;
	}

	if(replyJSON === null)
		return rankingRecordManager;

	var count = replyJSON.length;
	for(var i = 0; i < count; i++) {
		var record = new RankingRecord(
			i + 1,
			replyJSON[i]["PlayerID"],
			replyJSON[i]["Name"],
			replyJSON[i]["HighScore"]
		);
		rankingRecordManager.push(record);
	}

	return rankingRecordManager;
}


DBConnectManager.prototype.onFailed = function(errorMessage) {
	console.log("failed : " + errorMessage);
}


DBConnectManager.prototype.makeXHRWithParam = function(url, param, onSucceededListener, onFailedListener) {
	xhr.onreadystatechange = function() {
		if(xhr.readyState == 4 && xhr.status == 200) {
			var replyJSON = JSON.parse(xhr.responseText);

			if(replyJSON != null && replyJSON["PlayerID"] != null)
				onSucceededListener(replyJSON);
			else
				onFailedListener(replyJSON);
		}
	}

	return xhr;
}

DBConnectManager.prototype.requestHowToPlay = function(appID, onSucceededListener, onFailedListener) {
	var xhr = new XMLHttpRequest();
	xhr.open("POST", this.phpPath + "server/app/how_to_play.php", true);
	xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xhr.onreadystatechange = function() {
		if(xhr.readyState == 4 && xhr.status == 200) {
			var replyJSON = JSON.parse(xhr.responseText);

			if(replyJSON != null && replyJSON["HowToPlay"] != null)
				onSucceededListener(replyJSON);
			else
				onFailedListener(replyJSON);
		}
	};
	xhr.send("AppID=" + appID);
}

DBConnectManager.prototype.updateResultRecord = function(maestroID, playerID, appID, record) {
	var xhr = new XMLHttpRequest();
	xhr.open("POST", this.phpPath + "server/record/update_result_record.php", true);
	xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xhr.send(
		"MaestroID=" + maestroID
		+ "&PlayerID=" + playerID
		+ "&AppID=" + appID
		+ "&Record=" + record
	);
}

DBConnectManager.prototype.requestAppHighestRecord = function(maestroID, playerID, appID, onSucceededListener, onFailedListener) {
	var xhr = new XMLHttpRequest();
	xhr.open("POST", this.phpPath + "server/record/request_app_highest_record.php", true);
	xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xhr.onreadystatechange = function() {
		if(xhr.readyState == 4 && xhr.status == 200) {
			var replyJSON = JSON.parse(xhr.responseText);

			if(replyJSON != null && replyJSON["AppHighestRecord"] != null)
				onSucceededListener(replyJSON);
			else
				onFailedListener(replyJSON);
		}
	};
	xhr.send(
		"MaestroID=" + maestroID
		+ "&PlayerID=" + playerID
		+ "&AppID=" + appID
	);
}

DBConnectManager.prototype.requestAppRanking = function(maestroID, appID, onSucceededListener, onFailedListener) {
	var xhr = new XMLHttpRequest();
	xhr.open("POST", this.phpPath + "server/record/app_ranking.php", true);
	xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xhr.onreadystatechange = function() {
		if(xhr.readyState == 4 && xhr.status == 200) {
			var replyJSON = JSON.parse(xhr.responseText);

			if(replyJSON != null)
				onSucceededListener(replyJSON);
			else
				onFailedListener(replyJSON);
		}
	};
	xhr.send(
		"MaestroID=" + maestroID
		+ "&AppID=" + appID
	);
}

DBConnectManager.prototype.requestLicenseTimeData = function(maestroID, playerID, defaultStartTime, onSucceededListener, onFailedListener) {
	var xhr = new XMLHttpRequest();
	xhr.open("POST", this.phpPath + "server/license_timer/get_license_time_data.php", true);
	xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xhr.onreadystatechange = function() {
		if(xhr.readyState == 4 && xhr.status == 200) {
			var replyJSON = JSON.parse(xhr.responseText);

			if(replyJSON != null)
				onSucceededListener(replyJSON);
			else
				onFailedListener(replyJSON);
		}
	};
	xhr.send(
		"MaestroID=" + maestroID
		+ "&PlayerID=" + playerID
		+ "&DefaultStartTime=" + defaultStartTime
	);
}

DBConnectManager.prototype.updateLicenseStartTime = function(maestroID, playerID, startTime, subjectName, onSucceededListener, onFailedListener) {
	var xhr = new XMLHttpRequest();
	xhr.open("POST", this.phpPath + "server/license_timer/update_license_start_time.php", true);
	xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xhr.onreadystatechange = function() {
		if(xhr.readyState == 4 && xhr.status == 200) {
			var replyJSON = JSON.parse(xhr.responseText);

			if(replyJSON != null)
				onSucceededListener(replyJSON);
			else
				onFailedListener(replyJSON);
		}
	};
	xhr.send(
		"MaestroID=" + maestroID
		+ "&PlayerID=" + playerID
		+ "&StartTime=" + startTime
		+ "&SubjectName=" + subjectName
	);
}

DBConnectManager.prototype.updateLicenseLeftTime = function(maestroID, playerID, leftTime, subjectName, onSucceededListener, onFailedListener) {
	var xhr = new XMLHttpRequest();
	xhr.open("POST", this.phpPath + "server/license_timer/update_license_left_time.php", true);
	xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xhr.onreadystatechange = function() {
		if(xhr.readyState == 4 && xhr.status == 200) {
			var replyJSON = JSON.parse(xhr.responseText);

			if(replyJSON != null)
				onSucceededListener(replyJSON);
			else
				onFailedListener(replyJSON);
		}
	};
	xhr.send(
		"MaestroID=" + maestroID
		+ "&PlayerID=" + playerID
		+ "&LeftTime=" + leftTime
		+ "&SubjectName=" + subjectName
	);
}

DBConnectManager.prototype.requestLicenseMaestroPassword = function(maestroID, onSucceededListener, onFailedListener) {
	var xhr = new XMLHttpRequest();
	xhr.open("POST", this.phpPath + "server/license_timer/get_license_maestro_password.php", true);
	xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xhr.onreadystatechange = function() {
		if(xhr.readyState == 4 && xhr.status == 200) {
			var replyJSON = JSON.parse(xhr.responseText);

			if(replyJSON != null)
				onSucceededListener(replyJSON);
			else
				onFailedListener(replyJSON);
		}
	};
	xhr.send("MaestroID=" + maestroID);
}

DBConnectManager.prototype.updateLicenseMaestroPassword = function(maestroID, password, onSucceededListener, onFailedListener) {
	var xhr = new XMLHttpRequest();
	xhr.open("POST", this.phpPath + "server/license_timer/update_license_maestro_password.php", true);
	xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xhr.onreadystatechange = function() {
		if(xhr.readyState == 4 && xhr.status == 200) {
			var replyJSON = JSON.parse(xhr.responseText);

			if(replyJSON != null)
				onSucceededListener(replyJSON);
			else
				onFailedListener(replyJSON);
		}
	};
	xhr.send("MaestroID=" + maestroID + "&Password=" + password);
}

DBConnectManager.prototype.requestLicenseScore = function(maestroID, playerID, onSucceededListener, onFailedListener) {
	var xhr = new XMLHttpRequest();
	xhr.open("POST", this.phpPath + "server/license_timer/get_license_score.php", true);
	xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xhr.onreadystatechange = function() {
		if(xhr.readyState == 4 && xhr.status == 200) {
			var replyJSON = JSON.parse(xhr.responseText);

			if(replyJSON != null)
				onSucceededListener(replyJSON);
			else
				onFailedListener(replyJSON);
		}
	};
	xhr.send("MaestroID=" + maestroID + "&PlayerID=" + playerID);
}

DBConnectManager.prototype.addLicenseScore = function(maestroID, playerID, subjectName, score, onSucceededListener, onFailedListener) {
	var xhr = new XMLHttpRequest();
	xhr.open("POST", this.phpPath + "server/license_timer/add_license_score.php", true);
	xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xhr.onreadystatechange = function() {
		if(xhr.readyState == 4 && xhr.status == 200) {
			var replyJSON = JSON.parse(xhr.responseText);

			if(replyJSON != null)
				onSucceededListener(replyJSON);
			else
				onFailedListener(replyJSON);
		}
	};
	xhr.send("MaestroID=" + maestroID + "&PlayerID=" + playerID + "&SubjectName=" + subjectName + "&Score=" + score);
}



DBConnectManager.prototype.loadTempPlayerHistory = function(historyRecordManager) {
	historyRecordManager.push(new HistoryRecord("05/14", "space_invaders", 14460));
	historyRecordManager.push(new HistoryRecord("05/13", "space_invaders", 12860));
	historyRecordManager.push(new HistoryRecord("05/12", "typing_english_basic", 15460));
	historyRecordManager.push(new HistoryRecord("05/11", "typing_korean_basic", 13040));
	historyRecordManager.push(new HistoryRecord("05/10", "space_invaders", 15460));
	historyRecordManager.push(new HistoryRecord("05/09", "typing_english_basic", 14607));
	historyRecordManager.push(new HistoryRecord("05/08", "typing_korean_basic", 13058));
}

DBConnectManager.prototype.loadTempRanking = function(rankingRecordManager) {
	rankingRecordManager.push(new RankingRecord(1, 7, "강경모", 4400));
	rankingRecordManager.push(new RankingRecord(2, 2, "강성태", 3400));
	rankingRecordManager.push(new RankingRecord(3, 3, "김기덕", 2400));
	rankingRecordManager.push(new RankingRecord(4, 4, "김남희", 1400));
	rankingRecordManager.push(new RankingRecord(5, 5, "고봉순", 900));
	rankingRecordManager.push(new RankingRecord(6, 6, "파르마", 800));
	rankingRecordManager.push(new RankingRecord(7, 1, "박지상", 700));
	rankingRecordManager.push(new RankingRecord(8, 8, "신현주", 600));
	rankingRecordManager.push(new RankingRecord(9, 9, "꼬봉이", 500));
	rankingRecordManager.push(new RankingRecord(10, 10, "거북이", 400));
	rankingRecordManager.push(new RankingRecord(11, 11, "다람쥐", 300));
	rankingRecordManager.push(new RankingRecord(12, 12, "사시미", 200));
	rankingRecordManager.push(new RankingRecord(13, 13, "태권도", 100));
	rankingRecordManager.push(new RankingRecord(14, 14, "합기도", 40));
	rankingRecordManager.push(new RankingRecord(15, 15, "우습지", 10));
	rankingRecordManager.push(new RankingRecord(16, 16, "하하하", 4));
}

DBConnectManager.TYPE_MY_RANKING_HOUR		= 0;
DBConnectManager.TYPE_MY_RANKING_DAY		= 1;
DBConnectManager.TYPE_MY_RANKING_MONTH		= 2;

DBConnectManager.TYPE_ALL_RANKING_HOUR		= 3;
DBConnectManager.TYPE_ALL_RANKING_DAY		= 4;
DBConnectManager.TYPE_ALL_RANKING_MONTH		= 5;