<?php

	if(isset($_POST['dateRange'])) 
	{
		 
		 print_r($_POST['dateRange']);
	}

?>

<!DOCTYPE html>
<html lang="en"><head>
<meta http-equiv="content-type" content="text/html; charset=windows-1252">

<!--
<script src="week_fichiers/jquery.js"></script>
<script type="text/javascript" src="week_fichiers/jquery-ui.js"></script>
<link rel="stylesheet" type="text/css" media="screen" href="week_fichiers/jquery-ui.css">
-->
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.js"></script>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.14/jquery-ui.min.js"></script>
    <link rel="stylesheet" type="text/css" media="screen" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.14/themes/base/jquery-ui.css">

<script>	
	function truc()
  {
  alert(dateRange)
  }
</script>	


<style>
.disabled span{
    color:black !important;
    background:#808080 !important;    
}
.enabled a{
    color:black !important;
    background:#9DEA3A !important;    
}

</style>

<script type="text/javascript">
		
/*		var disabledSpecificDays = ["2014/11/2014"];
		
		function disableSpecificDaysAndWeekends(date) {
    var m = date.getMonth();
    var d = date.getDate();
    var y = date.getFullYear();
 
    for (var i = 0; i < disabledSpecificDays.length; i++) {
        if ($.inArray((m + 1) + '-' + d + '-' + y, disabledSpecificDays) != -1 || new Date() > date) {
            return [false];
        }
    }
 
    var noWeekend = $.week-picker.noWeekends(date);
    return !noWeekend[0] ? noWeekend : [true];
}
*/

var unavailableDates = ["8-11-2014","9-11-2014", "10-11-2014", "11-11-2014","12-11-2014","13-11-2014","14-11-2014", "15-11-2014"];

    function unavailable(date) {
        dmy = date.getDate() + "-" + (date.getMonth() + 1) + "-" + date.getFullYear();
        if ($.inArray(dmy, unavailableDates) == -1) {
            return [true, ""];
        } else {
            return [false, "", "Unavailable"];
        }
    }
	
	
	function getSelectedDates()
{
	var the_selected_dates = new Array();
    $.ajax(
    {
        url: 'calendrier_mysql.php',
        dataType: 'json',
        async: false,
        success: function(data)
        {	/*alert(data);
			$.each(data, function(n, val)
            {
                the_selected_dates[val.daten] = val;
            });*/
			the_selected_dates = data
        }
    });
	
    return the_selected_dates;
	
}  
		
		
function non(date)
		{
			dmy = date.getDate() + "-" + (date.getMonth()+1) + "-" + date.getFullYear();
		    if ($.inArray(dmy, selected_dates) >= 0) 
		    {
			  return [false,"disabled",""];   
		    }
	  		else  return [true,"enabled",""];
		}
		
		
