var contentCashDesk = {
	reloadSite: function() {
		$('#cashdesk_action').val('reload');
		
		$('#order_form').submit();
	},
	
	submitSite: function() {
		$('#cashdesk_action').val('ready2fly');
		
		$('#order_form').submit();
	}
};

$(document).ready(function() {
	
	if ($('#selection_klarna_raten').attr('checked') == 'checked' || $('#selection_klarna_rechnung').attr('checked') == 'checked') {
		$('#klarna_agreements_info').css({'display':'block'});
		$('#need_customer_burth_date').css({'display':'block'});
	}

	
	// gift certificate
	$('#giftCertificateCodeFields').change(function() {
		$('#has_gift_certificate').val('yes');
		contentCashDesk.reloadSite();
	});
	
	
//	$('#submitGiftCertificate').click(function(e) {
//		e.preventDefault();
//		$('#has_gift_certificate').val('yes');
//		contentCashDesk.reloadSite();
//	});
	
	// Addresses
	$('#billing_address_select,#shipping_address_select').change(function(e) { 
		if ($(this).prop('id') == 'billing_address_select') {
			var addressType = 'billing';
		} else if ($(this).prop('id') == 'shipping_address_select') {
			var addressType = 'shipping';
		}
		
		var billing = $(this).val();

		if (billing > 0) {
			if (addressType == 'shipping') {
				$('.shipping_address_form_container').css('display','inline');
			}
			var address = addresses[billing];

			for (var key in address) {
				if ($.trim(address[key]) != '') {
					$(':input[name="' + addressType + '[' + key +']"]').val(address[key]).css({
						'color': '#5C0069'
					});
				} else { 
					$(':input[name="' + addressType + '[' + key +']"]').css({
						'color': '#848484'
					});
				}
			}
			
			$('#' + addressType + '_save_message').html('');
			
		} else if (billing == -1) {
			if (addressType == 'shipping') {
				$('.shipping_address_form_container').css('display','none');
			}
		} else {  
			if (addressType == 'shipping') {
				$('.shipping_address_form_container').css('display','inline');
			}
			$('#' + addressType + '_address_container input.formatted').css({
				'color': '#848484'
			});
			
			$('#' + addressType + '_address_container select.formatted').each(function() {
				/*$('option[value=""]', this).prop({
					'selected': 'selected'
				});*/
			}).css({
				'color': '#848484'
			});
		}
	});
	
	
	$('#billing_address_select,#shipping_address_select').trigger('change');
	
	
	$('#billing_address_container input.formatted,#shipping_address_container input.formatted').focus(function() {
		if ($(this).val() == $(this).prop('title')) {
			$(this).val('');
		}
		
		$(this).css({
			'color': '#000000'
		});
	}).blur(function() {
		$(this).val($(this).val().trim());
		
		if ($(this).val() == '' || $(this).val() == $(this).prop('title')) {

		
			$(this).css({
				'color': '#848484'
			});
		} else {
			$(this).css({
				'color': '#5C0069'
			});
		}
	});
	

	$('#cashdesk_button_save_billing,#cashdesk_button_save_shipping').click(function(e) {
		e.preventDefault();
		var $this = this;
		
		if ($(this).prop('id') == 'cashdesk_button_save_billing') {
			var addressType = 'billing';
		} else if ($(this).prop('id') == 'cashdesk_button_save_shipping') {
			var addressType = 'shipping';
		}
		
		$('#' + addressType + '_save_message').html('Bitte warten ...').css({
			'color': '#000099'
		});
		
		var addressData = {
			'save_address': true
		};
		$('#' + addressType + '_address_container *[name^="' + addressType + '"]').each(function() {
			var field = $(this).prop('name')
				.replace(/billing\[/, '')
				.replace(/shipping\[/, '')
				.replace(/\]/, '')
			;

			if ($(this).val() != $(this).prop('title')) {
				addressData[field] = $(this).val();
			} else {
				addressData[field] = '';
			}
		});
		
		var jqxhr = $.ajax({
			url: window.location.href,
			type: "post",
			data: addressData,
			dataType: "json"
		})
		
		jqxhr.success(function(data){
			if (data.stat == 'OK') {
				$('#' + addressType + '_save_message').html('Daten erfolgreich gespeichert').css({
					'color': '#009900'
				});

				if (data.id > 0) {
					$('#' + addressType + '_address_select').append('<option value="' + data.id + '" selected="selected"></option>');
				}
				
				if ($($this).prop('id') == 'cashdesk_button_save_shipping' || $($this).prop('id') == 'cashdesk_button_save_billing' || data.id > 0) {
					$('#cashdesk_action').val('reload');

					$('#order_form').submit();
				}
			} else {
				$('#' + addressType + '_save_message').html('Bitte f&uuml;llen Sie alle Felder aus').css({
					'color': '#990000'
				});
			}
		});
		
		jqxhr.error(function() {
			$('#' + addressType + '_save_message').html('Bitte f&uuml;llen Sie alle Felder aus').css({
				'color': '#990000'
			});
		});
	});
	
	$('input[name="shipping_method"],input[name="payment_method"],#shipping_address_select').change(function() {
		if ($('#shipping_address_select').val() != '') {
			$('#cashdesk_action').val('reload');

			$('#order_form').submit();
		}
	});
        
	$('#billing_address_select').change(function() {
            
		if ($('#billing_address_select').val() != '') {
			$('#cashdesk_action').val('reload');

			$('#order_form').submit();
		}
	});
	
	$('.cashdesk_agreement_header').click(function() {
		var id = $(this).attr('id');
		
		$('#' + id + '_wrapper').slideToggle();
	});
	
        $('#billing_honorific').change(function(e) { 
            e.preventDefault();
            if ($(this).val() == '0' || $(this).val() == 1) {
                $('#billing_address_container .address_person').addClass('obligatory');
                $('#billing_address_container .address_company').removeClass('obligatory');
            }
            else if ($(this).val() == 2) {
                $('#billing_address_container .address_company').addClass('obligatory');
                $('#billing_address_container .address_person').removeClass('obligatory');
            }
            else {
                $('#billing_address_container .address_company').removeClass('obligatory');
                $('#billing_address_container .address_person').removeClass('obligatory');
            }

        });
	
        $('#shipping_honorific').change(function(e) { 
            e.preventDefault();
            if ($(this).val() == '0' || $(this).val() == 1) {
                $('#shipping_address_container .address_person').addClass('obligatory');
                $('#shipping_address_container .address_company').removeClass('obligatory');
            }
            else if ($(this).val() == 2) {
                $('#shipping_address_container .address_company').addClass('obligatory');
                $('#shipping_address_container .address_person').removeClass('obligatory');
            }
            else {
                $('#shipping_address_container .address_company').removeClass('obligatory');
                $('#shipping_address_container .address_person').removeClass('obligatory');
            }

        });
	
	
});
