﻿$(function () {
	$(".remove-from-waitlist").live('click', function () {
		var $me = $(this);
		var userId = $me.attr('userId');
		var serviceId = $me.attr('svcId');

		$.ajax({
			type: "POST",
			data: "{'userId': '" + userId + "', 'serviceId': '" + serviceId + "'}",
			url: "services/AjaxMethodsService.asmx/RemoveFromWaitlist",
			contentType: "application/json; charset=utf-8",
			dataType: "json",
			beforeSend: function () {
			},
			complete: function () {
			},
			success: function (jsonData) {
				$me.siblings('.in-wl').css("text-decoration", "line-through");
				$me.replaceWith(function () {
					return $("<span class='red b'>" + jsonData.d + "</span>");
				});
				//moveServiceBackToList(serviceId);
			},
			error: function (err) {
				$me.replaceWith(function () {
					return $("<span class='red b'>Error!</span>");
				});
			}
		});
		return false;
	});

	$(".can-attend").live('click', function () {
		var $me = $(this);
		var userId = $(".huid").val();
		var tsId = $me.attr('tsId');

		var scheduledDate = $(this).parent().attr("scheduledDate");
		var svcFullName = $(this).parent().attr("svcFullName");

		var msg = "You are confirming your availability on '" + scheduledDate + "' to get trained for " + svcFullName + ". You are not officially an attendee until the admin confirms that there is sufficient interest and availability in this date. Please make sure to check back on your Labrunr home page for updates.";

		if (confirm(msg)) {
			$.ajax({
				type: "POST",
				data: "{'userId': '" + userId + "', 'tsId': '" + tsId + "'}",
				url: "services/AjaxMethodsService.asmx/UserConfirmsAttendance",
				contentType: "application/json; charset=utf-8",
				dataType: "json",
				beforeSend: function () {
				},
				complete: function () {
				},
				success: function (result) {
					if (result.d == true) {
						window.location.href = "Home.aspx";
					}
				},
				error: function (err) {
					alert('Error');
				}
			});
		}

		return false;
	});

	$(".can-not-attend").live('click', function () {
		var $me = $(this);
		var userId = $(".huid").val();
		var tsId = $me.attr('tsId');

		var msg = "You will be removed from the upcoming training session, but if it gets rescheduled or another training session scheduled, you will be invited.";

		if (confirm(msg)) {
			$.ajax({
				type: "POST",
				data: "{'userId': '" + userId + "', 'tsId': '" + tsId + "'}",
				url: "services/AjaxMethodsService.asmx/UserRejectsAttendance",
				contentType: "application/json; charset=utf-8",
				dataType: "json",
				beforeSend: function () {
				},
				complete: function () {
				},
				success: function (result) {
					if (result.d == true) {
						window.location.href = "Home.aspx";
					}
				},
				error: function (err) {
					alert('Error');
				}
			});
		}

		return false;
	});


	$(".move-back-to-wl").live('click', function () {
		var $me = $(this);
		var svcId = $me.attr('svcid');
		var tsId = $me.attr('tsId');
		var userId = $(".huid").val();

		$.ajax({
			type: "POST",
			data: "{'svcId': '" + svcId + "', 'userId': '" + userId + "', 'tsId': '" + tsId + "'}",
			url: "services/AjaxMethodsService.asmx/MoveUserFromInvitedBackToWaitlist",
			contentType: "application/json; charset=utf-8",
			dataType: "json",
			beforeSend: function () {
			},
			complete: function () {
			},
			success: function (result) {
				if (result.d == true) {
					window.location.href = "Home.aspx";
				}
			},
			error: function (err) {
				alert('Error');
			}
		});

		return false;
	});


	// USE-LESS
	// Dynamically added dropdown options do not get posted back to the server - should find an elegant solution - maybe a hidden field
	function moveServiceBackToList(serviceId) {

		// first get the brand new service from the backend and then append it the drop down list
		$.ajax({
			type: "POST",
			data: "{'serviceId': '" + serviceId + "'}",
			url: "services/AjaxMethodsService.asmx/FetchLabService",
			contentType: "application/json; charset=utf-8",
			dataType: "json",
			success: function (jsonData) {
				var service = JSON.parse(jsonData.d);
				$(".drp-wait-list").append(new Option(service.short_plus_full_name, service.id));
			},
			error: function (err) {
			}
		});


	}
});
