AEGIS II Visit Scheduler (test mode)
$.noConflict();
jQuery( document ).ready(function( $ ) {
$( "#patientID, #assigned, #indexEvent, #indexEventTime").css({"display":"flex","flex-direction":"column","align-items":"flex-end"});
$( "#indexEvent, #indexEventTime, #visit-1, #visitTime-1" ).val("");
$( "#indexEvent" ).datepicker();
$( "#visit-1" ).datepicker({minDate: new Date()});
$( "#indexEvent" ).change(function(){
$( "#appointments" ).html("Visit NumberVisit DateDelayed Infusion?Visit Description");
$( "#delayedInfusionNote, #appointments, #save, #print" ).css('visibility','visible');
var schedule = visitSchedule($("patientID").val(),$("#assigned").val(),new Date($( "#indexEvent" ).val()));
function visitSchedule(patient, assigned, index){
var timeConstraints = {
			//	VisitNum : 
		this.patientID = patient;
		this.assigned = assigned;
		$.each(timeConstraints, function(key, value){
		if(key == 1){
			currentAppointment = new Date($( "#indexEvent" ).val());
		} else {
			currentAppointment = new Date($("#visit-"+(key-1)).val());
		currentAppointment = new Date(currentAppointment.getFullYear(), currentAppointment.getMonth(), currentAppointment.getDate()+value);
		if(key <= 4){
		$("#appointments").append(""+value+""+value+"");
		} else {
		$("#appointments").append(""+value+""+value+"");
		$("#visit-"+key).datepicker({minDate: new Date(currentAppointment.getFullYear(), currentAppointment.getMonth(), currentAppointment.getDate()-value), maxDate:new Date(currentAppointment.getFullYear(), currentAppointment.getMonth(), currentAppointment.getDate()+value)});
					validateConstraints();
		$(".appointment").change(function(){
			var modId = $(this).attr("id").split("-");
			var updatedAppointment = new Date($(this).val());
			$(this).val(updatedAppointment);
			$.each(timeConstraints, function(key, value){
                                $("#visit-"+key).css("border", "1px solid black");
				if(key > modId && key <= 5){
					updatedAppointment = new Date(updatedAppointment.getFullYear(), updatedAppointment.getMonth(), updatedAppointment.getDate()+value);
					$("#visit-"+key).val(updatedAppointment.toDateString());
					$("#visit-"+key).datepicker('destroy');
					$("#visit-"+key).datepicker({minDate: new Date(updatedAppointment.getFullYear(), updatedAppointment.getMonth(), updatedAppointment.getDate()-value), maxDate:new Date(updatedAppointment.getFullYear(), updatedAppointment.getMonth(), updatedAppointment.getDate()+value)});
				} else if(key == modId){
					$("#visit-"+key).val(updatedAppointment.toDateString());
			validateConstraints();
		$(".delayedInfusion").change(function(){
			$("#visit-"+$(this).val()).datepicker('destroy');
			$("#visit-"+$(this).val()).datepicker();
		$("#visitTime-1").change(function(){validateConstraints();});
		$( "#indexEventTime" ).change(function(){validateConstraints();});
		$("#visit-1").change(function(){
					currentAppointment = new Date($("#visit-1").val());
					$("#visit-6").datepicker('destroy');
					$("#visit-6").val(new Date(currentAppointment.getFullYear(), currentAppointment.getMonth(), currentAppointment.getDate()+60).toDateString());
					$("#visit-6").datepicker({minDate: new Date(currentAppointment.getFullYear(), currentAppointment.getMonth(), currentAppointment.getDate()-10), maxDate:new Date(currentAppointment.getFullYear(), currentAppointment.getMonth(), currentAppointment.getDate()+10)});
					$("#visit-7").datepicker('destroy');
					$("#visit-7").val(new Date(currentAppointment.getFullYear(), currentAppointment.getMonth(), currentAppointment.getDate()+90).toDateString());
					$("#visit-7").datepicker({minDate: new Date(currentAppointment.getFullYear(), currentAppointment.getMonth(), currentAppointment.getDate()-10), maxDate:new Date(currentAppointment.getFullYear(), currentAppointment.getMonth(), currentAppointment.getDate()+10)});
					$("#visit-8").datepicker('destroy');
					$("#visit-8").val(new Date(currentAppointment.getFullYear(), currentAppointment.getMonth(), currentAppointment.getDate()+180).toDateString());
					$("#visit-8").datepicker({minDate: new Date(currentAppointment.getFullYear(), currentAppointment.getMonth(), currentAppointment.getDate()-10), maxDate:new Date(currentAppointment.getFullYear(), currentAppointment.getMonth(), currentAppointment.getDate()+10)});
					$("#visit-9").datepicker('destroy');
					$("#visit-9").val(new Date(currentAppointment.getFullYear(), currentAppointment.getMonth(), currentAppointment.getDate()+270).toDateString());
					$("#visit-9").datepicker({minDate: new Date(currentAppointment.getFullYear(), currentAppointment.getMonth(), currentAppointment.getDate()-10), maxDate:new Date(currentAppointment.getFullYear(), currentAppointment.getMonth(), currentAppointment.getDate()+10)});
					$("#visit-10").datepicker('destroy');
					$("#visit-10").val(new Date(currentAppointment.getFullYear(), currentAppointment.getMonth(), currentAppointment.getDate()+365).toDateString());
					$("#visit-10").datepicker({minDate: new Date(currentAppointment.getFullYear(), currentAppointment.getMonth(), currentAppointment.getDate()-10), maxDate:new Date(currentAppointment.getFullYear(), currentAppointment.getMonth(), currentAppointment.getDate()+10)});
		function validateConstraints(){
			var errorFlag = false;
			if($( "#visitTime-1" ).length){
			var splitTime = $( "#indexEventTime" ).val().split(":");
			var hours = splitTime*60*60*1000;
			var minutes = splitTime*60*1000;
			var visit1 = $( "#visitTime-1" ).val().split(":");
			var visit1hours = visit1*60*60*1000;
			var visit1minutes = visit1*60*1000;
			if((new Date($("#visit-2").val()).getTime() - new Date($("#visit-1").val()).getTime()) > (24*60*60*1000*30)){
				errorFlag = true;
                        $("#visit-2").css("border", "1px solid red");
						$("#visit-2").addClass("invalid");
			if((new Date($("#visit-3").val()).getTime() - new Date($("#visit-1").val()).getTime()) > (24*60*60*1000*30)){
				errorFlag = true;
                        $("#visit-3").css("border", "1px solid red");
						$("#visit-3").addClass("invalid");
			if((new Date($("#visit-4").val()).getTime() - new Date($("#visit-1").val()).getTime()) > (24*60*60*1000*30)){
				errorFlag = true;
                        $("#visit-4").css("border", "1px solid red");
						$("#visit-4").addClass("invalid");
			if(errorFlag){alert("Visits in red fall out of the 30 day window and infusions may not be scheduled on those dates. For help with this matter, call the AEGIS II Hotline");}
		$("#print").click(function(){
			var newWin = window.open("");
			newWin.document.write($("#appointments").prop('outerHTML'));
			newWin.print();
			newWin.close();
		$("#save").click(function(){
			var cal = ics();
			var appointmentDateAndTime = new Date();
			$.each(timeConstraints, function(key, value){
				if(!$("#visit-"+key).hasClass("invalid")){
				cal.addEvent(value+" for "+$("#patientID").val(),value,"Office of "+$("#assigned").val(),$("#visit-"+key).val()+" 8:00:00",$("#visit-"+key).val()+" 20:00:00");
			cal.download();
Please Note:  Visit 1 is not displayed in the Visit Scheduler as Visit 1 is the Screening Visit in which Subject eligibility will be determined.  Should the Subject be deemed eligible to participate in AEGIS-II, please randomize the Subject in IRT.  This Visit Scheduler is to be used as an aid to calculate the remaining visits once randomization in IRT has occurred.  Please enter the same dates from IRT used for First Medical Contact (FMC) and Visit 2 into this Visit Scheduler to calculate the remaining Visits.
Please Note:  If your Subject missed an appointment for one of the infusion visits, you should adjust their visit schedule to ensure all 4 infusions are given wherever possible according to the Schedule of Assessments, and avoid skipping any doses by the end of the Active Treatment Period.  To adjust the visit schedule due to a delayed infusion (Infusions 2, 3, or 4), you may select the Delayed Infusion button to allow you to enter the date the respective delayed infusion was given.  As a reminder, all 4 infusions must be given within 30 days of the first infusion and no less than 5 days apart from each other.