/*global $, _, rfp*/

var rfp = {};

$(function(){
	$('#termin1von, #termin1bis').datepicker(
		{beforeShow: rfp.customRange}
	).change(rfp.validiereDatum);
	
	$('#fristende_datum').datepicker();
	
	//$('input').css('margin','1px solid blue');
	
	rfp.validiereDatum();
	
	var anzahlterminfelder = $('#terminebox dl dd').size();
	for(var i = 0; i < anzahlterminfelder; i++)
	{
		$('#termin' + (i + 2) + 'von').datepicker();
		$('#termin' + (i + 2) + 'von').change(rfp.weitererTerminChange);
	}

	$('input.terminetogglebox').live('click', function()
	{
		$('#'+this.id+'input').toggle();
	});
	
	$('#terminebutton').click(function(){
		$('#terminebox').show();

		var vorlage = $('#terminvorlage');

		for(var i = 0; i < 3; i++)
		{
			var terminNr = $('#terminebox dl dd').size()+2;

			$('#terminebox dl').append(vorlage.html().replace(/NEU/g, terminNr));
			$('#termin'+terminNr+'von').datepicker();
			$('#termin'+terminNr+'von').change(rfp.weitererTerminChange);
			if(terminNr == 2)
			{
				$('#termin2').click();
			}
		}
		
	});
	
	/*
	$('input.terminvon').live('change', function(){
		var anreisetermin = Date.parseExact($(this).val(),'dd.MM.yyyy');
		if(anreisetermin !== null)
		{
			$('#'+this.id.substring(0,this.id.indexOf('von'))+'bis').val(anreisetermin.addDays(rfp.getAnzahlUebernachtungen()).toString('dd.MM.yyyy'));
		}
		else
		{
			$(this).val("");
			$('#'+this.id.substring(0,this.id.indexOf('von'))+'bis').val("");
		}
	});
	*/
});

rfp.getAnzahlUebernachtungen = function ()
{
	var anreisedatum = Date.parseExact($('#termin1von').val(),'dd.MM.yyyy');
	var abreisedatum = Date.parseExact($('#termin1bis').val(),'dd.MM.yyyy');

	if(anreisedatum !== null && abreisedatum !== null)
	{
		var one_day=1000*60*60*24;
		return Math.round((abreisedatum.getTime()-anreisedatum.getTime())/(one_day));
	}
	else
	{
		return 0;
	}
};

rfp.updateTermine = function ()
{
	$('input.terminvon').each(function(){
		var anreisetermin = Date.parseExact($(this).val(),'dd.MM.yyyy');
		if(anreisetermin !== null)
		{
			$('#'+this.id.substring(0,this.id.indexOf('von'))+'bis').val(anreisetermin.addDays(rfp.getAnzahlUebernachtungen()).toString('dd.MM.yyyy'));
		}
	});
};

rfp.weitererTerminChange = function ()
{
	var anreisetermin = Date.parseExact($(this).val(),'dd.MM.yyyy');
	if(anreisetermin !== null)
	{
		$('#'+this.id.substring(0,this.id.indexOf('von'))+'bis').val(anreisetermin.addDays(rfp.getAnzahlUebernachtungen()).toString('dd.MM.yyyy'));
	}
	else
	{
		$(this).val("");
		$('#'+this.id.substring(0,this.id.indexOf('von'))+'bis').val("");
	}
};

rfp.formatUhrzeit = function ()
{
	var stunde, minute, wert;
	
	stunde = "";
	minute = "";

	wert = $(this).val().replace(/\s+/g, '');

	if (wert.indexOf(":") === -1  && (wert > 24 || wert < 0))
	{
		return;
	}

	if (!wert.match(/(\d{2}):(\d{2})/) && wert.length > 0)
	{
		if (wert.indexOf(":") === -1)
		{
			if (wert.length === 1)
			{
				stunde = "0" + wert;
			}
			else if (wert.length === 2)
			{
				stunde = wert;
			}
			minute = "00";
		}
		else
		{
			if (wert.indexOf(":") < 2)
			{
				stunde = "0" + wert.substring(0, 1);
			}
			else
			{
				stunde = wert.substring(0, 2);
			}

			minute = wert.substring(wert.indexOf(":") + 1);

			if ((minute.length === 0  || minute === ":"))
			{
				minute = "00";
			}
			else if ((minute.length === 1))
			{
				minute = "0" + minute;
			}
		}

		if (!stunde.match(/(\d{2})/) || !minute.match(/(\d{2})/))
		{
			return;
		}

		if (minute > 59)
		{
			minute = "00";
		}

		$(this).val(stunde + ":" + minute);
	}
};

