<link rel="stylesheet" href="../css/bootstrap-combobox.css" />
<script src="../js/bootstrap-combobox.js"></script>
<?php
if (!isset($_SESSION['ID'])) {
	header("Location: login.php");
	exit();
}
$admin = new Administrator($_SESSION['ID']);

?>
<style>
	.ajax-loading-modal {
		display:    none;
		position:   fixed;
		z-index:    1000;
		top:        0;
		left:       0;
		height:     100%;
		width:      100%;
		background: rgba( 255, 255, 255, .8 )
					url('../img/loading-web.gif')
					50% 50%
					no-repeat;
	}
	/* When the body has the loading class, we turn
	   the scrollbar off with overflow:hidden */
	body.loading {
		overflow: hidden;
	}

	/* Anytime the body has the loading class, our
	   modal element will be visible */
	body.loading .ajax-loading-modal {
		display: block;
	}
</style>
<link rel="stylesheet" type="text/css" href="../css/styles.css">
<script type="text/javascript" src="../js/bootstrap-multiselect.js"></script>

  <link href="../css/bootstrap-multiselect.css" rel="stylesheet">
<div class="ajax-loading-modal"></div>
<nav class="navbar navbar-default navbar-fixed-top hidden-print">
  <div class="container-fluid">
<!-- Brand and toggle get grouped for better mobile display -->
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <a class="navbar-brand" style="padding: 5px; padding-right: 200px;" href="index.php"><img class="img-responsive" style="width: 160px; position: absolute;  filter: brightness(100%);" src="../img/<?= $config['SmallLogoLight'] ?>" alt="" /></a>
    </div>

    <!-- Collect the nav links, forms, and other content for toggling -->
    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
      <ul class="nav navbar-nav">
        <li class="<?= (basename($_SERVER['PHP_SELF']) == "index.php")?"active":"" ?>"><a href="index.php">Home<span class="sr-only">"<?= (basename($_SERVER['PHP_SELF']) == "index.php")?"(current)":"" ?>"</span></a></li>
        <?php if($_SESSION['Level'] <= 0){ ?>
		<!--
        <li id="adminImportMenu" class="dropdown">
			<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">Import Data <span class="caret"></span></a>
			<ul class="dropdown-menu" role="menu">
				<li><a href="import.php?a=inventory">Inventory</a></li>
                <li><a href="import.php?a=invoice">Invoices (Aging)</a></li>
				<li><a href="import.php?a=credit">Credits</a></li>
				<li><a onclick="showModal('importAltas')" href="#">Alta</a></li>
				<li><a href="import.php?a=special">Specials</a></li>
				<li><a href="import.php?a=customers">Customers</a></li>
                <li><a href="import.php?a=pickingincentive">Picking Incentive</a></li>
				<li><a href="#" onclick="askForPricePlanList('importpplcustomers')">Price Plan - List (Customers)</a></li>
				<li><a href="#" onclick="askForPricePlanList('importpplparts')">Price Plan - List (Parts)</a></li>
				<li><a href="import.php?a=salesmen">Salesmen</a></li>

				<li><a href="import.php?a=salesmanquota">Salesman Quota (Daily MTD)</a></li>
				<li><a href="import.php?a=salesmanquotagoal">Salesman Quota (Goals)</a></li>

			</ul>
		</li>
		-->
		<?php } ?>
		<?php
			if (isset($_SESSION['Level'])){
				if($_SESSION['Level'] <= 0){ ?>
		<li id="adminManageMenu" class="dropdown">
			<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">Manage<span class="caret"></span></a>
			<ul class="dropdown-menu" role="menu">
				<!--<li><a href="manage.php?a=inventory">Inventory</a></li>-->
        <li><a href="manage.php?a=customers">Customers</a></li>
        <li><a href="manage.php?a=salesmen">Salesmen</a></li>
        <li><a href="manage.php?a=groups">Define Groups (Subcategories)</a></li>
        <li><a href="manage.php?a=lines">Define Lines (Categories)</a></li>
		<li><a href="mod.php?p=printcatalog&a=view">Print Catalog</a></li>
        <li><a href="manage.php?a=reasons">Pickup Reasons</a></li>
				<li><a href="manage.php?a=warrantyreasons">Warranty Reasons</a></li>

		    <li><a href="manage.php?a=priceplancalc">Price Plan - Calculated</a></li>
				<li><a href="#" onclick="askForPricePlanList('customers')">Price Plan - List (Customers)</a></li>
				<li><a href="#" onclick="askForPricePlanList('parts')">Price Plan - List (Product)</a></li>
		    <li><a onclick="showModal('manageCatalogCategory')" href="#">Illustrated Catalog</a></li>
		        <!--<li><a onclick="showModal('manageAltas')" href="#">Altas</a></li>-->
		        <!--<li><a href="manage.php?a=buyersguide">Buyer's Guide Definitions</a></li>-->
				<li><a onclick="$('#selectSalesmanQuotaYear').modal('show')" href="#">Salesman Quota</a></li>
				<li><a onclick="showModal('viewSalesAnalysisReport')" href="#">Salesman Analysis Report</a></li>
				<li><a onclick="showModal('viewCustomerAnalysisReport')" href="#">Customer Analysis Report</a></li>
				<!--<li><a onclick="showModal('viewPickingIncentives')" href="#">Picking Incentive</a></li>-->
				<li><a href="mod.php?p=billboard&a=admin.manage">Pop-up Promotions</a></li>
				<li><a onclick="$('#lastCustomerReportDays2').modal('show');" href="#">Customers Without Orders</a></li>
				<li><a onclick="showModal('lostSaleByDate');" href="#">Lost Sale by Date</a></li>
				<li><a onclick="showModal('lostSaleBySalesman');" href="#">Lost Sale by Salesman</a></li>
				<li><a href="mod.php?p=deliveryroutes&a=manageoptions">Delivery Options</a></li>
				<li><a href="mod.php?p=deliveryroutes&a=managezones">Delivery Zones & Cities</a></li>

				<li><a onclick="showModal('deliverySummaryReport');" href="#">Delivery Summary</a></li>
				<!--<li><a href="mod.php?p=zeroedoutitems&a=admin.report">Zeroed Out Items</a></li>-->
				<li><a href="mod.php?p=itemorderhistory&a=admin.report">Item Order History</a></li>

			</ul>
		</li>
		<?php 	}
			}
			if (isset($_SESSION['Level'])){
				if($_SESSION['Level'] <= 1){
		?>
       <li id="adminOrdersMenu" class="dropdown">
       	<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">Orders<span class="caret"></span></a>
       	<ul class="dropdown-menu" role="menu">
			<li><a href="mod.php?p=orderqueue&a=queue">Order Queue</a></li>
       		<li><a onclick="showModal('viewOrdersByCustomer')" href="#">Orders By Customer</a></li>
       		<li><a onclick="showModal('viewOrdersBySalesman')" href="#">Orders By Salesman</a></li>
            <li><a onclick="showModal('viewOrdersByDate')" href="#">Orders By Date</a></li>
			<li><a onclick="showModal('viewEstimatesByDate')" href="#">Estimates By Date</a></li>
			<li><a href="mod.php?p=estimate&a=queue">Estimate Queue</a></li>
			<li><a onclick="showModal('top25ByCustomer')" href="#">Top 25 By Customer</a></li>
       		<li><a onclick="showModal('top25BySalesman')" href="#">Top 25 By Salesman</a></li>
            <li><a onclick="showModal('top25ByDate')" href="#">Top 25 By Date</a></li>


       	</ul>
       	<?php 	}
       		}
       		if (isset($_SESSION['Level'])){
       			if($_SESSION['Level'] <= 2){
       	?>
       </li>

	   <li id="adminInvoicesMenu" class="dropdown">
       	<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">Invoices<span class="caret"></span></a>
       	<ul class="dropdown-menu" role="menu">
			<li><a href="mod.php?p=invoice&a=queue">Invoice Queue</a></li>
			<li><a href="#" onclick="window.open('/mod.php?p=invoice&a=view&id=' + prompt('Enter InvoiceID:'),'_blank')">Invoice by ID</a></li>
       		<li><a onclick="showModal('viewInvoicesByCustomer');" href="#">Invoices By Customer</a></li>
       		<li><a onclick="showModal('viewInvoicesBySalesman');" href="#">Invoices By Salesman</a></li>
            <li><a onclick="showModal('viewInvoicesByDate');" href="#">Invoices By Date</a></li>
			<li><a href="mod.php?p=itemorderhistory&a=admin.soldreport&year=<?= date("Y") ?>&line=%25&group=&sku=">Sold Items Report</a></li>
			<li><a onclick="createNewInvoice();return false;">Create New Invoice</a></li>
			<li><a href="mod.php?p=deliveryroutes&a=loadDeliveries">Load Deliveries</a></li>
			<li><a href="mod.php?p=assignments&a=queue">Assignment Queue</a></li>
			<li><a onclick="showModal('viewAssignmentsByDate')" href="#">Assignments By Date</a></li>
       	</ul>
		<script type="text/javascript">
			function createNewInvoice(){
				 $('#selectCustomerToDateGroup').hide();
				 $('#selectCustomerFromDateGroup').hide();
				 $('#selectCustomer').modal('toggle');
				 $('#modalGoCustomer').unbind('click');
				 $('#modalGoCustomer').on('click', function() {
					var an = $('#customerSelect').val();
    				$.get("mod.php?p=invoice&a=functions&f=createNewInvoice&accountNumber=" + an, function(data){
						if(data == "false"){
							alert("Error occurred");
						}else{
							window.location = "mod.php?p=invoice&a=review&id=" + data;
						}
					});

    			});
			}
		</script>
       	<?php 	}
       		}
       		if (isset($_SESSION['Level'])){
       			if($_SESSION['Level'] <= 2){
       	?>
       </li>
	   <li class="dropdown">
			<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">Purchases<span class="caret"></span></a>
			<ul class="dropdown-menu" role="menu">
				<li><a href="mod.php?p=purchasing&a=pocreate">Create Purchase Order</a></li>
				<li><a href="mod.php?p=purchasing&a=manage">Manage Purchase Orders</a></li>
				<li><a href="mod.php?p=purchasing&a=managevendors">Manage Vendors</a></li>
			</ul>
	   </li>
       <li id="adminPickupsMenu" class="dropdown">
       	<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">Pickups<span class="caret"></span></a>
       	<ul class="dropdown-menu" role="menu">
       		<li><a onclick="showModal('viewPickupsByCustomer')" href="#">By Customer</a></li>
       		<li><a onclick="showModal('viewPickupsBySalesman')" href="#">By Salesman</a></li>
            <li><a onclick="showModal('viewPickupsByDate')" href="#">By Date</a></li>
			<li><a onclick="showModal('viewPickupsByID')" href="#">By Pickup ID</a></li>
			<li><a onclick="showModal('viewWarrantiesByDate')" href="#">Warranties By Date</a></li>
       	</ul>
       </li>

       <?php 	}
       	}
		if (isset($_SESSION['Level'])){
				if($_SESSION['Level'] <= 0 && ($_SESSION['Username'] == "jcorrea" || $_SESSION['Username'] == "paco" ||  $_SESSION['Username'] == "elisamuel40" )){ ?>
		<li>
			<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">Accounts Receivable<span class="caret"></span></a>
			<ul class="dropdown-menu" role="menu">

		        <li><a onclick="showPaymentModal('customer')">Payments By Customer</a></li>
						<li><a onclick="showPaymentModal('salesman')">Payments By Salesman</a></li>
						<li><a onclick="showPaymentModal('date')">Payments By Date</a></li>
						<li><a href="mod.php?p=payment&a=paymentcashbox">Payments Cash Box</a></li>
						<li><a href="#" onclick="showModal('reconciliationhistory')">Salesperson Reconciliation History</a></li>
				<li><a href="mod.php?p=payment&a=createdeposit">Create Deposit</a></li>
				<li><a onclick="showModal('deposithistory')">Deposit History</a></li>
				<li><a href="mod.php?p=payment&a=completeaging#">Accounts Receivable Report</a></li>
				<li><a onclick="showModal('agingbysalesman')">Accounts Receivable Report By Salesman</a></li>
				<li><a onclick="showModal('checksearch')">Check Search</a></li>
				<li><a onclick="showModal('paymentdetails')">Payment Details By Payment ID</a></li>
				<!--<li><a href="mod.php?p=payment&a=pendingcredits">Pending Credits</a></li>-->

				<li><a onclick="showCPDModal('salesman')">Post-Dated Checks By Salesman</a></li>
				<li><a onclick="showCPDModal('date')">Post-Dated Checks By Date</a></li>

			</ul>
		</li>


		<?php 	}
			}
		if (isset($_SESSION['Level'])){
				if($_SESSION['Level'] <= 0){ ?>

		<?php 	}
			}
		if ($_SESSION['Username'] == "elisamuel40" || $_SESSION['Username'] == "jcorrea" || $_SESSION['Username'] == "paco"){
		?>
		<li class="dropdown">
			<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">Reports<span class="caret"></span></a>
			<ul class="dropdown-menu" role="menu">

				<li><a onclick="showModal('newCommission')">Commission Report</a></li>
				<li><a onclick="showModal('reportViewer')">Report Viewer</a></li>

			</ul>
		</li>
		<li><a href="#" onclick="showModal('taxReport')">Tax</a></li>
		<?php
		}
       	if (isset($_SESSION['Level'])){
       		if($_SESSION['Level'] <= 1){
       ?>
       <li class="dropdown">
			<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">Logs<span class="caret"></span></a>
			<ul class="dropdown-menu" role="menu">
		        <li class="<?= (basename($_SERVER['PHP_SELF']) == "eventlog.php")?"active":"" ?>"><a onclick="showModal('viewEventLog')" href="#">Event Log<span class="sr-only">"<?= (basename($_SERVER['PHP_SELF']) == "eventlog.php")?"(current)":"" ?>"</span></a></li>
				<li><a onclick="showModal('CartLogByCustomer')">Cart Log By Customer</a></li>
				<li><a onclick="showModal('CartLogBySalesman')">Cart Log By Salesman</a></li>
			</ul>
		</li>
      <?php 	}
      	}
      if($_SESSION['Username'] == "jcorrea" || $_SESSION['Username'] == "elisamuel40" || true){
      ?>

	  <?php
		}
	  ?>

		<li class="dropdown">
			<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">Help<span class="caret"></span></a>
			<ul class="dropdown-menu" role="menu">
				<li><a target="_blank" href="help/ELI-T%20HELP.html">Help Text</a></li>

				<li><a href="mod.php?p=ticket&a=manage">Support Tickets</a></li>
				<li><a href="#" onclick="$.get('https://<?= $root ?>/backup.php?sql=true&key=e3x5Ym93Pdv&ondemand=true',function(data){ alert ('Sent!'); })">Send Database Backup Via Email</a></li>
			</ul>
		</li>
      </ul>
      <ul class="nav navbar-nav navbar-right">
      	<li class="dropdown">
      		<a href="#" id="userFullName" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"><?= $admin->Name ?><span class="caret"></span></a>
      		<ul class="dropdown-menu" role="menu">
      	        <li><a onclick="showPasswordForm();" href="#">Change Password</a></li>
      	        <li><a href="logout.php">Logout</a></li>
      		</ul>
      	</li>

      </ul>
    </div><!-- /.navbar-collapse -->
    </div><!-- /.container-fluid -->
    </nav>

    <!-- Select Customer Modal -->
    <div class="modal fade" id="selectCustomer" 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 Customer</h4>
          </div>
          <div class="modal-body">
            <form>
            <div id="selectCustomerFromDateGroup" class="form-group">
            	<label for="fromDateCustomer">From Date:</label>
            		<input class="form-control" data="lol" type="date" name="fromDateCustomer" id="fromDateCustomer" value="<?= date("Y-m-d") ?>" />
            </div>
            <div id="selectCustomerToDateGroup" class="form-group">
            	<label for="toDateCustomer">To Date:</label>
            		<input class="form-control" type="date" name="toDateCustomer" id="toDateCustomer" value="<?= date("Y-m-d") ?>" />
            </div>
            <div class="form-group">
            	<label for="customerSelect">Customer:</label>
            		<select class="form-control combobox" id="customerSelect">
            		<option></option>
            		<?php
            			$query = "SELECT * FROM CUSTOMER ORDER BY Name";
            			$result = mysqli_query($con, $query);
            			while ($row = mysqli_fetch_array($result)) {
            		?>
            			<option value="<?= $row['AccountNumber'] ?>"><?= $row['AccountNumber'] . " - " . $row['Name'] . " - " . $row['City'] ?></option>
            		<?php
            			}
            		?>
            		</select>
            </div>
			<!--
            <div class="form-group form-inline" id="hideProcessedPaymentsInputDivCustomer">
				<input class="form-control"  type="checkbox" name="hideProcessedPaymentsCustomer" id="hideProcessedPaymentsCustomer" />
				<label for="hideProcessedPaymentsCustomer">Cash Box</label>
			</div>
			-->
            </form>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            <button type="button" id="modalGoCustomer" class="btn btn-primary">Go</button>
          </div>
        </div>
      </div>
    </div>
    <!-- Select Customer Modal End -->

    <!-- Select Salesman Modal -->
    <div class="modal fade" id="selectSalesman" 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 Salesman</h4>
          </div>
          <div class="modal-body">
            <form>
            <div class="form-group">
            	<label for="fromDateSalesman">From Date:</label>
            		<input class="form-control" type="date" name="fromDateSalesman" id="fromDateSalesman" value="<?= date("Y-m-d") ?>" />
            </div>
            <div class="form-group">
            	<label for="toDateSalesman">To Date:</label>
            		<input class="form-control" type="date" name="toDateSalesman" id="toDateSalesman" value="<?= date("Y-m-d") ?>" />
            </div>
            <div class="form-group">
            	<label for="fromDate">Salesman:</label>
            		<select class="form-control" id="salesmanSelect">
            		<?php
            			$query = "SELECT * FROM SALESMAN ORDER BY FullName";
            			$result = mysqli_query($con, $query);
            			while ($row = mysqli_fetch_array($result)) {
            		?>
            			<option data-active="<?= $row['Active']?"1":"0" ?>" value="<?= $row['ID'] ?>"><?= $row['FullName'] . (!$row['Active']?" - INACTIVE":"") ?></option>
            		<?php
            			}
            		?>
            		</select>
								<script type="text/javascript">
									var showInactiveSalesman = true;
									$(document).ready(function(){
										
										toggleShowInactiveSalesman();
									})
									function toggleShowInactiveSalesman(){
										showInactiveSalesman = !showInactiveSalesman;
										if(showInactiveSalesman){
											$('#salesmanSelect option[data-active=0]').show();							
										}else{
											$('#salesmanSelect option[data-active=0]').hide();
										}
										
									}
									
								</script>
            </div>
            <div class="form-group form-inline" id="hideProcessedPaymentsInputDivSalesman">
				<input class="form-control"  type="checkbox" name="hideProcessedPaymentsSalesman" id="hideProcessedPaymentsSalesman" />
				<label for="hideProcessedPaymentsSalesman">  Cash Box</label>
			</div>
            </form>
          </div>
          <div class="modal-footer">
						<button type="button" class="btn btn-warning" onclick="toggleShowInactiveSalesman()">Toggle Inactive</button>
            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            <button type="button" id="modalGoSalesman" class="btn btn-primary">Go</button>
          </div>
        </div>
      </div>
    </div>
    <!-- Select Salesman Modal End -->

    <!-- Select Date Modal -->
    <div class="modal fade" id="selectDate" 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 Date</h4>
          </div>
          <div class="modal-body">
            <form>
            <div class="form-group">
            	<label for="fromDate">From Date:</label>
            		<input class="form-control" type="date" name="fromDate" id="fromDate" value="<?= date("Y-m-d") ?>" />
            </div>
            <div class="form-group">
            	<label for="fromDate">To Date:</label>
            		<input class="form-control" type="date" name="toDate" id="toDate" value="<?= date("Y-m-d") ?>" />
            </div>
			<!--
			<div class="form-group form-inline" id="hideProcessedPaymentsInputDivDate">
				<input class="form-control"  type="checkbox" name="hideProcessedPaymentsDate" id="hideProcessedPaymentsDate" />
				<label for="hideProcessedPaymentsDate">  Cash Box</label>
			</div>
			-->
            </form>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            <button type="button" id="modalGoDate" class="btn btn-primary">Go</button>
          </div>
        </div>
      </div>
    </div>
    <!-- Select Date Modal End -->

	<!-- Select PickupID Modal -->
    <div class="modal fade" id="selectPickupID" 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">Enter Pickup ID</h4>
          </div>
          <div class="modal-body">
            <div>
            <div class="form-group">
            	<label for="pickupID"></label>
            		<input class="form-control" type="text" name="pickupID" id="pickupID" value="" />
            </div>
            </div>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            <button type="button" id="modalGoPickupID" class="btn btn-primary">Go</button>
          </div>
        </div>
      </div>
    </div>
	<script type="text/javascript">
		$('#selectPickupID').on('shown.bs.modal', function(){
				$('#pickupID').focus();
		});
		$('#pickupID').on('keyup', function(e){

			if(e.keyCode == 13){
				$('#modalGoPickupID').click();
			}
		});
	</script>
    <!-- Select PickupID Modal End -->

    <!-- Select Administrator Modal -->
    <div class="modal fade" id="selectAdministrator" 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 Administrator</h4>
          </div>
          <div class="modal-body">
            <form>
            <div class="form-group">
            	<label for="fromDateAdministrator">From Date:</label>
            		<input class="form-control" data="lol" type="date" name="fromDateAdministrator" id="fromDateAdministrator" value="<?= date("Y-m-d") ?>" />
            </div>
            <div class="form-group">
            	<label for="toDateAdministrator">To Date:</label>
            		<input class="form-control" type="date" name="toDateAdministrator" id="toDateAdministrator" value="<?= date("Y-m-d") ?>" />
            </div>
            <div class="form-group">
            	<label for="administratorSelect">Administrator:</label>
            		<select class="form-control" id="administratorSelect">
            		<option value="00">All</option>
            		<?php
            			$query = "SELECT * FROM ADMINISTRATOR ORDER BY FullName";
            			$result = mysqli_query($con, $query);
            			while ($row = mysqli_fetch_array($result)) {
            		?>
            			<option value="<?= $row['id'] ?>"><?= $row['FullName'] ?></option>
            		<?php
            			}
            		?>
            		</select>
            </div>

            </form>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            <button type="button" id="modalGoAdministrator" class="btn btn-primary">Go</button>
          </div>
        </div>
      </div>
    </div>
    <!-- Select Administrator Modal End -->

    <!-- PASSWORD CHANGE MODAL -->
    	<div class="modal fade" id="passwordChange" tabindex="-1" role="dialog" aria-labelledby="PasswordChange" 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-hidden="true">&times;</button>
                      <h4 class="modal-title" id="passwordChangeTitle">Change Password</h4>
              </div>
                    <form style="margin: 10px;">
                      <label class="form-label">Old Password</label>
                      <input type="password" name="oldPassword" class="form-control">
                      <label class="form-label">New Password</label>
                      <input type="password"  name="newPassword1" class="form-control">
                      <label class="form-label">Confirm Password</label>
                      <input type="password" name="newPassword2" class="form-control">

    				 <br><br>
                      <div class="from-group">
                        <a onclick="changePassword()" class="btn btn-success">Save</a>
                        <a onclick="$('#passwordChange').modal('hide')" class="btn btn-danger">Cancel</a>
                      </div>
                    </form>

            </div>
          </div>
        </div>
        <!-- END PASSWORD CHANGE MODAL -->

        <!-- Select Altas Modal -->
        <div class="modal fade" id="selectAltas" 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 Alta</h4>
              </div>
              <div class="modal-body">
                <form>
                <div class="form-group">
                	<label for="altasSelect">Alta:</label>
                		<select class="form-control combobox" id="altasSelect">
                		<option></option>
                		<?php
                			$query = "SELECT * FROM ALTAS_HEAD ORDER BY Name";
                			$result = mysqli_query($con, $query);
                			while ($row = mysqli_fetch_array($result)) {
                		?>
                			<option value="<?= $row['ID'] ?>"><?= $row['Name'] . " - " . $row['DateLine'] ?></option>
                		<?php
                			}
                		?>
                		</select>
                </div>

                </form>
              </div>
              <div class="modal-footer">

              	<button type="button" onclick="addAltasDefinition()" id="modalAddAltas" class="btn btn-success">Add New</button>
              	<button type="button" onclick="editAltasDefinition()" style="display: none;" id="modalEditAltas" class="btn btn-warning">Edit</button>
                <button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
                <button type="button" id="modalGoAltas" class="btn btn-primary">Go</button>
              </div>
            </div>
          </div>
        </div>
        <!-- Select Altas Modal End -->
        <!-- AddEdit Altas Modal -->
        <div class="modal fade" id="addEditAltas" 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="addEditAltasTitle">Add Catalog Category Definition</h4>
              </div>
              <div class="modal-body">
                <form>
                <input type="hidden" id="altasIsNew" name="isNew" value="" />
                <input type="hidden" id="altasID" name="ID" value="" />

                <div class="form-group">
                	<label for="altasName">Name:</label>
                		<input class="form-control" type="text" id="altasName" name="altasName" value="" />
                </div>
                <div class="form-group">
                	<label for="altasNameEN">Name (English):</label>
                		<input class="form-control" type="text" id="altasNameEN" name="altasNameEN" value="" />
                </div>
                <div class="form-group">
                	<label for="altasDate">Date</label>
                		<input class="form-control" type="text" id="altasDate" name="altasDate" value="" />
                </div>
                <div class="form-group">
                	<label for="altasSort">Sort Order: <i>(Fields: Line, Group, SKU, Description)</i><br /><small><i>Please fill with comma separated values</i></small></label>
                		<input class="form-control" type="text" id="altasSort" name="altasSort" value="" />
                </div>
                <div class="checkbox">
                    <label>
                      <input type="checkbox" name="altasActive" id="altasActive"> Active
                    </label>
                  </div>
                </form>
              </div>
              <div class="modal-footer">
              	<button type="button" onclick="deleteAltasDefinition()" id="modalDeleteAltas" style="display: none;" class="btn btn-danger">Delete</button>
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                <button type="button" onclick="saveAltasDefinition()" id="modalSaveAltas" class="btn btn-primary">Save</button>
              </div>
            </div>
          </div>
        </div>
        <!-- AddEdit Altas Modal End -->
        <!-- Select Catalog Modal -->
        <div class="modal fade" id="selectCatalogCategory" 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 Catalog</h4>
              </div>
              <div class="modal-body">
                <form>
                <div class="form-group">
                	<label for="catalogCategorySelect">Category:</label>
                		<select class="form-control combobox" id="catalogCategorySelect">
                		<option></option>
                		<?php
                			$query = "SELECT * FROM CATALOG_CATEGORY ORDER BY Name";
                			$result = mysqli_query($con, $query);
                			while ($row = mysqli_fetch_array($result)) {
                		?>
                			<option value="<?= $row['CatalogID'] ?>"><?= $row['Name'] ?></option>
                		<?php
                			}
                		?>
                		</select>
                </div>

                </form>
              </div>
              <div class="modal-footer">

              	<button type="button" onclick="addCatalogCategoryDefinition()" id="modalAddCatalogCategory" class="btn btn-success">Add New</button>
              	<button type="button" onclick="editCatalogCategoryDefinition()" style="display: none;" id="modalEditCatalogCategory" class="btn btn-warning">Edit</button>
                <button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
                <button type="button" id="modalGoCatalogCategory" class="btn btn-primary">Go</button>
              </div>
            </div>
          </div>
        </div>
        <!-- Select Catalog Category Modal End -->
        <!-- AddEdit Catalog Category Modal -->
        <div class="modal fade" id="addEditCatalogCategory" 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="addEditCatalogCategoryTitle">Add Catalog Category Definition</h4>
              </div>
              <div class="modal-body">
                <form>
                <input type="hidden" id="catalogCategoryIsNew" name="isNew" value="" />
                <input type="hidden" id="catalogCategoryID" name="ID" value="" />

                <div class="form-group">
                	<label for="catalogCategoryName">Name:</label>
                		<input class="form-control" type="text" id="catalogCategoryName" name="catalogCategoryName" value="" />
                </div>
                <div class="form-group">
                	<label for="catalogCategoryNameEN">Name (English):</label>
                		<input class="form-control" type="text" id="catalogCategoryNameEN" name="catalogCategoryNameEN" value="" />
                </div>
                <div class="form-group">
                	<label for="catalogCategorySort">Sort Order: <i>(Fields: Line, Group, SKU, Description)</i><br /><small><i>Please fill with comma separated values</i></small></label>
                		<input class="form-control" type="text" id="catalogCategorySort" name="catalogCategorySort" value="" />
                </div>
                </form>
              </div>
              <div class="modal-footer">
              	<button type="button" onclick="deleteCatalogCategoryDefinition()" id="modalDeleteCatalogCategory" style="display: none;" class="btn btn-danger">Delete</button>
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                <button type="button" onclick="saveCatalogCategoryDefinition()" id="modalSaveCatalogCategory" class="btn btn-primary">Save</button>
              </div>
            </div>
          </div>
        </div>
        <!-- AddEdit Catalog Category Modal End -->

		<!-- Check Search Modal -->
        <div class="modal fade" id="checkSearchModal" 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="checkSearchTitle">Check Search</h4>
              </div>
              <div class="modal-body">


                <div class="form-group">
                	<label for="checkSearchNumber">Number:</label>
                		<input class="form-control" type="text" id="checkSearchNumber" name="checkSearchNumber" value="" />
                </div>

              </div>
              <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                <button type="button" id="checkSearchButton" class="btn btn-primary">Search</button>
              </div>
            </div>
          </div>
        </div>
        <!-- Check Search Modal End -->
    <?php
		include_once("modules.modals.php");
	?>
    <script type="text/javascript">

		(function($){
			$(document).ready(function(){
				$('.combobox').combobox();
				$('ul.dropdown-menu [data-toggle=dropdown]').on('click', function(event) {
					event.preventDefault();
					event.stopPropagation();
					$(this).parent().siblings().removeClass('open');
					$(this).parent().toggleClass('open');
				});
			});
		})(jQuery);
		/* http://www.bootply.com/nZaxpxfiXz */
    	function showModal(which) {
			$('#selectCustomerFromDateGroup').show();
			$('#selectCustomerToDateGroup').show();
    		if (which == "viewOrdersByCustomer") {
    			$('#modalGoCustomer').unbind('click');
    			$('#modalGoCustomer').on('click', function(e) {
    				viewOrders('customer');
    			});
    			$('#selectCustomer').modal('toggle');
    		} else if (which == "viewOrdersBySalesman") {
    			$('#modalGoSalesman').unbind('click');
    			$('#modalGoSalesman').on('click', function(e) {
    				viewOrders('salesman');
    			});
    			$('#selectSalesman').modal('toggle');
    		} else if (which == "viewOrdersByDate") {
    			$('#modalGoDate').unbind('click');
    			$('#modalGoDate').on('click', function(e) {
    				viewOrders('date');
    			});
    			$('#selectDate').modal('toggle');
    		} else if (which == "top25ByCustomer") {
    			$('#modalGoCustomer').unbind('click');
    			$('#modalGoCustomer').on('click', function(e) {
    				viewTop25('customer');
    			});
    			$('#selectCustomer').modal('toggle');
    		} else if (which == "top25BySalesman") {
    			$('#modalGoSalesman').unbind('click');
    			$('#modalGoSalesman').on('click', function(e) {
    				viewTop25('salesman');
    			});
    			$('#selectSalesman').modal('toggle');
    		} else if (which == "top25ByDate") {
    			$('#modalGoDate').unbind('click');
    			$('#modalGoDate').on('click', function(e) {
    				viewTop25('date');
    			});
    			$('#selectDate').modal('toggle');
    		} else if (which == "viewPickupsByCustomer") {
    			$('#modalGoCustomer').unbind('click');
    			$('#modalGoCustomer').on('click', function(e) {
    				viewPickups('customer');
    			});
    			$('#selectCustomer').modal('toggle');
    		} else if (which == "viewPickupsBySalesman") {//href="mod.php?p=payment&a=completeagingbysalesman#"
    			$('#modalGoSalesman').unbind('click');
    			$('#modalGoSalesman').on('click', function(e) {
    				viewPickups('salesman');
    			});
    			$('#selectSalesman').modal('toggle');
    		} else if (which == "agingbysalesman") {//href="mod.php?p=payment&a=completeagingbysalesman#"
    			$('#modalGoSalesman').unbind('click');
				$('#fromDateSalesman').parent().hide();
				$('#toDateSalesman').parent().hide();
    			$('#modalGoSalesman').on('click', function(e) {
    				window.location = "mod.php?p=payment&a=completeagingbysalesman&sm=" + $('#salesmanSelect').val();
    			});
    			$('#selectSalesman').modal('toggle');
    		} else if (which == "viewPickupsByDate") {
    			$('#modalGoDate').unbind('click');
    			$('#modalGoDate').on('click', function(e) {
    				viewPickups('date');
    			});
    			$('#selectDate').modal('toggle');
			} else if (which == "viewWarrantiesByDate"){
				$('#modalGoDate').unbind('click');
    			$('#modalGoDate').on('click', function(e) {
    				window.location = "warrantyReport.php?a=date&fromDate=" + $('#fromDate').val() + "&toDate=" + $('#toDate').val() + "";
    			});
    			$('#selectDate').modal('toggle');
    		} else if (which == "viewEventLog") {
    			$('#modalGoAdministrator').unbind('click');
    			$('#modalGoAdministrator').on('click', function(e) {
    				var fromDate = $('#fromDateAdministrator').val();
    				var toDate = $('#toDateAdministrator').val();
    				window.location = "eventlog.php?id=" + $('#administratorSelect').val() + "&fromDate=" + fromDate + "&toDate=" + toDate;
    			});
    			$('#selectAdministrator').modal('toggle');
    		} else if (which == "manageCatalogCategory") {
    			$('#modalGoCatalogCategroy').unbind('click');
    			$('#modalGoCatalogCategory').on('click', function(e) {
    				window.location = "manage.php?a=catalog&id=" + $('#catalogCategorySelect').val();
    			});
    			$('#selectCatalogCategory').modal('toggle');
    		} else if (which == "manageAltas") {
    			$('#modalGoAltas').unbind('click');
    			$('#modalGoAltas').on('click', function(e) {
    				window.location = "manage.php?a=altas&id=" + $('#altasSelect').val();
    			});
    			$('#selectAltas').modal('toggle');
    		} else if (which == "importAltas") {
    			$('#modalGoAltas').unbind('click');
    			$('#modalGoAltas').on('click', function(e) {
    				window.location = "import.php?a=alta&id=" + $('#altasSelect').val();
    			});
    			$('#selectAltas').modal('toggle');
    		} else if( which == "agingedit"){
				$('#fromDateCustomer').parent().hide();
				$('#toDateCustomer').parent().hide();
				$('#modalGoCustomer').unbind('click');
    			$('#modalGoCustomer').on('click', function(e) {
					$('#fromDateCustomer').parent().show();
					$('#toDateCustomer').parent().show();
    				window.location = "mod.php?p=agingedit&a=admin.viewinvoices&an=" + $('#customerSelect').val();
    			});
    			$('#selectCustomer').modal('toggle');

			} else if (which == "viewPickingIncentives") {
    			$('#modalGoDate').unbind('click');
    			$('#modalGoDate').on('click', function(e) {
    				viewPickingIncentives('date');
    			});
    			$('#selectDate').modal('toggle');
    		} else if (which == "viewPickupsByID") {
    			$('#modalGoPickupID').unbind('click');
    			$('#modalGoPickupID').on('click', function(e) {
    				viewPickups('pickupID');
    			});
    			$('#selectPickupID').modal('toggle');
    		} else if (which == "viewSalesAnalysisReport") {
				$('#modalGoSalesman').unbind('click');
				$('#fromDateSalesman').val('<?= date("Y") . "-01-01" ?>');
    			$('#modalGoSalesman').on('click', function(e) {
					fromDate = $('#fromDateSalesman').val();
					toDate = $('#toDateSalesman').val();
					var salesman = $('#salesmanSelect').val();
					window.location = "mod.php?p=salesmananalysis&a=report2&sm=" + salesman + "&fromDate=" + fromDate + "&toDate=" + toDate;
    			});
				$('#salesmanSelect').prepend($('<option/>').text('ALL'));
    			$('#selectSalesman').modal('toggle');
			} else if (which == "viewCustomerAnalysisReport") {
				$('#modalGoCustomer').unbind('click');
				$('#fromDateCustomer').val('<?= date("Y") . "-01-01" ?>');
    			$('#modalGoCustomer').on('click', function(e) {
					fromDate = $('#fromDateCustomer').val();
					toDate = $('#toDateCustomer').val();
					var customer = $('#customerSelect').val();
					window.location = "mod.php?p=salesmananalysis&a=customerReport&an=" + customer + "&fromDate=" + fromDate + "&toDate=" + toDate;
    			});
				$('#customerSelect').prepend($('<option/>').text('ALL'));
    			$('#selectCustomer').modal('toggle');

			}else if (which == "lostSaleByCustomer"){


			} else if (which == "lostSaleBySalesman"){
				$('#modalGoSalesman').unbind('click');
				$('#selectSalesman form').append(
					$('<div/>').addClass('form-group').append(
						$('<label/>').attr('for', 'LostSaleFilter').text('Reason Filter'),
						$('<select/>').addClass('form-control').attr('name', 'LostSaleFilter').append(
							$('<option/>').text('All').attr('value', '%'),
							$('<option/>').text('No hay cantidad'),
							$('<option/>').text('Product Not Found'),
							$('<option/>').text('Precio')
						)
					)
				);
    			$('#modalGoSalesman').on('click', function(e) {
					fromDate = $('#fromDateSalesman').val();
					toDate = $('#toDateSalesman').val();
					var salesman = $('#salesmanSelect').val();
					window.location = "mod.php?p=lostsale&a=report&f=salesman&salesman=" + salesman + "&fromDate=" + fromDate + "&toDate=" + toDate + "&filter=" + $('[name=LostSaleFilter]').val();
    			});
    			$('#selectSalesman').modal('toggle');

			} else if (which == "lostSaleByDate"){
				$('#modalGoDate').unbind('click');
				$('#selectDate form').append(
					$('<div/>').addClass('form-group').append(
						$('<label/>').attr('for', 'LostSaleFilter').text('Reason Filter'),
						$('<select/>').addClass('form-control').attr('name', 'LostSaleFilter').append(
							$('<option/>').text('All').attr('value', '%'),
							$('<option/>').text('No hay cantidad'),
							$('<option/>').text('Product Not Found'),
							$('<option/>').text('Precio')
						)
					)
				);
    			$('#modalGoDate').on('click', function(e) {
    				fromDate = $('#fromDate').val();
					toDate = $('#toDate').val();
					window.location = "mod.php?p=lostsale&a=report&f=date&fromDate=" + fromDate + "&toDate=" + toDate + "&filter=" + $('[name=LostSaleFilter]').val();
    			});
    			$('#selectDate').modal('toggle');

			} else if (which == "CartLogByCustomer"){
				$('#modalGoCustomer').unbind('click');
    			$('#modalGoCustomer').on('click', function(e) {
					fromDate = $('#fromDateCustomer').val();
					toDate = $('#toDateCustomer').val();
					var salesman = $('#customerSelect').val();
					window.location = "mod.php?p=cartlog&a=admin.view&user=" + salesman + "&fromDate=" + fromDate + "&toDate=" + toDate;
    			});
    			$('#selectCustomer').modal('toggle');

			} else if (which == "CartLogBySalesman"){
				$('#modalGoSalesman').unbind('click');
    			$('#modalGoSalesman').on('click', function(e) {
					fromDate = $('#fromDateSalesman').val();
					toDate = $('#toDateSalesman').val();
					var salesman = $('#salesmanSelect').val();
					window.location = "mod.php?p=cartlog&a=admin.view&user=" + salesman + "&fromDate=" + fromDate + "&toDate=" + toDate;
    			});
    			$('#selectSalesman').modal('toggle');
			} else if (which == "deliverySummaryReport"){
				$('#modalGoDate').unbind('click');
				$('#selectDate form').append(
					$('<div/>').addClass('form-group').append(
						$('<label/>').attr('for', 'DeliveryCityFilter').text('City Filter'),
						$('<select/>').addClass('form-control').attr('multiple', 'multiple').attr('id', 'DeliveryCityFilter').attr('name', 'DeliveryCityFilter').append(
							$('<option/>').text('All').attr('value', '%'),
							<?php
								$query = "SELECT DISTINCT(City) as Cities FROM ORDER_HEAD join CUSTOMER USING(AccountNumber) HAVING Cities != '' ORDER BY City ASC";
								$result = mysqli_query($con, $query);
								$count = mysqli_num_rows($result);
								$i=0;
								while($row = mysqli_fetch_array($result)){
									$i++;
							?>
							$('<option/>').text('<?= $row['Cities'] ?>').attr('value', '<?= $row['Cities'] ?>')
							<?php
							if($i != $count){
								echo ",";
							}

								}

							?>
						)
					)
				);
				$('#DeliveryCityFilter').multiselect({
					enableFiltering: true,
					filterBehavior: 'value',
					includeSelectAllOption: false
				});
				$('#DeliveryCityFilter').hide();
    			$('#modalGoDate').on('click', function(e) {
    				fromDate = $('#fromDate').val();
					toDate = $('#toDate').val();
					city = $('#DeliveryCityFilter').val();
					window.location = "mod.php?p=deliveryroutes&a=summaryreport&fromDate=" + fromDate + "&toDate=" + toDate + "&city=" + city;
    			});
    			$('#selectDate').modal('toggle');

			} else if (which == "taxReport"){
				$('#modalGoDate').unbind('click');

    			$('#modalGoDate').on('click', function(e) {
    				fromDate = $('#fromDate').val();
					toDate = $('#toDate').val();

					window.location = "mod.php?p=taxreports&a=taxsummary&fdate=" + fromDate + "&tdate=" + toDate;
    			});
    			$('#selectDate').modal('toggle');

			} else if (which == "newCommission"){
				$('#modalGoDate').unbind('click');
				var now = new Date();
				var fDate = new Date(now.getFullYear(),now.getMonth(), 1);

				$('#fromDate').val(fDate.toJSON().slice(0,10));
    			$('#modalGoDate').on('click', function(e) {
    				fromDate = $('#fromDate').val();
					toDate = $('#toDate').val();
					if(toDate < fromDate){
						alert('Invalid date range');
						return false;
					}
					window.location = "mod.php?p=commission&a=report&fdate=" + fromDate + "&tdate=" + toDate;
    			});
    			$('#selectDate').modal('toggle');

			} else if(which == "viewEstimatesByDate"){
				$('#modalGoDate').unbind('click');
    			$('#modalGoDate').on('click', function(e) {
					fromDate = $('#fromDate').val();
					toDate = $('#toDate').val();
    				window.location = "estimatehistory.php?a=date&fromDate=" + fromDate + "&toDate=" + toDate;
    			});
    			$('#selectDate').modal('toggle');
			} else if (which == "viewInvoicesBySalesman") {
				$('#modalGoSalesman').unbind('click');
				$('#fromDateSalesman').val('<?= date("Y") . "-"  . date("m") . "-01" ?>');
    			$('#modalGoSalesman').on('click', function(e) {
					fromDate = $('#fromDateSalesman').val();
					toDate = $('#toDateSalesman').val();
					var salesman = $('#salesmanSelect').val();
					window.location = "mod.php?p=invoice&a=history&by=salesman&salesman=" + salesman + "&fromDate=" + fromDate + "&toDate=" + toDate;
    			});
    			$('#selectSalesman').modal('toggle');
			} else if (which == "viewInvoicesByDate") {
				$('#modalGoDate').unbind('click');
				$('#fromDate').val('<?= date("Y") . "-"  . date("m") . "-01" ?>');
    			$('#modalGoDate').on('click', function(e) {
					fromDate = $('#fromDate').val();
					toDate = $('#toDate').val();

					window.location = "mod.php?p=invoice&a=history&by=date&fromDate=" + fromDate + "&toDate=" + toDate;
    			});
    			$('#selectDate').modal('toggle');
			} else if (which == "viewInvoicesByCustomer") {
				$('#modalGoCustomer').unbind('click');
				$('#fromDateCustomer').val('<?= date("Y") . "-"  . date("m") . "-01" ?>');
    			$('#modalGoCustomer').on('click', function(e) {
					fromDate = $('#fromDateCustomer').val();
					toDate = $('#toDateCustomer').val();
					var customer = $('#customerSelect').val();
					window.location = "mod.php?p=invoice&a=history&by=customer&customer=" + customer  + "&fromDate=" + fromDate + "&toDate=" + toDate;
    			});
    			$('#selectCustomer').modal('toggle');
			} else if (which == "deposithistory") {
				$('#modalGoDate').unbind('click');
				$('#fromDate').val('<?= date("Y") . "-"  . date("m") . "-01" ?>');
    			$('#modalGoDate').on('click', function(e) {
					fromDate = $('#fromDate').val();
					toDate = $('#toDate').val();

					window.location = "mod.php?p=payment&a=deposithistory&fromDate=" + fromDate + "&toDate=" + toDate;
    			});
    			$('#selectDate').modal('toggle');

			}else if(which == "reconciliationhistory"){
				$('#modalGoDate').unbind('click');
				$('#fromDate').val('<?= date("Y") . "-"  . date("m") . "-01" ?>');
    			$('#modalGoDate').on('click', function(e) {
					fromDate = $('#fromDate').val();
					toDate = $('#toDate').val();

					window.location = "mod.php?p=payment&a=admin.reconciliationhistory&fromDate=" + fromDate + "&toDate=" + toDate;
    			});
    			$('#selectDate').modal('toggle');
			} else if (which == "checksearch") {
    			$('#checkSearchButton').unbind('click');
				$('#checkSearchNumber').on("keyup", function(e){

					if(e.keyCode == 13){
						$('#checkSearchButton').click();
					}
				});
    			$('#checkSearchButton').on('click', function(e) {
    				document.location = "mod.php?p=payment&a=admin.checksearch&ckno=" + $('#checkSearchNumber').val();
    			});
    			$('#checkSearchModal').modal('toggle');
    		}else if(which == "reportViewer"){
						$('#reportViewerModal').modal();
				}else if(which == "paymentdetails"){
					var pid = prompt("Enter Payment ID:");
					window.location = "mod.php?p=payment&a=admin.paymentdetail&pid=" + pid;
				}else if(which == "viewAssignmentsByDate"){
					$('#modalGoDate').unbind('click');
	    			$('#modalGoDate').on('click', function(e) {
						fromDate = $('#fromDate').val();
						toDate = $('#toDate').val();
	    				window.location = "mod.php?p=assignments&a=history&fromDate=" + fromDate + "&toDate=" + toDate;
	    			});
	    			$('#selectDate').modal('toggle');
				}

    	}

    	function addCatalogCategoryDefinition() {
    		$('#catalogCategoryName').val('');
    		$('#catalogCategoryNameEN').val('');
    		$('#catalogCategorySort').val('');
    		$('#addEditCatalogCategoryTitle').text('Add Catalog Category');
    		$('#catalogCategoryIsNew').val('1');
    		$('#modalDeleteCatalogCategory').css('display', 'none');
    		$('#addEditCatalogCategory').modal('toggle');
    	}

    	function editCatalogCategoryDefinition() {
    		$('#catalogCategoryName').val('');
    		$('#catalogCategoryNameEN').val('');
    		$('#catalogCategorySort').val('');
    		var id = $('#catalogCategorySelect').val();
    		$.get("functions.php?action=getCatalogCategoryInfo&id=" + id, function(data){
    		 	var catalogCategory = JSON.parse(data);
    		 	$('#catalogCategoryIsNew').val("0");
    			$('#addEditCatalogCategoryTitle').text('Edit Catalog Category');
    			$('#modalDeleteCatalogCategory').css('display', '');
    			$('#catalogCategoryID').val(catalogCategory.CatalogID);
    			$('#catalogCategoryName').val(catalogCategory.Name);
    			$('#catalogCategoryNameEN').val(catalogCategory.NameEN);
    			$('#catalogCategorySort').val(catalogCategory.Sort);
    			$('#addEditCatalogCategory').modal('toggle');
    		});

    	}
    	function saveCatalogCategoryDefinition() {

    		var catalogCategoryName = $('#catalogCategoryName').val();
    		var catalogCategoryNameEN = $('#catalogCategoryNameEN').val();
    		var catalogCategorySort = $('#catalogCategorySort').val();
    		var isNew = $('#catalogCategoryIsNew').val();
    		var ID = $('#catalogCategoryID').val();
    		if (catalogCategoryName.trim() == "" || catalogCategoryName == null) {
    			alert("A Name is required!");
    			return false;
    		}
    		if (catalogCategoryNameEN.trim() == "" || catalogCategoryNameEN == null) {
    			alert("A Name in English is required!");
    			return false;
    		}
    		var catalogCategoryData = '{ "NameEN" : "' + catalogCategoryNameEN + '", "CategoryID" : "' + ID + '", "Name" 	:	"' + catalogCategoryName + '", "Sort" : "' + catalogCategorySort + '", "IsNew" : "' + isNew + '"}';
    		catalogCategoryData = JSON.parse(catalogCategoryData);
    		$.post( "functions.php?action=saveCatalogCategoryInfo", catalogCategoryData)
    		  .done(function( data ) {
    		  	data = JSON.parse(data);
    		    if (data.result === true) {
    		      	$('#addEditCatalogCategory').modal('toggle');
    		      	if (isNew > 0) {
    		      		$('#catalogCategorySelect').append($("<option></option>").attr("value",data.ID).text(catalogCategoryData.Name));
    		      		$('#catalogCategorySelect').data('combobox').refresh()
    		      	}else {
    		      		$('#catalogCategorySelect option[value=' + ID + ']').text(catalogCategoryData.Name);
    		      		$('#catalogCategorySelect').data('combobox').refresh();
    		      	}


    		      }else {
    		      	alert("There has been an error saving the catalog category data.\r\n\r\nMySQL Error: " + data.error);
    		      }
    		    });
    	}
    	/*  */
    	function deleteCatalogCategoryDefinition() {
    		var ID = $('#catalogCategoryID').val();
    		if (confirm("Are you sure you want to delete this Catalog Category Definition?")) {
    			$.post( "functions.php?action=deleteCatalogCategory&id=" + ID, null)
    			  .done(function( data ) {
    			  data = $.parseJSON(data);
    			    if (data.result == true) {
    			    	$('#catalogCategorySelect option[value=' + ID + ']').remove();
    			    	$('#catalogCategorySelect').data('combobox').clearTarget()
    			    	$('#catalogCategorySelect').data('combobox').clearElement()
    			    	$('#modalEditCatalogCategory').css('display', 'none');
    			    	$('#catalogCategorySelect').data('combobox').refresh();
    			    	$('#addEditCatalogCategory').modal('toggle')
    			    }else {
    			    	alert("There has been an error deleting the Catalog Category");
    			    }
    			  });
    		}
    	}
    	function addAltasDefinition() {
    		$('#altasName').val('');
    		$('#altasNameEN').val('');
    		$('#altasSort').val('');
    		$('#addEditAltasTitle').text('Add New Alta');
    		$('#altasIsNew').val('1');
    		$('#modalDeleteAltas').css('display', 'none');
    		$('#addEditAltas').modal('toggle');
    	}
    	function editAltasDefinition() {
    		$('#altasName').val('');
    		$('#altasNameEN').val('');
    		$('#altasSort').val('');
    		var id = $('#altasSelect').val();
    		$.get("functions.php?action=getAltasInfo&id=" + id, function(data){
    		 	var altas = JSON.parse(data);
    		 	$('#altasIsNew').val("0");
    			$('#addEditAltasTitle').text('Edit Alta');
    			$('#modalDeleteAltas').css('display', '');
    			$('#altasID').val(altas.ID);
    			$('#altasName').val(altas.Name);
    			$('#altasNameEN').val(altas.NameEN);
    			$('#altasDate').val(altas.DateLine);
    			$('#altasActive').prop('checked', parseInt(altas.Active));
    			$('#altasSort').val(altas.Sort);
    			$('#addEditAltas').modal('toggle');
    		});

    	}
    	function saveAltasDefinition() {

    		var altasName = $('#altasName').val();
    		var altasNameEN = $('#altasNameEN').val();
    		var altasDate = $('#altasDate').val();
    		var altasActive = $('#altasActive').prop('checked');
    		var altasSort = $('#altasSort').val();
    		var isNew = $('#altasIsNew').val();
    		var ID = $('#altasID').val();
    		if (altasName.trim() == "" || altasName == null) {
    			alert("A Name is required!");
    			return false;
    		}
    		if (altasNameEN.trim() == "" || altasNameEN == null) {
    			alert("A Name in English is required!");
    			return false;
    		}
    		var altasData = '{ "NameEN" : "' + altasNameEN + '", "AltaID" : "' + ID + '", "Name" 	:	"' + altasName + '", "Active" 	:	"' + altasActive + '", "DateLine" 	:	"' + altasDate + '", "Sort" : "' + altasSort + '", "IsNew" : "' + isNew + '"}';
    		altasData = JSON.parse(altasData);
    		$.post( "functions.php?action=saveAltasInfo", altasData)
    		  .done(function( data ) {
    		  	data = JSON.parse(data);
    		    if (data.result === true) {
    		      	$('#addEditAltas').modal('toggle');
    		      	if (isNew > 0) {
    		      		$('#altasSelect').append($("<option></option>").attr("value",data.ID).text(altasData.Name));
    		      		$('#altasSelect').data('combobox').refresh()
    		      	}else {
    		      		$('#altasSelect option[value=' + ID + ']').text(altasData.Name);
    		      		$('#altasSelect').data('combobox').refresh();
    		      	}


    		      }else {
    		      	alert("There has been an error saving the catalog category data.\r\n\r\nMySQL Error: " + data.error);
    		      }
    		    });
    	}
    	/*  */
    	function deleteAltasDefinition() {
    		var ID = $('#altasID').val();
    		if (confirm("Are you sure you want to delete this Alta Definition?")) {
    			$.post( "functions.php?action=deleteAltas&id=" + ID, null)
    			  .done(function( data ) {
    			  data = $.parseJSON(data);
    			    if (data.result == true) {
    			    	$('#altasSelect option[value=' + ID + ']').remove();
    			    	$('#altasSelect').data('combobox').clearTarget()
    			    	$('#altasSelect').data('combobox').clearElement()
    			    	$('#modalEditAltas').css('display', 'none');
    			    	$('#altasSelect').data('combobox').refresh();
    			    	$('#addEditAltas').modal('toggle')
    			    }else {
    			    	alert("There has been an error deleting the Catalog Category");
    			    }
    			  });
    		}
    	}
       	function viewOrders(filter) {
    		var customer, salesman, fromDate, toDate;

    		if (filter == "customer") {
    			fromDate = $('#fromDateCustomer').val();
    			toDate = $('#toDateCustomer').val();
    			var customer = $('#customerSelect').val();
    			window.location = "orderhistory.php?a=customer&customer=" + customer + "&fromDate=" + fromDate + "&toDate=" + toDate;
    		}else if (filter == "salesman") {
    			fromDate = $('#fromDateSalesman').val();
    			toDate = $('#toDateSalesman').val();
    			var salesman = $('#salesmanSelect').val();
    			window.location = "orderhistory.php?a=salesman&salesman=" + salesman + "&fromDate=" + fromDate + "&toDate=" + toDate;
    		}else {
    			fromDate = $('#fromDate').val();
    			toDate = $('#toDate').val();
    			window.location = "orderhistory.php?a=date&fromDate=" + fromDate + "&toDate=" + toDate;
    		}
    	}
		function viewTop25(filter) {
    		var customer, salesman, fromDate, toDate;

    		if (filter == "customer") {
    			fromDate = $('#fromDateCustomer').val();
    			toDate = $('#toDateCustomer').val();
    			var customer = $('#customerSelect').val();
    			window.location = "top25products.php?a=customer&customer=" + customer + "&fromDate=" + fromDate + "&toDate=" + toDate;
    		}else if (filter == "salesman") {
    			fromDate = $('#fromDateSalesman').val();
    			toDate = $('#toDateSalesman').val();
    			var salesman = $('#salesmanSelect').val();
    			window.location = "top25products.php?a=salesman&salesman=" + salesman + "&fromDate=" + fromDate + "&toDate=" + toDate;
    		}else {
    			fromDate = $('#fromDate').val();
    			toDate = $('#toDate').val();
    			window.location = "top25products.php?a=date&fromDate=" + fromDate + "&toDate=" + toDate;
    		}
    	}
    	function viewPickups(filter) {
    		var customer, salesman, fromDate, toDate;

    		if (filter == "customer") {
    			fromDate = $('#fromDateCustomer').val();
    			toDate = $('#toDateCustomer').val();
    			var customer = $('#customerSelect').val();
    			window.location = "pickuphistory.php?a=customer&customer=" + customer + "&fromDate=" + fromDate + "&toDate=" + toDate;
    		}else if (filter == "salesman") {
    			fromDate = $('#fromDateSalesman').val();
    			toDate = $('#toDateSalesman').val();
    			var salesman = $('#salesmanSelect').val();
    			window.location = "pickuphistory.php?a=salesman&salesman=" + salesman + "&fromDate=" + fromDate + "&toDate=" + toDate;
    		} else if (filter == "pickupID"){
				var PickupID = $('#pickupID').val();
				window.location = "pickuphistory.php?a=pickupID&pickupID=" + PickupID;
			} else {
    			fromDate = $('#fromDate').val();
    			toDate = $('#toDate').val();
    			window.location = "pickuphistory.php?a=date&fromDate=" + fromDate + "&toDate=" + toDate;
    		}
    	}
		function viewPickingIncentives(filter){
			var fromDate, toDate;
			fromDate = $('#fromDate').val();
    		toDate = $('#toDate').val();
    		window.location = "mod.php?p=pickingincentive&a=manage&fromDate=" + fromDate + "&toDate=" + toDate;
		}
    	function changePassword() {
    		var oldPass = $('[name=oldPassword]').val();
    		var newPass1 = $('[name=newPassword1]').val();
    		var newPass2 = $('[name=newPassword2]').val();
    		if(newPass1 != newPass2){
    			alert("Error! Passwords does not match!");
    			return;
    		}else{
    			if(checkPwd(newPass1) == "too_short"){
    				alert("Error! The password is too short!");
    				return;
    			}
    			if(checkPwd(newPass1) == "too_long"){
    				alert("Error! The password is too long!");
    				return;
    			}
    			if(checkPwd(newPass1) == "bad_char"){
    				alert("Error! The password has an invalid character!");
    				return;
    			}
    			if(checkPwd(newPass1) == "ok"){
    				$.get('functions.php?action=changePasswordAdmin&old=' + oldPass + '&new=' + newPass1, function(e) {


    					if(e == "success"){
    						alert("Password changed!");
    						jQuery('#passwordChange').modal('hide');
    					}else{
    						alert("Error occurred, check your old password");
    					}
    				});
    			}
    		}
    	}
    	function checkPwd(str) {
    	    if (str.length < 4) {
    	        return("too_short");
    	    } else if (str.length > 20) {
    	        return("too_long");
    	    } else if (str.search(/[^a-zA-Z0-9\!\@\#\$\%\^\&\*\(\)\_\+]/) != -1) {
    	        return("bad_char");
    	    }
    	    return("ok");
    	}
    	function clearPasswordForm() {
    		$('[name=oldPassword]').val("");
    	  $('[name=newPassword1]').val("");
    	  $('[name=newPassword2]').val("");
    	}
    	function showPasswordForm() {
    		clearPasswordForm();
    		$('#passwordChange').modal('show');
    	}
		function setToday() {

		}

		$('#altasSelect').on('change', function() {
			if ($('#altasSelect').val() == "") {
				$('#modalEditAltas').css('display', 'none');
			}else {
				$('#modalEditAltas').css('display', '');
			}
		});
		$('#catalogCategorySelect').on('change', function() {
			if ($('#catalogCategorySelect').val() === "") {
				$('#modalEditCatalogCategory').css('display', 'none');
			}else {
				$('#modalEditCatalogCategory').css('display', '');
			}
		});
		$("#selectSalesman").on('keyup', function(e){
			if(e.keyCode == "13"){
				$('#modalGoSalesman').click();
			}
		 });
		$("#selectDate").on('keyup', function(e){
			if(e.keyCode == "13"){
				$('#modalGoDate').click();
			}
		 });
		$("#selectCustomer").on('keyup', function(e){
			if(e.keyCode == "13"){
				$('#modalGoCustomer').click();
			}
		 });

		function sortUnorderedList(ul, sortDescending) {
			if(typeof ul == "string")
			  ul = document.getElementById(ul);

			// Idiot-proof, remove if you want
			if(!ul) {
			  //alert("The UL object is null!");
			  return;
			}

			// Get the list items and setup an array for sorting
			var lis = ul.getElementsByTagName("LI");
			var vals = [];

			// Populate the array
			for(var i = 0, l = lis.length; i < l; i++)
			  vals.push({"key": $(lis[i]).text(), "value" : lis[i].innerHTML });

			// Sort it
			vals.sort(function(a, b) {
				  var x = a.key;
				  var y = b.key;
				  return ((x < y) ? -1 : ((x > y) ? 1 : 0));
			  });


			// Sometimes you gotta DESC
			if(sortDescending)
			  vals.reverse();

			// Change the list on the page
			for(i = 0, l = lis.length; i < l; i++)
			  lis[i].innerHTML = vals[i].value;
		  }
		  $(document).ready(function(){
			setTimeout(function(){
				sortUnorderedList('adminManageMenu');
			}, 500);
			$('.dropdown-menu').css('max-height',$(window).height()-75);
			$('.dropdown-menu').css('overflow',"scroll");
		});



		$body = $("body");
		<?php
			if(isset($_REQUEST['p']) && $_REQUEST['p'] == "orderqueue"){

			}else{
		?>
		$(document).on({
			ajaxStart: function() { $body.addClass("loading");    },
			 ajaxStop: function() { $body.removeClass("loading"); }
		});
		<?php
			}
		?>
    </script>
