(function($) {



	/**
	 * Standard LSO form
	 */
	
	$.fn.lsoForm = function() {
		
		return this.each(function() {
		
			var $form = $(this);
		
			var validate = function() { return false; }
			
			$form.find('.form-send').click(function() {
				$form.submit();
				return false;
			});
		
			var submitted = false;
			$form.submit(function() {
				
				// Don't resbumit the form
				if (submitted) {
					return false;
				}
				
				validate = function() {
					$form.find('.error').removeClass('error');
					$form.find('.text.required').each(function() {
						if ($.trim($(this).val()) == '') {
							$(this).parent().addClass('error');
						}
					});
					
					var valid = ($form.find('.error').length == 0);
					return valid;
				}
				
				
				var data = {};
				$.each($(this).serializeArray(), function(i,field) {
					data[field.name] = encodeURIComponent(field.value);
				});
								
				if (validate()) {
					submitted = true;
					$form.find('input,textarea').attr('disabled','disabled');
					$.post($form.attr('action').substring(1), data, function(html) {
						if ($.trim(html)=='ok') {
							$form.find('.lso-success').lsoPanel().click();
						}
					});
				}
			
			
				return false;
			});
			
		});
		
	}



	/**
	 * Shop
	 */
	
	$.fn.lsoShopOrderForm = function() {
					   
		return this.each(function() {

			var $obj = $(this);
						
			$obj.click(function() {
				
				var $wrapper = $obj.parents('.lso-shop-article').eq(0);
			
				$obj.css({visibility: 'hidden'});
			
				$.get($obj.attr('href'),function(html) {
					var action = $obj.attr('href').replace("/repository/proxy/content/service/sites/sinfonieorchester/forms/","/repository/proxy/content/service/sites/sinfonieorchester/forms/submit/");
					
					var validate = function() { return false; } /* The actual validate method gets defined in the submit event handler */

					var $form = $("<div></div>").attr('class','lso-shop-form lso-shop-article').append('<div class="lso-dotted-m-top"><\/div>').append(html);
					
					$wrapper.after($form);
					
					
					$form.find('.form-clear').click(function() {
						$form.find('form').get(0).reset();
						validate = function() { return false; } /* Disable validation until the next submit attempt */
						$form.find('.error').removeClass('error');
						return false;
					});
					
					$form.find('.form-send').click(function() {
						$form.find('form').submit();
						return false;
					});
					
					
					var submitted = false;
					$form.find('form').submit(function() {
						
						if (submitted) return;
						
						var $f = $(this);
												
						validate = function() { /* Define the actual validate hanlder */
							$f.find('.error').removeClass('error');
							$f.find('.text.required').each(function() {
								var $req = $(this);
								if ($.trim($req.val()) == '') {
									$req.parent().addClass('error');
								}
							});
							$f.find('.radio.required').each(function() {
								var c = $(this).find('.radio:checked');
								if (c.length == 0) {
									$(this).addClass('error');
								}
							});
						
							// $f.find('input').change(function() {
							// 	validate();
							// });
							
							return ($f.find('.error').length==0)
						}
						
						if (validate()) {
							submitted = true;
							var data = {}
							$.each($(this).serializeArray(), function(i,field) {
								data[field.name] = encodeURIComponent(field.value);
							});
							$.post(action, data, function(html) {
								if ($.trim(html)=='ok') {
									$form.find('.lso-success').lsoPanel().click();
									$form.remove();
								}
								else {
									$form.fadeTo(100,1);
									$('input').attr("disabled","false");
								}
							});
							$form.fadeTo(500,0.5);
							$('input').attr("disabled","true");
						}
						
						return false;
					});
					
					$form.fadeTo(1000,1);
				});
			
				return false;
				
			});
			
			
			
		});
		
	}
	


	/**
	 * Abo
	 */
	
	$.fn.lsoAboOrderForm = function() {
		
		return this.each(function() {

			var $obj = $(this);
						
			$obj.click(function() {
				
				
				var $wrapper = $obj.parents('.lso-event-category-description').eq(0);
				var $form = $wrapper.find('.lso-abo-order-form-wrapper').eq(0);
							
				$.get($obj.attr('href'),function(html) {
					var action = $obj.attr('href').replace(	"/repository/proxy/content/service/sites/sinfonieorchester/forms/",
															"/repository/proxy/content/service/sites/sinfonieorchester/forms/submit/"
															);
					
					var validate = function() { return false; } /* The actual validate method gets defined in the submit event handler */

					$form.html('<div class="lso-dotted-top"><\/div>'+html).show();
					$wrapper.css({height:'auto'}).show();
					
					
					$form.find('.form-clear').click(function() {
						$form.find('form').get(0).reset();
						validate = function() { return false; } /* Disable validation until the next submit attempt */
						$form.find('.error').removeClass('error');
						return false;
					});
					
					$form.find('.form-send').click(function() {
						$form.find('form').submit();
						return false;
					});
					
					
					var submitted = false;
					$form.find('form').submit(function() {
												
						if (submitted) return;
						
						var $f = $(this);
												
						validate = function() { /* Define the actual validate hanlder */
							$f.find('.error').removeClass('error');
							$f.find('.text.required').each(function() {
								var $req = $(this);
								if ($.trim($req.val()) == '') {
									$req.parent().addClass('error');
								}
							});
							$f.find('.radio.required').each(function() {
								var c = $(this).find('.radio:checked');
								if (c.length == 0) {
									$(this).addClass('error');
								}
							});
						
							// $f.find('input').change(function() {
							// 	validate();
							// });
							
							return ($f.find('.error').length==0)
						}
						
						if (validate()) {
							submitted = true;
							var data = {}
							$.each($(this).serializeArray(), function(i,field) {
								data[field.name] = encodeURIComponent(field.value);
							});
							$.post(action, data, function(html) {
								if ($.trim(html)=='ok') {
									//$itemWrapper.find('.lso-event-item-overview>a').click();
									$form.find('.lso-success').lsoPanel().click();
									$form.html('').css({opacity:1}).hide();
								}
								else {
									$form.fadeTo(100,1);
									$('input').attr("disabled","false");
								}
							});
							$form.fadeTo(500,0.5);
							$('input').attr("disabled","true");
						}
						
						return false;
					});
					
				});
			
				return false;
			
			});

		});
		
	}


	/**
	 * Ticket
	 */
	
	$.fn.lsoTicketOrderForm = function() {
					   
		return this.each(function() {

			var $obj = $(this);
						
			$obj.click(function() {
				
				var $wrapper = $obj.parents('.lso-event-item').children('.lso-event-detail').eq(0);
			
				$.get($obj.attr('href'),function(html) {
					var action = $obj.attr('href').replace("/repository/proxy/content/service/sites/sinfonieorchester/forms/","/repository/proxy/content/service/sites/sinfonieorchester/forms/submit/");
					
					var validate = function() { return false; } /* The actual validate method gets defined in the submit event handler */

					var $data = $wrapper.find('.lso-event-data');
					$data.css({visibility:'hidden'});

					$data.before('<div class="lso-event-form">'+html+'<\/div>');
					var $form = $wrapper.find('.lso-event-form');
					
					$form.css({opacity:0});
					
					$itemWrapper = $wrapper.parents('.lso-event-item-wrapper');
					
					var h = $itemWrapper.data('lso-height') + $wrapper.find('.lso-event-form').outerHeight() + ($itemWrapper.hasClass('last')?16:8) ;
					
					$itemWrapper.animate({height:h+'px'},function() {
						$data.css({visibility:'visible'}).hide();
						$form.find('input.text').eq(0).focus();
					});
					
					$form.find('.form-clear').click(function() {
						$form.find('form').get(0).reset();
						validate = function() { return false; } /* Disable validation until the next submit attempt */
						$form.find('.error').removeClass('error');
						return false;
					});
					
					$form.find('.form-send').click(function() {
						$form.find('form').submit();
						return false;
					});
					
					
					var submitted = false;
					$form.find('form').submit(function() {
						
						if (submitted) return;
						
						var $f = $(this);
												
						validate = function() { /* Define the actual validate hanlder */
							$f.find('.error').removeClass('error');
							$f.find('.text.required').each(function() {
								var $req = $(this);
								if ($.trim($req.val()) == '') {
									$req.parent().addClass('error');
								}
							});
							$f.find('.radio.required').each(function() {
								var c = $(this).find('.radio:checked');
								if (c.length == 0) {
									$(this).addClass('error');
								}
							});
						
							// $f.find('input').change(function() {
							// 	validate();
							// });
							
							return ($f.find('.error').length==0)
						}
						
						if (validate()) {
							submitted = true;
							var data = {}
							$.each($(this).serializeArray(), function(i,field) {
								data[field.name] = encodeURIComponent(field.value);
							});
							$.post(action, data, function(html) {
								if ($.trim(html)=='ok') {
									$itemWrapper.find('.lso-event-item-overview>a').click();
									$form.find('.lso-success').lsoPanel().click();
								}
								else {
									$form.fadeTo(100,1);
									$('input').attr("disabled","false");
								}
							});
							$form.fadeTo(500,0.5);
							$('input').attr("disabled","true");
						}
						
						return false;
					});
					
					$form.fadeTo(1000,1);
				});
			
				return false;
				
			});
			
			
			
		});
		
	}
	
})(jQuery);