/**************************************************************************************/
$(document).ready(function()
{
	var alletermine = {};	

	$('.checkmessetermin').live('change', function ()
	{
		if ($(this).attr('type') == 'checkbox')
		{
			var destination = $('#va_destination').val();
			var von 	 	= $(this).attr('id') + 'von';
			var bis		 	= $(this).attr('id') + 'bis';	
			var terminid 	= $('#' + von).attr('id').substr(0, ($('#' + von).attr('id').length - 3)).substr(6);
				

			if ($(this).is(':checked') && von != "" && destination != '')
			{			
				alletermine[terminid] = [$('#' + von).val(), $('#' + bis).val(), destination, terminid];
			}
			else
			{
				delete alletermine[terminid];
			}
			
			if (von != null && destination != '')
			{
				//////////////////
				//  Senden
				//////////////////
				$.post('rfp.php', {checkmessetermine: true, termine: alletermine}, function (data)
				{			
					if (data != '')
					{
						$('#infodiv').queue(function () 
						{
							$('#infodivcontent').html(data);
							$('#infodiv').fadeIn(500);
							$(this).dequeue();
						});
					}
					else
					{
						$('#infodiv').fadeOut(500);
					}
				});	
			}
		}
		else // Keine Checkbox
		{			
			if ($(this).hasClass('datum') || $(this).hasClass('datumklein'))
			{
				var destination = $('#va_destination').val();
				var von 		= $(this).attr('id').substr(0, ($(this).attr('id').length - 3)) + 'von';
				var bis 		= $(this).attr('id').substr(0, ($(this).attr('id').length - 3)) + 'bis';
				var terminid 	= $(this).attr('id').substr(0, ($(this).attr('id').length - 3)).substr(6);
					
				alletermine[terminid] = [$('#' + von).val(), $('#' + bis).val(), destination, terminid];

	
				if (von != null && destination != '')
				{
					//////////////////
					//  Senden
					//////////////////
					$.post('rfp.php', {checkmessetermine: true, termine: alletermine}, function (data)
					{			
						if (data != '')
						{
							$('#infodiv').queue(function () 
							{
								$('#infodivcontent').html(data);
								$('#infodiv').fadeIn(500);
								$(this).dequeue();
							});
						}
						else
						{
							$('#infodiv').fadeOut(500);
						}
					});
				}
				else
				{
					$('#infodiv').fadeOut(500);
				}
			}
			else // Destinationfeld
			{
				var destination = $('#va_destination').val();
				var datum 		= '';
				
				for (i in alletermine)
				{
					if (alletermine[i])
					{
						alletermine[i][2] = destination;
						
						if(datum == '')
						{
							datum = alletermine[i][0];
						}
					}						
				}

				if (destination != '' && datum != '')
				{
					//////////////////
					//  Senden
					//////////////////
					$.post('rfp.php', {checkmessetermine: true, termine: alletermine}, function (data)
					{									
						if (data != '')
						{
							$('#infodiv').queue(function () 
							{
								$('#infodivcontent').html(data);
								$('#infodiv').fadeIn(500);
								$(this).dequeue();
							});
						}
						else
						{
							$('#infodiv').fadeOut(500);
						}
					});
				}
				else
				{
					$('#infodiv').fadeOut(500);
				}
			}
		}
	});
	/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	
	
	$('#optionbis, #fristende_datum').datepicker({minDate: 0});
	
	$('#fristende_uhrzeit').change(rfp.formatUhrzeit);
	
	faerbeHotelauswahl();
	
	$(".hotelcheckbox").live("click", function() {
		
		var hotelid = Helper.matchId(this.id);
		
		if($("#hotelauswahl table.hotellist tbody tr").size() == 1)
		{
			$("#hotelauswahl, #anfragehotels").remove();
		}
		else
		{
			$('#hotelauswahl' + hotelid + ", #anfragehotel" + hotelid).remove();
			faerbeHotelauswahl();
		}
		$('#hotelcounter').text($("#hotelauswahl table.hotellist tbody tr").size());
		
		$.post("HotelAuswahlAjax.php", {hotelid: hotelid, action: 'delete'});
	});
	
	
});

function faerbeHotelauswahl()
{
	$("#hotelauswahl .hotellist tr:even").css({background: '#ffffff'});
	$("#hotelauswahl .hotellist tr:odd").css({background: '#F5F3F0'});
}


/*
function propagateChecked(hotelid, checked)
{
	$('input[name=hotel' + hotelid + ']').each(function() {
		this.checked = checked;
	});
	
	// Ajax
	var action = checked ? "add" : "delete";
	$.post("HotelAuswahlAjax.php", {hotelid: hotelid, action: action});

	// Kartenbezogenes
	if(typeof changeMarker != "undefined" && hotels[hotelid])
	{	
		//HotelsContainer
		hotels[hotelid].ausgewaehlt = checked;
		changeMarker(hotelid);
	}
	
	// Hotelauswahl anpassen
	if(checked)
	{
		$('#hotelauswahl').fadeIn(function(){if(jQuery.browser.msie) this.style.removeAttribute('filter');});
		$('#skyscraper').fadeOut();
		
		var tr = "<tr id='hotelauswahl" + hotelid + "' class='hidden'>"
				+ "<td>"
				+ "<input type='checkbox' name='hotel" + hotelid + "' class='inputbox hotelcheckbox' checked='checked' />"
				+ "</td>"
				+ "<td>"
				+ "<a href='../kunde/hotelanzeigen.php?hotelid=" + hotelid + "' target='_blank'>" + hotels[hotelid].name + "</a><br />"
				+ hotels[hotelid].ort
				+ "</td>"
				+ "</tr>"; 
		
		$(tr).appendTo("#hotelauswahl table.hotellist tbody");
		sortHotelauswahl();
		$("#hotelauswahl" + hotelid ).fadeIn(function(){if(jQuery.browser.msie) this.style.removeAttribute('filter');});
		
	}
	else
	{
		if($("#hotelauswahl table.hotellist tbody tr").size() == 1)
		{
			$("#hotelauswahl").fadeOut();
		}
		$('#hotelauswahl' + hotelid).fadeOut(function(){$(this).remove(); sortHotelauswahl();});
		
	}
	
}
*/
