function getAutoSuggest(obj) {
	document.location = obj.id;
}

function str_replace(search, replace, subject) {
	return subject.split(search).join(replace);
}

function setClass(element, className) {
	element.className = element.className + ' ' + className;
}

function removeClass(element, className) {
	element.className = str_replace(' ' + className, '', element.className);
}

function setSource(element, src) {
	element.src = src;
}

function hideAllProductDetails() {
	$("tr.product-details").hide();
}

function toggleProductDetails(elementToActivate, productID) {
	var tr = $("tr.selected");
	if (tr.attr('id') != undefined) {
		removeClass(document.getElementById(tr.attr('id')), 'selected');
		if (document.getElementById("product-details-"+productID).id == document.getElementById("product-details-" + str_replace('product-','',tr.attr('id'))).id) {
			// gleiche ID, also nochmal draufgeklickt
			$("#product-details-" + str_replace('product-','',tr.attr('id'))).fadeOut(500);
		}
		else {
			$("#product-details-" + str_replace('product-','',tr.attr('id'))).fadeOut(500, function() { selectProduct(elementToActivate, productID) });
		}
	}
	else {
		selectProduct(elementToActivate, productID);
	}
}

function selectProduct(elementToActivate, productID) {
	setClass(elementToActivate, 'selected');
	$("#product-details-"+productID).fadeIn(500);
}

function deleteCartItem(cartItemId, checkout) {
	$.getJSON ("/lib/ajax/deleteCartItem.php", {
			cartItemId: cartItemId,
			checkout: checkout
		},
		function(json) {
			$('tr#cart-item-' + cartItemId).remove();
			if (!checkout) {
				$('span#total-cart-value').text(json.cartValue);
				if ($('tr.item').length == 0) {
					/* Keine Waren mehr im Warenkorb */
					$('#cartView thead tr:last-child').hide();
					$('#cartView tbody tr').hide();
					$('#cartView tfoot').hide();
					$('tr#no-items').fadeIn();
				}
			}
			else {
				updateValues(json);
			}
		}
	);
}

function editCartItem(cartItemId) {
	var spanDays		= $('span#cart-item-' + cartItemId + '-days-span');
	var spanAmount	= $('span#cart-item-' + cartItemId + '-amount-span');
	var spanSubmit	= $('span#cart-item-' + cartItemId + '-submit-span');
	var inputDays		= $('input#cart-item-' + cartItemId + '-days');
	var inputAmount	= $('input#cart-item-' + cartItemId + '-amount');
	var inputSubmit	= $('input#cart-item-' + cartItemId + '-submit');
	
	spanDays.hide();
	spanAmount.hide();
	spanSubmit.hide();
	inputDays.attr('value', spanDays.text());
	inputAmount.attr('value', spanAmount.text());
	inputDays.show();
	inputAmount.show();
	inputSubmit.show();
}

function updateCartItem(cartItemId, what, amount) {
	if (what == 'days') {
		var spanDays = $('td#product-days-'+cartItemId);
		var spanAmount = $('td#product-amount-'+cartItemId);
		
		var days = parseInt(spanDays.text()) + amount;
		var amount = parseInt(spanAmount.text());
	}
	else if (what == 'amount') {
		var spanDays = $('td#product-days-'+cartItemId);
		var spanAmount = $('td#product-amount-'+cartItemId);
		
		var days = parseInt(spanDays.text());
		var amount = parseInt(spanAmount.text()) + amount;
	}
	else {
		var spanDays		= $('span#cart-item-' + cartItemId + '-days-span');
		var spanAmount	= $('span#cart-item-' + cartItemId + '-amount-span');
		var spanSubmit	= $('span#cart-item-' + cartItemId + '-submit-span');
		var spanTotal		= $('span#cart-item-' + cartItemId + '-total-span');
		var inputDays		= $('input#cart-item-' + cartItemId + '-days');
		var inputAmount	= $('input#cart-item-' + cartItemId + '-amount');
		var inputSubmit	= $('input#cart-item-' + cartItemId + '-submit');
		
		var days		= parseInt(inputDays.attr('value'));
		var amount	= parseInt(inputAmount.attr('value'));
	}
	
	if (isNaN(days) || isNaN(amount)) {
		alert("Bitte geben Sie nur Zahlen ein!");
	}
	else {
		$.getJSON("/lib/ajax/updateCartItem.php", {
				cartItemId: parseInt(cartItemId),
				cartDays: days,
				cartAmount: amount,
				shopAction: "updateItem"
			},
			function(json) {
				if (json == false) {
					alert('Ihre Eingabe ist fehlerhaft. Bitte überprüfen Sie gewählten Werte!');
				}
				else {
					if (what == 'days' || what == 'amount') {
						updateValues(json);
					}
					else {
						inputDays.hide();
						inputAmount.hide();
						inputSubmit.hide();
						spanDays.text(inputDays.attr('value'));
						spanAmount.text(inputAmount.attr('value'));
						spanSubmit.text(json.discount);
						spanTotal.text(json.total);
						spanDays.show();
						spanAmount.show();
						spanSubmit.show();
						$('span#total-cart-value').text(json.cartTotal);
						
						if (json.items) {
							for (var el in json.items) {
								$('#cart-item-'+el+'-submit-span').text(json.items[el]['discount']);
								$('#cart-item-'+el+'-total-span').text(json.items[el]['total']);
								$('#cart-item-'+el+'-days-span').text(json.items[el]['days']);
								$('#cart-item-'+el+'-amount-span').text(json.items[el]['amount']);
							}
						}
					}
				}
			}
		);
	}
}