function louer(date) {
		alert(selected_dates);

       dmy = date.getDate() + "-" + (date.getMonth() + 1) + "-" + date.getFullYear();
        if ($.inArray(dmy, selected_dates) == -1) {
            return [true, ""];
        } else {
            return [false, "", "Unavailable"];
        }
		
    }		
			var startDate,
				endDate,
				dateRange = [];
				
			var selectCurrentWeek = function() {
				window.setTimeout(function () {
					$('.week-picker').find('.ui-datepicker-current-day a').addClass('ui-state-active');
				}, 1);
			}
			
			var disableDates = function(date) {
				var dateString = jQuery.datepicker.formatDate('dd/mm/yyyy', date);
				return [dateRange.indexOf(dateString) == -1];
			}

			function addWeek(weekNum, startDate, endDate){
				$('.weeks-chosen').append('<div data-startdate="'+startDate+'" data-enddate="'+endDate+'"><span class="weekNum">Sem. '+ (weekNum - 1) +'</span> - <span class="startDate">'+startDate+'</span> - <span class="endDate">'+endDate+'</span> | <span class="remove">X -> Supprimer</span></div>');
			}
			
			function removeFromArray(array, item) {
				while((index = array.indexOf(item)) > -1)
					array.splice(index,1);
			}
			
			function enableDates(startDate, endDate) {
				for (var d = new Date(startDate);
					d <= new Date(endDate);
					d.setDate(d.getDate() + 1)) {
					
					removeFromArray(dateRange, $.datepicker.formatDate('dd/mm/yyyy', d));
				}
				buildDatepicker();
			}
			
			function buildDatepicker(){
			
				$('.week-picker').datepicker('destroy');
			
				$('.week-picker').datepicker( {
					//defaultDate: '01/01/2014',
					dateFormat: 'dd/mm/yy',
					//showOtherMonths: true,
					//selectOtherMonths: true,
					altField: "#datepicker",
					closeText: 'Fermer',
					prevText: 'Précédent',
					nextText: 'Suivant',
					currentText: 'Aujourd\'hui',
					monthNames: ['Janvier', 'Février', 'Mars', 'Avril', 'Mai', 'Juin', 'Juillet', 'Août', 'Septembre', 'Octobre', 'Novembre', 'Décembre'],
					monthNamesShort: ['Janv.', 'Févr.', 'Mars', 'Avril', 'Mai', 'Juin', 'Juil.', 'Août', 'Sept.', 'Oct.', 'Nov.', 'Déc.'],
					dayNames: ['Dimanche', 'Lundi', 'Mardi', 'Mercredi', 'Jeudi', 'Vendredi', 'Samedi'],
					dayNamesShort: ['Dim.', 'Lun.', 'Mar.', 'Mer.', 'Jeu.', 'Ven.', 'Sam.'],
					dayNamesMin: ['D', 'L', 'M', 'M', 'J', 'V', 'S'],
					weekHeader: 'Sem.',
					firstDay: 6,
					//minDate: '01/01/2014',
					//maxDate: '31/12/2014',
					changeMonth: true,
					changeYear: true,
					showWeek: true,
					showOtherMonths: true,
					selectOtherMonths: true,
					numberOfMonths: 12,
					onSelect: function(dateText, inst) { 
						var date = $(this).datepicker('getDate');
						startDate = new Date(date.getFullYear(), date.getMonth(), date.getDate() - date.getDay()-1);
						endDate = new Date(date.getFullYear(), date.getMonth(), date.getDate() - date.getDay() + 6);
						var dateFormat = inst.settings.dateFormat || $.datepicker._defaults.dateFormat;
						
						addWeek($.datepicker.iso8601Week(new Date(dateText)), $.datepicker.formatDate( dateFormat, startDate, inst.settings ), $.datepicker.formatDate( dateFormat, endDate, inst.settings ));

						for (var d = new Date(startDate);
							d <= new Date(endDate);
							d.setDate(d.getDate() + 1)) {
								dateRange.push($.datepicker.formatDate('dd/mm/yyyy', d));
						}
						
						buildDatepicker();						
						selectCurrentWeek();
					},
					beforeShowDay: 	disableDates, 
					onChangeMonthYear: function(year, month, inst) {
						selectCurrentWeek();
					}
				});
				
				$('#dateRange').val(dateRange);
				
			}

			
			$(document).ready(function() {
			
			var selected_dates = new Array();
			selected_dates = getSelectedDates();
			//alert(selected_dates);
			//alert("plouf");
			
				buildDatepicker();
				
				$('.week-picker .ui-datepicker-calendar tr').live('mousemove', function() { $(this).find('td a').addClass('ui-state-hover'); });
				$('.week-picker .ui-datepicker-calendar tr').live('mouseleave', function() { $(this).find('td a').removeClass('ui-state-hover'); });

				$('.remove').live('click', function(e){
					enableDates($(this).parent('div').attr('data-startdate'), $(this).parent('div').attr('data-enddate'));
					$(this).parent('div').remove();
				});

			});
		
		</script>
		
		<style type="text/css">
			
			body
			{
				font-family: Arial, sans-serif;
				font-size: 12px;
			}
			
			.ui-datepicker-multi
			{	
				font-size: 12px;
				width: 640px !important;
			}
				.ui-datepicker-multi .ui-datepicker-group
				{
					
					margin: 5px;
					border: #ccc 1px solid;
					padding: 5px;
				}
				
			span.remove
			{
				color: red;
				cursor: pointer;
			}
		
			.ui-datepicker-prev,
			.ui-datepicker-next
			{
				display: none !important;
			}
		
		</style>
		
	</head>
	<body>
		<div style="float:left;width:640px;">

			<div id="dp" class="week-picker hasDatepicker">

			</div>

		</div>
		
		<div style="float:left;width:320px;margin-left:40px;">
		
			<h3>Semaines selectionnées:</h3>
			
			<div class="weeks-chosen"></div>
			
		</div>
		

		<form action="test_week.php" method="post">
		<input id="dateRange" name = "dateRange"  type="hidden">
		<input type="submit" value="Envoyer" /> 
	<!--	<INPUT type="button" onClick="truc()"> -->
		</form>

<!--
<div id="ui-datepicker-div" class="ui-datepicker ui-widget ui-widget-content ui-helper-clearfix ui-corner-all"></div>
-->



</body></html>

<?php
// une redirection
//document.location.href = 'http://www.url.de/ta/page.php?tableau[]=javascript&tableau[]=toto&tableau[]=etcaetera';
//http://mapage.net?variable=valeur
?>