﻿$(document).ready(function () {
	$(".divTaught img").click(function (e) {
		e.stopImmediatePropagation();
		EditNote(e);
	});

	$(".divNote input").blur(function (e) {
		e.stopImmediatePropagation();
		SubmitNote(e);
	});

	$(".divNote .imgCDelete").click(function (e) {
		e.stopImmediatePropagation();
		if (confirm('Are you sure you want to delete this note?'))
			DeleteNote(e);
	});

	$(".divNote .imgCAccept").click(function (e) {
		e.stopImmediatePropagation();
		AcceptNote(e);
	});
});

function ShowContextMenu(e, taughtId, contextMenuClientId) {
	document.getElementById('inputTaughtId').value = taughtId;
	eo_ShowContextMenu(e, contextMenuClientId);
}

function AcceptNote(e) {
	var cell = $(e.target).parent().parent();
	var divNote = cell.find(".divNote")
	divNote.attr("style", "display:none");
	var divTaught = cell.find(".divTaught");
	divTaught.attr("style", "display:block");
	var note = divNote.find("input");
	e.target = note;
	SubmitNote(e);
}

function DeleteNote(e) {
	var cell = $(e.target).parent().parent();
	var divNote = cell.find(".divNote")
	divNote.attr("style", "display:none");
	var divTaught = cell.find(".divTaught");
	divTaught.attr("style", "display:block");

	var note = divNote.find("input");
	note.val('');
	e.target = note;
	SubmitNote(e);
}

function SubmitNote(e) {
	var cell = $(e.target).parent().parent();
	var taughtId = cell.parent().find("input:last").val();
	var note = $(e.target).val();
	note = note.replace("'", "\\'");

	$.ajax({
		type: "POST",
		url: "EditResults.aspx/SetNote",
		data: "{'taughtId':'" + taughtId + "', 'note':'" + note + "'}",
		contentType: "application/json",
		dataType: "json",
		success: function (msg) {
			var imgNote = cell.parent().find(".divTaught img");
			if (note != '') {
				imgNote.attr("title", note);
				imgNote.attr("src", "Images/note.jpg");
			}
			else {
				imgNote.attr("title", "[Click to edit note]");
				imgNote.attr("src", "Images/noteoff.jpg");
			}
		},
		error: function () {
		}
	});
}

function EditNote(e) {
	var cell = $(e.target).parent().parent();
	var taughtId = cell.parent().find("input:last").val();

	$(".divNote").attr("style", "display:none");
	$(".divTaught").attr("style", "display:block");

	var divNote = cell.find(".divNote");
	divNote.attr("style", "display:block");
	var divTaught = cell.find(".divTaught");
	divTaught.attr("style", "display:none");
	var noteText = divNote.find("input");

	$.ajax({
		type: "POST",
		url: "EditResults.aspx/GetNote",
		data: "{'taughtId':'" + taughtId + "'}",
		contentType: "application/json",
		dataType: "json", 
		success: function (msg) {
			noteText.val(msg.d);
		},
		error: function () {
		}
	});

	noteText.focus();
}

function TestMethod() {

	var result = '';

	$.ajax({
		type: "POST",
		url: "EditResults.aspx/TestMethod",
		data: "{}",
		contentType: "application/json",
		dataType: "json",
		success: function (msg) {
			alert(msg.d);
		},
		error: function () {
		}
	});
}

function ResultClicked(e, eventInfo) {
	var reportingMonth = document.getElementById('inputReportingMonth').value;
	var reportingYear = document.getElementById('inputReportingYear').value;
	var taughtId = document.getElementById('inputTaughtId').value;
	var adminUserName = document.getElementById('inputAdminUserName').value;
	var statusId = eventInfo.getItem().getValue();
	var leftIcon = eventInfo.getItem().getLeftIcon();
	var statusText = eventInfo.getItem().getText();

	if (statusId != 'N') {
		$.ajax({
			type: "POST",
			url: "EditResults.aspx/SubmitResult",
			data: "{'reportingMonth':'" + reportingMonth + "', 'reportingYear':'" + reportingYear + "', 'taughtId':'" + taughtId + "', 'statusId':'" + statusId + "'}",
			contentType: "application/json",
			dataType: "json",
			success: function (msg) {
				if (msg.d != '') {
					var rowTaught = $(".divTaughtId_" + taughtId).parent().parent();
					rowTaught.find(".txtStatus").html(statusText);
					rowTaught.find(".imgStatus").attr({ src: leftIcon });
					rowTaught.find(".txtReportDate").html(msg.d);

					var txtReportBy = rowTaught.find(".txtReportBy");

					txtReportBy.html(adminUserName);
					txtReportBy.removeClass("statusMessage");
					txtReportBy.removeClass("statusGeneric");

					rowTaught.find(".gridButton").attr("style", "visibility : visible;");
				}
				else {
					SessionTimedOut();
				}
			},
			error: function () {
				SessionTimedOut();
			}
		});
	}
}

function ResetConfirmation(month, userName, recordType) {
	return (confirm('Are you sure you want to reset ' + month + '\x27s report for ' + userName + '?'));
}

function DeleteConfirmation(groupType, hhType, month, userName, recordType) {
	return (confirm('WARNING:  Since this ' + hhType + ' has reported prior to the current month, this ' + hhType + ' is no longer associated with the current ' + groupType + ' Assignments List.  Clicking OK will delete any record of this ' + hhType + ' having reported for this month.  Are you certain you want to delete ' + month + '\x27s ' + hhType + ': ' + userName + '?'));
}