function checkAddItem(productId) {
	if (isNaN($('#product-' + productId + '-days').attr('value')) || isNaN($('#product-' + productId + '-amount').attr('value'))) {
		alert("Bitte geben Sie nur Zahlen ein!");
		return false;
	}
	return true;
}

function checkDate(element, type, doAlert) {
	$.getJSON("/lib/ajax/checkDate.php", {
		date: element.value,
		type: type
		},
		function(json) {
			if (json.reply == 'false') {
				document.getElementById(element.id).focus();
				if (doAlert) {
					if (json.error) {
						alert(json.error);
						$('#dateFrom').attr('value', json.dateFrom);
						$('#dateTo').attr('value', json.dateTo);
					}
					else alert('Bitte geben Sie ein korrektes Datum im Format dd.mm.yyyy ein!');
				}
				return false;
			}
			else {
				element.value = json.date;
				updateValues(json);
				return true;
			}
		}
	);
}

function updateValues(json) {
	$('span#total-subtotal').text(json.totalSubtotal);
	$('span#total-insurance').text(json.totalInsurance);
	$('span#total-discount').text(json.totalDiscount);
	$('span#total-net-value').text(json.totalNetto);
	$('span#total-tax').text(json.totalTax);
	$('span#total-total').text(json.totalTotal);
	if (json.items) {
		for (var el in json.items) {
			$('#product-discount-'+el).text(json.items[el]['discount']);
			$('#product-total-'+el).text(json.items[el]['total']);
			$('#product-days-'+el).text(json.items[el]['days']);
			$('#product-amount-'+el).text(json.items[el]['amount']);
		}
	}
	$('input#dateFrom').attr('value', json.dateFrom);
	$('input#dateTo').attr('value', json.dateTo);
	$('#dateFrom').datepicker('destroy');
	$('#dateFrom').datepicker({minDate:new Date(), maxDate:new Date(json.dateToJSYear,json.dateToJSMonth,(json.dateToJSDay-1))});
	$('#dateTo').datepicker('destroy');
	$('#dateTo').datepicker({minDate:new Date(json.dateFromJSYear,json.dateFromJSMonth,json.dateFromJSDay)});
	if (json.hasWeekend == 'true') {
		$('span.product-discount').text('0,00');
	}
}

function decreaseDays(cartItemId) {
	updateCartItem(cartItemId, 'days', -1);
}

function increaseDays(cartItemId) {
	updateCartItem(cartItemId, 'days', 1);
}

function decreaseAmount(cartItemId) {
	updateCartItem(cartItemId, 'amount', -1);
}

function increaseAmount(cartItemId) {
	updateCartItem(cartItemId, 'amount', 1);
}

function showTooltip(element, word) {
  var offset = $(element).offset();
	$('#tooltip').css('top', offset.top+10+'px');
	$('#tooltip').css('left', offset.left+20+'px');
	$('#tooltip h3').html('<img src="/lib/img/layout/loading.gif" />');
	$('div#tooltip').show();

	$.getJSON("/lib/ajax/tooltip.php", {
		word: word
		},
		function(json) {
			$('#tooltip h3').html(json);
		}
	);
}

function hideTooltip() {
	$('div#tooltip').hide();
}
