/*datetimepicker : to select several dates*/

$(function() {
    /*Le champs de selection de date(class dtp et le champs doit etre optionnel)*/
    var $dtp = $("div.dtp > div.content > .date-pick");
    var $div_date_picked = $("div.datepicker .content");
    /*Le champs textarea qui recevra les dates choisies (class datepicker et peut etre obligatoire)*/
    var $textarea_date_picked = $("div.datepicker div.content > textarea");
	$textarea_date_picked.prop("readonly", true);
	$textarea_date_picked.css("display", "None");

    /*A chaque changement de selection de date dans le champs datetimepicker*/	
    $dtp.change(function() {
	var count = $("div.rm_date > a").length;
	var currdate = $(this).val();
	var $txtarea = $textarea_date_picked.val();
	if($txtarea.indexOf($(this).val()) == -1) {
	    $div_date_picked.prepend("<div class='rm_date'>" + currdate + "&nbsp;<a href='#' class=\"rm_date rm_date_"+count+"\">[-]</a><br></div>");
            $("a.rm_date_"+count, "div.rm_date").bind('click', {currdate:currdate, obj:$textarea_date_picked}, click_rm_date);
	    $textarea_date_picked.val(currdate + "," + $txtarea);
	}
	$(this).val("");
    });

    /*Lors d'un retour en arriere ou lors de la page de validation dans le formulaire pour reafficher les dates choisies*/
    $div_date_picked.ready(function(){
	    var lst_dates = $textarea_date_picked.val().split(",");
	    if(lst_dates != Array.isArray("")){
	        for (var i = 0; i<lst_dates.length - 1; i++){
	            $div_date_picked.append("<div class='rm_date'>" + lst_dates[i] + "&nbsp;<a href='#' class=\"rm_date rm_date_"+i+"\">[-]</a><br></div>");
		    $("a.rm_date_"+i, "div.rm_date").bind('click', {currdate:lst_dates[i], obj:$textarea_date_picked}, click_rm_date);
	        }
            }
            $("div.form-validation a.rm_date").remove();
	});

    /*Pour supprimer des dates de la selection*/
    var click_rm_date = function(event) {
	$(this).parent().remove()
	var $textarea = event.data.obj;
	var currdate = event.data.currdate;
	$textarea.val($textarea.val().replace(currdate+",",""));
    }
});
