var date = new Date();
var d = date.getDate();
var m = date.getMonth();
var y = date.getFullYear();

var start_date;
var end_date;
var anzahl=0;
var eventclass;
var type=1;

function dump(obj) {
    var out = '';
    for (var i in obj) {
        out += i + ": " + obj[i] + "\n";
    }

    alert(out);

    // or, if you wanted to avoid alerts...

    var pre = document.createElement('pre');
    pre.innerHTML = out;
    document.body.appendChild(pre)
}



function ajaxValidationCallback(status, form, json, options){
	 if (window.console) {
         console.log(status);
	 }
    if (status === true) {
    	if(json[0][0]=='login_mail1' || json[0][0]=='login_mail2') {
    		window.location = json[0][3];
    	}
    }
}

function setEvent() {
	 $('#calendar').fullCalendar('removeEvents', 001);
		$('#calendar').fullCalendar('renderEvent', {
			title : 'Meine Reservierung',
			start : start_date,
			end : end_date,
			id : 001,
			allDay:true,
			className:eventclass,
		});
}

function startdate () {
	 if(!end_date) {$('.end-date').val($('.start-date').val());end_date=start_date;}
		if(end_date<start_date) {
				$('.end-date').val($('.start-date').val());
				end_date=start_date;
			}
	setDays();
 $('#calendar').fullCalendar('gotoDate', start_date); 
}

function enddate() {
	 if(!start_date){$('.start-date').val($('.end-date').val());start_date=end_date;}
     if(end_date<start_date) {
			$('.end-date').val($('.start-date').val());
			end_date=start_date;
		}
     setDays();
}

function refreshDate() {
	setEvent();
}

function setDays() {
	anzahl = Math.round((end_date-start_date)/60/60/24/1000+1);
	$('.anzahl').val(anzahl);
	setEvent();
	checkDate();
}

function checkDate() {
	if(start_date&&end_date){
	$('#response .msg').css('display','none');
	$('#response .loading').css('display','block');
	$('#submit').attr('disabled','disabled');

	$.post("scripts/functions.php", {ajax: "1", checkdate: "1", start:+start_date, ende:+end_date, anzahl:+$('.anzahl').val()},
		function(response) {
		if(response.sub){
			alert(response.sub);
		}
		if(response.preis){
			$('.preis .gesamt').html(response.preis);
			$('.preis .bill').html(response.bill);
		} else {
			$('.preis .gesamt').html('');
			$('.preis .bill').html('');
		}
		if(response.errormsg) {
			$('#response .errormsg').html(response.errormsg);
		}
		eventclass = response.className;
		anzahl = $('.anzahl').val();
		setEvent();
		$('#response .msg').css('display','none');
		$('#response .'+response.status).show('slide', {direction: 'right'}, 500);
		$('#response .loading').css('display','none');
		if(response.status!='locked') {$('#submit').removeAttr('disabled');}
	   }, 'json');
	  }
}
