/**
 * @author		Patrick Schröder
 * @copyright	2010 DI.NET AG
 * @license	GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
 */
function ShoppingCart() {

	this.init = function() {
		this.getCart();
	}

	this.getCart = function() {
		$.ajax({
			async:		false,
			cache:		false,
			type:		'POST',
			url:		RELATIVE_KAPPOO_DIR + 'index.php',
			data:		'action=ShoppingCartGet' + SID_ARG_2ND,
			dataType:	'json',
			global:		'false',
			success: function(data) {
				$("#shoppingCartItems").html(data['items']);
				$("#shoppingCartPrice").html(data['priceGrossTotal']);
			},
			error: function() {
				alert('Es ist ein Fehler aufgetreten. Das tut uns leid! :-(');
			}
		});
	}


	this.addToCartCalendar = function() {
		if($("#forename").val() == "" || $("#surname").val() == "") {
			alert("Bitte Vor- und Nachnamen angeben!");

			return false;
		}

		$.ajax({
			async:		false,
			cache:		false,
			type:		'POST',
			url:		RELATIVE_KAPPOO_DIR + 'index.php',
			data:		'form=ShoppingCartAdd&calendarType=' + $("#calendarType").val() + '&forename=' + $("#forename").val() + '&surname=' + $("#surname").val() + SID_ARG_2ND,
			dataType:	'json',
			global:		'false',
			success: function(data) {
				if(data['status'] == true) {
					shoppingcart.getCart();
				}
			},
			error: function() {
				alert('Es ist ein Fehler aufgetreten. Das tut uns leid! :-(');
			}
		});
	}

	this.deleteFromCart = function(itemID) {
		$.ajax({
			async:		false,
			cache:		false,
			type:		'POST',
			url:		RELATIVE_KAPPOO_DIR + 'index.php',
			data:		'form=ShoppingCartDelete&itemID=' + itemID + SID_ARG_2ND,
			dataType:	'json',
			global:		'false',
			success: function(data) {
				if(data['status'] == true) {
					window.location.reload();
				}
			},
			error: function() {
				alert('Es ist ein Fehler aufgetreten. Das tut uns leid! :-(');
			}
		});
	}

	this.proofCouponCode = function() {
		var couponCode = $("#couponCode").val();

		$.ajax({
			async:		false,
			cache:		false,
			type:		'POST',
			url:		RELATIVE_KAPPOO_DIR + 'index.php',
			data:		'form=CouponCode&couponCode=' + couponCode + SID_ARG_2ND,
			dataType:	'json',
			global:		'false',
			success: function(data) {
				if(data['status'] == true) {
					window.location.reload();
				} else {
					alert('Code nicht mehr verwendbar!');
				}
			},
			error: function() {
				alert('Es ist ein Fehler aufgetreten. Das tut uns leid! :-(');
			}
		});
	}
	
	this.getPaymentState = function(orderID) {
		$.ajax({
			async:		false,
			cache:		false,
			type:		'POST',
			url:		RELATIVE_KAPPOO_DIR + 'index.php',
			data:		'action=PaymentGetStatus&orderID=' + orderID + SID_ARG_2ND,
			dataType:	'json',
			global:		'false',
			success: function(data) {
				if(data['paid'] == 1) {
					window.location = RELATIVE_KAPPOO_DIR + 'index.php?page=ThankYou' + SID_ARG_2ND
				}
			},
			error: function() {
				alert('Es ist ein Fehler aufgetreten. Das tut uns leid! :-(');
			}
		});
	}
}

var shoppingcart = new ShoppingCart();

$(document).ready(function() {
	shoppingcart.init();
});
