<?php
	if(stripos($_SERVER['SCRIPT_NAME'],"/admin/")===false?false:true){
	      
	?>

<style>
	.bootstrap-datetimepicker-widget .datepicker-days table tbody tr:hover {
		background-color: #eee;
	}
	@media print {
		a[href]:after {
			content: none !important;
	}
	@media (min-width: 768px) {
  .modal-xl {
    width: 90%;
   max-width:1200px;
  }
}
</style>


<!-- WEEKLY PLANNER SELECT Modal -->
<div class="modal fade" id="weeklyPlannerSelectModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	<div class="modal-dialog modal-sm">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
				<h4 class="modal-title" id="myModalLabel">Select Week</h4>
			</div>
			<div class="modal-body">
				<div class="form-group">
					<label for="weeklyPlannerSelect">Week:</label>
					<div class="container">
						<div class="row">
							<div class="form-group">
								<div class="input-group" id="weeklyPlannerSelectPicker">
									<input class="form-control" type='text' id='weeklyPlannerSelect' placeholder="Select Week" />
								</div>
							</div>
						</div>
						<div class="row">
							<div class="form-group">
								<div class="input-group">
									<select class="form-control" id="weeklyPlannerSelectSalesman">
										<?php
											$query = "SELECT * FROM SALESMAN ORDER BY FullName";
											$result = mysqli_query($con, $query);
											while($row = mysqli_fetch_array($result)){
												echo "<option value=\"" . $row['ID'] . "\">" . $row['FullName'] . "</option>";
											}
										?>
									</select>
								</div>
							</div>
						</div>
					</div>
				</div>
			</div>
			<div class="modal-footer">
				<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
				<button type="button" id="weeklyPlannerSelectGo" class="btn btn-primary">Go</button>
			</div>
		</div>
	</div>
	<script>
		$(document).ready(function(){
			$('#adminManageMenu ul').append($('<\li>').append($('<\a>').html("Weekly Planner").attr('href', '#').attr('onclick', "$('#weeklyPlannerSelectModal').modal('toggle');")));
		});
		$(document).ready(function(){
		        moment.locale('en', {
		            week: { dow: 1 } // Monday is the first day of the week
		        });
		
		    //Initialize the datePicker(I have taken format as mm-dd-yyyy, you can     //have your owh)
		    $("#weeklyPlannerSelect").datetimepicker({
		            format: 'MM/DD/YYYY'
		    });
		
		     //Get the value of Start and End of Week
		    $('#weeklyPlannerSelect').on('dp.change', function () {
		            var value = $("#weeklyPlannerSelect").val();
		            var firstDate = moment(value, "MM/DD/YYYY").day(1).format("MM/DD/YYYY");
		            var lastDate =  moment(value, "MM/DD/YYYY").day(6).format("MM/DD/YYYY");
		            $("#weeklyPlannerSelect").val(firstDate + " - " + lastDate);
		    });
			$('#weeklyPlannerSelect').on('blur', function () {
		            var value = $("#weeklyPlannerSelect").val();
		            var firstDate = moment(value, "MM/DD/YYYY").day(1).format("MM/DD/YYYY");
		            var lastDate =  moment(value, "MM/DD/YYYY").day(6).format("MM/DD/YYYY");
		            $("#weeklyPlannerSelect").val(firstDate + " - " + lastDate);
		    });
				$('#weeklyPlannerSelectGo').on('click', function(data){
					fillWeeklyPlanner();
					$('#weeklyPlannerModal').modal('toggle');
				});
		});
		function fillWeeklyPlanner(){
					setTimeout(function(){ 
						$('body').addClass('modal-open');
					}, 450);
					var fromDate = $("#weeklyPlannerSelect").val().split(" - ")[0];
					var toDate = $("#weeklyPlannerSelect").val().split(" - ")[1];
					var salesman = $('#weeklyPlannerSelectSalesman').val();
					var salesmanName = $('#weeklyPlannerSelectSalesman option[value=' + salesman + ']').text();
					$('#WeeklyPlannerSalesmanName').html(salesmanName);
					$.get('mod.php?p=weeklyplanner&f=getSalesmanWeeklyPlanner&sm=' + salesman + '&fromDate=' + fromDate + '&toDate=' + toDate, function(data){
							$('#weeklyPlannerSelectModal').modal('hide');
							clearWeeklyPlanner();
							
							var weekdayDate = new Date(fromDate);
							var today = new Date();
							$('#weeklyPlannerMondayTitle').text(weekdayDate.getDate());
							weekdayDate.setDate(weekdayDate.getDate()+1);
							if(weekdayDate < today){
								$('#AddEntry-1').hide();	
							}
							$('#weeklyPlannerTuesdayTitle').text(weekdayDate.getDate());
							weekdayDate.setDate(weekdayDate.getDate()+1);
							if(weekdayDate < today){
								$('#AddEntry-2').hide();	
							}
							$('#weeklyPlannerWednesdayTitle').text(weekdayDate.getDate());
							weekdayDate.setDate(weekdayDate.getDate()+1);
							if(weekdayDate < today){
								$('#AddEntry-3').hide();	
							}
							$('#weeklyPlannerThursdayTitle').text(weekdayDate.getDate());
							weekdayDate.setDate(weekdayDate.getDate()+1);
							if(weekdayDate < today){
								$('#AddEntry-4').hide();	
							}
							$('#weeklyPlannerFridayTitle').text(weekdayDate.getDate());
							weekdayDate.setDate(weekdayDate.getDate()+1);
							if(weekdayDate < today){
								$('#AddEntry-5').hide();	
							}
							$('#WeeklyPlannerDateRangeTitle').text(fromDate + " - " + toDate);
							data = JSON.parse(data);
							$('#weeklyPlannerWeekID').html("Week<br>" + data.WeekNumber);
							visitsPerDay = [];
							for(var i = 0; i < data.Visits.length; i++){
								var visit = "<div class=\"WeeklyPlannerEntryDiv\" id=\"wpentry-" + data.Visits[i].VisitID + "\"   style=\"overflow-y:scroll;\" ><span class=\"pull-left\"  style=\"max-width: 160px;\">" + data.Visits[i].AccountNumber + "<br><b>" + data.Visits[i].Name + "<br>" + moneyFormat(data.Visits[i].OverDue) + "<br>" + data.Visits[i].Comments + "</span>";
								visit = visit + "<span class=\"pull-right\">";
								//Remove button
								if(today.getDay() < parseInt(data.Visits[i].Weekday)){
									visit = visit + "<a style=\"color: red;\" onclick=\"deleteWeeklyPlannerVisit(" + data.Visits[i].VisitID + ")\"><span class=\"glyphicon glyphicon-remove-circle\"></span></a><br>";	
								}
								var url = "";
								var weekdayDate = new Date(fromDate);
								weekdayDate.setDate(weekdayDate.getDate() + parseInt(data.Visits[i].Weekday)-1);
								//weekdayDate.setDate(weekdayDate.getDate() + parseInt(3)-1);
								//
								visitDate = weekdayDate.getFullYear() + "/" + (parseInt(weekdayDate.getMonth())+1) + "/" + weekdayDate.getDate();
								if(parseInt(data.Visits[i].MadePayment)){
									
									url = "mod.php?p=payment&a=admin.paymenthistory&for=customer&customer=" + data.Visits[i].AccountNumber + "&fromDate=" + visitDate + "&toDate=" + visitDate + "&hideProcessed=undefined";
									visit = visit + "<span id=\"PaymentMark-" + data.Visits[i].VisitID + "\"><a href=\"" + url + "\" target=\"_blank\">P</a></span><br>";	
								}
								if(parseInt(data.Visits[i].MadePickup)){
									url= "pickuphistory.php?a=customer&customer=" + data.Visits[i].AccountNumber + "&fromDate=" + visitDate + "&toDate=" + visitDate;;
									//visit = visit + "<span id=\"CreditMark-" + data.Visits[i].VisitID + "\">C</span><br>";
									visit = visit + "<a href=\"" + url + "\" target=\"_blank\"><span id=\"CreditMark-" + data.Visits[i].VisitID + "\">C</span></a><br>";
								}
								if(parseInt(data.Visits[i].MadeOrder)){
									url = "orderhistory.php?a=customer&customer=" + data.Visits[i].AccountNumber + "&fromDate=" + visitDate + "&toDate=" + visitDate;
									visit = visit + "<a href=\"" + url + "\" target=\"_blank\"><span id=\"OrderMark-" + data.Visits[i].VisitID + "\">O</span></a><br>";
									//visit = visit + "<a href=\"" + url + "\" target=\"_blank\"><span id=\"ReminderMark-" + data.Visits[i].VisitID + "\">R</span></a><br>";	
								}
								if(parseInt(data.Visits[i].MadeReminder)){
									url = "mod.php?p=payment&a=admin.paymenthistory&for=customer&customer=" + data.Visits[i].AccountNumber + "&fromDate=" + visitDate + "&toDate=" + visitDate + "&hideProcessed=undefined";
									visit = visit + "<a href=\"" + url + "\" target=\"_blank\"><span id=\"ReminderMark-" + data.Visits[i].VisitID + "\">R</span></a><br>";	
								}
								
								
								visit = visit + "</span>";
								visit = visit + "</div>";
								switch(data.Visits[i].Weekday){
									
									case "1":
										$('#MondayPlannerQueue').append(visit);
										break;
									case "2":
										$('#TuesdayPlannerQueue').append(visit);
										break;
									case "3":
										$('#WednesdayPlannerQueue').append(visit);
										break;
									case "4":
										$('#ThursdayPlannerQueue').append(visit);
										break;
									case "5":
										$('#FridayPlannerQueue').append(visit);
										break;
								}
								
								if(typeof visitsPerDay[parseInt(data.Visits[i].Weekday)] === "undefined"){
									visitsPerDay[parseInt(data.Visits[i].Weekday)] = 1;
								}else{
									visitsPerDay[parseInt(data.Visits[i].Weekday)]++;
								}
							}
							visitsPerDay[0] = 0;
							if(typeof visitsPerDay[1] === "undefined"){
									visitsPerDay[1] = 0;
								}
							var maxVisit = Math.max.apply(null,visitsPerDay);
							for(i = 1; i <= maxVisit; i++){
									var numberHTML = "<div class=\"WeeklyPlannerEntryDiv WeeklyPlannerNumbering\"><h2>" +  i +  "</h2><br> <Br> <br> </div>";
									$('#NumberingQueue').append(numberHTML);
								
							}
							$('#weeklyPlannerWPID').val(data.WeekID);
							
							setTimeout(function(){ 
								$('body').addClass('modal-open');
							}, 650);
					});
		}
		function clearWeeklyPlanner(){
			$('#WeeklyPlannerDateRangeTitle').html('');
			$('#WeeklyPlannerSalemsmanName').html('');
			$('#NumberingQueue').html('');
			$('#MondayPlannerQueue').html('');
			$('#TuesdayPlannerQueue').html('');
			$('#WednesdayPlannerQueue').html('');
			$('#ThursdayPlannerQueue').html('');
			$('#FridayPlannerQueue').html('');
			$('#weeklyPlannerMondayTitle').text('');
			$('#weeklyPlannerTuesdayTitle').text('');
			$('#weeklyPlannerWednesdayTitle').text('');
			$('#weeklyPlannerThursdayTitle').text('');
			$('#weeklyPlannerFridayTitle').text('');
			$('#AddEntry-1').show();
			$('#AddEntry-2').show();
			$('#AddEntry-3').show();
			$('#AddEntry-4').show();
			$('#AddEntry-5').show();
			
		}
	</script>
	<style>
		.WeeklyPlannerEntryDiv{
			height: 100px;
			max-height: 100px;
			border: solid 1px;
			padding: 3px;
		}
		.WeeklyPlannerNumbering{
			text-align: center;
			padding-top: 10px;
		}
	</style>
</div>
<!-- END WEEKLY PLANNER SELECT Modal -->

<!-- 	WEEKLY PLANNER MODAL -->
<div class="modal fade" id="weeklyPlannerModal" tabindex="-1" role="dialog">
	<div class="modal-dialog modal-xl" role="document">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
				<h4 class="modal-title">Weekly Planner - <i><span id="WeeklyPlannerSalesmanName"></span></i></h4>
				<span id="WeeklyPlannerDateRangeTitle"></span>
				<button class="btn btn-primary pull-right" onclick="window.print();">Print</button>
			</div>
			<div class="modal-body">
				<input type="hidden" id="weeklyPlannerWPID" value=""/>
				<table class="table table-bordered">
					<thead>
					<tr>
						<th style="width: 5%; text-align: center;"><span id="weeklyPlannerWeekID"></span></th>
						<th style="width: 19%;">Lunes <span id="weeklyPlannerMondayTitle"></span><a onclick="addNewWeeklyPlannerEntry('1')"><span id="AddEntry-1" class="glyphicon glyphicon-plus-sign pull-right"></span></a></th>
						<th style="width: 19%;">Martes <span id="weeklyPlannerTuesdayTitle"></span><a onclick="addNewWeeklyPlannerEntry('2')"><span id="AddEntry-2" class="glyphicon glyphicon-plus-sign pull-right"></a></th>
						<th style="width: 19%;">Miercoles <span id="weeklyPlannerWednesdayTitle"></span><a onclick="addNewWeeklyPlannerEntry('3')"><span id="AddEntry-3" class="glyphicon glyphicon-plus-sign pull-right"></a></th>
						<th style="width: 19%;">Jueves <span id="weeklyPlannerThursdayTitle"></span><a onclick="addNewWeeklyPlannerEntry('4')"><span id="AddEntry-4" class="glyphicon glyphicon-plus-sign pull-right"></a></th>
						<th style="width: 19%;">Viernes <span id="weeklyPlannerFridayTitle"></span><a onclick="addNewWeeklyPlannerEntry('5')"><span id="AddEntry-5" class="glyphicon glyphicon-plus-sign pull-right"></a></th>
					</tr>
					</thead>
					<tbody>
						<tr>
							<td id="NumberingQueue"></td>
							<td id="MondayPlannerQueue"></td>
							<td id="TuesdayPlannerQueue"></td>
							<td id="WednesdayPlannerQueue"></td>
							<td id="ThursdayPlannerQueue"></td>
							<td id="FridayPlannerQueue"></td>
						</tr>
					</tbody>
				</table>
				
				<script></script>
			</div>
			<div class="modal-footer">
				<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
				
			</div>
		</div>
		<!-- /.modal-content -->
	</div>
	<!-- /.modal-dialog -->
</div>
<!-- /.modal -->
<!-- END	WEEKLY PLANNER MODAL -->

<!-- WEEKLY PLANNER Add Edit Entry Modal -->
<div class="modal fade" id="weeklyPlannerAddEditEntryModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	<div class="modal-dialog modal-sm">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
				<h4 class="modal-title" id="myModalLabel">Add / Edit Planner Entry</h4>
			</div>
			<div class="modal-body">
				<div class="form-group">
					<label for="weeklyPlannerSelectCustomer">Customer:</label>
					<select class="form-control" id="weeklyPlannerSelectCustomer">
						
						<?php
							if($salesman->ID == "25" || $salesman->ID == "99" || $salesman->ID == "PXX"){
								
								$salesmanID = "%";
							}else{
								$salesmanID = $salesman->ID;
							}
							$query = "SELECT AccountNumber, Name, City FROM CUSTOMER WHERE Salesman LIKE '{$salesmanID}' ORDER BY Name";
							$result = mysqli_query($con,$query);
							while($row = mysqli_fetch_array($result)){
								echo "<option value=\"" . $row['AccountNumber'] . "\">" . $row['AccountNumber'] . " - " . $row['Name'] . " - " . $row['City'] . "</option>";
							}
							
							?>	
					</select>
				</div>
				<input type="hidden" id="weeklyPlannerAddEditIsNew" value="false"/>
				<input type="hidden" id="weeklyPlannerAddEditWeekday" value=""/>
				<input type="hidden" id="weeklyPlannerAddEditWPID" value=""/>
				<div class="form-group">
					<label for="weeklyPlannerOverDue">Over 45:</label>
					<input disabled readonly class="form-control" type="text" id="weeklyPlannerOverDue" />
				</div>
				<div class="form-group">
					<label for="weeklyPlannerComments">Comments:</label>
					<textarea class="form-control" id="weeklyPlannerComments"></textarea>
				</div>
			</div>
			<div class="modal-footer">
				<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
				<button type="button" id="weeklyPlannerAddEditEntrySave" onclick="saveWeeklyPlannerEntry();" class="btn btn-primary">Save</button>
			</div>
		</div>
	</div>
	<script>
		$('#weeklyPlannerAddEditEntryModal').on('hidden.bs.modal', function(){
			$('body').addClass('modal-open');
		});
		$('#weeklyPlannerSelectCustomer').on("change", function(){
			if($('#weeklyPlannerSelectCustomer').val() != "-1"){
				$.get('mod.php?p=weeklyplanner&f=getCustomerOverdueBalance&an=' + $('#weeklyPlannerSelectCustomer').val(), function(data){
					$('#weeklyPlannerOverDue').val(moneyFormat(data));
						
				});
			}
			
		});
		function addNewWeeklyPlannerEntry(day){
			$('#weeklyPlannerAddEditEntryModal').modal('toggle');
			$('#weeklyPlannerAddEditWeekday').val(day);
			$('#weeklyPlannerAddEditWPID').val($('#weeklyPlannerWPID').val());
			$('#weeklyPlannerAddEditIsNew').val('true');
			
			$('#weeklyPlannerSelectCustomer').val('');
			$('#weeklyPlannerSelectCustomer').data('combobox').clearTarget();
			$('#weeklyPlannerSelectCustomer').data('combobox').clearElement();
			$('#weeklyPlannerSelectCustomer').data('combobox').refresh();
			$('#weeklyPlannerOverDue').val("$0.00");
			$('#weeklyPlannerComments').val('');
		}
		$(document).ready(function(){
			$('#weeklyPlannerSelectCustomer').combobox();
			
		});
		function saveWeeklyPlannerEntry(){
			$.get('mod.php?p=weeklyplanner&f=saveWeeklyPlannerVisit',
						{
							'WPID' : $('#weeklyPlannerAddEditWPID').val(),
							'Weekday' : $('#weeklyPlannerAddEditWeekday').val(),
							'AccountNumber': $('#weeklyPlannerSelectCustomer').val(),
							'OverDue': $('#weeklyPlannerOverDue').val().replace("$","").replace(",",""),
							'Comments':  $('#weeklyPlannerComments').val(),
							
						
						} , function(data){
						if(data){
							fillWeeklyPlanner();
							$('#weeklyPlannerAddEditEntryModal').modal('hide');
									setTimeout(function(){ 
										$('body').addClass('modal-open');
									}, 450);
						}
					});
		}
		function deleteWeeklyPlannerVisit(wpid){
			$.get('mod.php?p=weeklyplanner&f=deleteWeeklyPlannerVisit&id=' + wpid, function(data){
				if(data == "true"){
					fillWeeklyPlanner();
				}
			});	
		}
	</script>
</div>
<!-- END WEEKLY PLANNER Add Edit Entry Modal -->


	
	
<?php
	}elseif(!$isCustomer){
	?>
<style>
	.bootstrap-datetimepicker-widget .datepicker-days table tbody tr:hover {
		background-color: #eee;
	}
</style>


<!-- WEEKLY PLANNER SELECT Modal -->
<div class="modal fade" id="weeklyPlannerSelectModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	<div class="modal-dialog modal-sm">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
				<h4 class="modal-title" id="myModalLabel">Select Week</h4>
			</div>
			<div class="modal-body">
				<div class="form-group">
					<label for="weeklyPlannerSelect">Week:</label>
					<div class="container">
						<div class="row">
							<div class="form-group">
								<div class="input-group" id="weeklyPlannerSelectPicker">
									<input class="form-control" type='text' id='weeklyPlannerSelect' placeholder="Select Week" />
								</div>
							</div>
						</div>
					</div>
				</div>
			</div>
			<div class="modal-footer">
				<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
				<button type="button" id="weeklyPlannerSelectGo" class="btn btn-primary">Go</button>
			</div>
		</div>
	</div>
	<script>
		$(document).ready(function(){
		        moment.locale('en', {
		            week: { dow: 1 } // Monday is the first day of the week
		        });
		
		    //Initialize the datePicker(I have taken format as mm-dd-yyyy, you can     //have your owh)
		    $("#weeklyPlannerSelect").datetimepicker({
		            format: 'MM/DD/YYYY'
		    });
		
		     //Get the value of Start and End of Week
		    $('#weeklyPlannerSelect').on('dp.change', function () {
		            var value = $("#weeklyPlannerSelect").val();
		            var firstDate = moment(value, "MM/DD/YYYY").day(1).format("MM/DD/YYYY");
		            var lastDate =  moment(value, "MM/DD/YYYY").day(6).format("MM/DD/YYYY");
		            $("#weeklyPlannerSelect").val(firstDate + " - " + lastDate);
		    });
			$('#weeklyPlannerSelect').on('blur', function () {
		            var value = $("#weeklyPlannerSelect").val();
		            var firstDate = moment(value, "MM/DD/YYYY").day(1).format("MM/DD/YYYY");
		            var lastDate =  moment(value, "MM/DD/YYYY").day(6).format("MM/DD/YYYY");
		            $("#weeklyPlannerSelect").val(firstDate + " - " + lastDate);
		    });
				$('#weeklyPlannerSelectGo').on('click', function(data){
					fillWeeklyPlanner();
					$('#weeklyPlannerModal').modal('toggle');
				});
		});
		function fillWeeklyPlanner(){
					setTimeout(function(){ 
						$('body').addClass('modal-open');
					}, 450);
			var fromDate = $("#weeklyPlannerSelect").val().split(" - ")[0];
					var toDate = $("#weeklyPlannerSelect").val().split(" - ")[1];
					var salesman = '<?= $salesman->ID ?>';
					
					$.get('mod.php?p=weeklyplanner&f=getSalesmanWeeklyPlanner&sm=' + salesman + '&fromDate=' + fromDate + '&toDate=' + toDate, function(data){
							$('#weeklyPlannerSelectModal').modal('hide');
							clearWeeklyPlanner();
							
							var weekdayDate = new Date(fromDate);
							var today = new Date();
							$('#weeklyPlannerMondayTitle').text(weekdayDate.getDate());
							weekdayDate.setDate(weekdayDate.getDate()+1);
							if(weekdayDate < today){
								$('#AddEntry-1').hide();	
							}
							$('#weeklyPlannerTuesdayTitle').text(weekdayDate.getDate());
							weekdayDate.setDate(weekdayDate.getDate()+1);
							if(weekdayDate < today){
								$('#AddEntry-2').hide();	
							}
							$('#weeklyPlannerWednesdayTitle').text(weekdayDate.getDate());
							weekdayDate.setDate(weekdayDate.getDate()+1);
							if(weekdayDate < today){
								$('#AddEntry-3').hide();	
							}
							$('#weeklyPlannerThursdayTitle').text(weekdayDate.getDate());
							weekdayDate.setDate(weekdayDate.getDate()+1);
							if(weekdayDate < today){
								$('#AddEntry-4').hide();	
							}
							$('#weeklyPlannerFridayTitle').text(weekdayDate.getDate());
							weekdayDate.setDate(weekdayDate.getDate()+1);
							if(weekdayDate < today){
								$('#AddEntry-5').hide();	
							}
							$('#WeeklyPlannerDateRangeTitle').text(fromDate + " - " + toDate);
							data = JSON.parse(data);
							$('#weeklyPlannerWeekID').html("Week<br>" + data.WeekNumber);
							visitsPerDay = [];
							for(var i = 0; i < data.Visits.length; i++){
								var visit = "<div class=\"WeeklyPlannerEntryDiv\" id=\"wpentry-" + data.Visits[i].VisitID + "\"   style=\"overflow-y:scroll;\" ><span class=\"pull-left\"  style=\"max-width: 160px;\">" + data.Visits[i].AccountNumber + "<br><b>" + data.Visits[i].Name + "<br>" + moneyFormat(data.Visits[i].OverDue) + "<br>" + data.Visits[i].Comments + "</span>";
								visit = visit + "<span class=\"pull-right\">";
								//Remove button
								if(today.getDay() < parseInt(data.Visits[i].Weekday)){
									visit = visit + "<a style=\"color: red;\" onclick=\"deleteWeeklyPlannerVisit(" + data.Visits[i].VisitID + ")\"><span class=\"glyphicon glyphicon-remove-circle\"></span></a><br>";	
								}
								
								//
								if(parseInt(data.Visits[i].MadePayment)){
									visit = visit + "<span id=\"PaymentMark-" + data.Visits[i].VisitID + "\">P</span><br>";	
								}
								if(parseInt(data.Visits[i].MadePickup)){
									visit = visit + "<span id=\"CreditMark-" + data.Visits[i].VisitID + "\">C</span><br>";
								}
								if(parseInt(data.Visits[i].MadeOrder)){
									visit = visit + "<span id=\"OrderMark-" + data.Visits[i].VisitID + "\">O</span><br>";
								}
								if(parseInt(data.Visits[i].MadeReminder)){
									visit = visit + "<span id=\"ReminderMark-" + data.Visits[i].VisitID + "\">R</span><br>";
								}
								
								
								visit = visit + "</span>";
								visit = visit + "</div>";
								switch(data.Visits[i].Weekday){
									
									case "1":
										$('#MondayPlannerQueue').append(visit);
										break;
									case "2":
										$('#TuesdayPlannerQueue').append(visit);
										break;
									case "3":
										$('#WednesdayPlannerQueue').append(visit);
										break;
									case "4":
										$('#ThursdayPlannerQueue').append(visit);
										break;
									case "5":
										$('#FridayPlannerQueue').append(visit);
										break;
								}
								
								if(typeof visitsPerDay[parseInt(data.Visits[i].Weekday)] === "undefined"){
									visitsPerDay[parseInt(data.Visits[i].Weekday)] = 1;
								}else{
									visitsPerDay[parseInt(data.Visits[i].Weekday)]++;
								}
							}
							visitsPerDay[0] = 0;
							if(typeof visitsPerDay[1] === "undefined"){
									visitsPerDay[1] = 0;
								}
							var maxVisit = Math.max.apply(null,visitsPerDay);
							for(i = 1; i <= maxVisit; i++){
									var numberHTML = "<div class=\"WeeklyPlannerEntryDiv WeeklyPlannerNumbering\"><h2>" +  i +  "</h2><br> <Br> <br> </div>";
									$('#NumberingQueue').append(numberHTML);
								
							}
							$('#weeklyPlannerWPID').val(data.WeekID);
							
							setTimeout(function(){ 
								$('body').addClass('modal-open');
							}, 650);
					});
		}
		function clearWeeklyPlanner(){
			$('#WeeklyPlannerDateRangeTitle').html('');
			$('#NumberingQueue').html('');
			$('#MondayPlannerQueue').html('');
			$('#TuesdayPlannerQueue').html('');
			$('#WednesdayPlannerQueue').html('');
			$('#ThursdayPlannerQueue').html('');
			$('#FridayPlannerQueue').html('');
			$('#weeklyPlannerMondayTitle').text('');
			$('#weeklyPlannerTuesdayTitle').text('');
			$('#weeklyPlannerWednesdayTitle').text('');
			$('#weeklyPlannerThursdayTitle').text('');
			$('#weeklyPlannerFridayTitle').text('');
			$('#AddEntry-1').show();
			$('#AddEntry-2').show();
			$('#AddEntry-3').show();
			$('#AddEntry-4').show();
			$('#AddEntry-5').show();
			
		}
	</script>
	<style>
		.WeeklyPlannerEntryDiv{
			height: 100px;
			max-height: 100px;
			border: solid 1px;
			padding: 3px;
		}
		.WeeklyPlannerNumbering{
			text-align: center;
			padding-top: 10px;
		}
	</style>
</div>
<!-- END WEEKLY PLANNER SELECT Modal -->

<!-- 	WEEKLY PLANNER MODAL -->
<div class="modal fade" id="weeklyPlannerModal" tabindex="-1" role="dialog">
	<div class="modal-dialog modal-xl" role="document">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
				<h4 class="modal-title">Weekly Planner</h4>
				<span id="WeeklyPlannerDateRangeTitle"></span>
			</div>
			<div class="modal-body">
				<input type="hidden" id="weeklyPlannerWPID" value=""/>
				<table class="table table-bordered">
					<thead>
					<tr>
						<th style="width: 5%; text-align: center;"><span id="weeklyPlannerWeekID"></span></th>
						<th style="width: 19%;">Lunes <span id="weeklyPlannerMondayTitle"></span><a onclick="addNewWeeklyPlannerEntry('1')"><span id="AddEntry-1" class="glyphicon glyphicon-plus-sign pull-right"></span></a></th>
						<th style="width: 19%;">Martes <span id="weeklyPlannerTuesdayTitle"></span><a onclick="addNewWeeklyPlannerEntry('2')"><span id="AddEntry-2" class="glyphicon glyphicon-plus-sign pull-right"></a></th>
						<th style="width: 19%;">Miercoles <span id="weeklyPlannerWednesdayTitle"></span><a onclick="addNewWeeklyPlannerEntry('3')"><span id="AddEntry-3" class="glyphicon glyphicon-plus-sign pull-right"></a></th>
						<th style="width: 19%;">Jueves <span id="weeklyPlannerThursdayTitle"></span><a onclick="addNewWeeklyPlannerEntry('4')"><span id="AddEntry-4" class="glyphicon glyphicon-plus-sign pull-right"></a></th>
						<th style="width: 19%;">Viernes <span id="weeklyPlannerFridayTitle"></span><a onclick="addNewWeeklyPlannerEntry('5')"><span id="AddEntry-5" class="glyphicon glyphicon-plus-sign pull-right"></a></th>
					</tr>
					</thead>
					<tbody>
						<tr>
							<td id="NumberingQueue"></td>
							<td id="MondayPlannerQueue"></td>
							<td id="TuesdayPlannerQueue"></td>
							<td id="WednesdayPlannerQueue"></td>
							<td id="ThursdayPlannerQueue"></td>
							<td id="FridayPlannerQueue"></td>
						</tr>
					</tbody>
				</table>
				
				<script></script>
			</div>
			<div class="modal-footer">
				<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
				
			</div>
		</div>
		<!-- /.modal-content -->
	</div>
	<!-- /.modal-dialog -->
</div>
<!-- /.modal -->
<!-- END	WEEKLY PLANNER MODAL -->

<!-- WEEKLY PLANNER Add Edit Entry Modal -->
<div class="modal fade" id="weeklyPlannerAddEditEntryModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	<div class="modal-dialog modal-sm">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
				<h4 class="modal-title" id="myModalLabel">Add / Edit Planner Entry</h4>
			</div>
			<div class="modal-body">
				<div class="form-group">
					<label for="weeklyPlannerSelectCustomer">Customer:</label>
					<select class="form-control" id="weeklyPlannerSelectCustomer">
						
						<?php
							if($salesman->ID == "25" || $salesman->ID == "99" || $salesman->ID == "PXX"){
								
								$salesmanID = "%";
							}else{
								$salesmanID = $salesman->ID;
							}
							$query = "SELECT AccountNumber, Name, City FROM CUSTOMER WHERE Salesman LIKE '{$salesmanID}' ORDER BY Name";
							$result = mysqli_query($con,$query);
							while($row = mysqli_fetch_array($result)){
								echo "<option value=\"" . $row['AccountNumber'] . "\">" . $row['AccountNumber'] . " - " . $row['Name'] . " - " . $row['City'] . "</option>";
							}
							
							?>	
					</select>
				</div>
				<input type="hidden" id="weeklyPlannerAddEditIsNew" value="false"/>
				<input type="hidden" id="weeklyPlannerAddEditWeekday" value=""/>
				<input type="hidden" id="weeklyPlannerAddEditWPID" value=""/>
				<div class="form-group">
					<label for="weeklyPlannerOverDue">Over 45:</label>
					<input disabled readonly class="form-control" type="text" id="weeklyPlannerOverDue" />
				</div>
				<div class="form-group">
					<label for="weeklyPlannerComments">Comments:</label>
					<textarea class="form-control" id="weeklyPlannerComments"></textarea>
				</div>
			</div>
			<div class="modal-footer">
				<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
				<button type="button" id="weeklyPlannerAddEditEntrySave" onclick="saveWeeklyPlannerEntry();" class="btn btn-primary">Save</button>
			</div>
		</div>
	</div>
	<script>
		$('#weeklyPlannerAddEditEntryModal').on('hidden.bs.modal', function(){
			$('body').addClass('modal-open');
		});
		$('#weeklyPlannerSelectCustomer').on("change", function(){
			if($('#weeklyPlannerSelectCustomer').val() != "-1"){
				$.get('mod.php?p=weeklyplanner&f=getCustomerOverdueBalance&an=' + $('#weeklyPlannerSelectCustomer').val(), function(data){
					$('#weeklyPlannerOverDue').val(moneyFormat(data));
						
				});
			}
			
		});
		function addNewWeeklyPlannerEntry(day){
			$('#weeklyPlannerAddEditEntryModal').modal('toggle');
			$('#weeklyPlannerAddEditWeekday').val(day);
			$('#weeklyPlannerAddEditWPID').val($('#weeklyPlannerWPID').val());
			$('#weeklyPlannerAddEditIsNew').val('true');
			
			$('#weeklyPlannerSelectCustomer').val('');
			$('#weeklyPlannerSelectCustomer').data('combobox').clearTarget();
			$('#weeklyPlannerSelectCustomer').data('combobox').clearElement();
			$('#weeklyPlannerSelectCustomer').data('combobox').refresh();
			$('#weeklyPlannerOverDue').val("$0.00");
			$('#weeklyPlannerComments').val('');
		}
		$(document).ready(function(){
			$('#weeklyPlannerSelectCustomer').combobox();
			
		});
		function saveWeeklyPlannerEntry(){
			$.get('mod.php?p=weeklyplanner&f=saveWeeklyPlannerVisit',
						{
							'WPID' : $('#weeklyPlannerAddEditWPID').val(),
							'Weekday' : $('#weeklyPlannerAddEditWeekday').val(),
							'AccountNumber': $('#weeklyPlannerSelectCustomer').val(),
							'OverDue': $('#weeklyPlannerOverDue').val().replace("$","").replace(",",""),
							'Comments':  $('#weeklyPlannerComments').val(),
							
						
						} , function(data){
						if(data){
							fillWeeklyPlanner();
							$('#weeklyPlannerAddEditEntryModal').modal('hide');
									setTimeout(function(){ 
										$('body').addClass('modal-open');
									}, 450);
						}
					});
		}
		function deleteWeeklyPlannerVisit(wpid){
			$.get('mod.php?p=weeklyplanner&f=deleteWeeklyPlannerVisit&id=' + wpid, function(data){
				if(data == "true"){
					fillWeeklyPlanner();
				}
			});	
		}
	</script>
</div>
<!-- END WEEKLY PLANNER Add Edit Entry Modal -->
<?php
	}
	
	?>